-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2 Crud de curso - Alteração de Review
- Loading branch information
Jhê Cardoso
committed
Feb 16, 2021
1 parent
9ebf261
commit 2253493
Showing
35 changed files
with
692 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
src/main/java/org/ayty/hatcher/api/v1/course/dto/Course.java
This file was deleted.
Oops, something went wrong.
60 changes: 0 additions & 60 deletions
60
src/main/java/org/ayty/hatcher/api/v1/course/dto/CourseController.java
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
src/main/java/org/ayty/hatcher/api/v1/course/dto/CourseControllerV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
src/main/java/org/ayty/hatcher/api/v1/course/dto/CourseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/ayty/hatcher/api/v1/course/exception/CourseNotDeleteException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/org/ayty/hatcher/api/v1/course/exception/CourseNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
src/main/java/org/ayty/hatcher/api/v1/course/exception/ObjectNotFoundException.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.