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);
|
||||
|
||||
}
|
||||
@@ -11,12 +11,12 @@
|
||||
<h3>게시글 내역</h3>
|
||||
<div class="row">
|
||||
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
|
||||
<div th:each="post : ${posts}">
|
||||
<div th:each="note : ${notes}">
|
||||
<p style="margin: 10px 0;">
|
||||
<strong th:text="${post.title}"></strong>
|
||||
<strong th:text="${note.title}"></strong>
|
||||
Posted by
|
||||
<strong th:if="${post.user}" th:text="${post.user.username}"></strong> on
|
||||
<strong th:text="${#temporals.format(post.createdAt, 'yyyy-MM-dd')}"></strong>
|
||||
<strong th:if="${note.user}" th:text="${note.user.username}"></strong> on
|
||||
<strong th:text="${#temporals.format(note.createdAt, 'yyyy-MM-dd')}"></strong>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -66,9 +66,9 @@
|
||||
<a
|
||||
class="nav-link active"
|
||||
sec:authorize="hasAnyRole('ROLE_USER')"
|
||||
th:href="@{/post}"
|
||||
th:href="@{/note}"
|
||||
>
|
||||
게시글
|
||||
개인노트
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
|
||||
@@ -13,14 +13,14 @@
|
||||
<header th:insert="fragments.html::nav"></header>
|
||||
<!-- 개인 user만 접근할 수 있는 페이지 -->
|
||||
<div class="container">
|
||||
<h1>게시글</h1>
|
||||
<h1>개인노트</h1>
|
||||
|
||||
<!-- Button trigger modal -->
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#newPostModal"
|
||||
data-bs-target="#newNoteModal"
|
||||
data-bs-whatever="@mdo">
|
||||
새 글 쓰기
|
||||
</button>
|
||||
@@ -28,7 +28,7 @@
|
||||
<!-- Modal -->
|
||||
<div
|
||||
class="modal fade"
|
||||
id="newPostModal"
|
||||
id="newNoteModal"
|
||||
tabindex="-1"
|
||||
aria-labelledby="newPostModalLabel"
|
||||
aria-hidden="true"
|
||||
@@ -36,7 +36,7 @@
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="newPostModalLabel">새 글 쓰기</h5>
|
||||
<h5 class="modal-title" id="newNoteModalLabel">새 글 쓰기</h5>
|
||||
<button
|
||||
type="button"
|
||||
class="btn-close"
|
||||
@@ -45,7 +45,7 @@
|
||||
</button>
|
||||
</div>
|
||||
<form
|
||||
th:action="@{/post}"
|
||||
th:action="@{/note}"
|
||||
method="post"
|
||||
>
|
||||
<div class="modal-body">
|
||||
@@ -69,15 +69,15 @@
|
||||
|
||||
<div class="row">
|
||||
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
|
||||
<div class="border border-dark" th:each="post : ${posts}">
|
||||
<h2 th:text="${post.title}"></h2>
|
||||
<div class="border border-dark" th:each="note : ${notes}">
|
||||
<h2 th:text="${note.title}"></h2>
|
||||
<div>
|
||||
<p th:text="${post.content}"></p>
|
||||
<form th:action="@{/post}" th:method="delete">
|
||||
<input type="hidden" name="id" th:value="${post.id}">
|
||||
<span style="margin: 10px 0px;">Posted by
|
||||
<strong th:if="${post.user}" th:text="${post.user.username}"></strong> on
|
||||
<strong th:text="${#temporals.format(post.createdAt, 'yyyy-MM-dd')}"></strong>
|
||||
<p th:text="${note.content}"></p>
|
||||
<form th:action="@{/note}" th:method="delete">
|
||||
<input type="hidden" name="id" th:value="${note.id}">
|
||||
<span style="margin: 10px 0;">Posted On
|
||||
<strong th:if="${note.user}" th:text="${note.user.username}"></strong> on
|
||||
<strong th:text="${#temporals.format(note.createdAt, 'yyyy-MM-dd')}"></strong>
|
||||
</span>
|
||||
<button type="submit" class="btn btn-secondary">삭제</button>
|
||||
</form>
|
||||
@@ -76,7 +76,7 @@
|
||||
<p th:text="${notice.content}"></p>
|
||||
<form th:action="@{/notice}" th:method="delete">
|
||||
<input type="hidden" name="id" th:value="${notice.id}">
|
||||
<span style="margin: 10px 0px;">Posted At
|
||||
<span style="margin: 10px 0;">Posted On
|
||||
<strong th:text="${#temporals.format(notice.createdAt, 'yyyy-MM-dd')}"></strong>
|
||||
</span>
|
||||
<button
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.example.springsecuritystudy.note;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.example.springsecuritystudy.helper.TestConfig;
|
||||
import com.example.springsecuritystudy.user.User;
|
||||
import com.example.springsecuritystudy.user.UserService;
|
||||
|
||||
import static org.assertj.core.api.BDDAssertions.*;
|
||||
|
||||
class NoteServiceTest extends TestConfig {
|
||||
|
||||
@Autowired
|
||||
private NoteService noteService;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Test
|
||||
void findByUser_유저가_게시물조회() {
|
||||
//given
|
||||
User user = userService.signup("user", "user");
|
||||
noteService.saveNote(user, "title1", "content1");
|
||||
noteService.saveNote(user, "title2", "content2");
|
||||
//when
|
||||
List<Note> notes = noteService.findByUser(user);
|
||||
//then
|
||||
then(notes.size()).isEqualTo(2);
|
||||
Note note1 = notes.get(0);
|
||||
Note note2 = notes.get(1);
|
||||
|
||||
// post1 = title2
|
||||
then(note1.getUser().getUsername()).isEqualTo("user");
|
||||
then(note1.getTitle()).isEqualTo("title2");
|
||||
then(note1.getContent()).isEqualTo("content2");
|
||||
// post2 = title1
|
||||
then(note2.getUser().getUsername()).isEqualTo("user");
|
||||
then(note2.getTitle()).isEqualTo("title1");
|
||||
then(note2.getContent()).isEqualTo("content1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByUser_어드민이_조회() {
|
||||
// given
|
||||
User admin = userService.signupAdmin("admin", "admin");
|
||||
User user1 = userService.signup("user1", "user1");
|
||||
User user2 = userService.signup("user2", "user2");
|
||||
noteService.saveNote(user1, "title1", "content1");
|
||||
noteService.saveNote(user1, "title2", "content2");
|
||||
noteService.saveNote(user2, "title3", "content3");
|
||||
// when
|
||||
List<Note> notes = noteService.findByUser(admin);
|
||||
// then
|
||||
then(notes.size()).isEqualTo(3);
|
||||
Note note1 = notes.get(0);
|
||||
Note note2 = notes.get(1);
|
||||
Note note3 = notes.get(2);
|
||||
|
||||
// post1 = title3
|
||||
then(note1.getUser().getUsername()).isEqualTo("user2");
|
||||
then(note1.getTitle()).isEqualTo("title3");
|
||||
then(note1.getContent()).isEqualTo("content3");
|
||||
// post1 = title2
|
||||
then(note2.getUser().getUsername()).isEqualTo("user1");
|
||||
then(note2.getTitle()).isEqualTo("title2");
|
||||
then(note2.getContent()).isEqualTo("content2");
|
||||
// post1 = title1
|
||||
then(note3.getUser().getUsername()).isEqualTo("user1");
|
||||
then(note3.getTitle()).isEqualTo("title1");
|
||||
then(note3.getContent()).isEqualTo("content1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void savePost() {
|
||||
// given
|
||||
User user = userService.signup("user", "user");
|
||||
// when
|
||||
noteService.saveNote(user, "title", "content");
|
||||
// then
|
||||
then(noteService.findByUser(user).size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletePost() {
|
||||
// given
|
||||
User user = userService.signup("user", "user");
|
||||
Note note = noteService.saveNote(user, "title", "content");
|
||||
// when
|
||||
noteService.deleteNote(user, note.getId());
|
||||
// then
|
||||
then(noteService.findByUser(user).size()).isZero();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
package com.example.springsecuritystudy.post;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.example.springsecuritystudy.helper.TestConfig;
|
||||
import com.example.springsecuritystudy.user.User;
|
||||
import com.example.springsecuritystudy.user.UserService;
|
||||
|
||||
import static org.assertj.core.api.BDDAssertions.*;
|
||||
|
||||
class PostServiceTest extends TestConfig {
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Test
|
||||
void findByUser_유저가_게시물조회() {
|
||||
//given
|
||||
User user = userService.signup("user", "user");
|
||||
postService.savePost(user, "title1", "content1");
|
||||
postService.savePost(user, "title2", "content2");
|
||||
//when
|
||||
List<Post> posts = postService.findByUser(user);
|
||||
//then
|
||||
then(posts.size()).isEqualTo(2);
|
||||
Post post1 = posts.get(0);
|
||||
Post post2 = posts.get(1);
|
||||
|
||||
// post1 = title2
|
||||
then(post1.getUser().getUsername()).isEqualTo("user");
|
||||
then(post1.getTitle()).isEqualTo("title2");
|
||||
then(post1.getContent()).isEqualTo("content2");
|
||||
// post2 = title1
|
||||
then(post2.getUser().getUsername()).isEqualTo("user");
|
||||
then(post2.getTitle()).isEqualTo("title1");
|
||||
then(post2.getContent()).isEqualTo("content1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByUser_어드민이_조회() {
|
||||
// given
|
||||
User admin = userService.signupAdmin("admin", "admin");
|
||||
User user1 = userService.signup("user1", "user1");
|
||||
User user2 = userService.signup("user2", "user2");
|
||||
postService.savePost(user1, "title1", "content1");
|
||||
postService.savePost(user1, "title2", "content2");
|
||||
postService.savePost(user2, "title3", "content3");
|
||||
// when
|
||||
List<Post> posts = postService.findByUser(admin);
|
||||
// then
|
||||
then(posts.size()).isEqualTo(3);
|
||||
Post post1 = posts.get(0);
|
||||
Post post2 = posts.get(1);
|
||||
Post post3 = posts.get(2);
|
||||
|
||||
// post1 = title3
|
||||
then(post1.getUser().getUsername()).isEqualTo("user2");
|
||||
then(post1.getTitle()).isEqualTo("title3");
|
||||
then(post1.getContent()).isEqualTo("content3");
|
||||
// post1 = title2
|
||||
then(post2.getUser().getUsername()).isEqualTo("user1");
|
||||
then(post2.getTitle()).isEqualTo("title2");
|
||||
then(post2.getContent()).isEqualTo("content2");
|
||||
// post1 = title1
|
||||
then(post3.getUser().getUsername()).isEqualTo("user1");
|
||||
then(post3.getTitle()).isEqualTo("title1");
|
||||
then(post3.getContent()).isEqualTo("content1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void savePost() {
|
||||
// given
|
||||
User user = userService.signup("user", "user");
|
||||
// when
|
||||
postService.savePost(user, "title", "content");
|
||||
// then
|
||||
then(postService.findByUser(user).size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletePost() {
|
||||
// given
|
||||
User user = userService.signup("user", "user");
|
||||
Post post = postService.savePost(user, "title", "content");
|
||||
// when
|
||||
postService.deletePost(user, post.getId());
|
||||
// then
|
||||
then(postService.findByUser(user).size()).isZero();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user