Skip to content

Commit

Permalink
#2 Crud de curso - Alteração de Review
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhê Cardoso committed Feb 16, 2021
1 parent 9ebf261 commit 2253493
Show file tree
Hide file tree
Showing 35 changed files with 692 additions and 257 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
Expand All @@ -20,7 +20,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.4.2</version>
<version>2.4.2</version>
</dependency>

<dependency>
Expand Down
33 changes: 0 additions & 33 deletions src/main/java/org/ayty/hatcher/api/v1/course/dto/Course.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.ayty.hatcher.api.v1.course.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.RequiredArgsConstructor;
import org.ayty.hatcher.api.v1.course.jpa.Course;
import org.ayty.hatcher.api.v1.course.service.CreateCourseServiceImpl;
import org.ayty.hatcher.api.v1.course.service.DeleteCourseServiceImpl;
import org.ayty.hatcher.api.v1.course.service.GetCourseServiceImpl;
import org.ayty.hatcher.api.v1.course.service.ListCourseServiceImpl;
import org.ayty.hatcher.api.v1.course.service.UpdateCourseServiceImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.io.Serializable;
import java.net.URI;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/courses")
@CrossOrigin("*")
public final class CourseControllerV1 implements Serializable {

private static final long serialVersionUID = 1L;

private final CreateCourseServiceImpl createCourse;
private final GetCourseServiceImpl getCourse;
private final ListCourseServiceImpl listCourse;
private final DeleteCourseServiceImpl deleCourse;
private final UpdateCourseServiceImpl updateCourse;

@GetMapping (produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(code = HttpStatus.OK)
public Page<CourseDTO> listAll(
@RequestParam(value = "idPaged", defaultValue = "true") boolean isPaged,
@RequestParam(value = "pageNum", defaultValue = "0") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
@RequestParam(value = "ord", defaultValue = "id") String ord,
@RequestParam(value = "sort", defaultValue = "ASC") String sort) {
if(isPaged) return CourseDTO.from(this.listCourse.findAll(PageRequest.of(pageNum, pageSize, Sort.by(Sort.Direction.fromString(sort), ord))));
else return CourseDTO.from(this.listCourse.findAll(Pageable.unpaged()));
}

@PatchMapping(value = "/{id}",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(code = HttpStatus.NO_CONTENT)
public void update (@PathVariable Long id, @RequestBody Course obj) {
this.updateCourse.update(id, obj);
}

@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(code = HttpStatus.OK)
public Course findById (@PathVariable Long id){
return this.getCourse.get(id);
}

@PostMapping (consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(code = HttpStatus.CREATED)
public void create(@RequestBody Course obj) {
this.createCourse.create(obj);
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(obj.getId()).toUri();
}

@DeleteMapping(value = "/{id}")
@ResponseStatus(code = HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id){
this.deleCourse.delete(id);
}

}
56 changes: 56 additions & 0 deletions src/main/java/org/ayty/hatcher/api/v1/course/dto/CourseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.ayty.hatcher.api.v1.course.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.ayty.hatcher.api.v1.course.jpa.Course;
import org.springframework.data.domain.Page;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Builder(builderClassName = "Builder")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_ABSENT)
public class CourseDTO implements Serializable {

private static final long serialVersionUID = 1L;

@NonNull
private Long id;

@NonNull
private String courseName;

private String description;

public static CourseDTO from(Course course) {
return CourseDTO
.builder()
.id(course.getId())
.courseName(course.getName())
.description(course.getDescription())
.build();
}

public static List<CourseDTO> from(List<Course> courses){
return courses.stream().map(CourseDTO::from).collect(Collectors.toList());
}

public static Page<CourseDTO> from(Page<Course> pages){
return pages.map(CourseDTO::from);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.ayty.hatcher.api.v1.course.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class CourseNotDeleteException extends RuntimeException{

public CourseNotDeleteException (){
super("Course not found!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.ayty.hatcher.api.v1.course.exception;

import org.ayty.hatcher.api.v1.course.dto.CourseDTO;
import org.ayty.hatcher.api.v1.course.jpa.Course;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class CourseNotFoundException extends RuntimeException{

private static final long serialVersionUID = 1L;

public CourseNotFoundException(String message, Throwable cause) {
super(message,cause);
}
public CourseNotFoundException(String message) {
super(message);
}
public CourseNotFoundException (){
super("Course not found!");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

@ControllerAdvice
public class ResourceExceptionHandler {
@ExceptionHandler(ObjectNotFoundException.class)
public ResponseEntity<StandardError> objectNotFound (ObjectNotFoundException e, HttpServletRequest request) {
StandardError error = new StandardError(HttpStatus.BAD_REQUEST.value(), System.currentTimeMillis(), e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error);

@ExceptionHandler(CourseNotFoundException.class)
public ResponseEntity<StandardError> objectNotFound (CourseNotFoundException e, HttpServletRequest request) {
StandardError error = new StandardError(HttpStatus.NOT_FOUND.value(), System.currentTimeMillis(), e.getMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@Getter @Setter
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
public class StandardError {

private Integer status;
Expand Down
Loading

0 comments on commit 2253493

Please sign in to comment.