Skip to content

Commit

Permalink
Merge pull request #145 from UMC-WOWMARKET/feat/like
Browse files Browse the repository at this point in the history
[Feat] 수요조사 Like 기능 #134
  • Loading branch information
yoonsseo authored Dec 30, 2023
2 parents 9158e4a + 24425d4 commit d178c88
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import wowmarket.wow_server.detail.demandproject.dto.*;
import wowmarket.wow_server.detail.demandproject.service.DemandItemService;
import wowmarket.wow_server.detail.demandproject.service.DemandProjectService;
import wowmarket.wow_server.detail.project.notice.dto.NoticeRequestDto;
import wowmarket.wow_server.detail.demandproject.service.DemandDetailService;
import wowmarket.wow_server.domain.User;

import java.util.List;

Expand All @@ -22,8 +24,8 @@ public class DemandProjectController {

//참여폼: 상세 정보 조회
@GetMapping("/{demand_project_id}")
public DemandProjectInfoResponseDto getProjectInfo(@PathVariable Long demand_project_id) {
return demandProjectService.getDemandProjectInfo(demand_project_id);
public DemandProjectInfoResponseDto getProjectInfo(@PathVariable Long demand_project_id, @AuthenticationPrincipal User user) {
return demandProjectService.getDemandProjectInfo(demand_project_id, user);
}

//참여폼: 굿즈 소개(이미지 3개) 조회
Expand All @@ -44,4 +46,8 @@ public ResponseEntity createDemandForm(@PathVariable Long demand_project_id, @Re
return demandDetailService.createDemandForm(demand_project_id, requestDto);
}

@PostMapping("/{demand_project_id}/like")
public ResponseEntity<?> likeProject(@PathVariable Long demand_project_id, @AuthenticationPrincipal User user) {
return demandProjectService.likeDemandProject(demand_project_id, user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public class DemandProjectInfoResponseDto {
private int participant_number; //참여인원
private int achieved; //달성률 분자
private int goal; //달성률 분모
private boolean isLiked;

public DemandProjectInfoResponseDto(DemandProject project, int achieved, int goal) {
public DemandProjectInfoResponseDto(DemandProject project, int achieved, int goal, boolean isLiked) {
this.thumbnail = project.getThumbnail(); //대표이미지
this.category = project.getCategory().getName(); //카테고리
this.name = project.getProjectName(); //프로젝트 이름
Expand All @@ -32,5 +33,6 @@ public DemandProjectInfoResponseDto(DemandProject project, int achieved, int goa
this.participant_number = project.getParticipant_number(); //참여인원
this.achieved = achieved;
this.goal = goal;
this.isLiked = isLiked;
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package wowmarket.wow_server.detail.demandproject.service;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.server.ResponseStatusException;
import wowmarket.wow_server.detail.demandproject.dto.DemandProjectImgResponseDto;
import wowmarket.wow_server.detail.demandproject.dto.DemandProjectInfoResponseDto;
import wowmarket.wow_server.domain.DemandLikes;
import wowmarket.wow_server.domain.DemandProject;
import wowmarket.wow_server.domain.Likes;
import wowmarket.wow_server.domain.User;
import wowmarket.wow_server.global.jwt.SecurityUtil;
import wowmarket.wow_server.repository.DemandItemRepository;
import wowmarket.wow_server.repository.DemandProjectRepository;
import wowmarket.wow_server.repository.UserRepository;
import wowmarket.wow_server.repository.*;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class DemandProjectService {
private final UserRepository userRepository;
private final DemandProjectRepository demandProjectRepository;
private final DemandItemRepository demandItemRepository;

public DemandProjectService(DemandProjectRepository demandProjectRepository, UserRepository userRepository, DemandItemRepository demandItemRepository) {
this.userRepository = userRepository;
this.demandProjectRepository = demandProjectRepository;
this.demandItemRepository = demandItemRepository;
}
private final DemandLikesRepository demandLikesRepository;

//참여폼: 굿즈 소개(이미지 3개) 조회
public DemandProjectImgResponseDto getDemandProjectImg(Long demand_project_id){
Expand All @@ -33,9 +35,39 @@ public DemandProjectImgResponseDto getDemandProjectImg(Long demand_project_id){
}

//참여폼: 상세 정보 조회
public DemandProjectInfoResponseDto getDemandProjectInfo(Long demand_project_id){
@Transactional
public DemandProjectInfoResponseDto getDemandProjectInfo(Long demand_project_id, User user){
boolean isLiked = false;
DemandProject demandProject = demandProjectRepository.findByDemandProject_Id(demand_project_id);
demandProjectRepository.updateView(demand_project_id); //상세정보 조회할 때마다 조회수 +1
return new DemandProjectInfoResponseDto(demandProject, demandItemRepository.getTotalOrderCountByDemandProject(demandProject), demandItemRepository.getTotalGoalByDemandProject(demandProject));
if (user != null && demandLikesRepository.findByUserAndDemandProject(user, demand_project_id).isPresent()) {
isLiked = true;
}
return new DemandProjectInfoResponseDto(demandProject, demandItemRepository.getTotalOrderCountByDemandProject(demandProject),
demandItemRepository.getTotalGoalByDemandProject(demandProject), isLiked);
}

@Transactional
public ResponseEntity<?> likeDemandProject(Long demandProjectId, User user) {
Optional<DemandLikes> demandLikeExist = demandLikesRepository.findByUserAndDemandProject(user, demandProjectId);
if (demandLikeExist.isEmpty()) {
demandLikesRepository.save(
DemandLikes.builder()
.user(user).demandProject(demandProjectRepository.findById(demandProjectId).get())
.build()
);
demandProjectRepository.updateDemandProjectLike(demandProjectId);
userRepository.updateDemandProjectLike(user);
} else {
if (demandProjectRepository.findById(demandProjectId).get().getLikeCnt() > 0
&& user.getDemandLike() > 0) {
demandLikesRepository.deleteDemandLikes(demandLikeExist.get().getId());
demandProjectRepository.updateDemandProjectUnLike(demandProjectId);
userRepository.updateDemandProjectUnLike(user);
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "like 수가 0이라 unlike할 수 없음");
}
}
return ResponseEntity.ok().build();
}
}
29 changes: 29 additions & 0 deletions src/main/java/wowmarket/wow_server/domain/DemandLikes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package wowmarket.wow_server.domain;

import jakarta.persistence.*;
import lombok.*;

@Entity
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class DemandLikes extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "demand_likes_id")
private Long id;

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "demand_project_id")
private DemandProject demandProject;

@Builder
public DemandLikes(User user, DemandProject demandProject) {
this.user = user;
this.demandProject = demandProject;
}
}
4 changes: 3 additions & 1 deletion src/main/java/wowmarket/wow_server/domain/DemandProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ public class DemandProject extends BaseEntity{
private int participant_number;
private Long final_achievement_rate;


@Setter
@Column(columnDefinition="tinyint(0) default 0")
private boolean isEnd;

@Column(columnDefinition = "integer default 0", nullable = false)
private int view; //조회수

@Column(columnDefinition = "integer default 0", nullable = false)
private int likeCnt;

public void setUser(User user){ this.user = user;}
public void setCategory(Category category){
this.category = category;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/wowmarket/wow_server/domain/Likes.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class Likes extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "like_id")
@Column(name = "likes_id")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package wowmarket.wow_server.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import wowmarket.wow_server.domain.DemandLikes;
import wowmarket.wow_server.domain.User;

import java.util.Optional;

@Repository
public interface DemandLikesRepository extends JpaRepository<DemandLikes, Long> {
@Query("SELECT dl FROM DemandLikes dl " +
"WHERE dl.user = :user AND dl.demandProject.id = :demandProjectId")
Optional<DemandLikes> findByUserAndDemandProject(@Param("user") User user, @Param("demandProjectId") Long demandProjectId);

@Modifying
@Query("DELETE FROM DemandLikes dl WHERE dl.id = :demandLikesId")
void deleteDemandLikes(@Param("demandLikesId") Long demandLikesId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ Page<DemandProject> findBySearchUserUniv(@Param("current_date") LocalDate curren
@Query(nativeQuery = true, value = "update demand_project p set p.participant_number=p.participant_number+1 where demand_project_id=?")
int updateParticipantNumber(Long demandProjectId);

@Modifying
@Query("UPDATE DemandProject dp SET dp.likeCnt = dp.likeCnt + 1 " +
"WHERE dp.id = :demandProjectId")
void updateDemandProjectLike(@Param("demandProjectId") Long demandProjectId);

@Modifying
@Query("UPDATE DemandProject dp SET dp.likeCnt = dp.likeCnt - 1 " +
"WHERE dp.id = :demandProjectId")
void updateDemandProjectUnLike(@Param("demandProjectId") Long demandProjectId);

}
12 changes: 12 additions & 0 deletions src/main/java/wowmarket/wow_server/repository/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,16 @@ public interface UserRepository extends JpaRepository<User, Long> {
@Query("UPDATE User u SET u.projectLike = u.projectLike - 1 " +
"WHERE u = :user")
void updateProjectUnLike(@Param("user") User user);

@Modifying
@Query("UPDATE User u SET u.demandLike = u.demandLike + 1 " +
"WHERE u = :user")
void updateDemandProjectLike(@Param("user") User user);

@Modifying
@Query("UPDATE User u SET u.demandLike = u.demandLike - 1 " +
"WHERE u = :user")
void updateDemandProjectUnLike(@Param("user") User user);


}

0 comments on commit d178c88

Please sign in to comment.