Modify Post를 Note로 수정
This commit is contained in:
@@ -6,7 +6,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
import com.example.springsecuritystudy.notice.NoticeService;
|
||||
import com.example.springsecuritystudy.post.PostService;
|
||||
import com.example.springsecuritystudy.note.NoteService;
|
||||
import com.example.springsecuritystudy.user.User;
|
||||
import com.example.springsecuritystudy.user.UserService;
|
||||
|
||||
@@ -21,7 +21,7 @@ import lombok.RequiredArgsConstructor;
|
||||
public class InitializeConfig {
|
||||
|
||||
private final UserService userService;
|
||||
private final PostService postService;
|
||||
private final NoteService noteService;
|
||||
private final NoticeService noticeService;
|
||||
|
||||
/**
|
||||
@@ -34,10 +34,10 @@ public class InitializeConfig {
|
||||
public void adminAccount() {
|
||||
User user = userService.signup("user", "user");
|
||||
userService.signupAdmin("admin", "admin");
|
||||
postService.savePost(user, "테스트", "테스트입니다.");
|
||||
postService.savePost(user, "테스트2", "테스트2입니다.");
|
||||
postService.savePost(user, "테스트3", "테스트3입니다.");
|
||||
postService.savePost(user, "여름 여행계획", "여름 여행계획 작성중...");
|
||||
noteService.saveNote(user, "테스트", "테스트입니다.");
|
||||
noteService.saveNote(user, "테스트2", "테스트2입니다.");
|
||||
noteService.saveNote(user, "테스트3", "테스트3입니다.");
|
||||
noteService.saveNote(user, "여름 여행계획", "여름 여행계획 작성중...");
|
||||
noticeService.saveNotice("환영합니다", "환영합니다 여러분");
|
||||
noticeService.saveNotice("게시글 작성 방법 공지", "1. 회원가입\n2. 로그인\n3. 게시글 작성\n4. 저장\n* 본인 외에는 게시글을 볼 수 없습니다.");
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public class SecurityConfig {
|
||||
.authorizeHttpRequests(auth -> auth
|
||||
.antMatchers("/", "/home", "/signup", "/example",
|
||||
"/css/**", "/h2-console/**").permitAll()
|
||||
.antMatchers("/post").hasRole("USER")
|
||||
.antMatchers("/note").hasRole("USER")
|
||||
.antMatchers("/admin").hasRole("ADMIN")
|
||||
.antMatchers(HttpMethod.POST, "/notice").hasRole("ADMIN")
|
||||
.antMatchers(HttpMethod.DELETE, "/notice").hasRole("ADMIN")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.springsecuritystudy.post;
|
||||
package com.example.springsecuritystudy.note;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -17,7 +17,7 @@ import lombok.RequiredArgsConstructor;
|
||||
@RequestMapping("/admin")
|
||||
public class AdminController {
|
||||
|
||||
private final PostService postService;
|
||||
private final NoteService noteService;
|
||||
|
||||
/**
|
||||
* 어드민인 경우 게시글 조회
|
||||
@@ -26,8 +26,8 @@ public class AdminController {
|
||||
@GetMapping
|
||||
public String getPostForAdmin(Authentication authentication, Model model) {
|
||||
User user = (User) authentication.getPrincipal();
|
||||
List<Post> posts = postService.findByUser(user);
|
||||
model.addAttribute("posts", posts);
|
||||
List<Note> notes = noteService.findByUser(user);
|
||||
model.addAttribute("notes", notes);
|
||||
return "admin/index";
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.springsecuritystudy.post;
|
||||
package com.example.springsecuritystudy.note;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
@@ -21,7 +21,7 @@ import lombok.NoArgsConstructor;
|
||||
@Table
|
||||
@Getter
|
||||
@NoArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class Post extends BaseTimeEntity {
|
||||
public class Note extends BaseTimeEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@@ -35,7 +35,7 @@ public class Post extends BaseTimeEntity {
|
||||
private User user;
|
||||
|
||||
@Builder
|
||||
public Post(String title, String content, User user) {
|
||||
public Note(String title, String content, User user) {
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
this.user = user;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.springsecuritystudy.post;
|
||||
package com.example.springsecuritystudy.note;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -18,30 +18,30 @@ import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Controller
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/post")
|
||||
public class PostController {
|
||||
@RequestMapping("/note")
|
||||
public class NoteController {
|
||||
|
||||
private final PostService postService;
|
||||
private final NoteService noteService;
|
||||
|
||||
@GetMapping
|
||||
public String getPost(Authentication authentication, Model model) {
|
||||
User user = (User) authentication.getPrincipal();
|
||||
List<Post> posts = postService.findByUser(user);
|
||||
model.addAttribute("posts", posts);
|
||||
return "post/index";
|
||||
List<Note> notes = noteService.findByUser(user);
|
||||
model.addAttribute("notes", notes);
|
||||
return "note/index";
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public String savePost(@ModelAttribute PostDto postDto, Authentication authentication) {
|
||||
public String savePost(@ModelAttribute NoteDto noteDto, Authentication authentication) {
|
||||
User user = (User) authentication.getPrincipal();
|
||||
postService.savePost(user, postDto.getTitle(), postDto.getContent());
|
||||
return "redirect:post";
|
||||
noteService.saveNote(user, noteDto.getTitle(), noteDto.getContent());
|
||||
return "redirect:note";
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public String deletePost(@RequestParam Long id, Authentication authentication) {
|
||||
User user = (User) authentication.getPrincipal();
|
||||
postService.deletePost(user, id);
|
||||
return "redirect:post";
|
||||
noteService.deleteNote(user, id);
|
||||
return "redirect:note";
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.example.springsecuritystudy.post;
|
||||
package com.example.springsecuritystudy.note;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class PostDto {
|
||||
public class NoteDto {
|
||||
|
||||
private String title;
|
||||
private String content;
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example.springsecuritystudy.note;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.example.springsecuritystudy.user.User;
|
||||
|
||||
public interface NoteRepository extends JpaRepository<Note, Long> {
|
||||
|
||||
List<Note> findByUserOrderByIdDesc(User user);
|
||||
|
||||
Note findByIdAndUser(Long id, User user);
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.example.springsecuritystudy.post;
|
||||
package com.example.springsecuritystudy.note;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -14,17 +14,17 @@ import lombok.RequiredArgsConstructor;
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Transactional
|
||||
public class PostService {
|
||||
public class NoteService {
|
||||
|
||||
private final PostRepository postRepository;
|
||||
private final NoteRepository noteRepository;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Post> findByUser(User user) {
|
||||
public List<Note> findByUser(User user) {
|
||||
userNullCheck(user);
|
||||
if (Boolean.TRUE.equals(user.isAdmin())) {
|
||||
return postRepository.findAll(Sort.by(Sort.Direction.DESC, "id"));
|
||||
return noteRepository.findAll(Sort.by(Sort.Direction.DESC, "id"));
|
||||
}
|
||||
return postRepository.findByUserOrderByIdDesc(user);
|
||||
return noteRepository.findByUserOrderByIdDesc(user);
|
||||
}
|
||||
|
||||
private static void userNullCheck(User user) {
|
||||
@@ -33,15 +33,15 @@ public class PostService {
|
||||
}
|
||||
}
|
||||
|
||||
public Post savePost(User user, String title, String content) {
|
||||
public Note saveNote(User user, String title, String content) {
|
||||
userNullCheck(user);
|
||||
return postRepository.save(new Post(title, content, user));
|
||||
return noteRepository.save(new Note(title, content, user));
|
||||
}
|
||||
|
||||
public void deletePost(User user, Long id) {
|
||||
public void deleteNote(User user, Long id) {
|
||||
userNullCheck(user);
|
||||
Post post = postRepository.findByIdAndUser(id, user);
|
||||
postRepository.delete(post);
|
||||
Note note = noteRepository.findByIdAndUser(id, user);
|
||||
noteRepository.delete(note);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import com.example.springsecuritystudy.post.PostDto;
|
||||
import com.example.springsecuritystudy.note.NoteDto;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@@ -33,8 +33,8 @@ public class NoticeController {
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public String savePost(@ModelAttribute PostDto postDto) {
|
||||
noticeService.saveNotice(postDto.getTitle(), postDto.getContent());
|
||||
public String savePost(@ModelAttribute NoteDto noteDto) {
|
||||
noticeService.saveNotice(noteDto.getTitle(), noteDto.getContent());
|
||||
return "redirect:notice";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.example.springsecuritystudy.post;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.example.springsecuritystudy.user.User;
|
||||
|
||||
public interface PostRepository extends JpaRepository<Post, Long> {
|
||||
|
||||
List<Post> findByUserOrderByIdDesc(User user);
|
||||
|
||||
Post findByIdAndUser(Long id, User user);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user