Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/add post path variable #39

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/api/educai/controllers/PostController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package api.educai.controllers;

import api.educai.dto.NewPostDTO;
import api.educai.dto.PostDTO;
import api.educai.entities.Post;
import api.educai.services.PostService;
Expand All @@ -24,8 +25,9 @@ public class PostController {
@Secured("ROLE_TEACHER")
@Operation(summary = "Cria um post")
@PostMapping
public ResponseEntity<Post> createPost(@RequestBody @Valid PostDTO post, @PathVariable String id){
return ResponseEntity.status(201).body(postService.createPost(post, id));
public ResponseEntity<Post> createPost(@RequestBody @Valid NewPostDTO post){
return ResponseEntity.status(201).body(postService.createPost(post));

}

@Operation(summary = "Retorna todos os posts")
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/api/educai/dto/NewPostDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package api.educai.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Getter;

import java.time.LocalDate;

@Getter
public class NewPostDTO {
@NotBlank
@Size(max = 100)
private String title;
@NotBlank
@Size(max = 100)
private String description;
private String url;
private LocalDate datePosting;
private String classroomId;
}
5 changes: 3 additions & 2 deletions src/main/java/api/educai/services/PostService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package api.educai.services;

import api.educai.dto.NewPostDTO;
import api.educai.dto.PostDTO;
import api.educai.entities.Classroom;
import api.educai.entities.Post;
Expand All @@ -23,8 +24,8 @@ public class PostService {
@Autowired
private ModelMapper mapper;

public Post createPost(PostDTO newPost, String classroomId) {
Classroom classroom = classroomRepository.findById(new ObjectId(classroomId));
public Post createPost(NewPostDTO newPost) {
Classroom classroom = classroomRepository.findById(new ObjectId(newPost.getClassroomId()));
Post post = mapper.map(newPost, Post.class);
postRepository.save(post);

Expand Down
Loading