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 ec25241 commit a39ec2f
Show file tree
Hide file tree
Showing 20 changed files with 220 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.woowacourse.zzazanstagram.model.article.domain.Article;
import com.woowacourse.zzazanstagram.model.article.domain.vo.Contents;
import com.woowacourse.zzazanstagram.model.article.domain.vo.ImageUrl;
import com.woowacourse.zzazanstagram.model.article.domain.vo.Image;
import com.woowacourse.zzazanstagram.model.article.dto.ArticleRequest;
import com.woowacourse.zzazanstagram.model.article.dto.ArticleResponse;

Expand All @@ -11,19 +11,19 @@
//Todo author 추가시 변경
public class ArticleAssembler {
public static Article toEntity(ArticleRequest dto) {
ImageUrl imageUrl = ImageUrl.of(dto.getImageUrl());
Image image = Image.of(dto.getImageUrl());
Contents contents = Contents.of(dto.getContents());

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

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

return new ArticleResponse(id, imageUrl.getUrl(), contents.getContents(), createdDate, lastModifiedDate);
return new ArticleResponse(id, image.getUrl(), contents.getContents(), createdDate, lastModifiedDate);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.woowacourse.zzazanstagram.model.article.domain;

import com.woowacourse.zzazanstagram.model.article.domain.vo.Contents;
import com.woowacourse.zzazanstagram.model.article.domain.vo.ImageUrl;
import com.woowacourse.zzazanstagram.model.article.domain.vo.Image;
import com.woowacourse.zzazanstagram.model.common.BaseEntity;

import javax.persistence.Entity;

@Entity
public class Article extends BaseEntity {
private ImageUrl imageUrl;
private Image image;
private Contents contents;

// @Column(name = "author", nullable = false)
Expand All @@ -17,24 +17,24 @@ public class Article extends BaseEntity {
private Article() {
}

private Article(ImageUrl imageUrl, Contents contents) {
this.imageUrl = imageUrl;
private Article(Image image, Contents contents) {
this.image = image;
this.contents = contents;
}

/* public Article(final ImageUrl imageUrl, final Contents contents, final Member author) {
this.imageUrl = imageUrl;
/* public Article(final Image image, final Contents contents, final Member author) {
this.image = image;
this.contents = contents;
this.author = author;
}*/


public static Article from(final ImageUrl imageUrl, final Contents contents) {
return new Article(imageUrl, contents);
public static Article from(final Image image, final Contents contents) {
return new Article(image, contents);
}

public ImageUrl getImageUrl() {
return imageUrl;
public Image getImage() {
return image;
}

public Contents getContents() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
package com.woowacourse.zzazanstagram.model.article.domain.vo;

import com.woowacourse.zzazanstagram.model.article.exception.ArticleException;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.net.HttpURLConnection;
import java.net.URL;

@Embeddable
public class ImageUrl {
public class Image {

@Column(name = "image_url", nullable = false)
private String url;

private ImageUrl() {
private Image() {
}

private Image(final String url) {
this.url = validateUrl(url);
}

private ImageUrl(final String url) {
this.url = url;
private String validateUrl(final String url) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("HEAD");
if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new ArticleException("리소스가 존재하지 않습니다");
}
} catch (Exception e) {
throw new ArticleException("리소스 경로가 올바르지 않습니다");
}
return url;
}

public static ImageUrl of(final String imageUrl) {
return new ImageUrl(imageUrl);
public static Image of(final String imageUrl) {
return new Image(imageUrl);
}

public String getUrl() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package com.woowacourse.zzazanstagram.model.article.dto;

import javax.persistence.Column;
import javax.persistence.Lob;
import javax.validation.constraints.NotBlank;

public class ArticleRequest {

@NotBlank
@Column(name = "image_url", nullable = false)
private String imageUrl;

@Lob
@Column(name = "contents")
private String contents;

@Column(name = "hash_tag")
private String hashTag;

public ArticleRequest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public class ArticleResponse {
private Long id;
private String imageUrl;
private String image;
private String contents;
// private String hashTag;
// private String hashTag;
private LocalDateTime createdDate;
private LocalDateTime lastModifiedDate;

Expand All @@ -15,9 +15,9 @@ public class ArticleResponse {
public ArticleResponse() {
}

public ArticleResponse(Long id, String imageUrl, String contents, LocalDateTime createdDate, LocalDateTime lastModifiedDate) {
public ArticleResponse(Long id, String image, String contents, LocalDateTime createdDate, LocalDateTime lastModifiedDate) {
this.id = id;
this.imageUrl = imageUrl;
this.image = image;
this.contents = contents;
this.createdDate = createdDate;
this.lastModifiedDate = lastModifiedDate;
Expand All @@ -27,8 +27,8 @@ public Long getId() {
return id;
}

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

public String getContents() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.woowacourse.zzazanstagram.model.article.exception;

public class ArticleException extends IllegalArgumentException {
public ArticleException() {
}

public ArticleException(String s) {
super(s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.woowacourse.zzazanstagram.model.member;

import com.woowacourse.zzazanstagram.model.member.exception.MemberException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice(assignableTypes = MemberController.class)
public class MemberControllerAdvice {
private static final Logger log = LoggerFactory.getLogger(MemberControllerAdvice.class);
private static final String TAG = "[MemberControllerAdvice]";

@ExceptionHandler(MemberException.class)
public String handleIllegalUserParamsException(MemberException e) {
log.error("{} MemberException >> {}", TAG, e.getMessage());

return "redirect:/signup";
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.woowacourse.zzazanstagram.model.member;

import com.woowacourse.zzazanstagram.model.member.vo.Email;
import com.woowacourse.zzazanstagram.model.member.vo.NickName;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findByEmail(Email email);

Optional<Member> findByNickName(NickName nickName);
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,56 @@
package com.woowacourse.zzazanstagram.model.member;

import com.woowacourse.zzazanstagram.model.member.exception.MemberException;
import com.woowacourse.zzazanstagram.model.member.vo.Email;
import com.woowacourse.zzazanstagram.model.member.vo.NickName;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class MemberService {
private static final String ERROR_ILLEGAL_LOGIN_MESSAGE = "로그인 정보가 올바르지 않습니다.";
private MemberRepository memberRepository;

public MemberService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}

public MemberResponse find(MemberLoginRequest request) {
Member member = validateEnrolledMember(request);
Member member = checkEnrolledMember(request);
return MemberAssembler.assemble(member);
}

private Member validateEnrolledMember(MemberLoginRequest request) {
return memberRepository.findByEmail(Email.of(request.getEmail()))
private Member checkEnrolledMember(MemberLoginRequest request) {
return findByEmail(request.getEmail())
.filter(m -> m.isMatchPassword(request.getPassword()))
.orElseThrow(() -> new IllegalArgumentException("로그인 정보가 올바르지 않습니다."));
.orElseThrow(() -> new MemberException(ERROR_ILLEGAL_LOGIN_MESSAGE));
}

private Optional<Member> findByEmail(String email) {
return memberRepository.findByEmail(Email.of(email));
}

public void save(MemberSignUpRequest memberSignupRequest) {
checkEnrolledEmail(memberSignupRequest.getEmail());
checkEnrolledNickName(memberSignupRequest.getNickName());
Member member = MemberAssembler.toEntity(memberSignupRequest);
memberRepository.save(member);
}

private void checkEnrolledEmail(String email) {
if (findByEmail(email).isPresent()) {
throw new MemberException("이미 존재하는 이메일 입니다.");
}
}

private void checkEnrolledNickName(String nickName) {
if (findByNickName(nickName).isPresent()) {
throw new MemberException("이미 존재하는 닉네임 입니다.");
}
}

private Optional<Member> findByNickName(String nickName) {
return memberRepository.findByNickName(NickName.of(nickName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.woowacourse.zzazanstagram.model.member.exception;

public class MemberException extends IllegalArgumentException {
public MemberException() {
}

public MemberException(String s) {
super(s);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.woowacourse.zzazanstagram.model.member.vo;

import com.woowacourse.zzazanstagram.model.member.exception.MemberException;

import javax.persistence.Column;
import javax.persistence.Embeddable;

Expand All @@ -23,7 +25,7 @@ public static Email of(final String email) {

private String validateEmail(final String email) {
if (!email.matches(EMAIL_REGEX)) {
throw new IllegalArgumentException("잘못된 형식의 이메일입니다.");
throw new MemberException("잘못된 형식의 이메일입니다.");
}
return email;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.woowacourse.zzazanstagram.model.member.vo;

import com.woowacourse.zzazanstagram.model.member.exception.MemberException;

import javax.persistence.Column;
import javax.persistence.Embeddable;

Expand All @@ -23,7 +25,7 @@ public static Name of(final String name) {

private String validateName(final String name) {
if (!name.matches(NAME_REGEX)) {
throw new IllegalArgumentException("이름은 2자 이상 10자 이하입니다.");
throw new MemberException("이름은 2자 이상 10자 이하입니다.");
}
return name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.woowacourse.zzazanstagram.model.member.vo;

import com.woowacourse.zzazanstagram.model.member.exception.MemberException;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class NickName {
private static final String NICK_NAME_REGEX = "[A-Za-zㄱ-ㅎㅏ-ㅣ가-힣]{2,10}";
private static final String NICK_NAME_REGEX = "[0-9A-Za-zㄱ-ㅎㅏ-ㅣ가-힣]{2,10}";

@Column(name = "nick_name", unique = true)
private String nickName;
Expand All @@ -23,7 +25,7 @@ public static NickName of(final String name) {

private String validateName(final String name) {
if (isMismatch(name)) {
throw new IllegalArgumentException("이름은 2자 이상 10자 이하입니다.");
throw new MemberException("이름은 2자 이상 10자 이하입니다.");
}
return name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.woowacourse.zzazanstagram.model.member.vo;

import com.woowacourse.zzazanstagram.model.member.exception.MemberException;
import org.mindrot.jbcrypt.BCrypt;

import javax.persistence.Column;
Expand All @@ -25,7 +26,7 @@ public static Password of(final String password) {

private String validatePassword(final String password) {
if (!password.matches(PASSWORD_REGEX)) {
throw new IllegalArgumentException("잘못된 형식의 비밀번호입니다.");
throw new MemberException("잘못된 형식의 비밀번호입니다.");
}
return encrypt(password);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.woowacourse.zzazanstagram.model.member.vo;

import com.woowacourse.zzazanstagram.model.member.exception.MemberException;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -28,10 +30,10 @@ private String validateUrl(final String url) {
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("HEAD");
if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IllegalArgumentException("리소스가 존재하지 않습니다");
throw new MemberException("리소스가 존재하지 않습니다");
}
} catch (Exception e) {
throw new IllegalArgumentException("리소스 경로가 올바르지 않습니다");
throw new MemberException("리소스 경로가 올바르지 않습니다");
}
return url;
}
Expand Down
Loading

0 comments on commit a39ec2f

Please sign in to comment.