Skip to content

Commit

Permalink
Merge pull request #151 from UMC-WOWMARKET/feat/like
Browse files Browse the repository at this point in the history
[Feat] 판매 및 수요조사 Like 목록 보기 구현 #134
  • Loading branch information
yoonsseo authored Jan 6, 2024
2 parents 672b869 + 526e3e7 commit e560d98
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package wowmarket.wow_server.mypage.myinfo.controller;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import wowmarket.wow_server.domain.User;
import wowmarket.wow_server.mypage.myinfo.dto.GetLikeProjectDto;
import wowmarket.wow_server.mypage.myinfo.dto.MyInfoResponseDto;
import wowmarket.wow_server.mypage.myinfo.service.MyInfoService;

Expand All @@ -18,8 +21,25 @@ public class MyInfoController {
private final MyInfoService myInfoService;

@GetMapping()
public MyInfoResponseDto getMyInfo(@AuthenticationPrincipal User user){
public MyInfoResponseDto getMyInfo(@AuthenticationPrincipal User user) {
return myInfoService.findMyInfo(user);
}

@GetMapping("/like/project")
public GetLikeProjectDto getLikeProjects(
@RequestParam(name = "pageNo", defaultValue = "1", required = true) int pageNo,
@AuthenticationPrincipal User user) {
Sort sort = Sort.by(Sort.Direction.DESC, "created_time");
Pageable pageable = PageRequest.of(pageNo - 1, 9, sort);
return myInfoService.getLikeProjects(user, pageable);
}

@GetMapping("/like/demandProject")
public GetLikeProjectDto getLikeDemandProjects(
@RequestParam(name = "pageNo", defaultValue = "1", required = true) int pageNo,
@AuthenticationPrincipal User user) {
Sort sort = Sort.by(Sort.Direction.DESC, "created_time");
Pageable pageable = PageRequest.of(pageNo - 1, 9, sort);
return myInfoService.getLikeDemandProjects(user, pageable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package wowmarket.wow_server.mypage.myinfo.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
public class GetLikeProjectDto {
private int totalPage;
private int currentPage;
private List<LikedProjectDto> projectList;

@Builder
public GetLikeProjectDto(int totalPage, int currentPage, List<LikedProjectDto> projectList) {
this.totalPage = totalPage;
this.currentPage = currentPage;
this.projectList = projectList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package wowmarket.wow_server.mypage.myinfo.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import wowmarket.wow_server.domain.DemandProject;
import wowmarket.wow_server.domain.Project;

@Getter
@NoArgsConstructor
public class LikedProjectDto {
private Long projectId;
private String sellerUniv;
private String projectName;
private String thumbnail;
private boolean isEnd;
private boolean isDel;

@Builder
public LikedProjectDto(Project project) {
this.projectId = project.getId();
this.sellerUniv = project.getUser().getUniv();
this.projectName = project.getProjectName();
this.thumbnail = project.getThumbnail();
this.isEnd = project.isEnd();
this.isDel = project.isDel();
}

@Builder
public LikedProjectDto(DemandProject demandProject) {
this.projectId = demandProject.getId();
this.sellerUniv = demandProject.getUser().getUniv();
this.projectName = demandProject.getProjectName();
this.thumbnail = demandProject.getThumbnail();
this.isEnd = demandProject.isEnd();
this.isDel = false;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package wowmarket.wow_server.mypage.myinfo.service;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
import wowmarket.wow_server.domain.DemandProject;
import wowmarket.wow_server.domain.Project;
import wowmarket.wow_server.domain.User;
import wowmarket.wow_server.global.jwt.SecurityUtil;
import wowmarket.wow_server.mypage.myinfo.dto.GetLikeProjectDto;
import wowmarket.wow_server.mypage.myinfo.dto.LikedProjectDto;
import wowmarket.wow_server.mypage.myinfo.dto.MyInfoResponseDto;
import wowmarket.wow_server.repository.UserRepository;
import wowmarket.wow_server.repository.DemandLikesRepository;
import wowmarket.wow_server.repository.LikesRepository;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class MyInfoService {
private final UserRepository userRepository;
private final LikesRepository likesRepository;
private final DemandLikesRepository demandLikesRepository;

@Transactional(readOnly = true)
public MyInfoResponseDto findMyInfo(User user){
if (user == null)
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "사용자 정보 없음");
Expand All @@ -27,4 +33,16 @@ public MyInfoResponseDto findMyInfo(User user){
.build();
return responseDto;
}

public GetLikeProjectDto getLikeProjects(User user, Pageable pageable) {
Page<Project> findProjects = likesRepository.findLikedProjects(user, pageable);
Page<LikedProjectDto> projectList = findProjects.map(LikedProjectDto::new);
return new GetLikeProjectDto(projectList.getTotalPages(), projectList.getNumber(), projectList.getContent());
}

public GetLikeProjectDto getLikeDemandProjects(User user, Pageable pageable) {
Page<DemandProject> findDemandProjects = demandLikesRepository.findLikedDemandProjects(user, pageable);
Page<LikedProjectDto> projectList = findDemandProjects.map(LikedProjectDto::new);
return new GetLikeProjectDto(projectList.getTotalPages(), projectList.getNumber(), projectList.getContent());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package wowmarket.wow_server.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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.DemandProject;
import wowmarket.wow_server.domain.User;

import java.util.Optional;
Expand All @@ -19,4 +22,7 @@ public interface DemandLikesRepository extends JpaRepository<DemandLikes, Long>
@Modifying
@Query("DELETE FROM DemandLikes dl WHERE dl.id = :demandLikesId")
void deleteDemandLikes(@Param("demandLikesId") Long demandLikesId);

@Query("SELECT dl.demandProject FROM DemandLikes dl WHERE dl.user = :user")
Page<DemandProject> findLikedDemandProjects(@Param("user") User user, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package wowmarket.wow_server.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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.Likes;
import wowmarket.wow_server.domain.Project;
import wowmarket.wow_server.domain.User;

import java.util.Optional;
Expand All @@ -19,4 +22,7 @@ public interface LikesRepository extends JpaRepository<Likes, Long> {
@Modifying
@Query("DELETE FROM Likes l WHERE l.id = :likesId")
void deleteLikes(@Param("likesId") Long likesId);

@Query("SELECT l.project FROM Likes l WHERE l.user = :user")
Page<Project> findLikedProjects(@Param("user") User user, Pageable pageable);
}

0 comments on commit e560d98

Please sign in to comment.