Add user, post 게시판 기능 추가

This commit is contained in:
Daeil Choi
2023-02-02 18:01:03 +09:00
parent 8a6acc9dfc
commit 41c2dc134a
29 changed files with 711 additions and 90 deletions

View File

@@ -0,0 +1,42 @@
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";
}
}