-
Notifications
You must be signed in to change notification settings - Fork 46
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
[게시판 회원 기능] 베디 미션 제출 합니다. #63
Changes from all commits
21df515
52f4012
2159261
2fb0c09
95fd8d0
1cad0c7
44dc298
a9fda55
0386658
0b5b1bd
cd21f32
821ecce
908ed88
7d3a682
49bf7b7
c0ee6a0
38a5156
946b966
85d66f2
24d9d5f
eff17e5
8e92b91
5924c54
c5f9692
d98f66c
a7d29a4
724fc14
39d11f7
9fafd01
07aa7d2
da27b3f
f8eaf0f
0d41c3c
64c43a4
de6c1c0
ee00f77
f5d62fa
81b5335
8ff0ca3
39ae181
5502bff
80d6306
654fd03
1fc5673
296c66f
fe563ad
8db3f71
28a1191
86143aa
1a9bcb1
ff6f559
95d4f77
c5c801e
5c27961
fbf4518
edf822e
864ba07
53bf60c
9e31983
bbe330e
56e0055
09a288b
dde12ec
945cb29
3c6c1ba
13c0e9e
3fd3101
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package techcourse.myblog.articles; | ||
|
||
import lombok.*; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
|
||
import javax.persistence.*; | ||
import java.time.LocalDateTime; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
@ToString | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. toString도 쓰는 곳이 없으면 제거해보아요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사실 지금 상황에서는 사용할 일이 없지만 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 자답 : jpa 공부중인데 양방향 관계시에 toString()을 잘 못 사용하면 무한루프가 발생할 수 있다는군요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 맞습니다 ㅎㅎ; |
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
@EqualsAndHashCode(of = "id") | ||
public class Article { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column | ||
private String title; | ||
|
||
@Column | ||
private String contents; | ||
|
||
@Column | ||
private String coverUrl; | ||
|
||
@CreatedDate | ||
@Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP", updatable = false) | ||
private LocalDateTime regDate; | ||
|
||
@LastModifiedDate | ||
@Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") | ||
private LocalDateTime updateDate; | ||
|
||
@Builder | ||
private Article(final String title, final String contents, final String coverUrl) { | ||
this.title = title; | ||
this.contents = contents; | ||
this.coverUrl = coverUrl; | ||
} | ||
|
||
public void update(Article other) { | ||
this.updateDate = LocalDateTime.now(); | ||
this.title = other.title; | ||
this.coverUrl = other.coverUrl; | ||
this.contents = other.contents; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package techcourse.myblog.articles; | ||
|
||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@Controller | ||
@RequestMapping("/articles") | ||
@RequiredArgsConstructor | ||
public class ArticleController { | ||
private final ArticleService articleService; | ||
|
||
@GetMapping("/new") | ||
public String writeForm() { | ||
return "article-edit"; | ||
} | ||
|
||
@PostMapping | ||
public String write(Article article) { | ||
Article savedArticle = articleService.save(article); | ||
return "redirect:/articles/" + savedArticle.getId(); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public String show(@PathVariable Long id, Model model) { | ||
Article article = articleService.findById(id); | ||
model.addAttribute(article); | ||
return "article"; | ||
} | ||
|
||
@GetMapping("/{id}/edit") | ||
public String editForm(@PathVariable Long id, Model model) { | ||
Article article = articleService.findById(id); | ||
model.addAttribute(article); | ||
return "article-edit"; | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public String edit(Article editedArticle) { | ||
Article article = articleService.edit(editedArticle); | ||
return "redirect:/articles/" + article.getId(); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public String delete(@PathVariable Long id) { | ||
articleService.deleteById(id); | ||
return "redirect:/"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package techcourse.myblog.articles; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ArticleRepository extends JpaRepository<Article, Long> { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package techcourse.myblog.articles; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class ArticleService { | ||
private final ArticleRepository articleRepository; | ||
|
||
public Article save(Article article) { | ||
return articleRepository.save(article); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public Article findById(Long id) { | ||
return articleRepository.findById(id) | ||
.orElseThrow(() -> new IllegalArgumentException("Can't find Article : " + id)); | ||
} | ||
|
||
public Article edit(Article editedArticle) { | ||
Article article = findById(editedArticle.getId()); | ||
|
||
article.update(editedArticle); | ||
|
||
return article; | ||
} | ||
|
||
public void deleteById(Long id) { | ||
articleRepository.deleteById(id); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public Page<Article> findAll(Pageable pageable) { | ||
return articleRepository.findAll(pageable); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package techcourse.myblog.config; | ||
|
||
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; | ||
import org.springframework.boot.web.servlet.error.ErrorAttributes; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.context.request.WebRequest; | ||
import techcourse.myblog.users.ValidUserException; | ||
|
||
import java.util.Map; | ||
|
||
@Configuration | ||
public class ErrorConfig { | ||
@Bean | ||
public ErrorAttributes errorAttributes() { | ||
return new DefaultErrorAttributes() { | ||
|
||
@Override | ||
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { | ||
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace); | ||
Throwable error = getError(webRequest); | ||
if (error instanceof ValidUserException) { | ||
errorAttributes.put("errors", ((ValidUserException) error).getErrors()); | ||
} | ||
return errorAttributes; | ||
} | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package techcourse.myblog.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
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.interceptor.AuthInterceptor; | ||
import techcourse.myblog.interceptor.LoginInterceptor; | ||
import techcourse.myblog.users.UserSessionArgumentResolver; | ||
|
||
import java.util.List; | ||
|
||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
private final LoginInterceptor loginInterceptor; | ||
private final AuthInterceptor authInterceptor; | ||
private final UserSessionArgumentResolver userSessionArgumentResolver; | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(loginInterceptor) | ||
.addPathPatterns("/users") | ||
.addPathPatterns("/users/**"); | ||
|
||
registry.addInterceptor(authInterceptor) | ||
.addPathPatterns("/users/{id}") | ||
.addPathPatterns("/users/{id}/edit") | ||
.excludePathPatterns("/users/logout"); | ||
} | ||
|
||
@Override | ||
public void addArgumentResolvers(final List<HandlerMethodArgumentResolver> resolvers) { | ||
resolvers.add(userSessionArgumentResolver); | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setter는 제거해보아요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기서 Setter를 사용한 이유는 �컨트롤러에서 파라미터로 받기 위해서 입니다.
public String write(Article article) {
setter를 없애려면 dto를 만들어야 하는데 현재는 dto를가 필요 없다고 판단해서 이렇게 했습니다.
setter를 제거하고 dto를 만드는 것이 좋을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
지금 과정에서는 dto 추가하지말고 지금 구조로 가시죠!