Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/#129 query issue #132

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/main/java/com/gam/api/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import lombok.val;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
Expand Down Expand Up @@ -118,10 +117,8 @@ ResponseEntity<ApiResponse> getPopularDesigners(@AuthenticationPrincipal GamUser
@ApiOperation(value = "๋ฐœ๊ฒฌ - ์œ ์ €")
@GetMapping("")
ResponseEntity<ApiResponse> getDiscoveryUsers(
@AuthenticationPrincipal GamUserDetails userDetails,
@RequestBody UserDiscoveryRequestDTO request
) {
val response = userService.getDiscoveryUsers(userDetails.getId(), request);
@AuthenticationPrincipal GamUserDetails userDetails, @RequestParam(required = false, defaultValue = "") int[] tags) {
val response = userService.getDiscoveryUsers(userDetails.getId(), tags);
return ResponseEntity.ok(ApiResponse.success(ResponseMessage.SUCCESS_DISCOVERY_GET_USERS.getMessage(), response));
}

Expand Down

This file was deleted.

41 changes: 39 additions & 2 deletions src/main/java/com/gam/api/repository/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.gam.api.entity.User;
import com.gam.api.entity.UserStatus;
import com.gam.api.repository.queryDto.user.UserScrapUserQueryDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -24,8 +25,44 @@ public interface UserRepository extends JpaRepository<User, Long> {

@Query("SELECT u FROM User u JOIN FETCH u.works WHERE u.userStatus = :userStatus")
List<User> findAllByUserStatusWithWorks( @Param("userStatus") UserStatus userStatus);

List<User>findAllByIdNotAndUserStatusOrderBySelectedFirstAtDesc(Long userId, UserStatus userStatus);

@Query("select distinct new com.gam.api.repository.queryDto.user.UserScrapUserQueryDto(us, 0L, coalesce(userScrap.status, false) as scrapStatus, us.selectedFirstAt) " +
"from User us " +
"JOIN Work work " +
" on work.user.id = us.id " +
"left join UserScrap userScrap" +
" on userScrap.targetId = us.id and userScrap.user.id = :userId " +
"WHERE us.id not in (:userId) " +
" and us.userStatus='PERMITTED' and us.role = 'USER' " +
"and us.id not in ( " +
" select block.targetId " +
" from Block block " +
" where block.user.id = :userId and block.status = true) " +
"GROUP BY us.id, userScrap.status " +
"order by us.selectedFirstAt desc")
List<UserScrapUserQueryDto>findAllDiscoveryUser(@Param("userId") long userId);


@Query("select distinct new com.gam.api.repository.queryDto.user.UserScrapUserQueryDto(us, count(userTag.tag.id) as correctCount, coalesce(userScrap.status, false) as scrapStatus, us.selectedFirstAt) " +
"from User us " +
"JOIN Work work " +
" on work.user.id = us.id " +
"JOIN UserTag userTag " +
" ON userTag.user.id = us.id " +
"left join UserScrap userScrap" +
" on userScrap.targetId = us.id and userScrap.user.id = :userId " +
"WHERE userTag.tag.id IN :tag " +
" and us.id not in (:userId) " +
" and us.userStatus='PERMITTED' and us.role = 'USER' " +
"and us.id not in ( " +
" select block.targetId " +
" from Block block " +
" where block.user.id = :userId and block.status = true) " +
"GROUP BY us.id, userScrap.status " +
"order by correctCount desc, us.selectedFirstAt desc")
List<UserScrapUserQueryDto>findAllDiscoveryUserWithTag(@Param("userId") long userId, @Param("tag") int[] tag);

List<User> findAll();

@Query(value = "SELECT u FROM User u WHERE LOWER(u.userName) LIKE LOWER(CONCAT('%', :keyword, '%')) and u.userStatus!='REPORTED' and u.id!=:userId ORDER BY u.createdAt DESC")
List<User> findByKeyWord(@Param("userId")Long userId, @Param("keyword") String keyword);
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/gam/api/repository/UserTagRepository.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.gam.api.repository;

import com.gam.api.entity.UserScrap;
import com.gam.api.entity.UserTag;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface UserTagRepository extends JpaRepository<UserTag, Long> {
void deleteAllByUser_id(Long userId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gam.api.repository.queryDto.user;

import com.gam.api.entity.User;
import java.time.LocalDateTime;

public record UserScrapUserQueryDto(User user, Long correctCount, boolean scrapStatus, LocalDateTime selectedFirstAt) {
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package com.gam.api.service.block;

import com.gam.api.common.exception.BlockException;
import com.gam.api.common.message.ExceptionMessage;
import com.gam.api.common.message.ResponseMessage;
import com.gam.api.dto.block.request.BlockRequestDTO;
import com.gam.api.dto.block.response.BlockResponseDTO;
import com.gam.api.dto.user.response.UserScrapResponseDTO;

import com.gam.api.entity.Block;
import com.gam.api.entity.User;
import com.gam.api.repository.BlockRepository;
import com.gam.api.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.persistence.EntityNotFoundException;
import javax.transaction.Transactional;
import java.util.Objects;
import java.util.Optional;

@RequiredArgsConstructor
@Service
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/gam/api/service/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface UserService {
List<UserResponseDTO> getPopularDesigners(Long userId);
WorkPortfolioListResponseDTO getMyPortfolio(Long userId);
WorkPortfolioGetResponseDTO getPortfolio(Long requestUserId, Long userId);
List<UserDiscoveryResponseDTO> getDiscoveryUsers(Long userId, UserDiscoveryRequestDTO request);
List<UserDiscoveryResponseDTO> getDiscoveryUsers(Long userId, int[] tags);
void updateInstagramLink(Long userId, UserUpdateLinkRequestDTO request);
void updateBehanceLink(Long userId, UserUpdateLinkRequestDTO request);
void updateNotionLink(Long userId, UserUpdateLinkRequestDTO request);
Expand Down
44 changes: 28 additions & 16 deletions src/main/java/com/gam/api/service/user/UserServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.gam.api.service.user;

import com.gam.api.common.exception.ScrapException;
import com.gam.api.common.exception.WorkException;
import com.gam.api.common.message.ExceptionMessage;
import com.gam.api.dto.search.response.SearchUserWorkDTO;
import com.gam.api.dto.user.request.*;
Expand All @@ -10,6 +9,7 @@
import com.gam.api.dto.work.response.WorkPortfolioListResponseDTO;
import com.gam.api.entity.*;
import com.gam.api.repository.*;
import com.gam.api.repository.queryDto.user.UserScrapUserQueryDto;
import com.gam.api.repository.queryDto.userScrap.UserScrapQueryDto;
import java.util.function.Function;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -261,29 +261,41 @@ public WorkPortfolioGetResponseDTO getPortfolio(Long requestUserId, Long userId)
}

@Override
public List<UserDiscoveryResponseDTO> getDiscoveryUsers(Long userId, UserDiscoveryRequestDTO request) { //TODO - ์ฟผ๋ฆฌ ์ง€์—ฐ
val users = userRepository.findAllByIdNotAndUserStatusOrderBySelectedFirstAtDesc(userId, UserStatus.PERMITTED);
public List<UserDiscoveryResponseDTO> getDiscoveryUsers(Long userId, int[] tags){
List<UserScrapUserQueryDto> users;

val me = findUser(userId);
removeBlockUsers(users, me);
if (tags.length == 0) {
users = userRepository.findAllDiscoveryUser(userId); //TODO - user ๊ด€๋ จ ์ฟผ๋ฆฌ ์žก๊ธฐ , ๋™์  ์ฟผ๋ฆฌ ํ•„์š”,,
}
else {
users = userRepository.findAllDiscoveryUserWithTag(userId, tags);
}

return users.stream().map((user) -> {
val targetUserId = user.getId();
val firstWorkId = user.getFirstWorkId();
return users.stream().map((dto) -> {
val firstWorkId = dto.user().getFirstWorkId();
Work firstWork;

if (firstWorkId == null && !user.getActiveWorks().isEmpty()) { // firstWork๊ฐ€ ์„ค์ •์ด ์ œ๋Œ€๋กœ ์•ˆ๋œ ๊ฒฝ์šฐ
firstWork = workRepository.findFirstByUserIdAndIsActiveOrderByCreatedAtDesc(userId, true)
.orElseThrow(() -> new EntityNotFoundException(ExceptionMessage.NOT_FOUND_WORK.getMessage()));
} else {
firstWork = findWork(firstWorkId);
if (firstWorkId == null || !dto.user().getActiveWorks().isEmpty()) { // User ๊ถŒํ•œ ์—๋Ÿฌ
firstWork = workRepository.findFirstByUserIdAndIsActiveOrderByCreatedAtDesc(dto.user().getId(), true)
.orElse(Work.builder()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์—๋Ÿฌ์˜ ์ƒํ™ฉ์ด ์•„๋‹Œ๊ฐ€์š”?! firstWorkId ๋ฐ์ดํ„ฐ ์ •ํ•ฉ์„ฑ์ด ๋งž์ง€ ์•Š๋Š”๋ฐ ๋„˜๊ธฐ๋Š” ๊ฒƒ์€ ์ข‹์ง€ ์•Š์•„๋ณด์ž…๋‹ˆ๋‹ค!
์—๋Ÿฌ๋ฅผ ํ„ฐ๋œจ๋ ค์„œ ์ •ํ•ฉ์„ฑ์„ ๋งž์ถ”๋Š”๊ฒŒ ๋งž์„๊ฑฐ ๊ฐ™์•„์š”.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ธฐ์กด ์ฝ”๋“œ๋Š” ์—๋Ÿฌ๋ฅผ ํ„ฐ๋œจ๋ฆฌ๋Š” ๊ฒƒ ์ด์—ˆ๋Š”๋ฐ,
์ด๋ ‡๊ฒŒ ๋˜๋‹ˆ ํ•œ๋ช…์˜ ์œ ์ €๊ฐ€ ์—๋Ÿฌ์ผ ๋ชจ๋“  ์œ ์ €๊ฐ€ '๋ฐœ๊ฒฌ-์œ ์ €'์„œ๋น„์Šค๋ฅผ ์ง„์ž…ํ•˜์ง€ ๋ชปํ•  ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ์—๋Ÿฌ๋ฅผ ํ„ฐ๋œจ๋ฆฌ์ง€ ์•Š๊ณ , ์ผ๋‹จ ์œ ์ € ๊ถŒํ•œ์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ์œผ๋กœ ์ˆ˜์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค!

try catch๋กœ log๋ฅผ ๋„์šฐ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ฒ ๋„ค์šฉ ํ˜น์‹œ ๋ง์”€ํ•˜์‹  ๋ถ€๋ถ„์ด ํ›„์ž๋ผ๋ฉด ์ˆ˜์ •ํ•˜์—ฌ ํ‘ธ์‹œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

.user(dto.user())
.photoUrl("ํ•ด๋‹นํ•˜๋Š” ์ž‘์—…๋ฌผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
.detail("ํ•ด๋‹นํ•˜๋Š” ์ž‘์—…๋ฌผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
.title("ํ•ด๋‹นํ•˜๋Š” ์ž‘์—…๋ฌผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
.build());
dto.user().setUserStatus(UserStatus.NOT_PERMITTED);
}
else {
firstWork = dto.user().getActiveWorks().stream()
.filter(work -> dto.user().getFirstWorkId().equals(work.getId()))
.findFirst().get();
}

val userScrap = userScrapRepository.findByUser_idAndTargetId(userId, targetUserId);
val userScrap = dto.scrapStatus();
if (Objects.isNull(userScrap)) {
return UserDiscoveryResponseDTO.of(user, false, firstWork);
return UserDiscoveryResponseDTO.of(dto.user(), false, firstWork);
}
return UserDiscoveryResponseDTO.of(user, userScrap.isStatus(), firstWork);
return UserDiscoveryResponseDTO.of(dto.user(), userScrap, firstWork);
}).collect(Collectors.toList());
}

Expand Down