Skip to content

Commit

Permalink
GETP-168 refactor: 프로젝트 좋아요 / 좋아요 취소 서비스를 추상화에 대한 구현체로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
scv1702 committed Aug 6, 2024
1 parent c563a88 commit 59032f7
Show file tree
Hide file tree
Showing 30 changed files with 213 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import es.princip.getp.domain.client.command.domain.Client;
import es.princip.getp.domain.client.command.domain.ClientRepository;
import es.princip.getp.domain.client.exception.NotFoundClientException;
import es.princip.getp.domain.like.command.domain.Liker;
import es.princip.getp.domain.like.command.domain.Unliker;
import es.princip.getp.domain.like.command.domain.people.PeopleLiker;
import es.princip.getp.domain.like.command.domain.people.PeopleUnliker;
import es.princip.getp.domain.people.command.domain.People;
import es.princip.getp.domain.people.command.domain.PeopleRepository;
import es.princip.getp.domain.people.exception.NotFoundPeopleException;
Expand All @@ -13,12 +13,12 @@
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class PeopleLikeService {

private final Liker peopleLiker;
private final Unliker peopleUnliker;
private final PeopleLiker peopleLiker;
private final PeopleUnliker peopleUnliker;

private final ClientRepository clientRepository;
private final PeopleRepository peopleRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package es.princip.getp.domain.project.command.application;
package es.princip.getp.domain.like.command.application;

import es.princip.getp.domain.like.command.domain.project.ProjectLiker;
import es.princip.getp.domain.like.command.domain.project.ProjectUnliker;
import es.princip.getp.domain.people.command.domain.People;
import es.princip.getp.domain.people.command.domain.PeopleRepository;
import es.princip.getp.domain.people.exception.NotFoundPeopleException;
import es.princip.getp.domain.project.command.domain.*;
import es.princip.getp.domain.project.command.domain.Project;
import es.princip.getp.domain.project.command.domain.ProjectRepository;
import es.princip.getp.domain.project.exception.NotFoundProjectException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -16,7 +19,6 @@ public class ProjectLikeService {

private final ProjectRepository projectRepository;
private final PeopleRepository peopleRepository;
private final ProjectLikeRepository projectLikeRepository;

private final ProjectLiker projectLiker;
private final ProjectUnliker projectUnliker;
Expand All @@ -34,9 +36,7 @@ public void like(final Long memberId, final Long projectId) {
final Project project = projectRepository.findById(projectId)
.orElseThrow(NotFoundProjectException::new);

final ProjectLike like = projectLiker.like(people, project);

projectLikeRepository.save(like);
projectLiker.like(people, project);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import es.princip.getp.domain.like.command.domain.Likeable;
import es.princip.getp.domain.like.command.domain.Liker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Component;

@Service
@Component
public class PeopleLiker extends Liker<PeopleLike> {

private final PeopleLikeRepository peopleLikeRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import es.princip.getp.domain.like.command.domain.Unliker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Component;

@Service
@Component
public class PeopleUnliker extends Unliker {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package es.princip.getp.domain.like.command.domain.project;

import es.princip.getp.domain.like.command.domain.Like;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@DiscriminatorColumn
@Table(name = "project_like")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@AttributeOverrides(
{
@AttributeOverride(name = "id", column = @Column(name = "project_like_id")),
@AttributeOverride(name = "likerId", column = @Column(name = "people_id")),
@AttributeOverride(name = "likedId", column = @Column(name = "project_id"))
}
)
public class ProjectLike extends Like {

private ProjectLike(final Long peopleId, final Long projectId) {
super(peopleId, projectId);
}

public static ProjectLike of(final Long peopleId, final Long projectId) {
return new ProjectLike(peopleId, projectId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package es.princip.getp.domain.like.command.domain.project;

import es.princip.getp.domain.like.command.domain.LikeRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProjectLikeRepository extends JpaRepository<ProjectLike, Long>, LikeRepository {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package es.princip.getp.domain.like.command.domain.project;

import es.princip.getp.domain.like.command.domain.LikeReceivable;
import es.princip.getp.domain.like.command.domain.Likeable;
import es.princip.getp.domain.like.command.domain.Liker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ProjectLiker extends Liker<ProjectLike> {

private final ProjectLikeRepository projectLikeRepository;

@Autowired
public ProjectLiker(ProjectLikeRepository projectLikeRepository) {
super(projectLikeRepository);
this.projectLikeRepository = projectLikeRepository;
}

@Override
public ProjectLike like(final Likeable likeable, final LikeReceivable likeReceivable) {
checkAlreadyLiked(likeable.getId(), likeReceivable.getId());
final ProjectLike like = ProjectLike.of(likeable.getId(), likeReceivable.getId());
projectLikeRepository.save(like);
return like;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package es.princip.getp.domain.like.command.domain.project;

import es.princip.getp.domain.like.command.domain.Unliker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ProjectUnliker extends Unliker {

@Autowired
public ProjectUnliker(ProjectLikeRepository projectLikeRepository) {
super(projectLikeRepository);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package es.princip.getp.domain.project.command.presentation;
package es.princip.getp.domain.like.command.presentation;

import es.princip.getp.domain.project.command.application.ProjectLikeService;
import es.princip.getp.domain.like.command.application.ProjectLikeService;
import es.princip.getp.infra.dto.response.ApiResponse;
import es.princip.getp.infra.dto.response.ApiResponse.ApiSuccessResult;
import es.princip.getp.infra.security.details.PrincipalDetails;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package es.princip.getp.domain.like.query.dao;

public interface ProjectLikeDao extends LikeDao {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package es.princip.getp.domain.like.query.dao;

import es.princip.getp.infra.support.QueryDslSupport;
import org.springframework.stereotype.Repository;

import java.util.Map;
import java.util.Optional;

import static es.princip.getp.domain.like.command.domain.project.QProjectLike.projectLike;
import static java.util.stream.Collectors.toMap;

@Repository
public class ProjectLikeQueryDslDao extends QueryDslSupport implements ProjectLikeDao {

@Override
// TODO: 좋아요 수 조회 성능 개선 필요
public Long countByLikedId(final Long projectId) {
return queryFactory.select(projectLike.count())
.from(projectLike)
.where(projectLike.likedId.eq(projectId))
.fetchOne();
}

@Override
public Map<Long, Long> countByLikedIds(final Long... likedIds) {
return queryFactory.select(projectLike.likedId, projectLike.count())
.from(projectLike)
.where(projectLike.likedId.in(likedIds))
.groupBy(projectLike.likedId)
.fetch()
.stream()
.collect(
toMap(
tuple -> tuple.get(projectLike.likedId),
tuple -> Optional.ofNullable(tuple.get(projectLike.count()))
.orElse(0L)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.Expressions;
import es.princip.getp.domain.like.query.dao.LikeDao;
import es.princip.getp.domain.like.query.dao.PeopleLikeDao;
import es.princip.getp.domain.people.command.domain.People;
import es.princip.getp.domain.people.command.domain.PeopleProfile;
import es.princip.getp.domain.people.command.domain.QPeople;
Expand Down Expand Up @@ -37,7 +37,7 @@
// TODO: 조회 성능 개선 필요
public class PeopleQueryDslDao extends QueryDslSupport implements PeopleDao {

private final LikeDao peopleLikeDao;
private final PeopleLikeDao peopleLikeDao;

private Map<Long, Tuple> findMemberAndPeopleByPeopleId(final Long... peopleId) {
return queryFactory.select(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import es.princip.getp.domain.common.domain.BaseTimeEntity;
import es.princip.getp.domain.common.domain.Duration;
import es.princip.getp.domain.common.domain.Hashtag;
import es.princip.getp.domain.like.command.domain.LikeReceivable;
import jakarta.persistence.*;
import lombok.*;

Expand All @@ -16,7 +17,7 @@
@Table(name = "project")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class Project extends BaseTimeEntity {
public class Project extends BaseTimeEntity implements LikeReceivable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down Expand Up @@ -127,4 +128,9 @@ public List<Hashtag> getHashtags() {
public boolean isApplicationClosed(final Clock clock) {
return applicationDuration.isEnded(clock) || status != ProjectStatus.APPLYING;
}

@Override
public Long getId() {
return projectId;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 59032f7

Please sign in to comment.