diff --git a/.env.dev b/.env.dev new file mode 100644 index 0000000..f137e28 --- /dev/null +++ b/.env.dev @@ -0,0 +1,4 @@ +DBMS_CONNECTION=jdbc:mysql://mysql:3306/Vocabulary_KDP +DBMS_USERNAME=root +DBMS_PASSWORD=123456 +SECRET_KEY=cEJXRvOIUGfe9KQfAh555TvLeOzQ30P7FxzQbZ5eDBOjOBCb26kQAMDbhZJahkJ1 diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..6ee0c15 --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +DBMS_CONNECTION= +DBMS_USERNAME= +DBMS_PASSWORD= +SECRET_KEY= diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0ee4276 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +# Stage 1: build +# Start with a Maven image that includes JDK 17 +FROM maven:3.9.9-amazoncorretto-17 AS build + +# Copy source code and pom.xml file to /app folder +WORKDIR /app +COPY pom.xml . +COPY src ./src + +# Build source code with maven +RUN mvn package -DskipTests + +# Stage 2: create image +# Start with Amazon Correto JDK 17 +FROM amazoncorretto:17 + +# Set working folder to App and copy complied file from above step +WORKDIR /app +COPY --from=build /app/target/*.jar app.jar + +# Command to run the application +ENTRYPOINT ["java", "-jar", "app.jar"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..8a29c0b --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# Learn Vocabulary KDP Service + +# Features + +# Set up +## Requirement +- JDK 17+ ([How to download](https://www.geeksforgeeks.org/download-and-install-java-development-kit-jdk-on-windows-mac-and-linux/)) +- IDE: [IntelliJ IDEA + ](https://www.jetbrains.com/idea/) (Optional) +- [Lombok](https://projectlombok.org/): Install plugin in your IDE or Text Editor. [How to install Lombok](https://www.google.com/search?q=how+to+install+lombok) +- MySQL (Use with Docker) +### 1. Clone this repository +```git +https://github.com/K1ethoang/BE_Learn-Vocabulary_KDP.git +``` +### 2. Go to project folder +```git +cd BE_Learn-Vocabulary_KDP +``` +## .ENV +- Has 2 `.env` file: [.env.example](.env.example) for you set up with your configuration and [.env.dev](.env.dev) to run if you do not care about configuration. + +## Install Docker (Required) +- Follow-up [document of Docker](https://docs.docker.com/compose/install/) + +## How to run + +### [Run manually Wiki](https://github.com/K1ethoang/BE_Learn-Vocabulary_KDP/wiki/Run-manually) + +### [Run with Docker Wiki](https://github.com/K1ethoang/BE_Learn-Vocabulary_KDP/wiki/Run-with-Docker) + +# Usage (Building) +Access http://localhost:9091/swagger-ui/index.html to see Api document, read more [Usage wiki]() diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..126ea41 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,28 @@ +services: + mysql: + image: mysql:8.3.0 + container_name: mysql-container-8.3.0 + environment: + MYSQL_ROOT_PASSWORD: 123456 + ports: + - "6603:3306" + volumes: + - mysql_data:/var/lib/mysql # Lưu dữ liệu MySQL + - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro # Chạy file SQL khi container khởi động lần đầu MySQL + restart: always + + kdp-service: + build: + context: . # Thư mục hiện tại + dockerfile: Dockerfile + container_name: kdp-service-container-0.1.0 + ports: + - "9091:9091" + depends_on: + - mysql # Đảm bảo MySQL khởi động trước + restart: always + env_file: + - .env.dev + +volumes: + mysql_data: diff --git a/init.sql b/init.sql new file mode 100644 index 0000000..749ed43 --- /dev/null +++ b/init.sql @@ -0,0 +1,8 @@ +/* + * Copyright (c) 2024. K1ethoang + * @Author: Kiet Hoang Gia + * @LastModified: 2024/12/19 - 00:27 AM (ICT) + */ + +-- Tạo schema (database) +CREATE DATABASE IF NOT EXISTS Vocabulary_KDP; diff --git a/pom.xml b/pom.xml index e2ddb6e..1924d4e 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ org.kdp Learn_Vocabulary_KDP - 0.0.1-SNAPSHOT + 0.1.0 Learn_Vocabulary_KDP Learn_Vocabulary_KDP @@ -102,6 +102,34 @@ + + com.diffplug.spotless + spotless-maven-plugin + + + + + + + + true + 4 + + + + java, jakarta, org, com, com.diffplug + + + + + + compile + + apply + + + + org.springframework.boot spring-boot-maven-plugin diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/LearnVocabularyKdpApplication.java b/src/main/java/org/kdp/learn_vocabulary_kdp/LearnVocabularyKdpApplication.java index 9d897c1..8cc5914 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/LearnVocabularyKdpApplication.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/LearnVocabularyKdpApplication.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 19:19 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp; import org.springframework.boot.SpringApplication; @@ -17,5 +16,4 @@ public class LearnVocabularyKdpApplication { public static void main(String[] args) { SpringApplication.run(LearnVocabularyKdpApplication.class, args); } - } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/Util/ContextHolderUtil.java b/src/main/java/org/kdp/learn_vocabulary_kdp/Util/ContextHolderUtil.java index bd79e14..b065153 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/Util/ContextHolderUtil.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/Util/ContextHolderUtil.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/15 - 15:51 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.Util; import lombok.NoArgsConstructor; @@ -38,6 +37,4 @@ public String getUserIdFromContext() { } return null; } - - } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/ApplicationInitConfig.java b/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/ApplicationInitConfig.java index c19b4d3..c14e97d 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/ApplicationInitConfig.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/ApplicationInitConfig.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/17 - 21:57 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.configuration; import lombok.AccessLevel; @@ -48,36 +47,71 @@ ApplicationRunner applicationRunner() { if (userRepository.findByEmail("kiethoang101@gmail.com").isEmpty()) { Role role = roleRepository.findByName(ERole.ADMIN.getName()); - User user = User.builder().email("kiethoang101@gmail.com").password(passwordEncoder.encode("12345678")).fullName("admin").role(role).isBlocked(false).build(); + User user = User.builder() + .email("kiethoang101@gmail.com") + .password(passwordEncoder.encode("12345678")) + .fullName("admin") + .role(role) + .isBlocked(false) + .build(); userRepository.save(user); - log.warn("Init account: Admin user has been created with: " + "'kiethoang101@gmail.com/12345678', " + "please change password"); + log.warn("Init account: Admin user has been created with: " + "'kiethoang101@gmail.com/12345678', " + + "please change password"); } // Init data for Type entity if (typeRepository.count() == 0) { - typeRepository.save(Type.builder().symbol("N").name("Noun").description("A person, place, thing, or idea.\nExample: cat, dog, house...").build()); + typeRepository.save(Type.builder() + .symbol("N") + .name("Noun") + .description("A person, place, thing, or idea.\nExample: cat, dog, house...") + .build()); log.info("Init: Noun (N)"); - typeRepository.save(Type.builder().symbol("V").name("Verb").description("An action word.\nExample: run, jump, eat, think...").build()); + typeRepository.save(Type.builder() + .symbol("V") + .name("Verb") + .description("An action word.\nExample: run, jump, eat, think...") + .build()); log.info("Init: Verb (V)"); - typeRepository.save(Type.builder().symbol("Adj").name("Adjective").description("A word that describes a noun.\nExample: big, small, happy, sad...").build()); + typeRepository.save(Type.builder() + .symbol("Adj") + .name("Adjective") + .description("A word that describes a noun.\nExample: big, small, happy, sad...") + .build()); log.info("Init: Adjective (Adj)"); - typeRepository.save(Type.builder().symbol("Prep").name("Preposition").description("A word that shows the relationship between nouns, pronouns, and other words in a sentence.\nExample: in, on, at, with...").build()); + typeRepository.save(Type.builder() + .symbol("Prep") + .name("Preposition") + .description( + "A word that shows the relationship between nouns, pronouns, and other words in a sentence.\nExample: in, on, at, with...") + .build()); log.info("Init: Preposition (Prep)"); - typeRepository.save(Type.builder().symbol("Det").name("Determiner").description("A word that modifies a noun.\nExample: the, a, an, this, that...").build()); + typeRepository.save(Type.builder() + .symbol("Det") + .name("Determiner") + .description("A word that modifies a noun.\nExample: the, a, an, this, that...") + .build()); log.info("Init: Determiner (Det)"); - typeRepository.save(Type.builder().symbol("Conj").name("Conjunction").description("A word that joins words phrases, or clauses.\nExample: and, but, or, because...").build()); + typeRepository.save(Type.builder() + .symbol("Conj") + .name("Conjunction") + .description("A word that joins words phrases, or clauses.\nExample: and, but, or, because...") + .build()); log.info("Init: Conjunction (Conj)"); - typeRepository.save(Type.builder().symbol("Intj").name("Interjection").description("A word or phrase that expresses strong emotion.\nExample: Wow!, Oh no!, Hey!...").build()); + typeRepository.save(Type.builder() + .symbol("Intj") + .name("Interjection") + .description("A word or phrase that expresses strong emotion.\nExample: Wow!, Oh no!, Hey!...") + .build()); log.info("Init: Interjection (Intj)"); } - }; } } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/JwtAuthenticationEntryPoint.java b/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/JwtAuthenticationEntryPoint.java index 9d9c681..a2e89ef 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/JwtAuthenticationEntryPoint.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/JwtAuthenticationEntryPoint.java @@ -3,9 +3,10 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 02:07 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.configuration; +import java.io.IOException; + import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; @@ -19,15 +20,16 @@ import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.AuthenticationEntryPoint; -import java.io.IOException; - public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { @Override - public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { + public void commence( + HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) + throws IOException, ServletException { response.setStatus(HttpStatus.UNAUTHORIZED.value()); response.setContentType(MediaType.APPLICATION_JSON_VALUE); - ResponseEntity apiResponse = ApiResponse.createErrorResponse(HttpStatus.UNAUTHORIZED, GlobalMessage.AUTHENTICATED); + ResponseEntity apiResponse = + ApiResponse.createErrorResponse(HttpStatus.UNAUTHORIZED, GlobalMessage.AUTHENTICATED); ObjectMapper mapper = new ObjectMapper(); diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/ModelMapperConfig.java b/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/ModelMapperConfig.java index 0bf6be1..47f98a2 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/ModelMapperConfig.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/ModelMapperConfig.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 20:17 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.configuration; import org.kdp.learn_vocabulary_kdp.entity.User; @@ -19,7 +18,10 @@ public class ModelMapperConfig { @Bean public ModelMapper modelMapper() { ModelMapper modelMapper = new ModelMapper(); - modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT).setSkipNullEnabled(true); + modelMapper + .getConfiguration() + .setMatchingStrategy(MatchingStrategies.STRICT) + .setSkipNullEnabled(true); modelMapper.addMappings(new PropertyMap() { @Override @@ -27,7 +29,7 @@ protected void configure() { map().setRole(source.getRole().getName()); } }); - + return modelMapper; } } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/OpenApiConfig.java b/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/OpenApiConfig.java index 2026740..fb975c1 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/OpenApiConfig.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/OpenApiConfig.java @@ -3,9 +3,10 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 01:01 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.configuration; +import java.util.List; + import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Contact; @@ -18,14 +19,11 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import java.util.List; - @Configuration public class OpenApiConfig { @Value("${server.port}") private String serverPort; - @Bean public OpenAPI defineOpenApi() { Server server = new Server(); @@ -37,16 +35,35 @@ public OpenAPI defineOpenApi() { myContact.setEmail("kiethoang101.dev@gmail.com"); myContact.setUrl("https://github.com/K1ethoang"); - Info information = new Info().title("Learn Vocabulary KDP System API").version("1.0").description("API documentation for website learn English vocabulary").contact(myContact); + Info information = new Info() + .title("Learn Vocabulary KDP System API") + .version("1.0") + .description("API documentation for website learn English vocabulary") + .contact(myContact); SecurityRequirement securityRequirement = new SecurityRequirement().addList("JWT"); - Components components = new Components().addSecuritySchemes("JWT", new SecurityScheme().name("JWT").type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")); + Components components = new Components() + .addSecuritySchemes( + "JWT", + new SecurityScheme() + .name("JWT") + .type(SecurityScheme.Type.HTTP) + .scheme("bearer") + .bearerFormat("JWT")); - return new OpenAPI().info(information).servers(List.of(server)).addSecurityItem(securityRequirement).components(components); + return new OpenAPI() + .info(information) + .servers(List.of(server)) + .addSecurityItem(securityRequirement) + .components(components); } @Bean public GroupedOpenApi apiGroup() { - return GroupedOpenApi.builder().group("V1").packagesToScan("org.kdp.learn_vocabulary_kdp.controller.v1").pathsToMatch("/api/v1/**").build(); + return GroupedOpenApi.builder() + .group("V1") + .packagesToScan("org.kdp.learn_vocabulary_kdp.controller.v1") + .pathsToMatch("/api/v1/**") + .build(); } } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/SecurityConfig.java b/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/SecurityConfig.java index f76db4b..65796c5 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/SecurityConfig.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/configuration/SecurityConfig.java @@ -3,9 +3,10 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 21:43 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.configuration; +import javax.crypto.spec.SecretKeySpec; + import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -23,8 +24,6 @@ import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter; import org.springframework.security.web.SecurityFilterChain; -import javax.crypto.spec.SecretKeySpec; - @Configuration @EnableWebSecurity @EnableMethodSecurity @@ -38,10 +37,17 @@ public class SecurityConfig { public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.cors(AbstractHttpConfigurer::disable).csrf(AbstractHttpConfigurer::disable); - http.sessionManagement(sessionManagementConfigurer -> sessionManagementConfigurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS)); - http.authorizeHttpRequests(authorizeRequests -> authorizeRequests.requestMatchers(PUBLIC_ENDPOINTS).permitAll().anyRequest().authenticated()); + http.sessionManagement(sessionManagementConfigurer -> + sessionManagementConfigurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS)); + http.authorizeHttpRequests(authorizeRequests -> authorizeRequests + .requestMatchers(PUBLIC_ENDPOINTS) + .permitAll() + .anyRequest() + .authenticated()); - http.oauth2ResourceServer(oauth2 -> oauth2.jwt(jwtConfigurer -> jwtConfigurer.decoder(jwtDecoder()).jwtAuthenticationConverter(jwtConverter())).authenticationEntryPoint(new JwtAuthenticationEntryPoint())); + http.oauth2ResourceServer(oauth2 -> oauth2.jwt( + jwtConfigurer -> jwtConfigurer.decoder(jwtDecoder()).jwtAuthenticationConverter(jwtConverter())) + .authenticationEntryPoint(new JwtAuthenticationEntryPoint())); return http.build(); } @@ -62,7 +68,9 @@ JwtAuthenticationConverter jwtConverter() { JwtDecoder jwtDecoder() { SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HS512"); - return NimbusJwtDecoder.withSecretKey(secretKeySpec).macAlgorithm(MacAlgorithm.HS512).build(); + return NimbusJwtDecoder.withSecretKey(secretKeySpec) + .macAlgorithm(MacAlgorithm.HS512) + .build(); } @Bean diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/AuthControllerV1.java b/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/AuthControllerV1.java index 520cb0d..07ee946 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/AuthControllerV1.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/AuthControllerV1.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 01:28 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.controller.v1; import jakarta.validation.Valid; @@ -34,12 +33,14 @@ public class AuthControllerV1 { @PostMapping("/log-in") public ResponseEntity login(@Valid @RequestBody LoginRequest request) { UserResponse userResponse = authService.login(request); - return ApiResponse.createSuccessResponse(HttpStatus.OK, GlobalMessage.SUCCESSFULLY, jwtService.generateToken(userResponse)); + return ApiResponse.createSuccessResponse( + HttpStatus.OK, GlobalMessage.SUCCESSFULLY, jwtService.generateToken(userResponse)); } @PostMapping("/register") public ResponseEntity register(@Valid @RequestBody UserCreationRequest userCreationRequest) { - return ApiResponse.createSuccessResponse(HttpStatus.CREATED, GlobalMessage.SUCCESSFULLY, authService.register(userCreationRequest)); + return ApiResponse.createSuccessResponse( + HttpStatus.CREATED, GlobalMessage.SUCCESSFULLY, authService.register(userCreationRequest)); } @PostMapping("/log-out") diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/TopicControllerV1.java b/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/TopicControllerV1.java index d8d3f99..6c605ee 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/TopicControllerV1.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/TopicControllerV1.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 01:28 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.controller.v1; import jakarta.validation.Valid; @@ -30,19 +29,26 @@ public class TopicControllerV1 { TopicService topicService; @GetMapping("") - public ResponseEntity getTopics(@RequestParam(defaultValue = DEFAULT_PAGE_NO) int pageNo, @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) int pageSize, @RequestParam(defaultValue = DEFAULT_TOPIC_SORT_BY) String sortBy) { + public ResponseEntity getTopics( + @RequestParam(defaultValue = DEFAULT_PAGE_NO) int pageNo, + @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) int pageSize, + @RequestParam(defaultValue = DEFAULT_TOPIC_SORT_BY) String sortBy) { Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.by(Sort.Direction.DESC, sortBy)); - return ApiResponse.createSuccessResponse(HttpStatus.OK, GlobalMessage.SUCCESSFULLY, topicService.getTopicsByUserId(pageable)); + return ApiResponse.createSuccessResponse( + HttpStatus.OK, GlobalMessage.SUCCESSFULLY, topicService.getTopicsByUserId(pageable)); } @PostMapping("") public ResponseEntity createTopic(@Valid @RequestBody TopicCreationRequest topicCreationRequest) { - return ApiResponse.createSuccessResponse(HttpStatus.CREATED, GlobalMessage.SUCCESSFULLY, topicService.createTopic(topicCreationRequest)); + return ApiResponse.createSuccessResponse( + HttpStatus.CREATED, GlobalMessage.SUCCESSFULLY, topicService.createTopic(topicCreationRequest)); } @PutMapping("/{topicId}") - public ResponseEntity updateTopic(@RequestBody TopicUpdateRequest topicUpdateRequest, @PathVariable String topicId) { - return ApiResponse.createSuccessResponse(HttpStatus.OK, GlobalMessage.SUCCESSFULLY, topicService.updateTopic(topicUpdateRequest, topicId)); + public ResponseEntity updateTopic( + @RequestBody TopicUpdateRequest topicUpdateRequest, @PathVariable String topicId) { + return ApiResponse.createSuccessResponse( + HttpStatus.OK, GlobalMessage.SUCCESSFULLY, topicService.updateTopic(topicUpdateRequest, topicId)); } } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/UserControllerV1.java b/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/UserControllerV1.java index c4a3c00..03cdd56 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/UserControllerV1.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/UserControllerV1.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 01:28 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.controller.v1; import jakarta.validation.Valid; @@ -32,12 +31,15 @@ public ResponseEntity getUsers() { @PreAuthorize("hasRole('ADMIN')") @GetMapping("{userId}") public ResponseEntity getUserById(@PathVariable("userId") String userId) { - return ApiResponse.createSuccessResponse(HttpStatus.OK, GlobalMessage.SUCCESSFULLY, userService.getUserById(userId)); + return ApiResponse.createSuccessResponse( + HttpStatus.OK, GlobalMessage.SUCCESSFULLY, userService.getUserById(userId)); } @PutMapping("{userId}") - public ResponseEntity updateUser(@Valid @RequestBody UserUpdateRequest userUpdateRequest, @PathVariable("userId") String userId) { - return ApiResponse.createSuccessResponse(HttpStatus.OK, GlobalMessage.SUCCESSFULLY, userService.updateUser(userUpdateRequest, userId)); + public ResponseEntity updateUser( + @Valid @RequestBody UserUpdateRequest userUpdateRequest, @PathVariable("userId") String userId) { + return ApiResponse.createSuccessResponse( + HttpStatus.OK, GlobalMessage.SUCCESSFULLY, userService.updateUser(userUpdateRequest, userId)); } @PreAuthorize("hasRole('ADMIN')") diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/WordControllerV1.java b/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/WordControllerV1.java index 1cd864d..64bb60e 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/WordControllerV1.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/controller/v1/WordControllerV1.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 01:28 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.controller.v1; import lombok.AllArgsConstructor; @@ -30,25 +29,32 @@ public class WordControllerV1 { private final WordServiceImpl wordService; @GetMapping("") - public ResponseEntity getAllWords(@RequestParam(defaultValue = DEFAULT_PAGE_NO) int pageNo, @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) int pageSize, @RequestParam(defaultValue = DEFAULT_SORT_BY) String sortBy) { + public ResponseEntity getAllWords( + @RequestParam(defaultValue = DEFAULT_PAGE_NO) int pageNo, + @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) int pageSize, + @RequestParam(defaultValue = DEFAULT_SORT_BY) String sortBy) { Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.by(Sort.Direction.DESC, sortBy)); - return ApiResponse.createSuccessResponse(HttpStatus.OK, GlobalMessage.SUCCESSFULLY, wordService.getAllWords(pageable)); + return ApiResponse.createSuccessResponse( + HttpStatus.OK, GlobalMessage.SUCCESSFULLY, wordService.getAllWords(pageable)); } @GetMapping("/{userId}") public ResponseEntity getWordByUserId(@PathVariable String userId) { - return ApiResponse.createSuccessResponse(HttpStatus.OK, GlobalMessage.SUCCESSFULLY, wordService.getWordByUserId(userId)); + return ApiResponse.createSuccessResponse( + HttpStatus.OK, GlobalMessage.SUCCESSFULLY, wordService.getWordByUserId(userId)); } @PostMapping("") public ResponseEntity createWord(@RequestBody WordDto wordDto) { - return ApiResponse.createSuccessResponse(HttpStatus.CREATED, GlobalMessage.SUCCESSFULLY, wordService.createWord(wordDto)); + return ApiResponse.createSuccessResponse( + HttpStatus.CREATED, GlobalMessage.SUCCESSFULLY, wordService.createWord(wordDto)); } @PutMapping("/{id}") public ResponseEntity updateWord(@PathVariable String id, @RequestBody WordDto wordDto) { - return ApiResponse.createSuccessResponse(HttpStatus.OK, GlobalMessage.SUCCESSFULLY, wordService.updateWord(id, wordDto)); + return ApiResponse.createSuccessResponse( + HttpStatus.OK, GlobalMessage.SUCCESSFULLY, wordService.updateWord(id, wordDto)); } @DeleteMapping("/{id}") diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Auditable.java b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Auditable.java index 6db3c94..d7b4a8b 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Auditable.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Auditable.java @@ -3,9 +3,9 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 19:03 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.entity; +import java.util.Date; import jakarta.persistence.*; import lombok.Getter; @@ -14,8 +14,6 @@ import org.hibernate.annotations.UpdateTimestamp; import org.springframework.data.jpa.domain.support.AuditingEntityListener; -import java.util.Date; - @EntityListeners(AuditingEntityListener.class) @MappedSuperclass @Getter diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Quizz.java b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Quizz.java index c70daf3..1087d8b 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Quizz.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Quizz.java @@ -3,9 +3,10 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/16 - 19:02 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.entity; +import java.util.Date; + import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Getter; @@ -14,8 +15,6 @@ import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UuidGenerator; -import java.util.Date; - @Entity(name = "quizzes") @Getter @Setter diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Role.java b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Role.java index f775827..1c1ca0b 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Role.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Role.java @@ -3,16 +3,15 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 19:42 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.entity; +import java.util.List; + import jakarta.persistence.*; import lombok.*; import lombok.experimental.FieldDefaults; import org.hibernate.annotations.UuidGenerator; -import java.util.List; - @Entity(name = "roles") @Getter @Setter diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Topic.java b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Topic.java index 13ab8f8..7ee5fb7 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Topic.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Topic.java @@ -3,15 +3,14 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/16 - 18:32 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.entity; +import java.util.List; + import jakarta.persistence.*; import lombok.*; import org.hibernate.annotations.UuidGenerator; -import java.util.List; - @Entity(name = "topics") @Getter @Setter diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Type.java b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Type.java index ad5dff3..ca94455 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Type.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Type.java @@ -3,9 +3,10 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/16 - 16:46 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.entity; +import java.util.List; + import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.Id; @@ -14,8 +15,6 @@ import lombok.experimental.FieldDefaults; import org.hibernate.annotations.UuidGenerator; -import java.util.List; - @Entity(name = "types") @Getter @Setter diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/User.java b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/User.java index 00c2d51..6f8d61b 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/User.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/User.java @@ -3,16 +3,15 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/16 - 18:33 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.entity; +import java.util.List; + import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.*; import org.hibernate.annotations.UuidGenerator; -import java.util.List; - @Entity(name = "users") @Getter @Setter diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Word.java b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Word.java index 4366d58..cc4abf2 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Word.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/Word.java @@ -3,16 +3,15 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/16 - 18:33 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.entity; +import java.util.List; + import jakarta.persistence.*; import lombok.*; import lombok.experimental.FieldDefaults; import org.hibernate.annotations.UuidGenerator; -import java.util.List; - @Entity @Table(name = "words") @Getter diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/WordType.java b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/WordType.java index cde3eb7..0ca3511 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/WordType.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/WordType.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/16 - 18:35 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.entity; import jakarta.persistence.Entity; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/keys/IdWordType.java b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/keys/IdWordType.java index 9f7871b..5011a71 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/entity/keys/IdWordType.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/entity/keys/IdWordType.java @@ -1,12 +1,12 @@ package org.kdp.learn_vocabulary_kdp.entity.keys; +import java.io.Serializable; + import jakarta.persistence.Column; import jakarta.persistence.Embeddable; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; -import java.io.Serializable; - @AllArgsConstructor @NoArgsConstructor @Embeddable diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/enums/ERole.java b/src/main/java/org/kdp/learn_vocabulary_kdp/enums/ERole.java index fc1cb1e..f9a2b31 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/enums/ERole.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/enums/ERole.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 20:10 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.enums; import lombok.AllArgsConstructor; @@ -12,7 +11,8 @@ @AllArgsConstructor @Getter public enum ERole { - ADMIN("ADMIN"), USER("USER"); + ADMIN("ADMIN"), + USER("USER"); private final String name; } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/exception/GlobalExceptionHandler.java b/src/main/java/org/kdp/learn_vocabulary_kdp/exception/GlobalExceptionHandler.java index 5808386..e49df44 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/exception/GlobalExceptionHandler.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/exception/GlobalExceptionHandler.java @@ -3,9 +3,10 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 02:04 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.exception; +import java.util.List; + import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.servlet.http.HttpServletRequest; @@ -23,11 +24,21 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.client.HttpClientErrorException; -import java.util.List; - - -@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "2xx", description = "Response thành công", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = SuccessResponse.class))) -@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "4xx, 5xx", description = "Response về danh sách lỗi, nếu chỉ có 1 lỗi thì field đầu tiên sẽ có giá trị \"general\". Còn lại là tên của filed lỗi", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ErrorResponse.class))) +@io.swagger.v3.oas.annotations.responses.ApiResponse( + responseCode = "2xx", + description = "Response thành công", + content = + @Content( + mediaType = MediaType.APPLICATION_JSON_VALUE, + schema = @Schema(implementation = SuccessResponse.class))) +@io.swagger.v3.oas.annotations.responses.ApiResponse( + responseCode = "4xx, 5xx", + description = + "Response về danh sách lỗi, nếu chỉ có 1 lỗi thì field đầu tiên sẽ có giá trị \"general\". Còn lại là tên của filed lỗi", + content = + @Content( + mediaType = MediaType.APPLICATION_JSON_VALUE, + schema = @Schema(implementation = ErrorResponse.class))) @RestControllerAdvice public class GlobalExceptionHandler { // System exception @@ -53,25 +64,33 @@ public ResponseEntity handlerNotFoundException(NotFoundException @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { - List errorDetails = e.getBindingResult().getFieldErrors().stream().map(fieldError -> new ErrorDetail(fieldError.getField(), fieldError.getDefaultMessage())).toList(); + List errorDetails = e.getBindingResult().getFieldErrors().stream() + .map(fieldError -> new ErrorDetail(fieldError.getField(), fieldError.getDefaultMessage())) + .toList(); return ApiResponse.createErrorResponse(e.getStatusCode(), errorDetails); } @ExceptionHandler(ConstraintViolationException.class) public ResponseEntity handleConstraintViolationException(ConstraintViolationException e) { - List errorDetails = e.getConstraintViolations().stream().map(violation -> { - String fieldPath = violation.getPropertyPath().toString(); - return new ErrorDetail(fieldPath.substring(fieldPath.lastIndexOf('.') + 1), // Lấy tên field cuối cùng - violation.getMessage()); - }).toList(); + List errorDetails = e.getConstraintViolations().stream() + .map(violation -> { + String fieldPath = violation.getPropertyPath().toString(); + return new ErrorDetail( + fieldPath.substring(fieldPath.lastIndexOf('.') + 1), // Lấy tên field cuối cùng + violation.getMessage()); + }) + .toList(); return ApiResponse.createErrorResponse(HttpStatus.BAD_REQUEST, errorDetails); } @ExceptionHandler(AccessDeniedException.class) - public ResponseEntity handlerAccessDeniedException(AccessDeniedException e, HttpServletRequest request) { - return ApiResponse.createErrorResponse(HttpStatus.FORBIDDEN, "You do not have permission to access this resource: " + request.getServletPath()); + public ResponseEntity handlerAccessDeniedException( + AccessDeniedException e, HttpServletRequest request) { + return ApiResponse.createErrorResponse( + HttpStatus.FORBIDDEN, + "You do not have permission to access this resource: " + request.getServletPath()); } @ExceptionHandler(HttpClientErrorException.Unauthorized.class) diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/exception/InvalidException.java b/src/main/java/org/kdp/learn_vocabulary_kdp/exception/InvalidException.java index 3c499c3..664ab86 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/exception/InvalidException.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/exception/InvalidException.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 11:45 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.exception; import lombok.AccessLevel; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/exception/NotFoundException.java b/src/main/java/org/kdp/learn_vocabulary_kdp/exception/NotFoundException.java index 00ac498..93501c3 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/exception/NotFoundException.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/exception/NotFoundException.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 11:45 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.exception; import lombok.AccessLevel; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/exception/NotNullException.java b/src/main/java/org/kdp/learn_vocabulary_kdp/exception/NotNullException.java index bcc85d1..5abe402 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/exception/NotNullException.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/exception/NotNullException.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 11:45 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.exception; import lombok.AccessLevel; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/jackson/TrimDeserializer.java b/src/main/java/org/kdp/learn_vocabulary_kdp/jackson/TrimDeserializer.java index 84bd31a..2b01abd 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/jackson/TrimDeserializer.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/jackson/TrimDeserializer.java @@ -3,15 +3,14 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/13 - 23:44 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.jackson; +import java.io.IOException; + import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; -import java.io.IOException; - public class TrimDeserializer extends JsonDeserializer { @Override public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/message/GlobalMessage.java b/src/main/java/org/kdp/learn_vocabulary_kdp/message/GlobalMessage.java index 9f637e4..aac9ba6 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/message/GlobalMessage.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/message/GlobalMessage.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 02:10 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.message; import lombok.AccessLevel; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/message/TopicMessage.java b/src/main/java/org/kdp/learn_vocabulary_kdp/message/TopicMessage.java index 303352d..37078c5 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/message/TopicMessage.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/message/TopicMessage.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 00:12 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.message; public class TopicMessage { @@ -11,7 +10,8 @@ public class TopicMessage { public static final int TITLE_MAX_LENGTH = 100; public static final int DESC_MAX_LENGTH = 300; - public static final String TITLE_LENGTH = "Length: min " + TITLE_MIN_LENGTH + " and max " + TITLE_MAX_LENGTH + " characters"; + public static final String TITLE_LENGTH = + "Length: min " + TITLE_MIN_LENGTH + " and max " + TITLE_MAX_LENGTH + " characters"; public static final String DESC_LENGTH = "Length: max " + DESC_MAX_LENGTH + " characters"; public static final String TOPIC_EXIST = "Topic is already exist"; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/message/UserMessage.java b/src/main/java/org/kdp/learn_vocabulary_kdp/message/UserMessage.java index 086a4e5..d225bdd 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/message/UserMessage.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/message/UserMessage.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 20:44 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.message; public class UserMessage { @@ -19,11 +18,13 @@ public class UserMessage { public static final String FULLNAME_REQUIRED = "Full_name is required"; // Format - public static final String FULLNAME_FORMAT = "Full_name's length is min " + FULLNAME_MIN_LENGTH + " and max " + FULLNAME_MAX_LENGTH + " characters"; - public static final String PASSWORD_FORMAT = "Password's length is min " + PASSWORD_MIN_LENGTH + " and max " + PASSWORD_MAX_LENGTH + " characters"; + public static final String FULLNAME_FORMAT = + "Full_name's length is min " + FULLNAME_MIN_LENGTH + " and max " + FULLNAME_MAX_LENGTH + " characters"; + public static final String PASSWORD_FORMAT = + "Password's length is min " + PASSWORD_MIN_LENGTH + " and max " + PASSWORD_MAX_LENGTH + " characters"; public static final String EMAIL_FORMAT = "Email is not right format"; - public static final String PASSWORD_FORMAT_MIN = "Password's length is at least " + PASSWORD_MIN_LENGTH + " characters"; - + public static final String PASSWORD_FORMAT_MIN = + "Password's length is at least " + PASSWORD_MIN_LENGTH + " characters"; public static final String EMAIL_PASSWORD_INCORRECT = "Email or password is incorrect"; public static final String EMAIL_EXIST = "Email is already exist"; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/paging/PageableDto.java b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/paging/PageableDto.java index 646a8a3..d9dcb36 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/paging/PageableDto.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/paging/PageableDto.java @@ -1,11 +1,11 @@ package org.kdp.learn_vocabulary_kdp.model.dto.paging; +import java.util.List; + import lombok.Data; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; -import java.util.List; - @Data public class PageableDto { List content; @@ -31,4 +31,3 @@ public PageableDto(Page page) { setEmpty(page.isEmpty()); } } - diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/auth/LoginRequest.java b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/auth/LoginRequest.java index 95ff499..0329bf1 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/auth/LoginRequest.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/auth/LoginRequest.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 11:52 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.model.dto.request.auth; import jakarta.validation.constraints.Email; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/topic/TopicCreationRequest.java b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/topic/TopicCreationRequest.java index 3716963..5625c4d 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/topic/TopicCreationRequest.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/topic/TopicCreationRequest.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 00:11 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.model.dto.request.topic; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/topic/TopicUpdateRequest.java b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/topic/TopicUpdateRequest.java index 8bca00e..305547c 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/topic/TopicUpdateRequest.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/topic/TopicUpdateRequest.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 00:11 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.model.dto.request.topic; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/user/UserCreationRequest.java b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/user/UserCreationRequest.java index c040375..d524eb8 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/user/UserCreationRequest.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/user/UserCreationRequest.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 17:54 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.model.dto.request.user; import jakarta.validation.constraints.Email; @@ -22,7 +21,10 @@ @FieldDefaults(level = AccessLevel.PRIVATE) public class UserCreationRequest { @NotBlank(message = UserMessage.FULLNAME_REQUIRED) - @Size(min = UserMessage.FULLNAME_MIN_LENGTH, max = UserMessage.FULLNAME_MAX_LENGTH, message = UserMessage.FULLNAME_FORMAT) + @Size( + min = UserMessage.FULLNAME_MIN_LENGTH, + max = UserMessage.FULLNAME_MAX_LENGTH, + message = UserMessage.FULLNAME_FORMAT) String fullName; @Email(message = UserMessage.EMAIL_FORMAT) @@ -30,6 +32,9 @@ public class UserCreationRequest { String email; @NotBlank(message = UserMessage.PASSWORD_REQUIRED) - @Size(min = UserMessage.PASSWORD_MIN_LENGTH, max = UserMessage.PASSWORD_MAX_LENGTH, message = UserMessage.PASSWORD_FORMAT) + @Size( + min = UserMessage.PASSWORD_MIN_LENGTH, + max = UserMessage.PASSWORD_MAX_LENGTH, + message = UserMessage.PASSWORD_FORMAT) String password; } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/user/UserUpdateRequest.java b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/user/UserUpdateRequest.java index 6aad14c..1ff298c 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/user/UserUpdateRequest.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/request/user/UserUpdateRequest.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 18:38 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.model.dto.request.user; import jakarta.validation.constraints.Email; @@ -20,12 +19,18 @@ @Data @FieldDefaults(level = AccessLevel.PRIVATE) public class UserUpdateRequest { - @Size(min = UserMessage.FULLNAME_MIN_LENGTH, max = UserMessage.FULLNAME_MAX_LENGTH, message = UserMessage.FULLNAME_FORMAT) + @Size( + min = UserMessage.FULLNAME_MIN_LENGTH, + max = UserMessage.FULLNAME_MAX_LENGTH, + message = UserMessage.FULLNAME_FORMAT) String fullName; @Email(message = UserMessage.EMAIL_FORMAT) String email; - @Size(min = UserMessage.PASSWORD_MIN_LENGTH, max = UserMessage.PASSWORD_MAX_LENGTH, message = UserMessage.PASSWORD_FORMAT) + @Size( + min = UserMessage.PASSWORD_MIN_LENGTH, + max = UserMessage.PASSWORD_MAX_LENGTH, + message = UserMessage.PASSWORD_FORMAT) String password; } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/response/topic/TopicResponse.java b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/response/topic/TopicResponse.java index 61df6a0..3b721bb 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/response/topic/TopicResponse.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/response/topic/TopicResponse.java @@ -3,9 +3,10 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/15 - 16:01 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.model.dto.response.topic; +import java.util.Date; + import com.fasterxml.jackson.annotation.JsonFormat; import lombok.AccessLevel; import lombok.AllArgsConstructor; @@ -13,8 +14,6 @@ import lombok.NoArgsConstructor; import lombok.experimental.FieldDefaults; -import java.util.Date; - @AllArgsConstructor @Data @NoArgsConstructor diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/response/user/UserResponse.java b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/response/user/UserResponse.java index 6dfde0b..55093b7 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/response/user/UserResponse.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/response/user/UserResponse.java @@ -3,9 +3,10 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 19:42 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.model.dto.response.user; +import java.util.Date; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.AccessLevel; @@ -14,8 +15,6 @@ import lombok.NoArgsConstructor; import lombok.experimental.FieldDefaults; -import java.util.Date; - @AllArgsConstructor @NoArgsConstructor @Data diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/word/WordDto.java b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/word/WordDto.java index 2d31ab0..f3d31f8 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/word/WordDto.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/model/dto/word/WordDto.java @@ -1,11 +1,11 @@ package org.kdp.learn_vocabulary_kdp.model.dto.word; +import java.time.LocalDateTime; + import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.AllArgsConstructor; import lombok.Data; -import java.time.LocalDateTime; - @AllArgsConstructor @Data public class WordDto { @@ -20,6 +20,7 @@ public class WordDto { @JsonIgnore private String id; + private String word; private String pronounce; } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/model/mapper/EntityToDto.java b/src/main/java/org/kdp/learn_vocabulary_kdp/model/mapper/EntityToDto.java index b9fbbfd..08b6853 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/model/mapper/EntityToDto.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/model/mapper/EntityToDto.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 17:55 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.model.mapper; import lombok.AllArgsConstructor; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/model/mapper/TopicMapper.java b/src/main/java/org/kdp/learn_vocabulary_kdp/model/mapper/TopicMapper.java index 9c46cb0..cf838d5 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/model/mapper/TopicMapper.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/model/mapper/TopicMapper.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/17 - 22:26 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.model.mapper; import lombok.AccessLevel; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/model/mapper/UserMapper.java b/src/main/java/org/kdp/learn_vocabulary_kdp/model/mapper/UserMapper.java index ec7a1fd..e863573 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/model/mapper/UserMapper.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/model/mapper/UserMapper.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 20:17 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.model.mapper; import lombok.AccessLevel; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/repository/QuizzeRepository.java b/src/main/java/org/kdp/learn_vocabulary_kdp/repository/QuizzeRepository.java index 77fbaf1..3b84598 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/repository/QuizzeRepository.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/repository/QuizzeRepository.java @@ -5,5 +5,4 @@ import org.springframework.stereotype.Repository; @Repository -public interface QuizzeRepository extends JpaRepository { -} +public interface QuizzeRepository extends JpaRepository {} diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/repository/RoleRepository.java b/src/main/java/org/kdp/learn_vocabulary_kdp/repository/RoleRepository.java index 5884fa3..2bdd94f 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/repository/RoleRepository.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/repository/RoleRepository.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 19:36 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.repository; import org.kdp.learn_vocabulary_kdp.entity.Role; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/repository/TopicRepository.java b/src/main/java/org/kdp/learn_vocabulary_kdp/repository/TopicRepository.java index e782c40..45be3f1 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/repository/TopicRepository.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/repository/TopicRepository.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 00:41 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.repository; import org.kdp.learn_vocabulary_kdp.entity.Topic; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/repository/TypeRepository.java b/src/main/java/org/kdp/learn_vocabulary_kdp/repository/TypeRepository.java index f4d0981..23cf038 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/repository/TypeRepository.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/repository/TypeRepository.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/16 - 17:21 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.repository; import org.kdp.learn_vocabulary_kdp.entity.Type; @@ -11,5 +10,4 @@ import org.springframework.stereotype.Repository; @Repository -public interface TypeRepository extends JpaRepository { -} +public interface TypeRepository extends JpaRepository {} diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/repository/UserRepository.java b/src/main/java/org/kdp/learn_vocabulary_kdp/repository/UserRepository.java index ac6ad50..96229c2 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/repository/UserRepository.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/repository/UserRepository.java @@ -3,15 +3,14 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/12 - 11:48 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.repository; +import java.util.Optional; + import org.kdp.learn_vocabulary_kdp.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.Optional; - @Repository public interface UserRepository extends JpaRepository { Optional findByEmail(String email); diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/repository/WordRepository.java b/src/main/java/org/kdp/learn_vocabulary_kdp/repository/WordRepository.java index 404847b..915002b 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/repository/WordRepository.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/repository/WordRepository.java @@ -5,6 +5,4 @@ import org.springframework.stereotype.Repository; @Repository -public interface WordRepository extends JpaRepository { - -} +public interface WordRepository extends JpaRepository {} diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/response/ApiResponse.java b/src/main/java/org/kdp/learn_vocabulary_kdp/response/ApiResponse.java index 2f9899e..be410a7 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/response/ApiResponse.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/response/ApiResponse.java @@ -3,31 +3,33 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 02:10 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.response; +import java.util.List; + import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; -import java.util.List; - @NoArgsConstructor(access = AccessLevel.PRIVATE) public class ApiResponse { - public static ResponseEntity createSuccessResponse(HttpStatusCode httpStatus, String message, Object responseObject) { + public static ResponseEntity createSuccessResponse( + HttpStatusCode httpStatus, String message, Object responseObject) { SuccessResponse successResponse = new SuccessResponse(httpStatus.value(), message, responseObject); return new ResponseEntity<>(successResponse, HttpStatus.OK); } public static ResponseEntity createErrorResponse(HttpStatusCode httpStatus, String message) { - ErrorResponse errorResponse = new ErrorResponse(httpStatus.value(), List.of(new ErrorDetail("general", message))); + ErrorResponse errorResponse = + new ErrorResponse(httpStatus.value(), List.of(new ErrorDetail("general", message))); return new ResponseEntity<>(errorResponse, httpStatus); } - public static ResponseEntity createErrorResponse(HttpStatusCode httpStatus, List errorDetails) { + public static ResponseEntity createErrorResponse( + HttpStatusCode httpStatus, List errorDetails) { ErrorResponse errorResponse = new ErrorResponse(httpStatus.value(), errorDetails); return new ResponseEntity<>(errorResponse, httpStatus); diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/response/ErrorDetail.java b/src/main/java/org/kdp/learn_vocabulary_kdp/response/ErrorDetail.java index bdd3b14..fc5e44a 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/response/ErrorDetail.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/response/ErrorDetail.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 02:07 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.response; import io.swagger.v3.oas.annotations.media.Schema; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/response/ErrorResponse.java b/src/main/java/org/kdp/learn_vocabulary_kdp/response/ErrorResponse.java index 6eb984c..700f6ce 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/response/ErrorResponse.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/response/ErrorResponse.java @@ -3,17 +3,16 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 02:06 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.response; +import java.util.List; + import io.swagger.v3.oas.annotations.media.Schema; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.experimental.FieldDefaults; -import java.util.List; - @AllArgsConstructor @Getter @FieldDefaults(level = AccessLevel.PRIVATE) @@ -21,5 +20,6 @@ public class ErrorResponse { @Schema(example = "401") int statusCode; + List errors; } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/response/SuccessResponse.java b/src/main/java/org/kdp/learn_vocabulary_kdp/response/SuccessResponse.java index be0e6a4..1fbe88a 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/response/SuccessResponse.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/response/SuccessResponse.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/18 - 02:11 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.response; import io.swagger.v3.oas.annotations.media.Schema; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/AuthServiceImpl.java b/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/AuthServiceImpl.java index badf92e..ba4e1d3 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/AuthServiceImpl.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/AuthServiceImpl.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 18:50 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.service.impl; import lombok.AllArgsConstructor; @@ -31,7 +30,9 @@ public class AuthServiceImpl implements AuthService { @Override public UserResponse login(LoginRequest request) throws InvalidException { - User user = userRepository.findByEmail(request.getEmail()).orElseThrow(() -> new NotFoundException(UserMessage.EMAIL_PASSWORD_INCORRECT)); + User user = userRepository + .findByEmail(request.getEmail()) + .orElseThrow(() -> new NotFoundException(UserMessage.EMAIL_PASSWORD_INCORRECT)); if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) { throw new InvalidException(UserMessage.EMAIL_PASSWORD_INCORRECT); diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/JwtServiceImpl.java b/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/JwtServiceImpl.java index f087977..f368d12 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/JwtServiceImpl.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/JwtServiceImpl.java @@ -3,9 +3,11 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/15 - 15:49 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.service.impl; +import java.text.ParseException; +import java.util.Date; + import com.nimbusds.jose.*; import com.nimbusds.jose.crypto.MACSigner; import com.nimbusds.jose.crypto.MACVerifier; @@ -20,9 +22,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import java.text.ParseException; -import java.util.Date; - @Slf4j @Service @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) @@ -40,7 +39,14 @@ public String generateToken(UserResponse userResponse) { JWSHeader jwsHeader = new JWSHeader(JWSAlgorithm.HS512); Date now = new Date(); - JWTClaimsSet jwtClaimsSet = new JWTClaimsSet.Builder().subject(userResponse.getEmail()).issuer("KDP").issueTime(now).expirationTime(Date.from(now.toInstant().plusMillis(jwtExpirationTime))).claim("user_id", userResponse.getId()).claim("scope", userResponse.getRole()).build(); + JWTClaimsSet jwtClaimsSet = new JWTClaimsSet.Builder() + .subject(userResponse.getEmail()) + .issuer("KDP") + .issueTime(now) + .expirationTime(Date.from(now.toInstant().plusMillis(jwtExpirationTime))) + .claim("user_id", userResponse.getId()) + .claim("scope", userResponse.getRole()) + .build(); Payload payload = new Payload(jwtClaimsSet.toJSONObject()); JWSObject jwsObject = new JWSObject(jwsHeader, payload); diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/TopicServiceImpl.java b/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/TopicServiceImpl.java index 8e2529f..a79a3d9 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/TopicServiceImpl.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/TopicServiceImpl.java @@ -3,9 +3,12 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/17 - 22:42 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.service.impl; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + import jakarta.validation.Valid; import lombok.AccessLevel; import lombok.AllArgsConstructor; @@ -30,10 +33,6 @@ import org.springframework.security.access.AccessDeniedException; import org.springframework.stereotype.Service; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; - @Slf4j @Service @AllArgsConstructor @@ -59,7 +58,8 @@ public PageableDto getTopicsByUserId(Pageable pageable) { List topicList = topicPage.getContent(); - List content = topicList.stream().map(topicMapper::toTopicResponse).toList(); + List content = + topicList.stream().map(topicMapper::toTopicResponse).toList(); PageableDto pageableDto = new PageableDto(topicPage); @@ -72,7 +72,8 @@ public PageableDto getTopicsByUserId(Pageable pageable) { * @hidden Auto get userId from Context Holder to checks */ @Override - public TopicResponse createTopic(TopicCreationRequest topicCreationRequest) throws InvalidException, NotFoundException { + public TopicResponse createTopic(TopicCreationRequest topicCreationRequest) + throws InvalidException, NotFoundException { String userId = contextHolderUtil.getUserIdFromContext(); if (topicRepository.existsTopicByTitleAndUser_Id(topicCreationRequest.getTitle(), userId)) { @@ -92,8 +93,11 @@ public TopicResponse createTopic(TopicCreationRequest topicCreationRequest) thro * @hidden Auto get userId from Context Holder to checks */ @Override - public TopicResponse updateTopic(@Valid TopicUpdateRequest topicUpdateRequest, String topicId) throws NotFoundException { - Topic topic = topicRepository.findById(topicId).orElseThrow(() -> new NotFoundException(TopicMessage.TOPIC_NOT_FOUND)); + public TopicResponse updateTopic(@Valid TopicUpdateRequest topicUpdateRequest, String topicId) + throws NotFoundException { + Topic topic = topicRepository + .findById(topicId) + .orElseThrow(() -> new NotFoundException(TopicMessage.TOPIC_NOT_FOUND)); String userId = contextHolderUtil.getUserIdFromContext(); // Check topic belong to this user @@ -114,7 +118,6 @@ public TopicResponse updateTopic(@Valid TopicUpdateRequest topicUpdateRequest, S public PageableDto getWordsByTopic(Pageable pageable, String topicId) { String userId = contextHolderUtil.getUserIdFromContext(); - return null; } } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/UserServiceImpl.java b/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/UserServiceImpl.java index df814f1..7a8824e 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/UserServiceImpl.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/UserServiceImpl.java @@ -3,9 +3,11 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/15 - 15:22 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.service.impl; +import java.util.ArrayList; +import java.util.List; + import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.kdp.learn_vocabulary_kdp.Util.ContextHolderUtil; @@ -25,9 +27,6 @@ import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; - @Slf4j @Service @AllArgsConstructor @@ -51,7 +50,8 @@ public List getUsers() { @Override public UserResponse getUserById(String userId) throws NotFoundException { - return userMapper.toUserResponse(userRepository.findById(userId).orElseThrow(() -> new NotFoundException(UserMessage.USER_NOT_FOUND))); + return userMapper.toUserResponse( + userRepository.findById(userId).orElseThrow(() -> new NotFoundException(UserMessage.USER_NOT_FOUND))); } @Override @@ -75,7 +75,8 @@ public UserResponse createUser(UserCreationRequest userCreationRequest) throws I @Override public UserResponse updateUser(UserUpdateRequest userUpdateRequest, String userId) { - User user = userRepository.findById(userId).orElseThrow(() -> new NotFoundException(UserMessage.USER_NOT_FOUND)); + User user = + userRepository.findById(userId).orElseThrow(() -> new NotFoundException(UserMessage.USER_NOT_FOUND)); userMapper.updateUser(userUpdateRequest, user); @@ -86,14 +87,17 @@ public UserResponse updateUser(UserUpdateRequest userUpdateRequest, String userI @Override public void deleteUser(String userId) { - User user = userRepository.findById(userId).orElseThrow(() -> new NotFoundException(UserMessage.USER_NOT_FOUND)); + User user = + userRepository.findById(userId).orElseThrow(() -> new NotFoundException(UserMessage.USER_NOT_FOUND)); userRepository.delete(user); } @Override public UserResponse getMyInfo() throws NotFoundException { - User user = userRepository.findByEmail(contextHolderUtil.getNameFromContext()).orElseThrow(() -> new NotFoundException(UserMessage.USER_NOT_FOUND)); + User user = userRepository + .findByEmail(contextHolderUtil.getNameFromContext()) + .orElseThrow(() -> new NotFoundException(UserMessage.USER_NOT_FOUND)); return userMapper.toUserResponse(user); } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/WordServiceImpl.java b/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/WordServiceImpl.java index 75b222f..2e69405 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/WordServiceImpl.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/service/impl/WordServiceImpl.java @@ -3,9 +3,11 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/08 - 01:31 AM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.service.impl; +import java.util.Arrays; +import java.util.List; + import lombok.AllArgsConstructor; import org.kdp.learn_vocabulary_kdp.entity.Word; import org.kdp.learn_vocabulary_kdp.exception.NotFoundException; @@ -19,9 +21,6 @@ import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; -import java.util.Arrays; -import java.util.List; - @Service @AllArgsConstructor public class WordServiceImpl implements WordService { @@ -63,6 +62,5 @@ public WordDto updateWord(String id, WordDto wordDto) { } @Override - public void deleteWord(String id) { - } + public void deleteWord(String id) {} } diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/AuthService.java b/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/AuthService.java index 9f37973..42e510b 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/AuthService.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/AuthService.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 17:55 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.service.interfaces; import org.kdp.learn_vocabulary_kdp.model.dto.request.auth.LoginRequest; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/JwtService.java b/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/JwtService.java index 1655b06..c5c8567 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/JwtService.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/JwtService.java @@ -3,7 +3,6 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 20:34 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.service.interfaces; import org.kdp.learn_vocabulary_kdp.model.dto.response.user.UserResponse; diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/TopicService.java b/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/TopicService.java index 99fdceb..544e6ee 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/TopicService.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/TopicService.java @@ -3,9 +3,10 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/17 - 22:41 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.service.interfaces; +import java.util.List; + import jakarta.validation.Valid; import org.kdp.learn_vocabulary_kdp.model.dto.paging.PageableDto; import org.kdp.learn_vocabulary_kdp.model.dto.request.topic.TopicCreationRequest; @@ -14,8 +15,6 @@ import org.springframework.data.domain.Pageable; import org.springframework.validation.annotation.Validated; -import java.util.List; - @Validated public interface TopicService { List getTopics(); diff --git a/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/UserService.java b/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/UserService.java index 1e85555..4ce24e7 100644 --- a/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/UserService.java +++ b/src/main/java/org/kdp/learn_vocabulary_kdp/service/interfaces/UserService.java @@ -3,16 +3,15 @@ * @Author: Kiet Hoang Gia * @LastModified: 2024/12/14 - 21:13 PM (ICT) ************************************************/ - package org.kdp.learn_vocabulary_kdp.service.interfaces; +import java.util.List; + import org.kdp.learn_vocabulary_kdp.model.dto.request.user.UserCreationRequest; import org.kdp.learn_vocabulary_kdp.model.dto.request.user.UserUpdateRequest; import org.kdp.learn_vocabulary_kdp.model.dto.response.user.UserResponse; import org.springframework.security.access.prepost.PostAuthorize; -import java.util.List; - public interface UserService { List getUsers(); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 63b3b9e..1be1221 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,14 +1,14 @@ # # Copyright (c) 2024. K1ethoang # @Author: Kiet Hoang Gia -# @LastModified: 2024/12/17 - 23:46 PM (ICT) +# @LastModified: 2024/12/18 - 18:01 PM (ICT) # spring.application.name=Learn_Vocabulary_KDP server.port=9091 # Data source -spring.datasource.url=jdbc:mysql://localhost:6603/Vocabulary_KDP -spring.datasource.username=root -spring.datasource.password=123456 +spring.datasource.url=${DBMS_CONNECTION:jdbc:mysql://localhost:6603/Vocabulary_KDP} +spring.datasource.username=${DBMS_USERNAME:root} +spring.datasource.password=${DBMS_PASSWORD:123456} spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver # JPA spring.jpa.hibernate.ddl-auto=update @@ -17,4 +17,4 @@ spring.jpa.properties.hibernate.format_sql=true # Security ## 1h = 3600s and 3600*1000 = 3600000 milliseconds security.jwt.expiration-time=3600000 -security.jwt.secret-key=LtSqLJ3p8/ppwQIaeDKS17xN3PjpBF9dK7qPAqEpL/AzlkGs/qAtsCpk6SDzrI4Q +security.jwt.secret-key=${SECRET_KEY:LtSqLJ3p8/ppwQIaeDKS17xN3PjpBF9dK7qPAqEpL/AzlkGs/qAtsCpk6SDzrI4Q} diff --git a/src/test/java/org/kdp/learn_vocabulary_kdp/LearnVocabularyKdpApplicationTests.java b/src/test/java/org/kdp/learn_vocabulary_kdp/LearnVocabularyKdpApplicationTests.java index 76ed167..3817f05 100644 --- a/src/test/java/org/kdp/learn_vocabulary_kdp/LearnVocabularyKdpApplicationTests.java +++ b/src/test/java/org/kdp/learn_vocabulary_kdp/LearnVocabularyKdpApplicationTests.java @@ -6,8 +6,6 @@ @SpringBootTest class LearnVocabularyKdpApplicationTests { - @Test - void contextLoads() { - } - + @Test + void contextLoads() {} }