패스트캠퍼스 환급챌린지 17일차 : 9개 도메인 프로젝트로 끝내는 백엔드 웹 개발 (Java/Spring) 초격차 패키지 Online 강의 후기
이태우(1990년)2025. 3. 21. 23:44
본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성하였습니다.
공부 시작 시각 인증
공부 시작 시각 인증
수강 인증 사진
수강 인증 사진
Post.java
package org.fastcampus.post.domain;
import org.fastcampus.common.domain.*;
import org.fastcampus.post.domain.content.*;
import org.fastcampus.user.domain.User;
public class Post {
private final Long id;
private final User author;
private final Content content;
private final PositiveIntegerCounter likeCount;
private PostPublicationState state;
public static Post createPost(Long id, User author, String content, PostPublicationState state) {
return new Post(id, author, new PostContent(content), state);
}
public static Post createDefaultPost(Long id, User author, String content) {
return new Post(id, author, new PostContent(content), PostPublicationState.PUBLIC);
}
protected Post(Long id, User author, String content) {
this(id, author, new PostContent(content), PostPublicationState.PUBLIC);
}
public Post(Long id, User author, Content content, PostPublicationState state) {
if (author == null) {
throw new IllegalArgumentException();
}
if (content == null) {
throw new IllegalArgumentException();
}
this.id = id;
this.author = author;
this.content = content;
this.likeCount = new PositiveIntegerCounter();
this.state = state;
}
public void like(User user) {
if (this.author.equals(user)) {
throw new IllegalArgumentException();
}
likeCount.increase();
}
public void unlike() {
likeCount.decrease();
}
public void updatePost(User user, String updateContent, PostPublicationState state) {
if (!this.author.equals(user)) {
throw new IllegalArgumentException();
}
this.content.updateContent(updateContent);
this.state = state;
}
public Long getId() {
return id;
}
public User getAuthor() {
return author;
}
public String getContent() {
return content.getContentText();
}
public int getLikeCount() {
return likeCount.getCount();
}
}
Comment.java
package org.fastcampus.post.domain.comment;
import org.fastcampus.common.domain.*;
import org.fastcampus.post.domain.Post;
import org.fastcampus.post.domain.content.CommentContent;
import org.fastcampus.post.domain.content.Content;
import org.fastcampus.user.domain.User;
public class Comment {
private final Long id;
private final Post post;
private final User author;
private final Content content;
private final PositiveIntegerCounter likeCount;
public static Comment createComment(Post post, User author, String content) {
return new Comment(null, post, author, new CommentContent(content));
}
public Comment(Long id, Post post, User author, Content content) {
if (author == null) {
throw new IllegalArgumentException();
}
if (post == null) {
throw new IllegalArgumentException();
}
if (content == null) {
throw new IllegalArgumentException();
}
this.id = id;
this.post = post;
this.author = author;
this.content = content;
this.likeCount = new PositiveIntegerCounter();
}
public void like(User user) {
if (this.author.equals(user)) {
throw new IllegalArgumentException();
}
likeCount.increase();
}
public void unlike() {
likeCount.decrease();
}
public void updateComment(User user, String updateContent) {
if (!this.author.equals(user)) {
throw new IllegalArgumentException();
}
this.content.updateContent(updateContent);
}
public Long getId() {
return id;
}
public Post getPost() {
return post;
}
public User getAuthor() {
return author;
}
public String getContent() {
return content.getContentText();
}
public Content getContentObject() {
return content;
}
public int getLikeCount() {
return likeCount.getCount();
}
}
FakeObjectFactory.java
package org.fastcampus.fake;
import org.fastcampus.post.application.*;
import org.fastcampus.post.application.Interfaces.*;
import org.fastcampus.post.repository.*;
import org.fastcampus.user.application.*;
import org.fastcampus.user.application.Interfaces.*;
import org.fastcampus.user.repository.*;
public class FakeObjectFactory {
private static final UserRepository fakeUserRepository = new FakeUserRepository();
private static final UserRelationRepository fakeUserRelationRepository = new FakeUserRelationRepository();
private static final PostRepository fakePostRepository = new FakePostRepository();
private static final CommentRepository fakeCommentRepository = new FakeCommentRepository();
private static final LikeRepository fakeLikeRepository = new FakeLikeRepository();
private static final UserService userService = new UserService(fakeUserRepository);
private static final UserRelationService userRelationService = new UserRelationService(userService, fakeUserRelationRepository);
private static final PostService postService = new PostService(userService, fakePostRepository, fakeLikeRepository);
private static final CommentService commentService = new CommentService(userService, postService, fakeCommentRepository, fakeLikeRepository);
private FakeObjectFactory() {
}
public static UserService getUserService() {
return userService;
}
public static UserRelationService getUserRelationService() {
return userRelationService;
}
public static PostService getPostService() {
return postService;
}
public static CommentService getCommentService() {
return commentService;
}
}
FakePostRepository.java
package org.fastcampus.post.repository;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.fastcampus.post.application.Interfaces.PostRepository;
import org.fastcampus.post.domain.*;
public class FakePostRepository implements PostRepository {
private final Map<Long, Post> store = new HashMap<>();
@Override
public Post save(Post post) {
if (post.getId() != null) {
store.put(post.getId(), post);
return post;
}
Long id = store.size() + 1L;
Post newPost = Post.createDefaultPost(id, post.getAuthor(), post.getContent());
store.put(id, newPost);
return newPost;
}
@Override
public Optional<Post> findById(Long id) {
return Optional.ofNullable(store.get(id));
}
}
FakeCommentRepository.java
package org.fastcampus.post.repository;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.fastcampus.post.application.Interfaces.CommentRepository;
import org.fastcampus.post.domain.comment.*;
public class FakeCommentRepository implements CommentRepository {
private final Map<Long, Comment> store = new HashMap<>();
@Override
public Comment save(Comment comment) {
if (comment.getId() != null) {
store.put(comment.getId(), comment);
return comment;
}
Long id = store.size() + 1L;
Comment newComment = new Comment(id, comment.getPost(), comment.getAuthor(), comment.getContentObject());
store.put(id, newComment);
return newComment;
}
@Override
public Optional<Comment> findById(Long id) {
return Optional.ofNullable(store.get(id));
}
}
LikeRepository.java
package org.fastcampus.post.application.Interfaces;
import org.fastcampus.post.domain.Post;
import org.fastcampus.post.domain.comment.*;
import org.fastcampus.user.domain.User;
public interface LikeRepository {
boolean checkLike(Post post, User user);
void like(Post post, User user);
void unlike(Post post, User user);
boolean checkLike(Comment comment, User user);
void like(Comment comment, User user);
void unlike(Comment comment, User user);
}
FakeLikeRepository.java
package org.fastcampus.post.repository;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.fastcampus.post.application.Interfaces.LikeRepository;
import org.fastcampus.post.domain.Post;
import org.fastcampus.post.domain.comment.*;
import org.fastcampus.user.domain.User;
public class FakeLikeRepository implements LikeRepository {
private final Map<Post, Set<User>> postLikes = new HashMap<>();
private final Map<Comment, Set<User>> commentLikes = new HashMap<>();
@Override
public boolean checkLike(Post post, User user) {
if (postLikes.get(post) == null) {
return false;
}
return postLikes.get(post).contains(user);
}
@Override
public void like(Post post, User user) {
Set<User> users = postLikes.get(post);
if (users == null) {
users = Set.of(user);
} else {
users.add(user);
}
postLikes.put(post, users);
}
@Override
public void unlike(Post post, User user) {
Set<User> users = postLikes.get(post);
if (users == null) {
return;
}
users.remove(user);
postLikes.put(post, users);
}
@Override
public boolean checkLike(Comment comment, User user) {
if (commentLikes.get(comment) == null) {
return false;
}
return commentLikes.get(comment).contains(user);
}
@Override
public void like(Comment comment, User user) {
Set<User> users = commentLikes.get(comment);
if (users == null) {
users = Set.of(user);
} else {
users.add(user);
}
commentLikes.put(comment, users);
}
@Override
public void unlike(Comment comment, User user) {
Set<User> users = commentLikes.get(comment);
if (users == null) {
return;
}
users.remove(user);
commentLikes.put(comment, users);
}
}