Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[댓글 기능] 제이 미션 제출합니다. #107

Merged
merged 72 commits into from
Aug 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
dd87430
docs: Readme 업데이트
MrKwon Jul 9, 2019
a2287b4
feat: 게시글 생성, 목록 조회 기능 구현
MrKwon Jul 9, 2019
940525c
feat: 게시글 조회 구현
MrKwon Jul 9, 2019
5430d29
feat: 공통 Head 분리
MrKwon Jul 9, 2019
1f9ef73
feat: html 중복제거
JunHoPark93 Jul 10, 2019
8dced8c
feat: 게시글 수정
JunHoPark93 Jul 10, 2019
9a3475c
feat: 게시글 삭제 구현
JunHoPark93 Jul 10, 2019
8731d42
feat: devtools 적용
JunHoPark93 Jul 10, 2019
66751cd
refactor: Js 분리
JunHoPark93 Jul 10, 2019
8901bf4
refactor: 컨트롤러 메서드 이름 수정, autowired 삭제
JunHoPark93 Jul 10, 2019
07ebbc1
refactor: 불필요한 메서드 제거
JunHoPark93 Jul 10, 2019
f2e700e
feat: Article 제목 검증 추가
JunHoPark93 Jul 11, 2019
1961b76
feat: error 페이지 추가
JunHoPark93 Jul 11, 2019
f44d9ff
refactor: 피드백 반영
JunHoPark93 Jul 15, 2019
8ef6ba5
feat: Jpa repository 적용
JunHoPark93 Jul 16, 2019
dc2a199
Merge branch 'temp' into JunHoPark93
JunHoPark93 Jul 16, 2019
087e5e0
docs: README 업데이트
JunHoPark93 Jul 16, 2019
15e570a
feat: 회원 등록 기능
JunHoPark93 Jul 16, 2019
47ae91d
feat: 유저 조회 기능 구현
JunHoPark93 Jul 16, 2019
6e9726b
feat: 로그인 및 로그아웃 기능 구현
JunHoPark93 Jul 17, 2019
c2ea962
feat: 회원 수정 및 탈퇴기능 구현
JunHoPark93 Jul 17, 2019
e6f5579
refactor: html 중복제거 및 세션적용
JunHoPark93 Jul 18, 2019
3e2bd3e
fix: Interceptor에서 /users post요청은 통과
JunHoPark93 Jul 18, 2019
63235c6
feat: Error 객체의 정의
JunHoPark93 Jul 18, 2019
924cee2
test: 세션관련 인수테스트 작성
JunHoPark93 Jul 18, 2019
7c83e30
test: 회원관련 인수테스트
JunHoPark93 Jul 18, 2019
bb280ba
refactor: 들여쓰기 및 사용하지 않는 import 제거
JunHoPark93 Jul 18, 2019
e00061f
feat: 회원가입시 이메일 중복 오류 처리
JunHoPark93 Jul 18, 2019
614121c
fix: 회원 가입시 email 중복 테스트 수정
JunHoPark93 Jul 18, 2019
8d0188b
refactor: UserService 분리
JunHoPark93 Jul 18, 2019
7a76891
feat: 회원 수정시 이름 형식 오류 처리
JunHoPark93 Jul 18, 2019
2553842
feat: 회원 가입시 유효하지 않은 입력 처리
JunHoPark93 Jul 18, 2019
c2f89df
refactor: Article 서비스 패키지 분리
JunHoPark93 Jul 18, 2019
dbe40f5
fix: 유저 리스트에서 다른 유저 정보에 접근하는 오류 처리
JunHoPark93 Jul 19, 2019
8985502
feat: 데이터베이스 구축
JunHoPark93 Jul 19, 2019
e537451
refactor: 삼항 연산자로 변경
JunHoPark93 Jul 19, 2019
c48f2f6
refactor: 삼항 연산자로 변경
JunHoPark93 Jul 19, 2019
e2fec3b
fix: 게시글 id 접근하는 로직 수정
JunHoPark93 Jul 19, 2019
6f21dfa
fix: 절대 경로로 변경
JunHoPark93 Jul 19, 2019
0ed6e1b
refactor: 삼항 연산자로 변경, url 변경
JunHoPark93 Jul 19, 2019
615e6fd
refactor: fragments 패키지로 분리
JunHoPark93 Jul 19, 2019
0e6e088
refactor: 피드백 반영
JunHoPark93 Jul 19, 2019
5447551
feat: 비밀번호 암호화 구현
JunHoPark93 Jul 20, 2019
cefea32
refactor: implements 삭제
JunHoPark93 Jul 20, 2019
b4d7ea5
feat: 비밀번호, 이메일 검증 로직 개선
JunHoPark93 Jul 22, 2019
e42ed91
feat: 검증 로직 추가
JunHoPark93 Jul 22, 2019
b90115e
fix: 없는 게시물 접근 처리
JunHoPark93 Jul 22, 2019
9f8190b
test: 테스트 파라미터 추가
JunHoPark93 Jul 22, 2019
59eaeb7
fix: email validation 추가
JunHoPark93 Jul 22, 2019
95ae854
test: cookie를 이용한 테스트
JunHoPark93 Jul 23, 2019
09b41c6
refactor: 피드백 반영
JunHoPark93 Jul 23, 2019
840253b
refactor: localport 제거
JunHoPark93 Jul 23, 2019
49fbaa5
refactor: 패키지 이름 변경
JunHoPark93 Jul 23, 2019
52a6d6c
refactor: 클래스 이름 변경
JunHoPark93 Jul 23, 2019
f4537c5
Merge remote-tracking branch 'upstream/JunHoPark93' into JunHoPark93
JunHoPark93 Jul 25, 2019
ed42da0
feat: paging 적용
JunHoPark93 Jul 26, 2019
cabf579
feat: article, user 관계 매핑
JunHoPark93 Jul 26, 2019
ccb502d
feat: article 작성자 매핑 기능 추가
JunHoPark93 Jul 26, 2019
8eb91d1
feat: Comment 작성, 조회
JunHoPark93 Jul 29, 2019
cc93b1a
feat: Comment 수정, 삭제
JunHoPark93 Jul 29, 2019
5304a84
refactor: comment redirect
JunHoPark93 Jul 30, 2019
0c28a01
feat: Comment 시간 계산
JunHoPark93 Jul 30, 2019
b4ef0a2
fix: 타인이 게시글 삭제 막기
JunHoPark93 Jul 30, 2019
85ff44c
test: 삭제 테스트 수정 및 정리
JunHoPark93 Jul 30, 2019
c915849
feat: JPA auditing 적용
JunHoPark93 Jul 31, 2019
fc5aa5c
feat: Comment 응답 dto에 대한 Custom Mapper의 구현
JunHoPark93 Jul 31, 2019
ed0e739
refactor: test 중복제거
JunHoPark93 Jul 31, 2019
e743f49
feat: JPA auditing, custom Context holder 적용
JunHoPark93 Aug 1, 2019
411388d
fix: session holder 코드 원복
JunHoPark93 Aug 2, 2019
6be9e90
refactor: Comment 검증 로직
JunHoPark93 Aug 2, 2019
c1b252e
feat: CoverUrl resource의 존재 확인
JunHoPark93 Aug 2, 2019
8454227
test: coverUrl 경로 수정
JunHoPark93 Aug 2, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/main/java/techcourse/myblog/domain/Article.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import techcourse.myblog.domain.common.ContentsAudit;

