Files
spring-security-study/src/main/java/com/example/springsecuritystudy/post/PostController.java
2023-02-02 18:01:03 +09:00

43 lines
1.3 KiB
Java

package com.example.springsecuritystudy.post;
import java.security.Principal;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import lombok.RequiredArgsConstructor;
@Controller
@RequiredArgsConstructor
@RequestMapping("/post")
public class PostController {
private final PostService postService;
@GetMapping
public String findByPost(Principal principal, Model model) {
List<Post> posts = postService.findByUserName(principal.getName());
model.addAttribute("posts", posts);
return "post/index";
}
@PostMapping
public String savePost(@ModelAttribute PostDto postDto, Principal principal) {
postService.savePost(principal.getName(), postDto.getTitle(), postDto.getContent());
return "redirect:post";
}
@DeleteMapping
public String deletePost(@RequestParam Long id, Principal principal) {
postService.deletePost(principal.getName(), id);
return "redirect:post";
}
}