From 4bdca4f243a4f31bffdf27505baa0c5975b543b0 Mon Sep 17 00:00:00 2001 From: Mario Sousa Date: Sun, 3 Oct 2021 12:18:09 +0100 Subject: [PATCH 1/8] Replace Cordel with CordelDto in public api responses. Add new field xilogravuraUrl to DTO. Remove Cordel from openapi spec. --- .github/workflows/new-pr.yml | 2 +- openapi.yaml | 41 +++---------------- .../itsmemario/ecordel/author/AuthorDto.java | 9 ++++ .../com/itsmemario/ecordel/cordel/Cordel.java | 3 +- .../ecordel/cordel/CordelController.java | 17 ++++---- .../itsmemario/ecordel/cordel/CordelDto.java | 18 ++++++++ .../ecordel/cordel/CordelControllerTest.java | 4 +- .../ecordel/security/jwt/JwtTokenTest.java | 40 ------------------ 8 files changed, 46 insertions(+), 88 deletions(-) delete mode 100644 src/test/java/br/com/itsmemario/ecordel/security/jwt/JwtTokenTest.java diff --git a/.github/workflows/new-pr.yml b/.github/workflows/new-pr.yml index 00f4fd8..84c3aa8 100644 --- a/.github/workflows/new-pr.yml +++ b/.github/workflows/new-pr.yml @@ -15,6 +15,6 @@ jobs: uses: Ilshidur/action-discord@0.3.0 with: args: | - Nova contribuição recebida. \(>_<)/ + Nova contribuição recebida. \\(>_<)/ ${{ github.event.pull_request.title }} ${{ github.event.pull_request.html_url}} diff --git a/openapi.yaml b/openapi.yaml index 9b846d0..066a446 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -42,7 +42,7 @@ paths: - bearerAuth: [] tags: - Author - /cordels: + /cordels/summaries: get: summary: "Retrieve all cordels by pages" parameters: @@ -79,6 +79,7 @@ paths: $ref: '#/components/schemas/ReponsePage' tags: - Cordel + /cordels: post: summary: "Create a new cordel" requestBody: @@ -108,7 +109,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/FullCordel' + $ref: '#/components/schemas/Cordel' 404: description: "Not found" tags: @@ -240,6 +241,8 @@ components: content: type: string description: cordel text + xilogravuraUrl: + type: string published: type: boolean tags: @@ -261,30 +264,6 @@ components: xilogravuraUrl: type: string description: image url - FullCordel: - type: object - description: Cordel data with content. For operations like POST and PUT the inner objects only need the id - properties: - id: - type: integer - description: cordel id - author: - $ref: '#/components/schemas/Author' - title: - type: string - description: cordel title - description: - type: string - description: cordel's description - content: - type: string - description: cordel text - xilogravura: - $ref: '#/components/schemas/Xilogravura' - tags: - type: array - items: - type: string Page: type: object properties: @@ -333,16 +312,6 @@ components: type: string expiresAt: type: integer - Xilogravura: - type: object - properties: - id: - type: integer - description: - type: string - url: - type: string - securitySchemes: bearerAuth: type: http diff --git a/src/main/java/br/com/itsmemario/ecordel/author/AuthorDto.java b/src/main/java/br/com/itsmemario/ecordel/author/AuthorDto.java index 7ae340e..149d2d9 100644 --- a/src/main/java/br/com/itsmemario/ecordel/author/AuthorDto.java +++ b/src/main/java/br/com/itsmemario/ecordel/author/AuthorDto.java @@ -30,6 +30,15 @@ public class AuthorDto { private String name; private String email; + public static AuthorDto of(Author author) { + var dto = new AuthorDto(); + dto.setName(author.getName()); + dto.setAbout(author.getAbout()); + dto.setEmail(author.getEmail()); + dto.setId(author.getId()); + return dto; + } + public Author toEntity() { var author = new Author(); author.setId(id); diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/Cordel.java b/src/main/java/br/com/itsmemario/ecordel/cordel/Cordel.java index c0b5596..f52287f 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/Cordel.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/Cordel.java @@ -22,6 +22,7 @@ import javax.persistence.*; import javax.validation.constraints.NotBlank; +import java.util.Collections; import java.util.Objects; import java.util.Set; @@ -104,7 +105,7 @@ public void setDescription(String description) { } public Set getTags() { - return tags; + return Collections.unmodifiableSet(tags); } public void setTags(Set tags) { diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelController.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelController.java index cb0218d..03ea5e8 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelController.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelController.java @@ -45,19 +45,20 @@ public class CordelController { } @GetMapping("{id}") - public ResponseEntity getCordel(@PathVariable Long id) { + public ResponseEntity getCordel(@PathVariable Long id) { logger.info("request received get cordel by id: {}", id); Optional cordel = service.findById(id); if (cordel.isPresent()) { - return ResponseEntity.ok(cordel.get()); + CordelDto body = CordelDto.of(cordel.get()); + return ResponseEntity.ok(body); } else { logger.info("cordel with id {} not fond", id); return ResponseEntity.notFound().build(); } } - @GetMapping + @GetMapping("summaries") public Page getCordels(@RequestParam(required = false) String title, @RequestParam(defaultValue = "true") boolean published, Pageable pageable) { @@ -76,14 +77,14 @@ public ResponseEntity create(@RequestBody @Valid CordelDto dto, UriCompo } @PutMapping("{id}/xilogravura") - public ResponseEntity putXilogravura(@PathVariable Long id, Xilogravura xilogravura, @RequestParam("file") MultipartFile file) { + public ResponseEntity putXilogravura(@PathVariable Long id, Xilogravura xilogravura, @RequestParam("file") MultipartFile file) { logger.info("request received, update xilogravura for cordel: {}", id); Cordel cordel = service.updateXilogravura(id, xilogravura, file); - return ResponseEntity.ok(cordel); + return ResponseEntity.ok(CordelDto.of(cordel)); } @PutMapping("{id}") - public ResponseEntity update(@RequestBody @Valid CordelDto dto, @PathVariable Long id) { + public ResponseEntity update(@RequestBody @Valid CordelDto dto, @PathVariable Long id) { logger.info("request received update cordel with id: {}", id); Optional existingCordel = service.findById(id); @@ -93,8 +94,8 @@ public ResponseEntity update(@RequestBody @Valid CordelDto dto, @PathVar var cordel = dto.toEntity(); cordel.setId(id); - - return ResponseEntity.ok(service.save(cordel)); + Cordel updatedCordel = service.save(cordel); + return ResponseEntity.ok(CordelDto.of(updatedCordel)); } } diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelDto.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelDto.java index b9c0e7a..399a056 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelDto.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelDto.java @@ -41,6 +41,23 @@ public class CordelDto { private String content; private boolean published; private Set tags; + private String xilogravuraUrl; + + public static CordelDto of( Cordel cordel ){ + var dto = new CordelDto(); + var authorDto = AuthorDto.of(cordel.getAuthor()); + dto.setAuthor(authorDto); + dto.setTags(cordel.getTags()); + dto.setPublished(cordel.isPublished()); + dto.setId(cordel.getId()); + dto.setContent(cordel.getContent()); + dto.setDescription(cordel.getDescription()); + dto.setTitle(cordel.getTitle()); + if(cordel.getXilogravura()!=null) { + dto.setXilogravuraUrl(cordel.getXilogravura().getUrl()); + } + return dto; + } public Cordel toEntity() { var cordel = new Cordel(); @@ -51,6 +68,7 @@ public Cordel toEntity() { cordel.setContent( content ); cordel.setPublished( published ); cordel.setTags( tags ); +// TODO create xilogravura from url return cordel; } diff --git a/src/test/java/br/com/itsmemario/ecordel/cordel/CordelControllerTest.java b/src/test/java/br/com/itsmemario/ecordel/cordel/CordelControllerTest.java index 457f32b..60808a7 100644 --- a/src/test/java/br/com/itsmemario/ecordel/cordel/CordelControllerTest.java +++ b/src/test/java/br/com/itsmemario/ecordel/cordel/CordelControllerTest.java @@ -85,7 +85,7 @@ void ifACordelDoesNotExists_ItMustReturn404() { void ifPublishedParamIsFalse_theOnlyDraftCordelsMustBeRetrieved() { insertCordel(false); - ResponseEntity response = restTemplate.getForEntity(getBaseUrl()+"?published=false", Map.class); + ResponseEntity response = restTemplate.getForEntity(getBaseUrl()+"/summaries?published=false", Map.class); assertThat(response.getBody()).containsEntry("totalElements",1); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); } @@ -94,7 +94,7 @@ void ifPublishedParamIsFalse_theOnlyDraftCordelsMustBeRetrieved() { void ifPublishedParamIsNotInformed_itMustBeConsideredTrue() { insertCordel(true); - ResponseEntity response = restTemplate.getForEntity(getBaseUrl(), Map.class); + ResponseEntity response = restTemplate.getForEntity(getBaseUrl()+"/summaries", Map.class); assertThat(response.getBody()).containsEntry("totalElements",1); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); } diff --git a/src/test/java/br/com/itsmemario/ecordel/security/jwt/JwtTokenTest.java b/src/test/java/br/com/itsmemario/ecordel/security/jwt/JwtTokenTest.java deleted file mode 100644 index 5f963de..0000000 --- a/src/test/java/br/com/itsmemario/ecordel/security/jwt/JwtTokenTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2021 Projeto e-cordel (http://ecordel.com.br) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package br.com.itsmemario.ecordel.security.jwt; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import static org.assertj.core.api.Assertions.assertThat; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class JwtTokenTest { - - @Autowired - private JwtToken jwtToken; - - @Test - public void getJwtTokenTest() { - assertThat(jwtToken).isNotNull(); - } - -} From e0e272f6c804106f789bcc6398de8202795d8e91 Mon Sep 17 00:00:00 2001 From: Mario Sousa Date: Wed, 6 Oct 2021 21:01:58 +0100 Subject: [PATCH 2/8] Remove Xilogravura entity --- pom.xml | 2 +- .../com/itsmemario/ecordel/cordel/Cordel.java | 13 ++-- .../ecordel/cordel/CordelController.java | 5 +- .../itsmemario/ecordel/cordel/CordelDto.java | 6 +- .../ecordel/cordel/CordelService.java | 11 +--- .../itsmemario/ecordel/cordel/CordelView.java | 3 +- .../cordel/CustomCordelRepositoryImpl.java | 7 +- .../ecordel/xilogravura/Xilogravura.java | 65 ------------------- .../xilogravura/XilogravuraRepository.java | 24 ------- .../xilogravura/XilogravuraService.java | 15 ++--- .../V1.3__create-jwt-token-table.sql | 2 +- .../db/migration/V1__schema-definition.sql | 24 +------ .../XilogravuraRepositoryTest.java | 31 --------- .../xilogravura/XilogravuraServiceTest.java | 21 ++---- .../resources/db/data}/data.sql | 49 ++++++-------- 15 files changed, 48 insertions(+), 230 deletions(-) delete mode 100644 src/main/java/br/com/itsmemario/ecordel/xilogravura/Xilogravura.java delete mode 100644 src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraRepository.java delete mode 100644 src/test/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraRepositoryTest.java rename src/{main/resources/db/migration => test/resources/db/data}/data.sql (66%) diff --git a/pom.xml b/pom.xml index a750040..f90b10d 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ br.com.itsmemario ecordel - 0.6.0-SNAPSHOT + 0.7.0-SNAPSHOT e-cordel e-reader for cordels diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/Cordel.java b/src/main/java/br/com/itsmemario/ecordel/cordel/Cordel.java index f52287f..93b648d 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/Cordel.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/Cordel.java @@ -18,7 +18,6 @@ package br.com.itsmemario.ecordel.cordel; import br.com.itsmemario.ecordel.author.Author; -import br.com.itsmemario.ecordel.xilogravura.Xilogravura; import javax.persistence.*; import javax.validation.constraints.NotBlank; @@ -39,9 +38,7 @@ public class Cordel implements CordelView { private String title; @NotBlank private String content; - @OneToOne - @JoinColumn(name = "xilogravura_id") - private Xilogravura xilogravura; + private String xilogravuraUrl; private String description; @ElementCollection @CollectionTable(name = "cordel_tags") @@ -88,12 +85,12 @@ public void setContent(String content) { this.content = content; } - public Xilogravura getXilogravura() { - return xilogravura; + public String getXilogravuraUrl() { + return xilogravuraUrl; } - public void setXilogravura(Xilogravura xilogravura) { - this.xilogravura = xilogravura; + public void setXilogravuraUrl(String xilogravuraUrl) { + this.xilogravuraUrl = xilogravuraUrl; } public String getDescription() { diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelController.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelController.java index 03ea5e8..574a94c 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelController.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelController.java @@ -17,7 +17,6 @@ package br.com.itsmemario.ecordel.cordel; -import br.com.itsmemario.ecordel.xilogravura.Xilogravura; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -77,9 +76,9 @@ public ResponseEntity create(@RequestBody @Valid CordelDto dto, UriCompo } @PutMapping("{id}/xilogravura") - public ResponseEntity putXilogravura(@PathVariable Long id, Xilogravura xilogravura, @RequestParam("file") MultipartFile file) { + public ResponseEntity putXilogravura(@PathVariable Long id, @RequestParam("file") MultipartFile file) { logger.info("request received, update xilogravura for cordel: {}", id); - Cordel cordel = service.updateXilogravura(id, xilogravura, file); + Cordel cordel = service.updateXilogravura(id, file); return ResponseEntity.ok(CordelDto.of(cordel)); } diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelDto.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelDto.java index 399a056..1761b5a 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelDto.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelDto.java @@ -53,9 +53,7 @@ public static CordelDto of( Cordel cordel ){ dto.setContent(cordel.getContent()); dto.setDescription(cordel.getDescription()); dto.setTitle(cordel.getTitle()); - if(cordel.getXilogravura()!=null) { - dto.setXilogravuraUrl(cordel.getXilogravura().getUrl()); - } + dto.setXilogravuraUrl(cordel.getXilogravuraUrl()); return dto; } @@ -68,7 +66,7 @@ public Cordel toEntity() { cordel.setContent( content ); cordel.setPublished( published ); cordel.setTags( tags ); -// TODO create xilogravura from url + cordel.setXilogravuraUrl( xilogravuraUrl ); return cordel; } diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelService.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelService.java index 9f17d62..5776bff 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelService.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelService.java @@ -17,7 +17,6 @@ package br.com.itsmemario.ecordel.cordel; -import br.com.itsmemario.ecordel.xilogravura.Xilogravura; import br.com.itsmemario.ecordel.xilogravura.XilogravuraService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; @@ -41,10 +40,6 @@ public CordelService(CordelRepository repository, XilogravuraService xilogravura this.xilogravuraService = xilogravuraService; } - public Page getCordels(Pageable pageable) { - return repository.findAllProjectedBy(pageable); - } - public Cordel save(Cordel cordel) { return repository.save(cordel); } @@ -61,13 +56,13 @@ public Page findPublishedByTitle(boolean published, String title, return repository.findPublishedByTitleLike(published, title, pageable); } - public Cordel updateXilogravura(Long cordelId, Xilogravura xilogravura, MultipartFile file) { + public Cordel updateXilogravura(Long cordelId, MultipartFile file) { Optional byId = findById(cordelId); if(byId.isPresent()) { Cordel cordel = byId.get(); - Xilogravura xilogravuraWithFile = xilogravuraService.createXilogravuraWithFile(xilogravura, file); - cordel.setXilogravura(xilogravuraWithFile); + String xilogravuraUrl = xilogravuraService.createXilogravuraWithFile(file); + cordel.setXilogravuraUrl( xilogravuraUrl ); return save(cordel); }else{ throw new CordelNotFoundException(); diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelView.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelView.java index 42ee317..51cada9 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelView.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelView.java @@ -18,7 +18,6 @@ package br.com.itsmemario.ecordel.cordel; import br.com.itsmemario.ecordel.author.AuthorView; -import br.com.itsmemario.ecordel.xilogravura.Xilogravura; import java.util.Set; @@ -28,7 +27,7 @@ public interface CordelView { Long getId(); AuthorView getAuthor(); String getTitle(); - Xilogravura getXilogravura(); + String getXilogravuraUrl(); String getDescription(); Set getTags(); diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepositoryImpl.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepositoryImpl.java index 7d95605..8f8ed26 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepositoryImpl.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepositoryImpl.java @@ -52,11 +52,10 @@ class CustomCordelRepositoryImpl implements CustomCordelRepository { private static final String FIND_BY_TAGS_SQL_COUNT = "select COUNT(distinct(c.id)) " + FIND_BY_TAGS_SQL_FROM ; - private static final String CORDEL_SUMMARY = " SELECT new br.com.itsmemario.ecordel.cordel.CordelSummary(c.id, c.title, x.url, a.name) " + - " FROM Cordel c JOIN c.author a LEFT JOIN c.xilogravura x WHERE c.published = :published "; + private static final String CORDEL_SUMMARY = " SELECT new br.com.itsmemario.ecordel.cordel.CordelSummary(c.id, c.title, c.xilogravuraUrl, a.name) " + + " FROM Cordel c JOIN c.author a WHERE c.published = :published "; - private static final String COUNT_CORDEL_SUMMARY = "SELECT COUNT(c.id) FROM Cordel c JOIN c.author a " + - " LEFT JOIN c.xilogravura x WHERE c.published = :published "; + private static final String COUNT_CORDEL_SUMMARY = "SELECT COUNT(c.id) FROM Cordel c JOIN c.author a WHERE c.published = :published "; private static final String TITLE = "title"; public static final String PUBLISHED = "published"; diff --git a/src/main/java/br/com/itsmemario/ecordel/xilogravura/Xilogravura.java b/src/main/java/br/com/itsmemario/ecordel/xilogravura/Xilogravura.java deleted file mode 100644 index 1d6a5d9..0000000 --- a/src/main/java/br/com/itsmemario/ecordel/xilogravura/Xilogravura.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2020 Projeto e-cordel (http://ecordel.com.br) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package br.com.itsmemario.ecordel.xilogravura; - -import br.com.itsmemario.ecordel.author.Author; - -import javax.persistence.*; - -@Entity -public class Xilogravura { - - @Id - @GeneratedValue(strategy= GenerationType.IDENTITY) - private Long id; - - private String url; - private String description; - - @ManyToOne - @JoinColumn(name = "xilografo_id") - private Author xilografo; - - public Long getId() { - return id; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public Author getXilografo() { - return xilografo; - } - - public void setXilografo(Author xilografo) { - this.xilografo = xilografo; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } -} diff --git a/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraRepository.java b/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraRepository.java deleted file mode 100644 index d402c2e..0000000 --- a/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraRepository.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2020 Projeto e-cordel (http://ecordel.com.br) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package br.com.itsmemario.ecordel.xilogravura; - -import org.springframework.data.jpa.repository.JpaRepository; - -interface XilogravuraRepository extends JpaRepository { - -} diff --git a/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraService.java b/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraService.java index 1c1c4b3..2e95220 100644 --- a/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraService.java +++ b/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraService.java @@ -32,25 +32,18 @@ public class XilogravuraService { private static final String JPG = ".jpg"; private final String XILOGRAVURA_NAME_PATTERN = "http://xilos.ecordel.com.br/{file}"; - private XilogravuraRepository repository; private FileManager fileManager; @Autowired - public XilogravuraService(XilogravuraRepository repository, FileManager fileManager) { - this.repository = repository; + public XilogravuraService(FileManager fileManager) { this.fileManager = fileManager; } - public Xilogravura save(Xilogravura xilogravura){ - return repository.save(xilogravura); - } - - public Xilogravura createXilogravuraWithFile(Xilogravura xilogravura, MultipartFile file){ + public String createXilogravuraWithFile(MultipartFile file){ try { +// todo move this code to file manager? String fileName = generateRandomFileName(); - String url = fileManager.saveFile(file.getBytes(), fileName); - xilogravura.setUrl(url); - return repository.save(xilogravura); + return fileManager.saveFile(file.getBytes(), fileName); } catch (IOException e) { e.printStackTrace(); throw new FileProcessException("Error while saving file", e); diff --git a/src/main/resources/db/migration/V1.3__create-jwt-token-table.sql b/src/main/resources/db/migration/V1.3__create-jwt-token-table.sql index 9ef1049..fab9d62 100644 --- a/src/main/resources/db/migration/V1.3__create-jwt-token-table.sql +++ b/src/main/resources/db/migration/V1.3__create-jwt-token-table.sql @@ -21,4 +21,4 @@ ALTER SEQUENCE public.jwt_token_id_seq OWNED BY jwt_token.id; ALTER TABLE ONLY jwt_token ALTER COLUMN id SET DEFAULT nextval('public.jwt_token_id_seq'::regclass); -INSERT INTO jwt_token (id, expiration, secret_key, active) VALUES ((SELECT nextval('public.jwt_token_id_seq')), 86400000, 'rm''!@N=Ke!~p8VTA2ZRK~nMDQX5Uvm!m''D&]{@Vr?G;2?XhbC:Qa#9#eMLN\}x3?JR3.2zr~v)gYF^8\:8>:XfB:Ww75N/emt9Yj[bQMNCWwW\J?N,nvH.<2\.r~w]*e~vgak)X"v8H`MH/7"2E`,^k@nL/zBq`}C6tT*cCSVC^c]-L}&/', true); \ No newline at end of file +INSERT INTO jwt_token (id, expiration, secret_key, active) VALUES ((SELECT nextval('public.jwt_token_id_seq')), 86400000, 'rm''!@N=Ke!~p8VTA2ZRK~nMDQX5Uvm!m''D&]{@Vr?G;2?XhbC:Qa#9#eMLN\}x3?JR3.2zr~v)gYF^8\:8>:XfB:Ww75N/emt9Yj[bQMNCWwW\J?N,nvH.<2\.r~w]*e~vgak)X"v8H`MH/7"2E`,^k@nL/zBq`}C6tT*cCSVC^c]-L}&/', true); diff --git a/src/main/resources/db/migration/V1__schema-definition.sql b/src/main/resources/db/migration/V1__schema-definition.sql index 729231d..c206681 100644 --- a/src/main/resources/db/migration/V1__schema-definition.sql +++ b/src/main/resources/db/migration/V1__schema-definition.sql @@ -19,32 +19,14 @@ CREATE SEQUENCE public.author_id_seq ALTER SEQUENCE public.author_id_seq OWNED BY public.author.id; -CREATE TABLE public.xilogravura ( - id bigint primary key, - description character varying(255), - url character varying(255), - xilografo_id bigint, - FOREIGN KEY (xilografo_id) REFERENCES author (id) -); - -CREATE SEQUENCE public.xilogravura_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - -ALTER SEQUENCE public.xilogravura_id_seq OWNED BY public.xilogravura.id; - CREATE TABLE public.cordel ( id bigint primary key, author_id bigint, content text, description character varying(255), title character varying(255), - xilogravura_id bigint, - FOREIGN KEY (author_id) REFERENCES author (id), - FOREIGN KEY (xilogravura_id) REFERENCES xilogravura (id) + xilogravura_url character varying(255), + FOREIGN KEY (author_id) REFERENCES author (id) ); CREATE SEQUENCE public.cordel_id_seq @@ -101,8 +83,6 @@ ALTER TABLE ONLY public.cordel ALTER COLUMN id SET DEFAULT nextval('public.corde ALTER TABLE ONLY public.author ALTER COLUMN id SET DEFAULT nextval('public.author_id_seq'::regclass); -ALTER TABLE ONLY public.xilogravura ALTER COLUMN id SET DEFAULT nextval('public.xilogravura_id_seq'::regclass); - ALTER TABLE ONLY public.cordel_user ALTER COLUMN id SET DEFAULT nextval('public.cordel_user_id_seq'::regclass); ALTER TABLE ONLY public.cordel_authority ALTER COLUMN id SET DEFAULT nextval('public.cordel_authority_id_seq'::regclass); diff --git a/src/test/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraRepositoryTest.java b/src/test/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraRepositoryTest.java deleted file mode 100644 index dd1665a..0000000 --- a/src/test/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraRepositoryTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package br.com.itsmemario.ecordel.xilogravura; - - -import br.com.itsmemario.ecordel.AbstractIntegrationTest; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -import static org.assertj.core.api.Assertions.assertThat; - -@ExtendWith(SpringExtension.class) -@SpringBootTest -public class XilogravuraRepositoryTest extends AbstractIntegrationTest { - - @Autowired - XilogravuraRepository repository; - - @Test - public void saveTest() { - Xilogravura xilogravura = new Xilogravura(); - xilogravura.setUrl("url"); - xilogravura.setDescription("description"); - - Xilogravura saved = repository.save(xilogravura); - - assertThat(saved).isNotNull(); - assertThat(saved.getId()).isGreaterThan(0); - } -} diff --git a/src/test/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraServiceTest.java b/src/test/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraServiceTest.java index a19b25a..2377e11 100644 --- a/src/test/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraServiceTest.java +++ b/src/test/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraServiceTest.java @@ -2,7 +2,6 @@ import br.com.itsmemario.ecordel.AbstractIntegrationTest; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -29,9 +28,6 @@ public class XilogravuraServiceTest extends AbstractIntegrationTest { @Autowired XilogravuraService service; - @Autowired - XilogravuraRepository repository; - static final String FTP_HOME = "/ftp"; static final String USER = "user"; static final String PASSWORD = "pass"; @@ -57,23 +53,14 @@ public static void afterClass() throws Exception { fakeFtpServer.stop(); } - @AfterEach - public void tearDown() throws Exception { - repository.deleteAll(); - } - @Test public void createXilogravuraWithFile() { - Xilogravura xilogravura = new Xilogravura(); - xilogravura.setDescription("Description"); - MockMultipartFile file = new MockMultipartFile("file.txt", "content".getBytes()); - Xilogravura xilogravuraWithFile = service.createXilogravuraWithFile(xilogravura, file); - System.out.println(xilogravuraWithFile.getUrl()); + String xilogravuraUrl = service.createXilogravuraWithFile(file); + System.out.println(xilogravuraUrl); - assertThat(xilogravuraWithFile.getUrl()).isNotEmpty(); - assertThat(xilogravuraWithFile.getId()).isGreaterThan(0); - assertThat(repository.findById(1l)).isNotEmpty(); + assertThat(xilogravuraUrl).isNotEmpty(); + //TODO check if file exists } } \ No newline at end of file diff --git a/src/main/resources/db/migration/data.sql b/src/test/resources/db/data/data.sql similarity index 66% rename from src/main/resources/db/migration/data.sql rename to src/test/resources/db/data/data.sql index 330f946..6dcac47 100644 --- a/src/main/resources/db/migration/data.sql +++ b/src/test/resources/db/data/data.sql @@ -7,50 +7,41 @@ values ('Author 2', 'About Author 2', 'author2@ecordel.com'); insert into author (name, about, email) values ('Author 3', 'About Author 3', 'author3@ecordel.com'); -insert into xilogravura (description, xilografo_id, url) -values ('xilo', 1, 'https://i.pinimg.com/originals/25/9d/47/259d47304bf26a4678cb039b8d8ce7f9.jpg'); - -insert into xilogravura (description, xilografo_id, url) -values ('xilo', 2, 'https://i.pinimg.com/564x/81/bf/ba/81bfbafaba269d02d357c9fa88e1a856.jpg'); - -insert into xilogravura (description, xilografo_id, url) -values ('xilo', 3, 'https://www5.usp.br/wp-content/uploads/portal20140507_1.jpg'); - -insert into cordel (title, author_id, content, xilogravura_id, description) -values ('Title 01', 1, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 1, 'Cordel descrition'); +insert into cordel (title, author_id, content, xilogravura_url, description) +values ('Title 01', 1, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 'https://i.pinimg.com/originals/25/9d/47/259d47304bf26a4678cb039b8d8ce7f9.jpg', 'Cordel descrition'); insert into cordel_tags (cordel_id, tags) values (1,'tag1'); insert into cordel_tags (cordel_id, tags) values (1,'tag2'); insert into cordel_tags (cordel_id, tags) values (1,'tag3'); -insert into cordel (title, author_id, content, xilogravura_id, description) -values ('Title 01', 2, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 2, 'Cordel descrition'); +insert into cordel (title, author_id, content, xilogravura_url, description) +values ('Title 01', 2, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 'https://i.pinimg.com/originals/25/9d/47/259d47304bf26a4678cb039b8d8ce7f9.jpg', 'Cordel descrition'); insert into cordel_tags (cordel_id, tags) values (2,'tag1'); insert into cordel_tags (cordel_id, tags) values (2,'tag2'); insert into cordel_tags (cordel_id, tags) values (2,'tag3'); -insert into cordel (title, author_id, content, xilogravura_id, description) -values ('Title 03', 3, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 3, 'Cordel descrition'); +insert into cordel (title, author_id, content, xilogravura_url, description) +values ('Title 03', 3, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 'https://i.pinimg.com/originals/25/9d/47/259d47304bf26a4678cb039b8d8ce7f9.jpg', 'Cordel descrition'); -insert into cordel (title, author_id, content, xilogravura_id, description) -values ('Title 04', 1, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 1, 'Cordel descrition'); +insert into cordel (title, author_id, content, xilogravura_url, description) +values ('Title 04', 1, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 'https://i.pinimg.com/originals/25/9d/47/259d47304bf26a4678cb039b8d8ce7f9.jpg', 'Cordel descrition'); -insert into cordel (title, author_id, content, xilogravura_id, description) -values ('Title 05', 2, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 2, 'Cordel descrition'); +insert into cordel (title, author_id, content, xilogravura_url, description) +values ('Title 05', 2, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 'https://i.pinimg.com/originals/25/9d/47/259d47304bf26a4678cb039b8d8ce7f9.jpg', 'Cordel descrition'); -insert into cordel (title, author_id, content, xilogravura_id, description) -values ('Title 06', 3, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 3, 'Cordel descrition'); +insert into cordel (title, author_id, content, xilogravura_url, description) +values ('Title 06', 3, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 'https://i.pinimg.com/originals/25/9d/47/259d47304bf26a4678cb039b8d8ce7f9.jpg', 'Cordel descrition'); -insert into cordel (title, author_id, content, xilogravura_id, description) -values ('Title 07', 1, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 1, 'Cordel descrition'); +insert into cordel (title, author_id, content, xilogravura_url, description) +values ('Title 07', 1, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 'https://i.pinimg.com/originals/25/9d/47/259d47304bf26a4678cb039b8d8ce7f9.jpg', 'Cordel descrition'); -insert into cordel (title, author_id, content, xilogravura_id, description) -values ('Title 08', 2, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 2, 'Cordel descrition'); +insert into cordel (title, author_id, content, xilogravura_url, description) +values ('Title 08', 2, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 'https://i.pinimg.com/originals/25/9d/47/259d47304bf26a4678cb039b8d8ce7f9.jpg', 'Cordel descrition'); -insert into cordel (title, author_id, content, xilogravura_id, description) -values ('Title 09', 3, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 3, 'Cordel descrition'); +insert into cordel (title, author_id, content, xilogravura_url, description) +values ('Title 09', 3, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 'https://i.pinimg.com/originals/25/9d/47/259d47304bf26a4678cb039b8d8ce7f9.jpg', 'Cordel descrition'); -insert into cordel (title, author_id, content, xilogravura_id, description) -values ('Title 10', 1, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 1, 'Cordel descrition'); +insert into cordel (title, author_id, content, xilogravura_url, description) +values ('Title 10', 1, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis nisi, mollis vitae odio ac, dignissim gravida nunc. Nam ac bibendum lectus. Nulla id fermentum eros, sed ornare risus. Pellentesque faucibus dui et luctus efficitur.', 'https://i.pinimg.com/originals/25/9d/47/259d47304bf26a4678cb039b8d8ce7f9.jpg', 'Cordel descrition'); insert into cordel_authority(authority) values ('ADMIN'); insert into cordel_authority(authority) values ('USER'); From e960f4ee3b787327e3aad1905ca99b0fe9294e7e Mon Sep 17 00:00:00 2001 From: Mario Sousa Date: Wed, 6 Oct 2021 22:04:51 +0100 Subject: [PATCH 3/8] Merge migrations files --- pom.xml | 2 +- .../cordel/CustomCordelRepository.java | 1 + .../xilogravura/XilogravuraService.java | 1 - ...tion.sql => V1.0.0__schema-definition.sql} | 27 +++++++++++++++++-- .../migration/V1.1__add-published-field.sql | 8 ------ .../V1.2__author-type-changed-field.sql | 8 ------ .../V1.3__create-jwt-token-table.sql | 24 ----------------- src/test/resources/application.yml | 5 ---- 8 files changed, 27 insertions(+), 49 deletions(-) rename src/main/resources/db/migration/{V1__schema-definition.sql => V1.0.0__schema-definition.sql} (70%) delete mode 100644 src/main/resources/db/migration/V1.1__add-published-field.sql delete mode 100644 src/main/resources/db/migration/V1.2__author-type-changed-field.sql delete mode 100644 src/main/resources/db/migration/V1.3__create-jwt-token-table.sql diff --git a/pom.xml b/pom.xml index f90b10d..80922fe 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.springframework.boot spring-boot-starter-parent - 2.5.1 + 2.5.5 diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepository.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepository.java index 64c23dc..754a5a2 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepository.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepository.java @@ -24,6 +24,7 @@ interface CustomCordelRepository { +// TODO remove? use CordelSummary Page findByTags(List tags, Pageable pageable); Page findPublishedByTitleLike(boolean published, String title, Pageable pageable); diff --git a/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraService.java b/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraService.java index 2e95220..159d078 100644 --- a/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraService.java +++ b/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraService.java @@ -30,7 +30,6 @@ public class XilogravuraService { private static final String JPG = ".jpg"; - private final String XILOGRAVURA_NAME_PATTERN = "http://xilos.ecordel.com.br/{file}"; private FileManager fileManager; diff --git a/src/main/resources/db/migration/V1__schema-definition.sql b/src/main/resources/db/migration/V1.0.0__schema-definition.sql similarity index 70% rename from src/main/resources/db/migration/V1__schema-definition.sql rename to src/main/resources/db/migration/V1.0.0__schema-definition.sql index c206681..8d4e469 100644 --- a/src/main/resources/db/migration/V1__schema-definition.sql +++ b/src/main/resources/db/migration/V1.0.0__schema-definition.sql @@ -5,8 +5,8 @@ CREATE TABLE public.author ( id bigint primary key, - name character varying(255), - about character varying(255), + name character varying(255) not null , + about text, email character varying(255) ); @@ -25,6 +25,7 @@ CREATE TABLE public.cordel ( content text, description character varying(255), title character varying(255), + published boolean not null default false, xilogravura_url character varying(255), FOREIGN KEY (author_id) REFERENCES author (id) ); @@ -86,3 +87,25 @@ ALTER TABLE ONLY public.author ALTER COLUMN id SET DEFAULT nextval('public.autho ALTER TABLE ONLY public.cordel_user ALTER COLUMN id SET DEFAULT nextval('public.cordel_user_id_seq'::regclass); ALTER TABLE ONLY public.cordel_authority ALTER COLUMN id SET DEFAULT nextval('public.cordel_authority_id_seq'::regclass); + +-- token + +CREATE TABLE IF NOT EXISTS jwt_token ( + id bigint primary key, + expiration bigint, + secret_key TEXT, + active boolean not null +); + +CREATE SEQUENCE IF NOT EXISTS jwt_token_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + +ALTER SEQUENCE public.jwt_token_id_seq OWNED BY jwt_token.id; + +ALTER TABLE ONLY jwt_token ALTER COLUMN id SET DEFAULT nextval('public.jwt_token_id_seq'::regclass); + +INSERT INTO jwt_token (id, expiration, secret_key, active) VALUES ((SELECT nextval('public.jwt_token_id_seq')), 86400000, 'rm''!@N=Ke!~p8VTA2ZRK~nMDQX5Uvm!m''D&]{@Vr?G;2?XhbC:Qa#9#eMLN\}x3?JR3.2zr~v)gYF^8\:8>:XfB:Ww75N/emt9Yj[bQMNCWwW\J?N,nvH.<2\.r~w]*e~vgak)X"v8H`MH/7"2E`,^k@nL/zBq`}C6tT*cCSVC^c]-L}&/', true); diff --git a/src/main/resources/db/migration/V1.1__add-published-field.sql b/src/main/resources/db/migration/V1.1__add-published-field.sql deleted file mode 100644 index fc9f115..0000000 --- a/src/main/resources/db/migration/V1.1__add-published-field.sql +++ /dev/null @@ -1,8 +0,0 @@ --- schema definition v1 --- --- Copyright 2021 Projeto e-cordel. --- e-cordel project (http://www.ecordel.com.br/). - -alter table public.cordel add column published boolean not null default false; - -update public.cordel set published = true; diff --git a/src/main/resources/db/migration/V1.2__author-type-changed-field.sql b/src/main/resources/db/migration/V1.2__author-type-changed-field.sql deleted file mode 100644 index e3f4988..0000000 --- a/src/main/resources/db/migration/V1.2__author-type-changed-field.sql +++ /dev/null @@ -1,8 +0,0 @@ --- schema definition v1 --- --- Copyright 2021 Projeto e-cordel. --- e-cordel project (http://www.ecordel.com.br/). - -ALTER TABLE public.author ALTER COLUMN about TYPE text; - -ALTER TABLE public.author ALTER COLUMN name SET NOT NULL; diff --git a/src/main/resources/db/migration/V1.3__create-jwt-token-table.sql b/src/main/resources/db/migration/V1.3__create-jwt-token-table.sql deleted file mode 100644 index fab9d62..0000000 --- a/src/main/resources/db/migration/V1.3__create-jwt-token-table.sql +++ /dev/null @@ -1,24 +0,0 @@ --- schema definition v1 --- --- Copyright 2021 Projeto e-cordel. --- e-cordel project (http://www.ecordel.com.br/). - -CREATE TABLE IF NOT EXISTS jwt_token ( - id bigint primary key, - expiration bigint, - secret_key TEXT, - active boolean not null -); - -CREATE SEQUENCE IF NOT EXISTS jwt_token_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - -ALTER SEQUENCE public.jwt_token_id_seq OWNED BY jwt_token.id; - -ALTER TABLE ONLY jwt_token ALTER COLUMN id SET DEFAULT nextval('public.jwt_token_id_seq'::regclass); - -INSERT INTO jwt_token (id, expiration, secret_key, active) VALUES ((SELECT nextval('public.jwt_token_id_seq')), 86400000, 'rm''!@N=Ke!~p8VTA2ZRK~nMDQX5Uvm!m''D&]{@Vr?G;2?XhbC:Qa#9#eMLN\}x3?JR3.2zr~v)gYF^8\:8>:XfB:Ww75N/emt9Yj[bQMNCWwW\J?N,nvH.<2\.r~w]*e~vgak)X"v8H`MH/7"2E`,^k@nL/zBq`}C6tT*cCSVC^c]-L}&/', true); diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index c60bd40..7232db7 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -7,11 +7,6 @@ ftp: password: pass baseUrl: "http://xilos.ecordel.com.br/" -jwt: - token: - expiration: 86400000 - secretKey: rm'!@N=Ke!~p8VTA2ZRK~nMDQX5Uvm!m'D&]{@Vr?G;2?XhbC:Qa#9#eMLN\}x3?JR3.2zr~v)gYF^8\:8>:XfB:Ww75N/emt9Yj[bQMNCWwW\J?N,nvH.<2\.r~w]*e~vgak)X"v8H`MH/7"2E`,^k@nL/zBq`}C6tT*cCSVC^c]-L}&/ - spring: datasource: url: jdbc:postgresql://localhost:5432/ecd From 0a2a8b26901430e50eeae8dba9387d659a4ef311 Mon Sep 17 00:00:00 2001 From: Mario Sousa Date: Thu, 7 Oct 2021 19:51:03 +0100 Subject: [PATCH 4/8] Update and config dependency check --- pom.xml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 80922fe..451c2ae 100644 --- a/pom.xml +++ b/pom.xml @@ -180,7 +180,10 @@ org.owasp dependency-check-maven - 6.2.0 + 6.3.2 + + 6 + From 6cad52697ee513385385615e941e1328d9cd0380 Mon Sep 17 00:00:00 2001 From: Kelvin Santos Date: Thu, 7 Oct 2021 19:07:26 -0300 Subject: [PATCH 5/8] Replace CustomCordelRepository for a @Query in CordelRepository Alter method findPublishedByTitle in CordelService for usage @Query created in CordelRepository Created a test for CordelService --- .../ecordel/cordel/CordelRepository.java | 9 +++ .../ecordel/cordel/CordelService.java | 12 +++- .../cordel/CustomCordelRepository.java | 1 + .../cordel/CustomCordelRepositoryImpl.java | 1 + .../ecordel/cordel/CordelRepositoryTest.java | 6 +- .../ecordel/cordel/CordelServiceTest.java | 68 +++++++++++++++++++ 6 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/test/java/br/com/itsmemario/ecordel/cordel/CordelServiceTest.java diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelRepository.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelRepository.java index 25bfc6f..24027ae 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelRepository.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelRepository.java @@ -20,9 +20,18 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; interface CordelRepository extends CustomCordelRepository, JpaRepository{ Page findAllProjectedBy(Pageable pageable); + @Query("SELECT new br.com.itsmemario.ecordel.cordel.CordelSummary(c.id, c.title, c.xilogravuraUrl, a.name) " + + "FROM Cordel c JOIN c.author a WHERE c.published = :published") + Page findAllByPublished(boolean published, Pageable pageable); + + @Query("SELECT new br.com.itsmemario.ecordel.cordel.CordelSummary(c.id, c.title, c.xilogravuraUrl, a.name) " + + "FROM Cordel c JOIN c.author a WHERE c.published = :published and lower(c.title) LIKE lower( :title ) ") + Page findAllByPublishedAndTitleLike(boolean published, String title, Pageable pageable); + } diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelService.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelService.java index 5776bff..b71c2a8 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelService.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelService.java @@ -30,6 +30,8 @@ @Service public class CordelService { + private static final int MINIMUM_SIZE = 3; + private CordelRepository repository; private XilogravuraService xilogravuraService; @@ -53,7 +55,11 @@ public Optional findById(Long id) { } public Page findPublishedByTitle(boolean published, String title, Pageable pageable) { - return repository.findPublishedByTitleLike(published, title, pageable); + if (isAValidString(title)) { + return repository.findAllByPublishedAndTitleLike(published, String.format("%%%s%%", title), pageable); + } + + return repository.findAllByPublished(published, pageable); } public Cordel updateXilogravura(Long cordelId, MultipartFile file) { @@ -68,4 +74,8 @@ public Cordel updateXilogravura(Long cordelId, MultipartFile file) { throw new CordelNotFoundException(); } } + + private boolean isAValidString(String title) { + return title != null && title.length() >= MINIMUM_SIZE; + } } diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepository.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepository.java index 754a5a2..051cf15 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepository.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepository.java @@ -27,5 +27,6 @@ interface CustomCordelRepository { // TODO remove? use CordelSummary Page findByTags(List tags, Pageable pageable); + // TODO remove using Query in CordelRepository Page findPublishedByTitleLike(boolean published, String title, Pageable pageable); } diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepositoryImpl.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepositoryImpl.java index 8f8ed26..43b316e 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepositoryImpl.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepositoryImpl.java @@ -34,6 +34,7 @@ @Repository @Transactional(readOnly = true) +// TODO remove? class CustomCordelRepositoryImpl implements CustomCordelRepository { private static final int MINIMUM_SIZE = 3; diff --git a/src/test/java/br/com/itsmemario/ecordel/cordel/CordelRepositoryTest.java b/src/test/java/br/com/itsmemario/ecordel/cordel/CordelRepositoryTest.java index c0da494..e48fe2b 100644 --- a/src/test/java/br/com/itsmemario/ecordel/cordel/CordelRepositoryTest.java +++ b/src/test/java/br/com/itsmemario/ecordel/cordel/CordelRepositoryTest.java @@ -75,11 +75,11 @@ void findByTagsProjectedBy() { @Test void findByPublishedTitleLike() { insertNewCordel(true); - Page page = repository.findPublishedByTitleLike(true, "tit", PageRequest.of(0,10)); - page.getContent().forEach(cordel -> System.out.println(cordel.getTitle())); + Page page = repository.findAllByPublishedAndTitleLike(true, "%tit%", PageRequest.of(0,10)); + assertThat(page.getContent().get(0).getAuthorName()).isEqualTo("name"); assertThat(page).hasSize(1); - page = repository.findPublishedByTitleLike(true, "aaa", PageRequest.of(0,10)); + page = repository.findAllByPublishedAndTitleLike(true, "aaa", PageRequest.of(0,10)); assertThat(page).hasSize(0); } diff --git a/src/test/java/br/com/itsmemario/ecordel/cordel/CordelServiceTest.java b/src/test/java/br/com/itsmemario/ecordel/cordel/CordelServiceTest.java new file mode 100644 index 0000000..5b03b54 --- /dev/null +++ b/src/test/java/br/com/itsmemario/ecordel/cordel/CordelServiceTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2021 Projeto e-cordel (http://ecordel.com.br) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package br.com.itsmemario.ecordel.cordel; + +import br.com.itsmemario.ecordel.xilogravura.XilogravuraService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.data.domain.Pageable; + +import static org.mockito.Mockito.times; + +class CordelServiceTest { + + @Mock + private XilogravuraService xilogravuraService; + @Mock + private CordelRepository cordelRepository; + + private final Pageable page = Pageable.unpaged(); + private CordelService cordelService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + cordelService = new CordelService(cordelRepository, xilogravuraService); + } + + @Test + void testIfTitleIsInvalid_QueryOnlyByPublished() { + String invalidTitle = ""; + + cordelService.findPublishedByTitle(true, invalidTitle, page); + Mockito.verify(cordelRepository, times(1)).findAllByPublished(true, page); + + cordelService.findPublishedByTitle(true, null, page); + Mockito.verify(cordelRepository, times(2)).findAllByPublished(true, page); + + Mockito.verify(cordelRepository, times(0)).findAllByPublishedAndTitleLike(true, invalidTitle, page); + } + + @Test + void testIfTitleIsValid_QueryOnlyByPublishedAndTitleLike() { + String validTitle = "cordel"; + + cordelService.findPublishedByTitle(true, validTitle, page); + Mockito.verify(cordelRepository, times(1)).findAllByPublishedAndTitleLike(true, String.format("%%%s%%", validTitle), page); + + Mockito.verify(cordelRepository, times(0)).findAllByPublished(true, page); + } +} \ No newline at end of file From 9d626722af7aa002f37cd1e6f97b0026788e291a Mon Sep 17 00:00:00 2001 From: Mario Sousa Date: Fri, 8 Oct 2021 13:39:18 +0100 Subject: [PATCH 6/8] Remove CustomCordelRepository --- .../ecordel/cordel/CordelRepository.java | 2 +- .../ecordel/cordel/CordelService.java | 5 - .../cordel/CustomCordelRepository.java | 32 ---- .../cordel/CustomCordelRepositoryImpl.java | 142 ------------------ .../ecordel/cordel/CordelRepositoryTest.java | 17 +-- 5 files changed, 5 insertions(+), 193 deletions(-) delete mode 100644 src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepository.java delete mode 100644 src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepositoryImpl.java diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelRepository.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelRepository.java index 24027ae..43405cb 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelRepository.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelRepository.java @@ -22,7 +22,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; -interface CordelRepository extends CustomCordelRepository, JpaRepository{ +interface CordelRepository extends JpaRepository{ Page findAllProjectedBy(Pageable pageable); diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelService.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelService.java index b71c2a8..ebdae6c 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelService.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelService.java @@ -24,7 +24,6 @@ import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import java.util.List; import java.util.Optional; @Service @@ -46,10 +45,6 @@ public Cordel save(Cordel cordel) { return repository.save(cordel); } - public Page findByTags(List tags, Pageable pageable){ - return repository.findByTags(tags, pageable); - } - public Optional findById(Long id) { return repository.findById(id); } diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepository.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepository.java deleted file mode 100644 index 051cf15..0000000 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepository.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2020-2021 Projeto e-cordel (http://ecordel.com.br) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package br.com.itsmemario.ecordel.cordel; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; - -import java.util.List; - -interface CustomCordelRepository { - -// TODO remove? use CordelSummary - Page findByTags(List tags, Pageable pageable); - - // TODO remove using Query in CordelRepository - Page findPublishedByTitleLike(boolean published, String title, Pageable pageable); -} diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepositoryImpl.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepositoryImpl.java deleted file mode 100644 index 43b316e..0000000 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CustomCordelRepositoryImpl.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright 2020-2021 Projeto e-cordel (http://ecordel.com.br) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package br.com.itsmemario.ecordel.cordel; - -import br.com.itsmemario.ecordel.author.Author; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.TypedQuery; -import java.math.BigInteger; -import java.util.List; -import java.util.stream.Collectors; - - -@Repository -@Transactional(readOnly = true) -// TODO remove? -class CustomCordelRepositoryImpl implements CustomCordelRepository { - - private static final int MINIMUM_SIZE = 3; - private static final String TAGS = "tags"; - private static final String LIMIT = "limit"; - private static final String OFFSET = "offset"; - - private static final String LIMIT_SQL = " limit :limit offset :offset "; - - private static final String FIND_BY_TAGS_SQL_FROM = " from cordel_tags ct \n" + - "join cordel c on c.id = ct.cordel_id \n" + - "join author a on a.id = c.author_id \n" + - "where ct.tags in (:tags) \n"; - - private static final String FIND_BY_TAGS_SQL = "select distinct(c.id) as id, c.title, c.description, a.name, a.email " + FIND_BY_TAGS_SQL_FROM + LIMIT_SQL; - - private static final String FIND_BY_TAGS_SQL_COUNT = "select COUNT(distinct(c.id)) " + FIND_BY_TAGS_SQL_FROM ; - - private static final String CORDEL_SUMMARY = " SELECT new br.com.itsmemario.ecordel.cordel.CordelSummary(c.id, c.title, c.xilogravuraUrl, a.name) " + - " FROM Cordel c JOIN c.author a WHERE c.published = :published "; - - private static final String COUNT_CORDEL_SUMMARY = "SELECT COUNT(c.id) FROM Cordel c JOIN c.author a WHERE c.published = :published "; - - private static final String TITLE = "title"; - public static final String PUBLISHED = "published"; - - @PersistenceContext - private EntityManager entityManager; - - @Override - public Page findByTags(List tags, Pageable pageable) { - long count = countResults(tags); - if(count == 0) return Page.empty(pageable); - - var query = entityManager.createNativeQuery(FIND_BY_TAGS_SQL); - query.setParameter(TAGS, tags); - query.setParameter(LIMIT, pageable.getPageSize()); - query.setParameter(OFFSET, pageable.getOffset()); - List resultList = query.getResultList(); - List cordels = resultList.stream().map(this::buildCordel).collect(Collectors.toList()); - - return new PageImpl<>(cordels, pageable, count); - } - - private long countResults(List tags) { - BigInteger count = (BigInteger) entityManager.createNativeQuery(FIND_BY_TAGS_SQL_COUNT).setParameter(TAGS, tags).getSingleResult(); - return count.longValue(); - } - - private Cordel buildCordel(Object[] fields) { - Cordel cordel = new Cordel(); - cordel.setId(((BigInteger)fields[0]).longValue()); - cordel.setTitle(String.valueOf(fields[1])); - cordel.setDescription(String.valueOf(fields[2])); - cordel.setAuthor(buildAuthor(fields[3],fields[4])); - return cordel; - } - - private Author buildAuthor(Object name, Object email) { - Author author = new Author(String.valueOf(name)); - author.setEmail(String.valueOf(email)); - return author; - } - - @Override - public Page findPublishedByTitleLike(boolean published, String title, Pageable pageable) { - - StringBuilder sql = new StringBuilder(CORDEL_SUMMARY); - StringBuilder countSql = new StringBuilder(COUNT_CORDEL_SUMMARY); - - TypedQuery countQuery = entityManager.createQuery(countSql.toString(), Long.class); - TypedQuery query = entityManager.createQuery(sql.toString(), CordelSummary.class); - - if(isAValidString(title)){ - query = addTitleFilter(title, sql, CordelSummary.class); - countQuery = addTitleFilter(title, countSql, Long.class); - } - - countQuery.setParameter(PUBLISHED, published); - Long count = countQuery.getSingleResult(); - if(count == 0) return Page.empty(pageable); - - query.setMaxResults(pageable.getPageSize()); - query.setFirstResult((int)pageable.getOffset()); - query.setParameter(PUBLISHED, published); - - List resultList = query.getResultList(); - - return new PageImpl<>(resultList, pageable, count); - } - - private boolean isAValidString(String title) { - return title != null && title.length() >= MINIMUM_SIZE; - } - - private TypedQuery addTitleFilter(String title, StringBuilder sql, Class clazz) { - var where = " AND lower(c.title) LIKE lower( :title ) "; - TypedQuery query; - sql.append(where); - query = entityManager.createQuery(sql.toString(), clazz); - query.setParameter(TITLE, String.format("%%%s%%", title)); - return query; - } - -} diff --git a/src/test/java/br/com/itsmemario/ecordel/cordel/CordelRepositoryTest.java b/src/test/java/br/com/itsmemario/ecordel/cordel/CordelRepositoryTest.java index e48fe2b..09c5c3a 100644 --- a/src/test/java/br/com/itsmemario/ecordel/cordel/CordelRepositoryTest.java +++ b/src/test/java/br/com/itsmemario/ecordel/cordel/CordelRepositoryTest.java @@ -17,7 +17,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Arrays; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -64,14 +63,6 @@ void findAllProjectedBy() { assertThat(cordelSummaries.getContent().get(0)).isInstanceOf(CordelView.class); } - @Test - void findByTagsProjectedBy() { - insertNewCordel(true); - Page cordels = repository.findByTags(Arrays.asList("tag1", "tag2"), PageRequest.of(0,1)); - assertThat(cordels).hasSize(1); - assertThat(cordels.getContent().get(0)).extracting(CordelView::getDescription).isEqualTo("description"); - } - @Test void findByPublishedTitleLike() { insertNewCordel(true); @@ -79,7 +70,7 @@ void findByPublishedTitleLike() { assertThat(page.getContent().get(0).getAuthorName()).isEqualTo("name"); assertThat(page).hasSize(1); - page = repository.findAllByPublishedAndTitleLike(true, "aaa", PageRequest.of(0,10)); + page = repository.findAllByPublishedAndTitleLike(true, "%aaa%", PageRequest.of(0,10)); assertThat(page).hasSize(0); } @@ -87,7 +78,7 @@ void findByPublishedTitleLike() { void testPaginationResultsByPublishedTitle() throws Exception { IntStream.range(0,5).forEach( i -> insertNewCordel(true)); - Page page = repository.findPublishedByTitleLike(true, "tit", PageRequest.of(1,3)); + Page page = repository.findAllByPublishedAndTitleLike(true, "%tit%", PageRequest.of(1,3)); page.getContent().forEach(cordel -> System.out.println(cordel.getTitle())); assertThat(page).hasSize(2); } @@ -95,10 +86,10 @@ void testPaginationResultsByPublishedTitle() throws Exception { @Test void findNotPublishedWorkTest() { insertNewCordel(false); - Page page = repository.findPublishedByTitleLike(true, "tit", PageRequest.of(0, 10)); + Page page = repository.findAllByPublishedAndTitleLike(true, "%tit%", PageRequest.of(0, 10)); assertThat(page).isEmpty(); - page = repository.findPublishedByTitleLike(false, "tit", PageRequest.of(0, 10)); + page = repository.findAllByPublishedAndTitleLike(false, "%tit%", PageRequest.of(0, 10)); assertThat(page).isNotEmpty().hasSize(1); } From c84e9ce426925a5127af08369a2959b72ea9d9b3 Mon Sep 17 00:00:00 2001 From: Mario Sousa Date: Fri, 8 Oct 2021 13:46:51 +0100 Subject: [PATCH 7/8] Remove CordelView --- .../com/itsmemario/ecordel/cordel/Cordel.java | 2 +- .../ecordel/cordel/CordelRepository.java | 2 -- .../itsmemario/ecordel/cordel/CordelView.java | 34 ------------------- .../ecordel/cordel/CordelRepositoryTest.java | 8 ----- 4 files changed, 1 insertion(+), 45 deletions(-) delete mode 100644 src/main/java/br/com/itsmemario/ecordel/cordel/CordelView.java diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/Cordel.java b/src/main/java/br/com/itsmemario/ecordel/cordel/Cordel.java index 93b648d..a9ef3b7 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/Cordel.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/Cordel.java @@ -26,7 +26,7 @@ import java.util.Set; @Entity -public class Cordel implements CordelView { +public class Cordel { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelRepository.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelRepository.java index 43405cb..01c3392 100644 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelRepository.java +++ b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelRepository.java @@ -24,8 +24,6 @@ interface CordelRepository extends JpaRepository{ - Page findAllProjectedBy(Pageable pageable); - @Query("SELECT new br.com.itsmemario.ecordel.cordel.CordelSummary(c.id, c.title, c.xilogravuraUrl, a.name) " + "FROM Cordel c JOIN c.author a WHERE c.published = :published") Page findAllByPublished(boolean published, Pageable pageable); diff --git a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelView.java b/src/main/java/br/com/itsmemario/ecordel/cordel/CordelView.java deleted file mode 100644 index 51cada9..0000000 --- a/src/main/java/br/com/itsmemario/ecordel/cordel/CordelView.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2020 Projeto e-cordel (http://ecordel.com.br) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package br.com.itsmemario.ecordel.cordel; - -import br.com.itsmemario.ecordel.author.AuthorView; - -import java.util.Set; - - -public interface CordelView { - - Long getId(); - AuthorView getAuthor(); - String getTitle(); - String getXilogravuraUrl(); - String getDescription(); - Set getTags(); - -} diff --git a/src/test/java/br/com/itsmemario/ecordel/cordel/CordelRepositoryTest.java b/src/test/java/br/com/itsmemario/ecordel/cordel/CordelRepositoryTest.java index 09c5c3a..5e1d80e 100644 --- a/src/test/java/br/com/itsmemario/ecordel/cordel/CordelRepositoryTest.java +++ b/src/test/java/br/com/itsmemario/ecordel/cordel/CordelRepositoryTest.java @@ -55,14 +55,6 @@ void saveBigTextAsContent() throws IOException { .extracting(Cordel::getContent).asString().isNotEmpty(); } - @Test - void findAllProjectedBy() { - insertNewCordel(true); - Page cordelSummaries = repository.findAllProjectedBy(Pageable.unpaged()); - assertThat(cordelSummaries).isNotEmpty(); - assertThat(cordelSummaries.getContent().get(0)).isInstanceOf(CordelView.class); - } - @Test void findByPublishedTitleLike() { insertNewCordel(true); From 1ac76cfacfe2e26131b986ecbe636db1c6aef843 Mon Sep 17 00:00:00 2001 From: Mario Sousa Date: Sun, 10 Oct 2021 10:50:01 +0100 Subject: [PATCH 8/8] Prepare v1.0.0-SNAPSHOT --- README.md | 10 +++++++++- openapi.yaml | 4 ++-- pom.xml | 2 +- .../ecordel/xilogravura/XilogravuraService.java | 6 ++---- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2ef7edb..80d4d06 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,14 @@ Add in VMArguments to run in an IDE -Dspring.profiles.active=local +### Development data + +You can use the file [data.sql](src/test/resources/db/data/data.sql) to insert data to your local db. + +### Cleaning DB + +All data volume is mapped to directory `tmp`. You can delete this folder to get rid of development data. + ## How to run You can run with maven @@ -44,7 +52,7 @@ tip: --rm parameter will exclude container image after execution and it cause da ### Writing Tests -If you wanna use db connection on your test you must extend the class `AbstractIntegrationTest`. This class will run the docker container and configure the spring datasource. +If you want to use db connection on your test you must extend the class `AbstractIntegrationTest`. This class will run the docker container and configure the spring datasource. ## How to contribute diff --git a/openapi.yaml b/openapi.yaml index 066a446..5c66ed0 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -44,7 +44,7 @@ paths: - Author /cordels/summaries: get: - summary: "Retrieve all cordels by pages" + summary: "Retrieve all summaries by pages" parameters: - name: page in: query @@ -95,7 +95,7 @@ paths: - Cordel /cordels/{id}: get: - summary: "Get a specific cordel with all content" + summary: "Get a cordel with full content" parameters: - name: "id" in: "path" diff --git a/pom.xml b/pom.xml index 451c2ae..01c3345 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ br.com.itsmemario ecordel - 0.7.0-SNAPSHOT + 1.0.0-SNAPSHOT e-cordel e-reader for cordels diff --git a/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraService.java b/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraService.java index 159d078..095a1bb 100644 --- a/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraService.java +++ b/src/main/java/br/com/itsmemario/ecordel/xilogravura/XilogravuraService.java @@ -40,17 +40,15 @@ public XilogravuraService(FileManager fileManager) { public String createXilogravuraWithFile(MultipartFile file){ try { -// todo move this code to file manager? - String fileName = generateRandomFileName(); + var fileName = generateRandomFileName(); return fileManager.saveFile(file.getBytes(), fileName); } catch (IOException e) { - e.printStackTrace(); throw new FileProcessException("Error while saving file", e); } } private String generateRandomFileName() { - return UUID.randomUUID().toString() + JPG; + return UUID.randomUUID() + JPG; } }