Skip to content

Commit

Permalink
[#44] refactor: 프론트엔드 로직과 백엔드 로직 연동
Browse files Browse the repository at this point in the history
  • Loading branch information
wbluke committed Aug 15, 2019
1 parent 07f7f4b commit 1a09c8a
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@

public class ArticleAssembler {
public static Article toEntity(ArticleRequest dto, Member author) {
Image image = Image.of(dto.getImageUrl());
Image image = Image.of(dto.getImage());
Contents contents = Contents.of(dto.getContents());

return Article.from(image, contents, author);
}

public static ArticleResponse toDto(Article article) {
Long id = article.getId();
Image image = article.getImage();
Contents contents = article.getContents();
LocalDateTime createdDate = article.getCreatedDate();
LocalDateTime lastModifiedDate = article.getLastModifiedDate();

return new ArticleResponse(id, image.getUrl(), contents.getContents(), createdDate, lastModifiedDate);
return ArticleResponse.ArticleResponseBuilder.anArticleResponse()
.id(id)
.image(article.image())
.contents(article.contents())
.nickName(article.getAuthor().nickName())
.profileImage(article.getAuthor().profileImage())
.createdDate(createdDate)
.lastModifiedDate(lastModifiedDate)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

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

@Controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public Contents getContents() {
return contents;
}

public String image() {
return image.getUrl();
}

public String contents() {
return contents.getContents();
}

public Member getAuthor() {
return author;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
public class ArticleRequest {

@NotBlank
private String imageUrl;
private String image;

private String contents;
private String hashTag;

public ArticleRequest() {
}

public ArticleRequest(String imageUrl, String contents, String hashTag) {
this.imageUrl = imageUrl;
public ArticleRequest(String image, String contents, String hashTag) {
this.image = image;
this.contents = contents;
this.hashTag = hashTag;
}

public String getImageUrl() {
return imageUrl;
public String getImage() {
return image;
}

public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
public void setImage(String image) {
this.image = image;
}

public String getContents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ public class ArticleResponse {
private String image;
private String contents;
// private String hashTag;
private String nickName;
private String profileImage;
private LocalDateTime createdDate;
private LocalDateTime lastModifiedDate;

//Todo author, 댓글, 좋아요 추가하자
//Todo 댓글, 좋아요 추가하자

public ArticleResponse() {
}

public ArticleResponse(Long id, String image, String contents, LocalDateTime createdDate, LocalDateTime lastModifiedDate) {
private ArticleResponse(Long id, String image, String contents, String nickName, String profileImage, LocalDateTime createdDate, LocalDateTime lastModifiedDate) {
this.id = id;
this.image = image;
this.contents = contents;
this.nickName = nickName;
this.profileImage = profileImage;
this.createdDate = createdDate;
this.lastModifiedDate = lastModifiedDate;
}
Expand All @@ -35,11 +39,77 @@ public String getContents() {
return contents;
}

public String getNickName() {
return nickName;
}

public String getProfileImage() {
return profileImage;
}

public LocalDateTime getCreatedDate() {
return createdDate;
}

public LocalDateTime getLastModifiedDate() {
return lastModifiedDate;
}


public static final class ArticleResponseBuilder {
private Long id;
private String image;
private String contents;
// private String hashTag;
private String nickName;
private String profileImage;
private LocalDateTime createdDate;
private LocalDateTime lastModifiedDate;

private ArticleResponseBuilder() {
}

public static ArticleResponseBuilder anArticleResponse() {
return new ArticleResponseBuilder();
}

public ArticleResponseBuilder id(Long id) {
this.id = id;
return this;
}

public ArticleResponseBuilder image(String image) {
this.image = image;
return this;
}

public ArticleResponseBuilder contents(String contents) {
this.contents = contents;
return this;
}

public ArticleResponseBuilder nickName(String nickName) {
this.nickName = nickName;
return this;
}

public ArticleResponseBuilder profileImage(String profileImage) {
this.profileImage = profileImage;
return this;
}

public ArticleResponseBuilder createdDate(LocalDateTime createdDate) {
this.createdDate = createdDate;
return this;
}

public ArticleResponseBuilder lastModifiedDate(LocalDateTime lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
return this;
}

public ArticleResponse build() {
return new ArticleResponse(id, image, contents, nickName, profileImage, createdDate, lastModifiedDate);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.woowacourse.zzazanstagram.model.article.repository.ArticleRepository;
import com.woowacourse.zzazanstagram.model.member.Member;
import com.woowacourse.zzazanstagram.model.member.MemberService;
import com.woowacourse.zzazanstagram.model.member.exception.MemberException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -38,7 +37,7 @@ public List<ArticleResponse> getArticleResponses() {

public void save(ArticleRequest dto, String email) {
Member author = memberService.findMemberByEmail(email);

Article article = ArticleAssembler.toEntity(dto, author);
articleRepository.save(article);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public String loginForm() {
@PostMapping("/login")
public String login(MemberLoginRequest memberLoginRequest, HttpSession httpSession) {
MemberResponse memberResponse = loginService.find(memberLoginRequest);
httpSession.setAttribute(MEMBER, new MemberSession(memberResponse.getEmail()));
httpSession.setAttribute(MEMBER, new MemberSession(memberResponse.getEmail(), memberResponse.getNickName(), memberResponse.getProfileImage()));
return "redirect:/";
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.woowacourse.zzazanstagram.model.member;

public class MemberSession {
import java.io.Serializable;

public class MemberSession implements Serializable {
private String email;
private String nickName;
private String profileImage;

public MemberSession(String email) {
public MemberSession(String email, String nickName, String profileImage) {
this.email = email;
this.nickName = nickName;
this.profileImage = profileImage;
}

public String getEmail() {
Expand All @@ -14,4 +20,20 @@ public String getEmail() {
public void setEmail(String email) {
this.email = email;
}

public String getNickName() {
return nickName;
}

public void setNickName(String nickName) {
this.nickName = nickName;
}

public String getProfileImage() {
return profileImage;
}

public void setProfileImage(String profileImage) {
this.profileImage = profileImage;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.devtools.restart.enabled=true
spring.devtools.restart.enabled=false
spring.devtools.livereload.enabled=true

# DataSource
Expand Down
8 changes: 3 additions & 5 deletions src/main/resources/templates/article-edit.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
Expand Down Expand Up @@ -70,11 +70,9 @@
<div class="feed-header padding-15">
<ul class="list-unstyled list-info">
<li>
<!-- TODO : th:src 적용 -->
<img class="thumb-img img-circle" src="http://newsimg.hankookilbo.com/2016/04/13/201604131460701467_1.jpg" alt="">
<img class="thumb-img img-circle" th:src="${session.member.profileImage}" alt="">
<div class="info">
<!-- TODO : th:nickname 적용 -->
<a href="" class="title no-pdd-vertical text-bold inline-block font-size-15">eastjun</a>
<p th:text="${session.member.nickName}" class="title no-pdd-vertical text-bold inline-block font-size-15"></p>
</div>
<div class="info">
<text style="font-size: 12px; color: gray;">새 글 쓰기</text>
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@
<div class="feed-header padding-15">
<ul class="list-unstyled list-info">
<li>
<img class="thumb-img img-circle" src="images/default/eastjun_profile.jpg" alt="">
<img class="thumb-img img-circle" th:src="${article.profileImage}" alt="">
<div class="info">
<a th:text="user넣으렴" href="" class="title no-pdd-vertical text-bold inline-block font-size-15"></a>
<a th:text="${article.nickName}" href="" class="title no-pdd-vertical text-bold inline-block font-size-15"></a>
</div>
</li>
<a class="pointer absolute top-10 right-0" data-toggle="dropdown" aria-expanded="false">
Expand Down Expand Up @@ -149,7 +149,7 @@
<ul class="list-unstyled list-info pdd-horizon-5">
<li class="comment-item no-pdd">
<div class="info pdd-left-15 pdd-vertical-5">
<a th:text="user넣으렴" href="" class="title no-pdd-vertical text-bold inline-block font-size-15"></a>
<a th:text="${article.nickName}" href="" class="title no-pdd-vertical text-bold inline-block font-size-15"></a>
<!--<span class="font-size-14">안돌 어디갔다 온거였지?</span>-->
<p th:text="${article.contents}"></p><p>
<a href="" class="hashtag">#우테코1기</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private WebTestClient.ResponseSpec showArticles() {

private WebTestClient.ResponseSpec createArticle() {
return postHeaderWithLogin("/articles")
.body(BodyInserters.fromFormData("imageUrl", IMAGE_URL)
.body(BodyInserters.fromFormData("image", IMAGE_URL)
.with("contents", CONTENTS)
.with("hashTag", HASHTAG))
.exchange();
Expand Down

0 comments on commit 1a09c8a

Please sign in to comment.