import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.*;

@Entity
public class Article extends ContentsAudit {
@Column(nullable = false)
private String title;

@Column(nullable = false)
private String coverUrl;

@Column(nullable = false)
@Lob
private String contents;

@ManyToOne
@JoinColumn(name = "author", foreignKey = @ForeignKey(name = "fk_article_to_user"))
@JoinColumn(foreignKey = @ForeignKey(name = "FK_article_to_user"))
private User author;

private Article() {
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/techcourse/myblog/domain/Comment.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package techcourse.myblog.domain;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import techcourse.myblog.domain.common.ContentsAudit;

import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.*;

@Entity
public class Comment extends ContentsAudit {
@Lob
@Column(nullable = false)
private String contents;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 구조에서는 contents는 varchar(255)로 세팅될텐데요. 더 긴 문자열을 받을 수 있도록 데이터타입을 바꿔주는 것은 어떨까요
지금과 같이 데이터 크기를 결정하기 힘든 큰 크기의 데이터는 @lob을 쓰는 것도 한번 고려해보세요.


@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "FK_comment_to_user"), nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private User commenter;

@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "FK_comment_to_article"), nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Article article;

private Comment() {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/techcourse/myblog/domain/User.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package techcourse.myblog.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -17,8 +14,13 @@ public class User {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false, length = 50)
private String name;

@Column(nullable = false)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

java bean validator를 사용하여 email 등 포맷의 유효성 검증을 할 수 있어요

https://www.baeldung.com/javax-validation

private String email;

@Column(nullable = false)
private String password;

private User() {
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/techcourse/myblog/domain/common/ContentsAudit.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ public abstract class ContentsAudit {
@LastModifiedDate
private LocalDateTime lastModifiedDate;

@CreatedBy
@OneToOne
private User createdBy;

@LastModifiedBy
@OneToOne
private User lastModifiedBy;
Expand All @@ -44,10 +40,6 @@ public LocalDateTime getLastModifiedDate() {
return lastModifiedDate;
}

public User getCreatedBy() {
return createdBy;
}

public User getLastModifiedBy() {
return lastModifiedBy;
}
Expand All @@ -64,10 +56,6 @@ public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}

public void setCreatedBy(User createdBy) {
this.createdBy = createdBy;
}

public void setLastModifiedBy(User lastModifiedBy) {
this.lastModifiedBy = lastModifiedBy;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package techcourse.myblog.support.auth;

public class UserAuthentication {
private String sessionId;

public String getSessionId() {
return sessionId;
}

public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package techcourse.myblog.support.auth;

import techcourse.myblog.domain.User;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserSessionContextHolder {
private static Map<UserAuthentication, User> context = new ConcurrentHashMap<>();

public static void bindContext(UserAuthentication authentication, User user) {
context.put(authentication, user);
}

public static User getUserAuthentication(UserAuthentication authentication) {
return context.get(authentication);
}

public static void removeAuthentication(UserAuthentication authentication) {
context.remove(authentication);
}
}

This file was deleted.

22 changes: 21 additions & 1 deletion src/main/java/techcourse/myblog/support/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
import org.modelmapper.ModelMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import techcourse.myblog.support.auth.UserAuthentication;
import techcourse.myblog.support.config.mapper.CommentResponseMapper;
import techcourse.myblog.support.encryptor.EncryptHelper;
import techcourse.myblog.support.encryptor.SaltEncrypt;
import techcourse.myblog.web.interceptor.AuthInterceptor;
import techcourse.myblog.web.interceptor.SessionContextInterceptor;
import techcourse.myblog.web.resolver.SessionResolver;

import java.util.List;

@Configuration
public class WebConfig {
Expand All @@ -17,13 +23,22 @@ public WebMvcConfigurer interceptorConfigure() {
return new WebMvcConfigurer() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuthInterceptor())
registry.addInterceptor(new AuthInterceptor(userAuthentication()))
.addPathPatterns("/users")
.addPathPatterns("/mypage-edit")
.addPathPatterns("/mypage")
.addPathPatterns("/mypage/*")
.addPathPatterns("/writing")
.addPathPatterns("/articles");

registry.addInterceptor(new SessionContextInterceptor(userAuthentication()))
.addPathPatterns("/logout")
.addPathPatterns("/users/*");
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new SessionResolver());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
};
}
Expand All @@ -39,4 +54,9 @@ public ModelMapper modelMapper() {
modelMapper.addMappings(new CommentResponseMapper());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return modelMapper;
}

@Bean
public UserAuthentication userAuthentication() {
return new UserAuthentication();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import techcourse.myblog.domain.User;
import techcourse.myblog.support.config.UserSessionContext;
import techcourse.myblog.support.auth.UserAuthentication;
import techcourse.myblog.support.auth.UserSessionContextHolder;

import java.util.Optional;

@Configuration
@EnableJpaAuditing(auditorAwareRef = "blogAuditorAware")
public class JpaAuditingConfig {
private UserAuthentication userAuthentication;

public JpaAuditingConfig(UserAuthentication userAuthentication) {
this.userAuthentication = userAuthentication;
}

@Bean
public AuditorAware<User> blogAuditorAware() {
return () -> Optional.of(UserSessionContext.getUser());
return () -> Optional.of(UserSessionContextHolder.getUserAuthentication(userAuthentication));
}
}
22 changes: 7 additions & 15 deletions src/main/java/techcourse/myblog/web/ArticleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import techcourse.myblog.service.CommentService;
import techcourse.myblog.service.dto.ArticleRequest;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@Controller
Expand All @@ -23,21 +22,14 @@ public ArticleController(ArticleService articleService, CommentService commentSe
this.commentService = commentService;
}

// @GetMapping("/")
// public String index(@RequestParam(defaultValue = "1") int page, Model model) {
// model.addAttribute("articles", articleService.findAll(page));
// return "index";
// }

@GetMapping()
public String formArticle(Model model) {
model.addAttribute("article", null);
return "article-edit";
}

@PostMapping()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

()는 제거해주셔도 됩니다.

public String saveArticle(@Valid ArticleRequest articleRequest, Model model, HttpSession httpSession) {
User user = (User) httpSession.getAttribute("user");
public String saveArticle(@Valid ArticleRequest articleRequest, Model model, User user) {
Article article = articleService.save(articleRequest, user);
model.addAttribute("article", article);
return "redirect:/articles/" + article.getId();
Expand All @@ -52,22 +44,22 @@ public String selectArticle(@PathVariable("articleId") long articleId, Model mod
}

@GetMapping("/{articleId}/edit")
public String edit(@PathVariable("articleId") long articleId, Model model, HttpSession httpSession) {
Article article = articleService.findByIdWithUser(articleId, (User) httpSession.getAttribute("user"));
public String edit(@PathVariable("articleId") long articleId, Model model, User user) {
Article article = articleService.findByIdWithUser(articleId, user);
model.addAttribute("article", article);
return "article-edit";
}

@PutMapping("/{articleId}")
public String editArticle(@PathVariable("articleId") long articleId, @ModelAttribute ArticleRequest articleRequest, HttpSession httpSession, Model model) {
Article article = articleService.editArticle(articleRequest, articleId, (User) httpSession.getAttribute("user"));
public String editArticle(@PathVariable("articleId") long articleId, @ModelAttribute ArticleRequest articleRequest, User user, Model model) {
Article article = articleService.editArticle(articleRequest, articleId, user);
model.addAttribute("article", article);
return "redirect:/articles/" + articleId;
}

@DeleteMapping("/{articleId}")
public String deleteArticle(@PathVariable("articleId") long articleId, HttpSession httpSession) {
articleService.deleteById(articleId, (User) httpSession.getAttribute("user"));
public String deleteArticle(@PathVariable("articleId") long articleId, User user) {
articleService.deleteById(articleId, user);
return "redirect:/";
}
}
12 changes: 6 additions & 6 deletions src/main/java/techcourse/myblog/web/CommentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public CommentController(CommentService commentService, ArticleService articleSe
}

@PostMapping
public String saveComment(@Valid CommentRequest commentRequest, HttpSession httpSession) {
public String saveComment(@Valid CommentRequest commentRequest, User user) {
Article article = articleService.findById(commentRequest.getArticleId());
commentService.save(commentRequest, article, (User) httpSession.getAttribute("user"));
commentService.save(commentRequest, article, user);
return "redirect:/articles/" + commentRequest.getArticleId();
}

Expand All @@ -38,14 +38,14 @@ public String editCommentPage(HttpSession httpSession, @PathVariable("commentId"
}

@PutMapping("/{commentId}")
public String editComment(@Valid CommentRequest commentRequest, @PathVariable("commentId") Long commentId, HttpSession httpSession) {
commentService.update(commentRequest, (User) httpSession.getAttribute("user"), commentId);
public String editComment(@Valid CommentRequest commentRequest, @PathVariable("commentId") Long commentId, User user) {
commentService.update(commentRequest, user, commentId);
return "redirect:/articles/" + commentRequest.getArticleId();
}

@DeleteMapping("/{commentId}")
public String deleteComment(@PathVariable("commentId") Long commentId, HttpSession httpSession) {
commentService.deleteById(commentId, (User) httpSession.getAttribute("user"));
public String deleteComment(@PathVariable("commentId") Long commentId, User user) {
commentService.deleteById(commentId, user);
return "redirect:/";
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/techcourse/myblog/web/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import techcourse.myblog.service.dto.UserEditRequest;
import techcourse.myblog.service.dto.UserLoginRequest;
import techcourse.myblog.service.dto.UserRequest;
import techcourse.myblog.support.config.UserSessionContext;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
Expand All @@ -29,7 +28,6 @@ public String createLoginForm(HttpServletRequest request, UserLoginRequest userL
if (request.getSession().getAttribute(USER) == null) {
return "login";
}

return "redirect:/";
}

Expand All @@ -54,8 +52,8 @@ public String showUsers(Model model) {
}

@GetMapping("/mypage")
public String myPageForm(Model model, HttpServletRequest request) {
model.addAttribute(USER, request.getSession().getAttribute(USER));
public String myPageForm(Model model, User user) {
model.addAttribute(USER, user);
return "mypage";
}

Expand All @@ -68,14 +66,12 @@ public String myPageEditForm(UserEditRequest userEditRequest) {
public String login(UserLoginRequest userLoginRequest, HttpServletRequest request) {
User user = userService.findUserByEmail(userLoginRequest);
request.getSession().setAttribute(USER, user);
UserSessionContext.set(user);
return "redirect:/";
}

@GetMapping("/logout")
public String logout(HttpServletRequest request) {
request.getSession().removeAttribute(USER);
UserSessionContext.remove();
return "redirect:/";
}

Expand All @@ -93,7 +89,6 @@ public String editUser(@PathVariable("userId") Long userId, HttpServletRequest r
public String deleteUser(@PathVariable("userId") Long userId, HttpServletRequest request) {
userService.deleteById(userId);
request.getSession().removeAttribute(USER);
UserSessionContext.remove();
return "redirect:/";
}
}
Loading