Skip to content

Commit

Permalink
refactor: ListEntity 관련 Domain 객체 전체 리팩터링 (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdkdhoho committed Feb 16, 2024
1 parent c5ce9b3 commit ca16882
Show file tree
Hide file tree
Showing 27 changed files with 435 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class JwtManager {

private static final SecretKey key = Jwts.SIG.HS256.key().build();
private static final Long ACCESS_TOKEN_VALID_MILLISECOND = HOURS.toMillis(8);
private static final String PREFIX = "Bearer ";

public String createToken(Long userId) {
Date now = new Date();
Expand All @@ -30,10 +31,10 @@ public String createToken(Long userId) {
}

public Long read(String token) {
if (token.isBlank() || !token.startsWith("Bearer ")) {
if (token.isBlank() || !token.startsWith(PREFIX)) {
throw new CustomException(REQUIRED_ACCESS_TOKEN);
}
token = token.substring(7);
token = token.substring(PREFIX.length());

String subject = Jwts.parser()
.verifyWith(key)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.listywave.collaborator.application.domain;

import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import com.listywave.list.application.domain.list.ListEntity;
import com.listywave.user.application.domain.User;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -19,26 +20,23 @@
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = PROTECTED)
public class Collaborator {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "user_id")
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "list_id")
private ListEntity list;

public static Collaborator createCollaborator(User user, ListEntity list) {
return Collaborator.builder()
.user(user)
.list(list)
.build();
public static Collaborator init(User user, ListEntity list) {
return new Collaborator(null, user, list);
}

public String getUserNickname() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.listywave.collaborator.application.domain;

import static com.listywave.common.exception.ErrorCode.DUPLICATE_USER;

import com.listywave.common.exception.CustomException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Collaborators {

private final List<Collaborator> collaborators;

public Collaborators(List<Collaborator> collaborators) {
this.collaborators = new ArrayList<>(collaborators);
}

public static void validateDuplicateCollaboratorIds(List<Long> collaboratorIds) {
Set<Long> uniqueIds = new HashSet<>(collaboratorIds);
if (collaboratorIds.size() != uniqueIds.size()) {
throw new CustomException(DUPLICATE_USER, "중복된 콜라보레이터를 선택할 수 없습니다.");
}
}

public List<Collaborator> getCollaborators() {
return new ArrayList<>(collaborators);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

public interface CollaboratorRepository extends JpaRepository<Collaborator, Long> {

List<Collaborator> findAllByListId(Long listId);

List<Collaborator> findAllByList(ListEntity list);
@Query("select c from Collaborator c join fetch c.list l where c.user.id =:userId")
List<Collaborator> findAllByUserId(Long userId);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.listywave.collection.application.domain;

import com.listywave.list.application.domain.ListEntity;
import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import com.listywave.list.application.domain.list.ListEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -19,15 +20,15 @@
@Getter
@AllArgsConstructor
@Table(name = "collection")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = PROTECTED)
public class Collect {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = IDENTITY)
private Long id;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "list_id")
@ManyToOne(fetch = FetchType.LAZY)
private ListEntity list;

@Column(name = "user_id", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.listywave.collection.repository;

import com.listywave.collection.application.domain.Collect;
import com.listywave.list.application.domain.ListEntity;
import com.listywave.list.application.domain.list.ListEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CollectionRepository extends JpaRepository<Collect, Long> {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/listywave/common/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.listywave.common;

import static jakarta.persistence.GenerationType.IDENTITY;
import static jakarta.persistence.TemporalType.TIMESTAMP;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.Temporal;
import java.time.LocalDateTime;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
Expand All @@ -18,13 +21,15 @@
public class BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = IDENTITY)
protected Long id;

@CreatedDate
@Column(updatable = false)
@Temporal(TIMESTAMP)
protected LocalDateTime createdDate;

@LastModifiedDate
@Temporal(TIMESTAMP)
protected LocalDateTime updatedDate;
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
package com.listywave.list.application.domain.comment;

import static jakarta.persistence.FetchType.LAZY;
import static lombok.AccessLevel.PROTECTED;

import com.listywave.common.BaseEntity;
import com.listywave.list.application.domain.list.Content;
import com.listywave.list.application.domain.list.ListEntity;
import com.listywave.user.application.domain.User;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = PROTECTED)
public class Comment extends BaseEntity {

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "list_id")
@ManyToOne(fetch = FetchType.LAZY)
private ListEntity list;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "user_id")
@ManyToOne(fetch = FetchType.LAZY)
private User user;

@Embedded
private Content content;
private CommentContent commentContent;

@Column(nullable = false, length = 5)
private Boolean isDeleted;

public static Comment create(ListEntity list, User user, String content) {
return new Comment(list, user, new Content(content), false);
public static Comment create(ListEntity list, User user, CommentContent content) {
return new Comment(list, user, content, false);
}

public boolean canDeleteBy(User user) {
Expand All @@ -47,8 +47,8 @@ public void softDelete() {
this.isDeleted = true;
}

public String getContent() {
return content.getValue();
public String getCommentContent() {
return commentContent.getValue();
}

public boolean isDeleted() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.listywave.list.application.domain.list;
package com.listywave.list.application.domain.comment;

import com.listywave.common.exception.CustomException;
import com.listywave.common.exception.ErrorCode;
Expand All @@ -13,14 +13,14 @@
@Embeddable
@EqualsAndHashCode
@NoArgsConstructor(access = AccessLevel.PROTECTED, force = true)
public class Content {
public class CommentContent {

private static final int LENGTH_LIMIT = 500;

@Column(name = "content", nullable = false, length = LENGTH_LIMIT)
private final String value;

public Content(String value) {
public CommentContent(String value) {
validate(value);
this.value = value;
}
Expand Down
57 changes: 10 additions & 47 deletions src/main/java/com/listywave/list/application/domain/item/Item.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.listywave.list.application.domain.item;

import static jakarta.persistence.FetchType.LAZY;
import static lombok.AccessLevel.PROTECTED;

import com.listywave.common.BaseEntity;
import com.listywave.list.application.domain.list.ListEntity;
import com.listywave.list.presentation.dto.request.ItemCreateRequest;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -19,10 +19,10 @@
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = PROTECTED)
public class Item extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "list_id")
private ListEntity list;

Expand All @@ -43,56 +43,19 @@ public class Item extends BaseEntity {

private String imageKey;

public static Item createItem(ItemCreateRequest items, ListEntity list) {
return Item.builder()
.list(list)
.ranking(items.rank())
.title(
ItemTitle.builder()
.value(items.title())
.build()
)
.comment(
ItemComment.builder()
.value(items.comment())
.build()
)
.link(
ItemLink.builder()
.value(items.link())
.build()
)
.imageUrl(
ItemImageUrl.builder()
.value(items.imageUrl())
.build()
)
.build();
public static Item init(int ranking, ItemTitle title, ItemComment comment, ItemLink link, ItemImageUrl imageUrl) {
return new Item(null, ranking, title, comment, link, imageUrl, null);
}

public void updateItemImageKey(String imageKey) {
this.imageKey = imageKey;
}

public void updateItemImageUrl(String imageUrl) {
this.imageUrl = ItemImageUrl.builder()
.value(imageUrl)
.build();
}

public String getTitle() {
return title.getValue();
}

public String getComment() {
return comment.getValue();
}

public String getLink() {
return link.getValue();
this.imageUrl = new ItemImageUrl(imageUrl);
}

public String getImageUrl() {
return imageUrl.getValue();
public void updateList(ListEntity list) {
this.list = list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ private void validate(String value) {
throw new CustomException(ErrorCode.LENGTH_EXCEEDED, "아이템의 이미지 URL은 " + LENGTH_LIMIT + "자를 넘을 수 없습니다.");
}
}

public boolean hasValue() {
return value != null && !value.isBlank();
}
}
Loading

0 comments on commit ca16882

Please sign in to comment.