Skip to content

Commit

Permalink
Merge pull request #25 from e-cordel/127-refactor-backend
Browse files Browse the repository at this point in the history
127 - Refactor backend
  • Loading branch information
itsmemarioss authored Oct 15, 2021
2 parents 90794b0 + 1ac76cf commit 970af66
Show file tree
Hide file tree
Showing 28 changed files with 221 additions and 601 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/new-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
45 changes: 7 additions & 38 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ paths:
- bearerAuth: []
tags:
- Author
/cordels:
/cordels/summaries:
get:
summary: "Retrieve all cordels by pages"
summary: "Retrieve all summaries by pages"
parameters:
- name: page
in: query
Expand Down Expand Up @@ -79,6 +79,7 @@ paths:
$ref: '#/components/schemas/ReponsePage'
tags:
- Cordel
/cordels:
post:
summary: "Create a new cordel"
requestBody:
Expand All @@ -94,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"
Expand All @@ -108,7 +109,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/FullCordel'
$ref: '#/components/schemas/Cordel'
404:
description: "Not found"
tags:
Expand Down Expand Up @@ -240,6 +241,8 @@ components:
content:
type: string
description: cordel text
xilogravuraUrl:
type: string
published:
type: boolean
tags:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<version>2.5.5</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<groupId>br.com.itsmemario</groupId>
<artifactId>ecordel</artifactId>
<version>0.6.0-SNAPSHOT</version>
<version>1.0.0-SNAPSHOT</version>
<name>e-cordel</name>
<description>e-reader for cordels</description>

Expand Down Expand Up @@ -180,7 +180,10 @@
<plugin>
<groupId>org.owasp</groupId>
<artifactId>dependency-check-maven</artifactId>
<version>6.2.0</version>
<version>6.3.2</version>
<configuration>
<failBuildOnCVSS>6</failBuildOnCVSS>
</configuration>
<executions>
<execution>
<goals>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/br/com/itsmemario/ecordel/author/AuthorDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/br/com/itsmemario/ecordel/cordel/Cordel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
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;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;

@Entity
public class Cordel implements CordelView {
public class Cordel {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
Expand All @@ -38,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")
Expand Down Expand Up @@ -87,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() {
Expand All @@ -104,7 +102,7 @@ public void setDescription(String description) {
}

public Set<String> getTags() {
return tags;
return Collections.unmodifiableSet(tags);
}

public void setTags(Set<String> tags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,19 +44,20 @@ public class CordelController {
}

@GetMapping("{id}")
public ResponseEntity<Cordel> getCordel(@PathVariable Long id) {
public ResponseEntity<CordelDto> getCordel(@PathVariable Long id) {
logger.info("request received get cordel by id: {}", id);

Optional<Cordel> 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<CordelSummary> getCordels(@RequestParam(required = false) String title,
@RequestParam(defaultValue = "true") boolean published,
Pageable pageable) {
Expand All @@ -76,14 +76,14 @@ public ResponseEntity<String> create(@RequestBody @Valid CordelDto dto, UriCompo
}

@PutMapping("{id}/xilogravura")
public ResponseEntity<Cordel> putXilogravura(@PathVariable Long id, Xilogravura xilogravura, @RequestParam("file") MultipartFile file) {
public ResponseEntity<CordelDto> putXilogravura(@PathVariable Long id, @RequestParam("file") MultipartFile file) {
logger.info("request received, update xilogravura for cordel: {}", id);
Cordel cordel = service.updateXilogravura(id, xilogravura, file);
return ResponseEntity.ok(cordel);
Cordel cordel = service.updateXilogravura(id, file);
return ResponseEntity.ok(CordelDto.of(cordel));
}

@PutMapping("{id}")
public ResponseEntity<Cordel> update(@RequestBody @Valid CordelDto dto, @PathVariable Long id) {
public ResponseEntity<CordelDto> update(@RequestBody @Valid CordelDto dto, @PathVariable Long id) {
logger.info("request received update cordel with id: {}", id);

Optional<Cordel> existingCordel = service.findById(id);
Expand All @@ -93,8 +93,8 @@ public ResponseEntity<Cordel> 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));
}

}
16 changes: 16 additions & 0 deletions src/main/java/br/com/itsmemario/ecordel/cordel/CordelDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ public class CordelDto {
private String content;
private boolean published;
private Set<String> 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());
dto.setXilogravuraUrl(cordel.getXilogravuraUrl());
return dto;
}

public Cordel toEntity() {
var cordel = new Cordel();
Expand All @@ -51,6 +66,7 @@ public Cordel toEntity() {
cordel.setContent( content );
cordel.setPublished( published );
cordel.setTags( tags );
cordel.setXilogravuraUrl( xilogravuraUrl );
return cordel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@
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<Cordel, Long>{
interface CordelRepository extends JpaRepository<Cordel, Long>{

Page<CordelView> 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<CordelSummary> 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<CordelSummary> findAllByPublishedAndTitleLike(boolean published, String title, Pageable pageable);

}
Loading

0 comments on commit 970af66

Please sign in to comment.