From 62b8d74015087278a9c3934b83333921e2867269 Mon Sep 17 00:00:00 2001 From: Denis Boyko Date: Fri, 7 Apr 2023 15:12:06 +0300 Subject: [PATCH 1/8] add information in DB --- .../com/provedcode/ProvedCodeApplication.java | 8 +- src/main/resources/data.sql | 88 +++++++++++++++++-- 2 files changed, 83 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/provedcode/ProvedCodeApplication.java b/src/main/java/com/provedcode/ProvedCodeApplication.java index e780cab..78360e0 100644 --- a/src/main/java/com/provedcode/ProvedCodeApplication.java +++ b/src/main/java/com/provedcode/ProvedCodeApplication.java @@ -6,10 +6,10 @@ @SpringBootApplication @ConfigurationPropertiesScan -public class ProvedCodeApplication { +public class ProvedCodeApplication { - public static void main(String[] args) { - SpringApplication.run(ProvedCodeApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(ProvedCodeApplication.class, args); + } } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 494a7af..35e7efc 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -37,11 +37,11 @@ values ((select id from talent order by id desc limit 1), 'http://second_file'); insert into talent_attached_file (talent_id, attached_file) values ((select id from talent order by id desc limit 1), 'http://third_file'); insert into talent_proofs (talent_id, link, text, status, created) -values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'PUBLISHED', '2023-06-04 16:00:19'); +values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'PUBLISHED', '2022-01-04 16:00:19'); insert into talent_proofs (talent_id, link, text, status, created) -values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'DRAFT', '2023-06-04 16:00:19'); +values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'DRAFT', '2023-03-04 16:00:19'); insert into talent_proofs (talent_id, link, text, status, created) -values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'DRAFT', '2023-06-04 16:00:19'); +values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'HIDDEN', '2021-06-08 16:00:19'); insert into user_info (talent_id, login, password) values ((select id from talent order by id desc limit 1), 'SerhiiSoloviov', 'password'); @@ -80,11 +80,11 @@ values ((select id from talent order by id desc limit 1), 'http://second_file'); insert into talent_attached_file (talent_id, attached_file) values ((select id from talent order by id desc limit 1), 'http://third_file'); insert into talent_proofs (talent_id, link, text, status, created) -values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'DRAFT', '2023-06-04 16:00:19'); +values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'DRAFT', '2022-08-07 16:00:19'); insert into talent_proofs (talent_id, link, text, status, created) -values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'DRAFT', '2023-06-04 16:00:19'); +values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'HIDDEN', '2022-04-08 16:00:19'); insert into talent_proofs (talent_id, link, text, status, created) -values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'DRAFT', '2023-06-04 16:00:19'); +values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'DRAFT', '2022-09-02 16:00:19'); insert into user_info (talent_id, login, password) values ((select id from talent order by id desc limit 1), 'MykhailoOrdyntsev', 'password'); @@ -121,11 +121,11 @@ values ((select id from talent order by id desc limit 1), 'http://second_file'); insert into talent_attached_file (talent_id, attached_file) values ((select id from talent order by id desc limit 1), 'http://third_file'); insert into talent_proofs (talent_id, link, text, status, created) -values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'DRAFT', '2023-06-04 16:00:19'); +values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'DRAFT', '2022-02-04 16:00:19'); insert into talent_proofs (talent_id, link, text, status, created) -values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'DRAFT', '2023-06-04 16:00:19'); +values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'DRAFT', '2021-09-04 16:00:19'); insert into talent_proofs (talent_id, link, text, status, created) -values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'DRAFT', '2023-06-04 16:00:19'); +values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'DRAFT', '2023-04-04 16:00:19'); insert into user_info (talent_id, login, password) values ((select id from talent order by id desc limit 1), 'DenisBoyko', 'password'); @@ -160,6 +160,13 @@ values ((select id from talent order by id desc limit 1), 'http://second_file'); insert into talent_attached_file (talent_id, attached_file) values ((select id from talent order by id desc limit 1), 'http://third_file'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'PUBLISHED', '2021-08-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'DRAFT', '2022-06-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'DRAFT', '2023-05-04 16:00:19'); + insert into user_info (talent_id, login, password) values ((select id from talent order by id desc limit 1), 'DmytroUzun', 'password'); insert into user_authorities (user_id, authority_id) @@ -195,6 +202,13 @@ values ((select id from talent order by id desc limit 1), 'http://second_file'); insert into talent_attached_file (talent_id, attached_file) values ((select id from talent order by id desc limit 1), 'http://third_file'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'PUBLISHED', '2023-02-08 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'DRAFT', '2021-03-03 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'DRAFT', '2023-09-05 16:00:19'); + insert into user_info (talent_id, login, password) values ((select id from talent order by id desc limit 1), 'DmytroUzun', 'password'); insert into user_authorities (user_id, authority_id) @@ -228,6 +242,13 @@ values ((select id from talent order by id desc limit 1), 'http://second_file'); insert into talent_attached_file (talent_id, attached_file) values ((select id from talent order by id desc limit 1), 'http://third_file'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'HIDDEN', '2022-02-09 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'DRAFT', '2020-04-02 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'DRAFT', '2023-08-06 16:00:19'); + insert into user_info (talent_id, login, password) values ((select id from talent order by id desc limit 1), 'ViktorVoloshko', 'password'); insert into user_authorities (user_id, authority_id) @@ -263,6 +284,13 @@ values ((select id from talent order by id desc limit 1), 'http://second_file'); insert into talent_attached_file (talent_id, attached_file) values ((select id from talent order by id desc limit 1), 'http://third_file'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'HIDDEN', '2023-06-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'PUBLISHED', '2022-09-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'DRAFT', '2021-01-09 16:00:19'); + insert into user_info (talent_id, login, password) values ((select id from talent order by id desc limit 1), 'OlhaMoiseienko', 'password'); insert into user_authorities (user_id, authority_id) @@ -296,6 +324,13 @@ values ((select id from talent order by id desc limit 1), 'http://second_file'); insert into talent_attached_file (talent_id, attached_file) values ((select id from talent order by id desc limit 1), 'http://third_file'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'PUBLISHED', '2023-08-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'DRAFT', '2023-01-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'PUBLISHED', '2023-02-09 16:00:19'); + insert into user_info (talent_id, login, password) values ((select id from talent order by id desc limit 1), 'MaximKiyashko', 'password'); insert into user_authorities (user_id, authority_id) @@ -331,6 +366,13 @@ values ((select id from talent order by id desc limit 1), 'http://second_file'); insert into talent_attached_file (talent_id, attached_file) values ((select id from talent order by id desc limit 1), 'http://third_file'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'PUBLISHED', '2023-06-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'DRAFT', '2023-06-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'HIDDEN', '2023-06-04 16:00:19'); + insert into user_info (talent_id, login, password) values ((select id from talent order by id desc limit 1), 'NikolaievOleksiio', 'password'); insert into user_authorities (user_id, authority_id) @@ -364,6 +406,13 @@ values ((select id from talent order by id desc limit 1), 'http://second_file'); insert into talent_attached_file (talent_id, attached_file) values ((select id from talent order by id desc limit 1), 'http://third_file'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'PUBLISHED', '2023-06-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'DRAFT', '2023-06-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'HIDDEN', '2023-06-04 16:00:19'); + insert into user_info (talent_id, login, password) values ((select id from talent order by id desc limit 1), 'ArtemLytvynenko', 'password'); insert into user_authorities (user_id, authority_id) @@ -397,6 +446,13 @@ values ((select id from talent order by id desc limit 1), 'http://second_file'); insert into talent_attached_file (talent_id, attached_file) values ((select id from talent order by id desc limit 1), 'http://third_file'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'PUBLISHED', '2023-06-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'HIDDEN', '2023-06-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'DRAFT', '2023-06-04 16:00:19'); + insert into user_info (talent_id, login, password) values ((select id from talent order by id desc limit 1), 'DaniilYevtukhov', 'password'); insert into user_authorities (user_id, authority_id) @@ -432,6 +488,13 @@ values ((select id from talent order by id desc limit 1), 'http://second_file'); insert into talent_attached_file (talent_id, attached_file) values ((select id from talent order by id desc limit 1), 'http://third_file'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'PUBLISHED', '2023-06-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'DRAFT', '2023-06-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'DRAFT', '2023-06-04 16:00:19'); + insert into user_info (talent_id, login, password) values ((select id from talent order by id desc limit 1), 'RuslanMorozov', 'password'); insert into user_authorities (user_id, authority_id) @@ -467,6 +530,13 @@ values ((select id from talent order by id desc limit 1), 'http://second_file'); insert into talent_attached_file (talent_id, attached_file) values ((select id from talent order by id desc limit 1), 'http://third_file'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://first_link', 'text to first proof', 'PUBLISHED', '2023-06-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://second_link', 'text to second proof', 'PUBLISHED', '2023-06-04 16:00:19'); +insert into talent_proofs (talent_id, link, text, status, created) +values ((select id from talent order by id desc limit 1), 'http://third_link', 'text to third proof', 'DRAFT', '2023-06-04 16:00:19'); + insert into user_info (talent_id, login, password) values ((select id from talent order by id desc limit 1), 'IhorKopieichykov', 'password'); insert into user_authorities (user_id, authority_id) From e8b4dddb3443866ef13dbb7b756ceb55e99a7e2e Mon Sep 17 00:00:00 2001 From: Denis Boyko Date: Fri, 7 Apr 2023 16:10:12 +0300 Subject: [PATCH 2/8] add sorting proofs by date of creation --- .../com/provedcode/config/PageProperties.java | 3 ++- .../controller/TalentProofController.java | 5 ++-- .../talent/service/TalentProofService.java | 26 +++++++++++++++---- src/main/resources/application.properties | 1 + 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/provedcode/config/PageProperties.java b/src/main/java/com/provedcode/config/PageProperties.java index 48ab911..f3e17d3 100644 --- a/src/main/java/com/provedcode/config/PageProperties.java +++ b/src/main/java/com/provedcode/config/PageProperties.java @@ -16,7 +16,8 @@ @Slf4j public record PageProperties( int defaultPageNum, - int defaultPageSize + int defaultPageSize, + String defaultSortBy ) { @PostConstruct void print() { diff --git a/src/main/java/com/provedcode/talent/controller/TalentProofController.java b/src/main/java/com/provedcode/talent/controller/TalentProofController.java index f9a2101..60643ad 100644 --- a/src/main/java/com/provedcode/talent/controller/TalentProofController.java +++ b/src/main/java/com/provedcode/talent/controller/TalentProofController.java @@ -21,7 +21,8 @@ public class TalentProofController { @GetMapping("/proofs") Page getAllProofs(@RequestParam(value = "page") Optional page, - @RequestParam(value = "size") Optional size) { - return talentProofService.getAllProofsPage(page, size).map(talentProofMapper::toProofDTO); + @RequestParam(value = "size") Optional size, + @RequestParam(value = "sortDir") Optional sortDir) { + return talentProofService.getAllProofsPage(page, size, sortDir).map(talentProofMapper::toProofDTO); } } diff --git a/src/main/java/com/provedcode/talent/service/TalentProofService.java b/src/main/java/com/provedcode/talent/service/TalentProofService.java index a4d817a..fd0dc40 100644 --- a/src/main/java/com/provedcode/talent/service/TalentProofService.java +++ b/src/main/java/com/provedcode/talent/service/TalentProofService.java @@ -7,6 +7,7 @@ import lombok.AllArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; @@ -20,17 +21,32 @@ public class TalentProofService { TalentProofRepository talentProofRepository; PageProperties pageProperties; - public Page getAllProofsPage(Optional page, Optional size) { + public Page getAllProofsPage(Optional page, Optional size, + Optional sortDir) { if (page.orElse(pageProperties.defaultPageNum()) < 0) { throw new ResponseStatusException(BAD_REQUEST, "'page' query parameter must be greater than or equal to 0"); } if (size.orElse(pageProperties.defaultPageSize()) <= 0) { throw new ResponseStatusException(BAD_REQUEST, "'size' query parameter must be greater than or equal to 1"); } + + if (sortDir.isPresent()) { + if (!sortDir.get().equalsIgnoreCase(Sort.Direction.ASC.name()) && + !sortDir.get().equalsIgnoreCase(Sort.Direction.DESC.name())) { + throw new ResponseStatusException(BAD_REQUEST, "'sortDir' query parameter must be ASC or DESC"); + } + Sort sort = sortDir.get().equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(pageProperties.defaultSortBy()).ascending() + : Sort.by(pageProperties.defaultSortBy()).descending(); + return talentProofRepository.findByStatus(ProofStatus.PUBLISHED, + PageRequest.of(page.orElse( + pageProperties.defaultPageNum()), + size.orElse( + pageProperties.defaultPageSize()), sort)); + } return talentProofRepository.findByStatus(ProofStatus.PUBLISHED, - PageRequest.of(page.orElse( - pageProperties.defaultPageNum()), - size.orElse( - pageProperties.defaultPageSize()))); + PageRequest.of(page.orElse( + pageProperties.defaultPageNum()), + size.orElse( + pageProperties.defaultPageSize()))); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9373df8..9c92a4e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -11,5 +11,6 @@ spring.jpa.hibernate.ddl-auto=none ##DEFAULT PAGE PROPS page-config.default-page-num=0 page-config.default-page-size=5 +page-config.default-sort-by=created ## From 7842fc619af7fb693464b31e7c0b0d877c444328 Mon Sep 17 00:00:00 2001 From: Denis Boyko Date: Fri, 7 Apr 2023 16:13:31 +0300 Subject: [PATCH 3/8] returned OpenAPI --- pom.xml | 8 ++++ .../com/provedcode/config/SecurityConfig.java | 42 ++++++++++--------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index 64ee618..07b8dcc 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,14 @@ 17 + + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + 2.0.0 + + org.springframework.boot spring-boot-starter-data-jpa diff --git a/src/main/java/com/provedcode/config/SecurityConfig.java b/src/main/java/com/provedcode/config/SecurityConfig.java index 13c0ee8..044a13d 100644 --- a/src/main/java/com/provedcode/config/SecurityConfig.java +++ b/src/main/java/com/provedcode/config/SecurityConfig.java @@ -44,6 +44,7 @@ @EnableWebSecurity @EnableMethodSecurity public class SecurityConfig { + //http://localhost:8080/swagger-ui/index.html @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.authorizeHttpRequests(c -> c @@ -51,26 +52,29 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti .requestMatchers(antMatcher("/h2/**")).permitAll() .requestMatchers(antMatcher("/api/talents/**")).permitAll() .requestMatchers(antMatcher("/error")).permitAll() + .requestMatchers(antMatcher("/v3/api-docs/**")).permitAll() // for openAPI + .requestMatchers(antMatcher("/swagger-ui/**")).permitAll() // for openAPI + .requestMatchers(antMatcher("/swagger-ui.html")).permitAll() // for openAPI .anyRequest().authenticated() ); http.exceptionHandling(c -> c .authenticationEntryPoint((request, response, authException) -> { - log.info("Authentication failed {}, message:{}", - describe(request), - authException.getMessage()); - response.sendError( - HttpStatus.UNAUTHORIZED.value(), - authException.getMessage()); - } + log.info("Authentication failed {}, message:{}", + describe(request), + authException.getMessage()); + response.sendError( + HttpStatus.UNAUTHORIZED.value(), + authException.getMessage()); + } ) .accessDeniedHandler((request, response, accessDeniedException) -> { - log.info("Authorization failed {},message: {}", - describe(request), - accessDeniedException.getMessage()); - response.sendError(HttpStatus.FORBIDDEN.value(), - accessDeniedException.getMessage()); - } + log.info("Authorization failed {},message: {}", + describe(request), + accessDeniedException.getMessage()); + response.sendError(HttpStatus.FORBIDDEN.value(), + accessDeniedException.getMessage()); + } ) ); @@ -82,10 +86,10 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti http.sessionManagement().sessionCreationPolicy(STATELESS); http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt) - .exceptionHandling(c -> c - .authenticationEntryPoint(new BearerTokenAuthenticationEntryPoint()) - .accessDeniedHandler(new BearerTokenAccessDeniedHandler()) - ); + .exceptionHandling(c -> c + .authenticationEntryPoint(new BearerTokenAuthenticationEntryPoint()) + .accessDeniedHandler(new BearerTokenAccessDeniedHandler()) + ); return http.build(); } @@ -135,8 +139,8 @@ UserDetailsService userDetailsService( UserInfoMapper mapper ) { return login -> repository.findByLogin(login) - .map(mapper::toUserDetails) - .orElseThrow(() -> new UsernameNotFoundException(login + " not found")); + .map(mapper::toUserDetails) + .orElseThrow(() -> new UsernameNotFoundException(login + " not found")); } @Bean From f38e2f0a72f096aee50034a421cc93d59788ae4e Mon Sep 17 00:00:00 2001 From: Denis Boyko Date: Fri, 7 Apr 2023 16:51:57 +0300 Subject: [PATCH 4/8] remove the proof of the specified talent --- .../controller/TalentProofController.java | 16 ++++++-- .../talent/service/TalentProofService.java | 39 ++++++++++++++++++- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/provedcode/talent/controller/TalentProofController.java b/src/main/java/com/provedcode/talent/controller/TalentProofController.java index 60643ad..3126d39 100644 --- a/src/main/java/com/provedcode/talent/controller/TalentProofController.java +++ b/src/main/java/com/provedcode/talent/controller/TalentProofController.java @@ -3,12 +3,12 @@ import com.provedcode.talent.mapper.TalentProofMapper; import com.provedcode.talent.model.dto.ProofDTO; import com.provedcode.talent.service.TalentProofService; +import com.provedcode.user.model.dto.SessionInfoDTO; import lombok.AllArgsConstructor; import org.springframework.data.domain.Page; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.Authentication; +import org.springframework.web.bind.annotation.*; import java.util.Optional; @@ -25,4 +25,12 @@ Page getAllProofs(@RequestParam(value = "page") Optional page @RequestParam(value = "sortDir") Optional sortDir) { return talentProofService.getAllProofsPage(page, size, sortDir).map(talentProofMapper::toProofDTO); } + + @PreAuthorize("hasRole('TALENT')") + @DeleteMapping("/{talent-id}/proofs/{proof-id}") + SessionInfoDTO deleteProof(@PathVariable(value = "talent-id") long talent_id, + @PathVariable(value = "proof-id") long proof_id, + Authentication authentication) { + return talentProofService.deleteProofById(talent_id, proof_id, authentication); + } } diff --git a/src/main/java/com/provedcode/talent/service/TalentProofService.java b/src/main/java/com/provedcode/talent/service/TalentProofService.java index fd0dc40..857b3f1 100644 --- a/src/main/java/com/provedcode/talent/service/TalentProofService.java +++ b/src/main/java/com/provedcode/talent/service/TalentProofService.java @@ -2,23 +2,32 @@ import com.provedcode.config.PageProperties; import com.provedcode.talent.model.ProofStatus; +import com.provedcode.talent.model.entity.Talent; import com.provedcode.talent.model.entity.TalentProof; import com.provedcode.talent.repo.TalentProofRepository; +import com.provedcode.talent.repo.TalentRepository; +import com.provedcode.user.model.dto.SessionInfoDTO; +import com.provedcode.user.model.entity.UserInfo; +import com.provedcode.user.repo.UserInfoRepository; import lombok.AllArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; +import org.springframework.security.core.Authentication; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.server.ResponseStatusException; import java.util.Optional; -import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.HttpStatus.*; @Service @AllArgsConstructor public class TalentProofService { TalentProofRepository talentProofRepository; + TalentRepository talentRepository; + UserInfoRepository userInfoRepository; PageProperties pageProperties; public Page getAllProofsPage(Optional page, Optional size, @@ -49,4 +58,32 @@ public Page getAllProofsPage(Optional page, Optional talent = talentRepository.findById(talentId); + Optional talentProof = talentProofRepository.findById(proofId); + Optional userInfo = userInfoRepository.findByLogin(authentication.getName()); + userVerification(talent, talentProof, userInfo, talentId, proofId); + talentProofRepository.delete(talentProof.orElseThrow(() -> new ResponseStatusException(NOT_IMPLEMENTED))); + return new SessionInfoDTO("deleted", "null"); + } + + private void userVerification(Optional talent, + Optional talentProof, + Optional userInfo, long talentId, long proofId) { + if (talent.isEmpty() && userInfo.isEmpty()) { + throw new ResponseStatusException(NOT_FOUND, String.format("talent with id = %d not found", talentId)); + } + if (userInfo.get().getTalent().getId() != talentId) { + throw new ResponseStatusException(FORBIDDEN, "you can`t delete/update another user"); + } + if (talentProof.isEmpty()) { + throw new ResponseStatusException(NOT_FOUND, String.format("proof with id = %d not found", proofId)); + } + if (talentProof.get().getTalentId() != talentId) { + throw new ResponseStatusException(FORBIDDEN, "you can`t delete/update another proof"); + } + } } From 085bbc86eab6ef7b1da799acd46e374e18977adf Mon Sep 17 00:00:00 2001 From: Denis Boyko Date: Fri, 7 Apr 2023 18:10:33 +0300 Subject: [PATCH 5/8] proof creation --- .../controller/TalentProofController.java | 11 +++++ .../talent/model/dto/AddProofDTO.java | 7 ++++ .../talent/service/TalentProofService.java | 41 +++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 src/main/java/com/provedcode/talent/model/dto/AddProofDTO.java diff --git a/src/main/java/com/provedcode/talent/controller/TalentProofController.java b/src/main/java/com/provedcode/talent/controller/TalentProofController.java index 3126d39..7a990fa 100644 --- a/src/main/java/com/provedcode/talent/controller/TalentProofController.java +++ b/src/main/java/com/provedcode/talent/controller/TalentProofController.java @@ -1,11 +1,13 @@ package com.provedcode.talent.controller; import com.provedcode.talent.mapper.TalentProofMapper; +import com.provedcode.talent.model.dto.AddProofDTO; import com.provedcode.talent.model.dto.ProofDTO; import com.provedcode.talent.service.TalentProofService; import com.provedcode.user.model.dto.SessionInfoDTO; import lombok.AllArgsConstructor; import org.springframework.data.domain.Page; +import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; @@ -33,4 +35,13 @@ SessionInfoDTO deleteProof(@PathVariable(value = "talent-id") long talent_id, Authentication authentication) { return talentProofService.deleteProofById(talent_id, proof_id, authentication); } + + @PreAuthorize("hasRole('TALENT')") + @PostMapping("/{talent-id}/proofs") + ResponseEntity addProof(@PathVariable(value = "talent-id") long talentId, + @RequestBody AddProofDTO addProofDTO, + Authentication authentication) { + + return talentProofService.addProof(addProofDTO, talentId, authentication); + } } diff --git a/src/main/java/com/provedcode/talent/model/dto/AddProofDTO.java b/src/main/java/com/provedcode/talent/model/dto/AddProofDTO.java new file mode 100644 index 0000000..96729f2 --- /dev/null +++ b/src/main/java/com/provedcode/talent/model/dto/AddProofDTO.java @@ -0,0 +1,7 @@ +package com.provedcode.talent.model.dto; + +public record AddProofDTO( + String link, + String text +) { +} diff --git a/src/main/java/com/provedcode/talent/service/TalentProofService.java b/src/main/java/com/provedcode/talent/service/TalentProofService.java index 857b3f1..de1270b 100644 --- a/src/main/java/com/provedcode/talent/service/TalentProofService.java +++ b/src/main/java/com/provedcode/talent/service/TalentProofService.java @@ -2,6 +2,7 @@ import com.provedcode.config.PageProperties; import com.provedcode.talent.model.ProofStatus; +import com.provedcode.talent.model.dto.AddProofDTO; import com.provedcode.talent.model.entity.Talent; import com.provedcode.talent.model.entity.TalentProof; import com.provedcode.talent.repo.TalentProofRepository; @@ -13,11 +14,15 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; +import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.server.ResponseStatusException; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; +import java.net.URI; +import java.time.LocalDateTime; import java.util.Optional; import static org.springframework.http.HttpStatus.*; @@ -86,4 +91,40 @@ private void userVerification(Optional talent, throw new ResponseStatusException(FORBIDDEN, "you can`t delete/update another proof"); } } + + public ResponseEntity addProof(AddProofDTO addProofDTO, long talentId, Authentication authentication) { + + Optional talent = talentRepository.findById(talentId); + Optional userInfo = userInfoRepository.findByLogin(authentication.getName()); + + userVerification(talent, userInfo, talentId); + + TalentProof talentProof = TalentProof.builder() + .talent(talent.get()) + .talentId(talentId) + .link(addProofDTO.link()) + .text(addProofDTO.text()) + .status(ProofStatus.DRAFT) + .created(LocalDateTime.now()) + .build(); + + talentProofRepository.save(talentProof); + + URI location = ServletUriComponentsBuilder + .fromCurrentRequest() + .path("/{id}") + .buildAndExpand(talentProof.getId()) + .toUri(); + + return ResponseEntity.created(location).build(); + } + + private void userVerification(Optional talent, Optional userInfo, long id) { + if (talent.isEmpty() || userInfo.isEmpty()) { + throw new ResponseStatusException(NOT_FOUND, String.format("talent with id = %d not found", id)); + } + if (userInfo.get().getTalent().getId() != id) { + throw new ResponseStatusException(FORBIDDEN, "you can`t add proof another user"); + } + } } From 59164976dba3e3f2e4902eecfa9f9972c690d1b6 Mon Sep 17 00:00:00 2001 From: Denis Boyko Date: Fri, 7 Apr 2023 19:22:10 +0300 Subject: [PATCH 6/8] refactor validation: create class ValidateTalentForCompliance --- .../controller/TalentProofController.java | 1 - .../talent/service/TalentProofService.java | 32 +------- .../service/impl/TalentServiceImpl.java | 79 +++++++++---------- .../utill/ValidateTalentForCompliance.java | 42 ++++++++++ 4 files changed, 82 insertions(+), 72 deletions(-) create mode 100644 src/main/java/com/provedcode/utill/ValidateTalentForCompliance.java diff --git a/src/main/java/com/provedcode/talent/controller/TalentProofController.java b/src/main/java/com/provedcode/talent/controller/TalentProofController.java index 7a990fa..50c4c02 100644 --- a/src/main/java/com/provedcode/talent/controller/TalentProofController.java +++ b/src/main/java/com/provedcode/talent/controller/TalentProofController.java @@ -41,7 +41,6 @@ SessionInfoDTO deleteProof(@PathVariable(value = "talent-id") long talent_id, ResponseEntity addProof(@PathVariable(value = "talent-id") long talentId, @RequestBody AddProofDTO addProofDTO, Authentication authentication) { - return talentProofService.addProof(addProofDTO, talentId, authentication); } } diff --git a/src/main/java/com/provedcode/talent/service/TalentProofService.java b/src/main/java/com/provedcode/talent/service/TalentProofService.java index de1270b..967dccf 100644 --- a/src/main/java/com/provedcode/talent/service/TalentProofService.java +++ b/src/main/java/com/provedcode/talent/service/TalentProofService.java @@ -10,6 +10,7 @@ import com.provedcode.user.model.dto.SessionInfoDTO; import com.provedcode.user.model.entity.UserInfo; import com.provedcode.user.repo.UserInfoRepository; +import com.provedcode.utill.ValidateTalentForCompliance; import lombok.AllArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -34,6 +35,7 @@ public class TalentProofService { TalentRepository talentRepository; UserInfoRepository userInfoRepository; PageProperties pageProperties; + ValidateTalentForCompliance validateTalentForCompliance; public Page getAllProofsPage(Optional page, Optional size, Optional sortDir) { @@ -70,34 +72,17 @@ public SessionInfoDTO deleteProofById(long talentId, long proofId, Authenticatio Optional talent = talentRepository.findById(talentId); Optional talentProof = talentProofRepository.findById(proofId); Optional userInfo = userInfoRepository.findByLogin(authentication.getName()); - userVerification(talent, talentProof, userInfo, talentId, proofId); + validateTalentForCompliance.userVerification(talent, talentProof, userInfo, talentId, proofId); talentProofRepository.delete(talentProof.orElseThrow(() -> new ResponseStatusException(NOT_IMPLEMENTED))); return new SessionInfoDTO("deleted", "null"); } - private void userVerification(Optional talent, - Optional talentProof, - Optional userInfo, long talentId, long proofId) { - if (talent.isEmpty() && userInfo.isEmpty()) { - throw new ResponseStatusException(NOT_FOUND, String.format("talent with id = %d not found", talentId)); - } - if (userInfo.get().getTalent().getId() != talentId) { - throw new ResponseStatusException(FORBIDDEN, "you can`t delete/update another user"); - } - if (talentProof.isEmpty()) { - throw new ResponseStatusException(NOT_FOUND, String.format("proof with id = %d not found", proofId)); - } - if (talentProof.get().getTalentId() != talentId) { - throw new ResponseStatusException(FORBIDDEN, "you can`t delete/update another proof"); - } - } - public ResponseEntity addProof(AddProofDTO addProofDTO, long talentId, Authentication authentication) { Optional talent = talentRepository.findById(talentId); Optional userInfo = userInfoRepository.findByLogin(authentication.getName()); - userVerification(talent, userInfo, talentId); + validateTalentForCompliance.userVerification(talent, userInfo, talentId); TalentProof talentProof = TalentProof.builder() .talent(talent.get()) @@ -118,13 +103,4 @@ public ResponseEntity addProof(AddProofDTO addProofDTO, long talentId, Authen return ResponseEntity.created(location).build(); } - - private void userVerification(Optional talent, Optional userInfo, long id) { - if (talent.isEmpty() || userInfo.isEmpty()) { - throw new ResponseStatusException(NOT_FOUND, String.format("talent with id = %d not found", id)); - } - if (userInfo.get().getTalent().getId() != id) { - throw new ResponseStatusException(FORBIDDEN, "you can`t add proof another user"); - } - } } diff --git a/src/main/java/com/provedcode/talent/service/impl/TalentServiceImpl.java b/src/main/java/com/provedcode/talent/service/impl/TalentServiceImpl.java index fd3b24b..d472df5 100644 --- a/src/main/java/com/provedcode/talent/service/impl/TalentServiceImpl.java +++ b/src/main/java/com/provedcode/talent/service/impl/TalentServiceImpl.java @@ -8,6 +8,7 @@ import com.provedcode.user.model.dto.SessionInfoDTO; import com.provedcode.user.model.entity.UserInfo; import com.provedcode.user.repo.UserInfoRepository; +import com.provedcode.utill.ValidateTalentForCompliance; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; @@ -30,6 +31,7 @@ public class TalentServiceImpl implements TalentService { TalentRepository talentRepository; UserInfoRepository userInfoRepository; PageProperties pageProperties; + ValidateTalentForCompliance validateTalentForCompliance; @Override @@ -42,7 +44,7 @@ public Page getTalentsPage(Optional page, Optional siz throw new ResponseStatusException(BAD_REQUEST, "'size' query parameter must be greater than or equal to 1"); } return talentRepository.findAll(PageRequest.of(page.orElse(pageProperties.defaultPageNum()), - size.orElse(pageProperties.defaultPageSize()))); + size.orElse(pageProperties.defaultPageSize()))); } @@ -61,7 +63,7 @@ public Talent editTalent(long id, FullTalentDTO fullTalent, Authentication authe Optional talent = talentRepository.findById(id); Optional userInfo = userInfoRepository.findByLogin(authentication.getName()); - userVerification(talent, userInfo, id); + validateTalentForCompliance.userVerification(talent, userInfo, id); Talent oldTalent = talent.get(); long oldTalentId = oldTalent.getId(); @@ -78,60 +80,60 @@ public Talent editTalent(long id, FullTalentDTO fullTalent, Authentication authe .setBio(fullTalent.bio()); } else { oldTalentDescription = TalentDescription.builder() - .talentId(oldTalentId) - .additionalInfo(fullTalent.additionalInfo()) - .bio(fullTalent.bio()) - .talent(oldTalent) - .build(); + .talentId(oldTalentId) + .additionalInfo(fullTalent.additionalInfo()) + .bio(fullTalent.bio()) + .talent(oldTalent) + .build(); } oldTalentTalents.clear(); if (fullTalent.talents() != null) { oldTalentTalents.addAll(fullTalent.talents().stream().map(s -> TalentTalents.builder() - .talentId(oldTalentId) - .talent(oldTalent) - .talentName(s) - .build()).toList()); + .talentId(oldTalentId) + .talent(oldTalent) + .talentName(s) + .build()).toList()); } oldTalentLinks.clear(); if (fullTalent.links() != null) { oldTalentLinks.addAll(fullTalent.links().stream().map(l -> TalentLink.builder() - .talentId(oldTalentId) - .talent(oldTalent) - .link(l) - .build()).toList()); + .talentId(oldTalentId) + .talent(oldTalent) + .link(l) + .build()).toList()); } oldTalentContacts.clear(); if (fullTalent.contacts() != null) { oldTalentContacts.addAll(fullTalent.contacts().stream().map(s -> TalentContact.builder() - .talentId(oldTalentId) - .talent(oldTalent) - .contact(s) - .build()).toList()); + .talentId(oldTalentId) + .talent(oldTalent) + .contact(s) + .build()).toList()); } oldTalentAttachedFile.clear(); if (fullTalent.attachedFiles() != null) { oldTalentAttachedFile.addAll(fullTalent.attachedFiles().stream().map(s -> TalentAttachedFile.builder() - .talentId( - oldTalentId) - .talent(oldTalent) - .attachedFile(s) - .build()) - .toList()); + .talentId( + oldTalentId) + .talent(oldTalent) + .attachedFile(s) + .build()) + .toList()); } oldTalent.setFirstName(fullTalent.firstName()) - .setLastName(fullTalent.lastName()) - .setSpecialization(fullTalent.specialization()) - .setImage(fullTalent.image()) - .setTalentDescription(oldTalentDescription) - .setTalentTalents(oldTalentTalents) - .setTalentLinks(oldTalentLinks) - .setTalentContacts(oldTalentContacts) - .setTalentAttachedFiles(oldTalentAttachedFile); + .setLastName(fullTalent.lastName()) + .setSpecialization(fullTalent.specialization()) + .setImage(fullTalent.image()) + .setTalentDescription(oldTalentDescription) + .setTalentTalents(oldTalentTalents) + .setTalentLinks(oldTalentLinks) + .setTalentContacts(oldTalentContacts) + .setTalentAttachedFiles(oldTalentAttachedFile); Talent newTalent = talentRepository.save(oldTalent); @@ -143,19 +145,10 @@ public SessionInfoDTO deleteTalentById(long id, Authentication authentication) { Optional talent = talentRepository.findById(id); Optional userInfo = userInfoRepository.findByLogin(authentication.getName()); - userVerification(talent, userInfo, id); + validateTalentForCompliance.userVerification(talent, userInfo, id); userInfoRepository.delete(userInfo.orElseThrow(() -> new ResponseStatusException(NOT_IMPLEMENTED))); talentRepository.delete(talent.orElseThrow(() -> new ResponseStatusException(NOT_IMPLEMENTED))); return new SessionInfoDTO("deleted", "null"); } - - private void userVerification(Optional talent, Optional userInfo, long id) { - if (talent.isEmpty() || userInfo.isEmpty()) { - throw new ResponseStatusException(NOT_FOUND, String.format("talent with id = %d not found", id)); - } - if (userInfo.get().getTalent().getId() != id) { - throw new ResponseStatusException(FORBIDDEN, "you can`t delete/update another user"); - } - } } diff --git a/src/main/java/com/provedcode/utill/ValidateTalentForCompliance.java b/src/main/java/com/provedcode/utill/ValidateTalentForCompliance.java new file mode 100644 index 0000000..b6103e0 --- /dev/null +++ b/src/main/java/com/provedcode/utill/ValidateTalentForCompliance.java @@ -0,0 +1,42 @@ +package com.provedcode.utill; + +import com.provedcode.talent.model.entity.Talent; +import com.provedcode.talent.model.entity.TalentProof; +import com.provedcode.user.model.entity.UserInfo; +import org.springframework.stereotype.Service; +import org.springframework.web.server.ResponseStatusException; + +import java.util.Optional; + +import static org.springframework.http.HttpStatus.FORBIDDEN; +import static org.springframework.http.HttpStatus.NOT_FOUND; + +@Service +public class ValidateTalentForCompliance { + + public void userVerification(Optional talent, Optional userInfo, long id) { + if (talent.isEmpty() || userInfo.isEmpty()) { + throw new ResponseStatusException(NOT_FOUND, String.format("talent with id = %d not found", id)); + } + if (userInfo.get().getTalent().getId() != id) { + throw new ResponseStatusException(FORBIDDEN, "you can`t add proof another user"); + } + } + + public void userVerification(Optional talent, + Optional talentProof, + Optional userInfo, long talentId, long proofId) { + if (talent.isEmpty() && userInfo.isEmpty()) { + throw new ResponseStatusException(NOT_FOUND, String.format("talent with id = %d not found", talentId)); + } + if (userInfo.get().getTalent().getId() != talentId) { + throw new ResponseStatusException(FORBIDDEN, "you can`t delete/update another user"); + } + if (talentProof.isEmpty()) { + throw new ResponseStatusException(NOT_FOUND, String.format("proof with id = %d not found", proofId)); + } + if (talentProof.get().getTalentId() != talentId) { + throw new ResponseStatusException(FORBIDDEN, "you can`t delete/update another proof"); + } + } +} From 27c4b488458baec698073611ee62b3087c7affef Mon Sep 17 00:00:00 2001 From: Ren Date: Sat, 8 Apr 2023 21:49:29 +0300 Subject: [PATCH 7/8] Little optimize and cleanup code, rename "sortDir" to "orderBy" --- .../controller/TalentProofController.java | 10 ++-- .../talent/service/TalentProofService.java | 52 ++++++++++--------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/provedcode/talent/controller/TalentProofController.java b/src/main/java/com/provedcode/talent/controller/TalentProofController.java index 50c4c02..cd2e0e7 100644 --- a/src/main/java/com/provedcode/talent/controller/TalentProofController.java +++ b/src/main/java/com/provedcode/talent/controller/TalentProofController.java @@ -24,16 +24,16 @@ public class TalentProofController { @GetMapping("/proofs") Page getAllProofs(@RequestParam(value = "page") Optional page, @RequestParam(value = "size") Optional size, - @RequestParam(value = "sortDir") Optional sortDir) { - return talentProofService.getAllProofsPage(page, size, sortDir).map(talentProofMapper::toProofDTO); + @RequestParam(value = "order-by") Optional orderBy) { + return talentProofService.getAllProofsPage(page, size, orderBy).map(talentProofMapper::toProofDTO); } @PreAuthorize("hasRole('TALENT')") @DeleteMapping("/{talent-id}/proofs/{proof-id}") - SessionInfoDTO deleteProof(@PathVariable(value = "talent-id") long talent_id, - @PathVariable(value = "proof-id") long proof_id, + SessionInfoDTO deleteProof(@PathVariable(value = "talent-id") long talentId, + @PathVariable(value = "proof-id") long proofId, Authentication authentication) { - return talentProofService.deleteProofById(talent_id, proof_id, authentication); + return talentProofService.deleteProofById(talentId, proofId, authentication); } @PreAuthorize("hasRole('TALENT')") diff --git a/src/main/java/com/provedcode/talent/service/TalentProofService.java b/src/main/java/com/provedcode/talent/service/TalentProofService.java index 967dccf..40e0d9f 100644 --- a/src/main/java/com/provedcode/talent/service/TalentProofService.java +++ b/src/main/java/com/provedcode/talent/service/TalentProofService.java @@ -26,7 +26,8 @@ import java.time.LocalDateTime; import java.util.Optional; -import static org.springframework.http.HttpStatus.*; +import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.HttpStatus.NOT_IMPLEMENTED; @Service @AllArgsConstructor @@ -38,7 +39,7 @@ public class TalentProofService { ValidateTalentForCompliance validateTalentForCompliance; public Page getAllProofsPage(Optional page, Optional size, - Optional sortDir) { + Optional orderBy) { if (page.orElse(pageProperties.defaultPageNum()) < 0) { throw new ResponseStatusException(BAD_REQUEST, "'page' query parameter must be greater than or equal to 0"); } @@ -46,24 +47,27 @@ public Page getAllProofsPage(Optional page, Optional addProof(AddProofDTO addProofDTO, long talentId, Authen validateTalentForCompliance.userVerification(talent, userInfo, talentId); TalentProof talentProof = TalentProof.builder() - .talent(talent.get()) - .talentId(talentId) - .link(addProofDTO.link()) - .text(addProofDTO.text()) - .status(ProofStatus.DRAFT) - .created(LocalDateTime.now()) - .build(); + .talent(talent.get()) + .talentId(talentId) + .link(addProofDTO.link()) + .text(addProofDTO.text()) + .status(ProofStatus.DRAFT) + .created(LocalDateTime.now()) + .build(); talentProofRepository.save(talentProof); @@ -103,4 +107,4 @@ public ResponseEntity addProof(AddProofDTO addProofDTO, long talentId, Authen return ResponseEntity.created(location).build(); } -} +} \ No newline at end of file From 235314b1d84474d1d132437475be457671e0704f Mon Sep 17 00:00:00 2001 From: Ren Date: Sat, 8 Apr 2023 21:53:16 +0300 Subject: [PATCH 8/8] Little optimize and cleanup code --- .../talent/service/TalentProofService.java | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/provedcode/talent/service/TalentProofService.java b/src/main/java/com/provedcode/talent/service/TalentProofService.java index c481748..772133b 100644 --- a/src/main/java/com/provedcode/talent/service/TalentProofService.java +++ b/src/main/java/com/provedcode/talent/service/TalentProofService.java @@ -3,7 +3,6 @@ import com.provedcode.config.PageProperties; import com.provedcode.talent.model.ProofStatus; import com.provedcode.talent.model.dto.AddProofDTO; -import com.provedcode.talent.model.entity.Talent; import com.provedcode.talent.model.dto.FullProofDTO; import com.provedcode.talent.model.dto.ProofDTO; import com.provedcode.talent.model.entity.Talent; @@ -14,18 +13,13 @@ import com.provedcode.user.model.entity.UserInfo; import com.provedcode.user.repo.UserInfoRepository; import com.provedcode.utill.ValidateTalentForCompliance; -import com.provedcode.talent.repo.TalentRepository; -import com.provedcode.user.model.entity.UserInfo; -import com.provedcode.user.repo.UserInfoRepository; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; -import org.springframework.http.ResponseEntity; -import org.springframework.security.core.Authentication; -import org.springframework.data.domain.Sort; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -120,11 +114,16 @@ public ResponseEntity addProof(AddProofDTO addProofDTO, long talentId, Authen } public FullProofDTO getTalentProofs(Long talentId, Optional page, Optional size, - Optional direction, Authentication authentication, String... sortProperties) { + Optional direction, Authentication authentication, + String... sortProperties) { Talent talent = talentRepository.findById(talentId) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Talent with id = %s not found".formatted(talentId))); + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, + "Talent with id = %s not found".formatted( + talentId))); UserInfo userInfo = userInfoRepository.findByLogin(authentication.getName()) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Talent with id = %s not found".formatted(talentId))); + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, + "Talent with id = %s not found".formatted( + talentId))); Page proofs; PageRequest pageRequest; String sortDirection = direction.orElseGet(Sort.DEFAULT_DIRECTION::name); @@ -135,7 +134,8 @@ public FullProofDTO getTalentProofs(Long talentId, Optional page, Optio if (size.orElse(pageProperties.defaultPageSize()) <= 0) { throw new ResponseStatusException(BAD_REQUEST, "'size' query parameter must be greater than or equal to 1"); } - if (!sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name()) && !sortDirection.equalsIgnoreCase(Sort.Direction.DESC.name())) { + if (!sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name()) && + !sortDirection.equalsIgnoreCase(Sort.Direction.DESC.name())) { throw new ResponseStatusException(BAD_REQUEST, "'direction' query param must be equals ASC or DESC"); } @@ -156,17 +156,17 @@ public FullProofDTO getTalentProofs(Long talentId, Optional page, Optio } return FullProofDTO.builder() - .id(talent.getId()) - .image(talent.getImage()) - .firstName(talent.getFirstName()) - .lastName(talent.getLastName()) - .specialization(talent.getSpecialization()) - .proofs(proofs.map(i -> ProofDTO.builder() - .id(i.getId()) - .created(i.getCreated().toString()) - .link(i.getLink()) - .text(i.getText()) - .status(i.getStatus()).build())) - .build(); + .id(talent.getId()) + .image(talent.getImage()) + .firstName(talent.getFirstName()) + .lastName(talent.getLastName()) + .specialization(talent.getSpecialization()) + .proofs(proofs.map(i -> ProofDTO.builder() + .id(i.getId()) + .created(i.getCreated().toString()) + .link(i.getLink()) + .text(i.getText()) + .status(i.getStatus()).build())) + .build(); } } \ No newline at end of file