Skip to content

Commit

Permalink
Fix: 유저 선호 태그 수정 리스트 방식으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
bin-pro committed Aug 2, 2024
1 parent 87b466a commit 9769418
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.blackshoe.esthete.controller;

import com.blackshoe.esthete.dto.FilterCreateDto;
import com.blackshoe.esthete.dto.FilterDto;
import com.blackshoe.esthete.dto.LikeDto;
import com.blackshoe.esthete.dto.ResponseDto;
import com.blackshoe.esthete.dto.*;
import com.blackshoe.esthete.service.*;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -287,9 +284,20 @@ public ResponseEntity<ResponseDto> deleteTag(
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

@Operation(summary = "사용자 선호 필터 태그 수정")
@PutMapping("/tags")
public ResponseEntity<ResponseDto<List<FilterDto.TagResponse>>> editTagList(
@RequestHeader("Authorization") String accessToken,
@RequestBody UserDto.EditTagsDto editTagsRequest) {

UUID userId = jwtService.extractUserId(accessToken);
List<FilterDto.TagResponse> tagList = recommendService.editUserTags(userId, editTagsRequest);

return ResponseEntity.status(HttpStatus.OK).body(ResponseDto.success(tagList));
}
@Operation(summary = "사용자 선호 필터 태그 리스트 조회")
@GetMapping("/tags")
public ResponseEntity<ResponseDto<List<FilterDto.TagResponse>> > getTagList(
public ResponseEntity<ResponseDto<List<FilterDto.TagResponse>>> getTagList(
@RequestHeader("Authorization") String accessToken) {

UUID userId = jwtService.extractUserId(accessToken);
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/blackshoe/esthete/dto/UserDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.blackshoe.esthete.dto;

import com.blackshoe.esthete.entity.UserTag;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;


public class UserDto {
@Data
@NoArgsConstructor
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class EditTagsDto {
private List<String> tagList;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/blackshoe/esthete/exception/TagErrorResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.blackshoe.esthete.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@Getter
@RequiredArgsConstructor
public enum TagErrorResult {
NOT_FOUND_TAG(HttpStatus.NOT_FOUND, "존재하지 않는 태그입니다."),
DUPLICATE_TAG(HttpStatus.CONFLICT, "중복된 태그가 존재합니다."),
NOT_FOUND_FILTER_TAGS(HttpStatus.NOT_FOUND, "필터 태그 목록을 찾는데 실패했습니다.")
;
private final HttpStatus httpStatus;
private final String message;

}
15 changes: 15 additions & 0 deletions src/main/java/com/blackshoe/esthete/exception/TagException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.blackshoe.esthete.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class TagException extends RuntimeException {
private final TagErrorResult tagErrorResult;

@Override
public String getMessage() {
return tagErrorResult.getMessage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ public interface TagRepository extends JpaRepository<Tag, Long> {

@Query("SELECT t FROM Tag t WHERE t.tagId = :tagId")
Optional<Tag> findByTagId(UUID tagId);

@Query("SELECT t FROM Tag t WHERE t.name = :tagName")
Optional<Tag> findByName(String tagName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public interface UserTagRepository extends JpaRepository<UserTag, Long> {
@Modifying
@Query("DELETE FROM UserTag ut WHERE ut.user = :user")
void deleteByUser(User user);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.blackshoe.esthete.service;

import com.blackshoe.esthete.dto.FilterDto;
import com.blackshoe.esthete.dto.UserDto;

import java.util.List;
import java.util.UUID;
Expand All @@ -12,4 +13,6 @@ public interface RecommendService {
void deleteTag(UUID userId, UUID tagId);

List<FilterDto.TagResponse> getTagList(UUID userId);

List<FilterDto.TagResponse> editUserTags(UUID userId, UserDto.EditTagsDto editTagsDto);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.blackshoe.esthete.service;

import com.blackshoe.esthete.dto.FilterDto;
import com.blackshoe.esthete.dto.UserDto;
import com.blackshoe.esthete.entity.Tag;
import com.blackshoe.esthete.entity.User;
import com.blackshoe.esthete.entity.UserTag;
import com.blackshoe.esthete.exception.RecommendErrorResult;
import com.blackshoe.esthete.exception.RecommendException;
import com.blackshoe.esthete.exception.UserErrorResult;
import com.blackshoe.esthete.exception.UserException;
import com.blackshoe.esthete.exception.*;
import com.blackshoe.esthete.repository.TagRepository;
import com.blackshoe.esthete.repository.UserRepository;
import com.blackshoe.esthete.repository.UserTagRepository;
Expand All @@ -16,8 +14,11 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@Slf4j
Expand All @@ -30,7 +31,6 @@ public class RecommendServiceImpl implements RecommendService{
@Transactional
@Override
public void addTag(UUID userId, UUID tagId) {
// TODO: Implement this method
final User user = userRepository.findByUserId(userId).orElseThrow(() -> new UserException(UserErrorResult.NOT_FOUND_USER));
final Tag tag = tagRepository.findByTagId(tagId).orElseThrow(() -> new RecommendException(RecommendErrorResult.NOT_FOUND_TAG_ID));

Expand Down Expand Up @@ -58,6 +58,50 @@ public void deleteTag(UUID userId, UUID tagId) {
userTagRepository.deleteByUserAndTag(user, tag);
}

@Override
@Transactional
public List<FilterDto.TagResponse> editUserTags(UUID userId, UserDto.EditTagsDto editTagsDto) {
User user = userRepository.findByUserId(userId)
.orElseThrow(() -> new UserException(UserErrorResult.NOT_FOUND_USER));

// 중복 태그 확인
Set<String> tagNames = new HashSet<>();
for (String tagName : editTagsDto.getTagList()) {
if (!tagNames.add(tagName)) {
// 중복된 태그가 있는 경우
throw new TagException(TagErrorResult.DUPLICATE_TAG);
}
}

// 기존 태그들 제거
userTagRepository.deleteByUser(user);

// 새로운 태그들을 생성하여 UserTag 엔티티로 변환하여 저장
List<UserTag> newTagList = editTagsDto.getTagList().stream()
.map(tagName -> {
Tag tag = tagRepository.findByName(tagName)
.orElseThrow(() -> new TagException(TagErrorResult.NOT_FOUND_TAG));
return UserTag.builder()
.user(user)
.tag(tag)
.build();
})
.collect(Collectors.toList());

// 새로 생성한 UserTag 엔티티들을 저장
userTagRepository.saveAll(newTagList);

// 응답에 유저의 태그 리스트를 포함하여 반환
List<FilterDto.TagResponse> tagResponses = newTagList.stream().map(
userTag -> FilterDto.TagResponse.builder()
.tagId(String.valueOf(userTag.getTag().getTagId()))
.tagName(userTag.getTag().getName())
.build()
).toList();

return tagResponses;
}

@Override
public List<FilterDto.TagResponse> getTagList(UUID userId) {
final User user = userRepository.findByUserId(userId).orElseThrow(() -> new UserException(UserErrorResult.NOT_FOUND_USER));
Expand Down

0 comments on commit 9769418

Please sign in to comment.