CRUD sur post (pas de auth)
This commit is contained in:
parent
d769bee503
commit
64569c4f81
@ -0,0 +1,82 @@
|
||||
package com.guams.review.controller;
|
||||
|
||||
import com.guams.review.modele.Post;
|
||||
import com.guams.review.modele.User;
|
||||
import com.guams.review.repository.PostRepository;
|
||||
import com.guams.review.repository.UserRepository;
|
||||
import com.guams.review.service.PostService;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/posts")
|
||||
public class PostController
|
||||
{
|
||||
private final PostService postService;
|
||||
private final PostRepository postRepository;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
|
||||
public PostController(PostService postService, PostRepository postRepository, UserRepository userRepository) {
|
||||
this.postService = postService;
|
||||
this.postRepository = postRepository;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<List<Post>> getPosts()
|
||||
{
|
||||
return new ResponseEntity<>(postRepository.findAll(), new HttpHeaders(), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("{post-id}")
|
||||
public ResponseEntity<Post> getPost(@PathVariable("post-id") Long id)
|
||||
{
|
||||
Optional<Post> post = postRepository.findById(id);
|
||||
if (post.isPresent()) {
|
||||
return new ResponseEntity<>(post.get(), new HttpHeaders(), HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<String> getPost(@RequestBody Post post, @RequestParam Long userId)
|
||||
{
|
||||
Optional<User> userExists = userRepository.findById(userId);
|
||||
if (userExists.isPresent()) {
|
||||
post.setUser(userExists.get());
|
||||
postRepository.save(post);
|
||||
return new ResponseEntity<>("Created", HttpStatus.CREATED);
|
||||
}
|
||||
return new ResponseEntity<>("Erreur", HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
@PutMapping("{post-id}")
|
||||
public ResponseEntity<String> updatePost(@PathVariable("post-id") Long id, @RequestBody Post post)
|
||||
{
|
||||
Optional<Post> postExists = postRepository.findById(id);
|
||||
if (postExists.isPresent()) {
|
||||
Post newPost = postExists.get();
|
||||
newPost.setTitle(post.getTitle());
|
||||
newPost.setBody(post.getBody());
|
||||
newPost.setImage(post.getImage());
|
||||
postRepository.save(newPost);
|
||||
return new ResponseEntity<>("Updated", HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>("Erreur", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@DeleteMapping("{post-id}")
|
||||
public ResponseEntity<String> deletePost(@PathVariable("post-id") Long id)
|
||||
{
|
||||
Optional<Post> postExists = postRepository.findById(id);
|
||||
if (postExists.isPresent()) {
|
||||
postRepository.delete(postExists.get());
|
||||
return new ResponseEntity<>("Deleted", HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>("Erreur", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.guams.review.modele;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
@ -10,8 +11,8 @@ import lombok.Setter;
|
||||
@Table(name = "POST")
|
||||
public class Post
|
||||
{
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Id()
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
@Column(name = "TITLE", nullable = false)
|
||||
private String title;
|
||||
@ -21,6 +22,7 @@ public class Post
|
||||
private String image;
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "USER_ID", nullable = false)
|
||||
@JsonIgnore // Pour pas avoir un json infini
|
||||
private User user;
|
||||
|
||||
public Post()
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.guams.review.modele;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
@ -16,7 +17,7 @@ import java.util.List;
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "LOGIN", nullable = false, unique = true)
|
||||
|
@ -0,0 +1,9 @@
|
||||
package com.guams.review.repository;
|
||||
|
||||
import com.guams.review.modele.Post;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface PostRepository extends JpaRepository<Post, Long> {
|
||||
}
|
Loading…
Reference in New Issue
Block a user