Skip to content

Commit

Permalink
Merge pull request TAVE-balak#40 from jyjyjy25/feature/TAVE-balak#21-…
Browse files Browse the repository at this point in the history
…user-genre

Feature/TAVE-balak#21 user genre
  • Loading branch information
jyjyjy25 authored Jan 11, 2024
2 parents ec31f96 + 22b44d8 commit b23b190
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 19 deletions.
16 changes: 0 additions & 16 deletions src/main/java/tavebalak/OTTify/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ public enum ErrorCode {
GENRE_NOT_FOUND("장르를 찾을 수 없습니다."),
USER_NOT_FOUND("유저를 찾을 수 없습니다."),
OTT_NOT_FOUND("OTT를 찾을 수 없습니다."),


PROGRAM_GENRE_NOT_FOUND("프로그램과 관련된 장르를 찾을 수 없습니다."),
PROGRAM_NOT_FOUND("프로그램을 찾을 수 없습니다"),

USER_FIRST_GENRE_NOT_FOUND("USER는 첫번째 우선 순위 장르를 지정하지 않았습니다"),

REVIEW_TAG_NOT_FOUND("지정된 리뷰 태그가 존재하지 않습니다"),

REVIEW_NOT_FOUND("리뷰를 찾을 수 없습니다"),

/**
Expand All @@ -66,16 +61,5 @@ public enum ErrorCode {
*/
INTERNAL_SERVER_ERROR("서버 내부 오류입니다.");












private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tavebalak.OTTify.genre.dto.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;

@Getter
@NoArgsConstructor
public class GenreUpdateDTO {

@NotNull
private Long id;
}
4 changes: 4 additions & 0 deletions src/main/java/tavebalak/OTTify/genre/entity/UserGenre.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ public static UserGenre create(User user, Genre genre, boolean isFirst) {
.isFirst(isFirst)
.build();
}

public void changeGenre(Genre genre) {
this.genre = genre;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
public interface UserGenreRepository extends JpaRepository<UserGenre, Long> {
Optional<UserGenre> findByUserIdAndIsFirst(Long userId, boolean isFirst);
List<UserGenre> findByGenreId(Long genreId);

@Query("select ug from UserGenre ug where ug.user.id =:userId and ug.isFirst = true")
UserGenre find1stGenreByUserId(@Param("userId") Long userId);

Expand All @@ -26,6 +27,7 @@ public interface UserGenreRepository extends JpaRepository<UserGenre, Long> {
@Query("select ug from UserGenre ug join fetch ug.genre where ug.user.id =:userId and ug.isFirst = false")
List<UserGenre> find2ndGenreByUserIdFetchJoin(@Param("userId") Long userId);


Optional<UserGenre> findByUserAndIsFirst(User user, boolean isFirst);

Optional<UserGenre> findByGenreIdAndUserIdAndIsFirst(@Param("genreId") Long genreId, @Param("userId") Long userId, boolean isFirst);
}
16 changes: 16 additions & 0 deletions src/main/java/tavebalak/OTTify/user/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import tavebalak.OTTify.user.service.UserService;

import java.util.List;
import tavebalak.OTTify.genre.dto.request.GenreUpdateDTO;
import tavebalak.OTTify.user.service.UserServiceImpl;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -47,4 +49,18 @@ public BaseResponse<Long> updateUserProfile(@PathVariable("id") Long userId, @Va
public BaseResponse<Long> updateUserOTT(@PathVariable("id") Long userId, @RequestBody List<UserOttUpdateDTO> updateRequestDTO) {
return BaseResponse.success(userService.updateUserOTT(userId, updateRequestDTO));
}

@PatchMapping("/{id}/1stGenre")
@ResponseStatus(HttpStatus.OK)
public BaseResponse update1stLikedGenre(@PathVariable("id") Long userId, @Validated @RequestBody GenreUpdateDTO updateRequestDto) {
userService.update1stGenre(userId, updateRequestDto);
return BaseResponse.success("성공적으로 1순위 장르가 업데이트 되었습니다.");
}

@PatchMapping("/{id}/2ndGenre")
@ResponseStatus(HttpStatus.OK)
public BaseResponse update2ndLikedGenre(@PathVariable("id") Long userId, @Validated @RequestBody GenreUpdateDTO updateRequestDTO) {
userService.update2ndGenre(userId, updateRequestDTO);
return BaseResponse.success("성공적으로 2순위 장르가 업데이트 되었습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package tavebalak.OTTify.user.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import tavebalak.OTTify.common.constant.SocialType;
import tavebalak.OTTify.user.entity.User;

import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmailAndSocialType(String email, SocialType socialType);

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/tavebalak/OTTify/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tavebalak.OTTify.user.dto.Request.UserProfileUpdateDTO;
import tavebalak.OTTify.user.dto.Response.UserOttListDTO;
import tavebalak.OTTify.user.dto.Response.UserProfileDTO;
import tavebalak.OTTify.genre.dto.request.GenreUpdateDTO;

import java.util.List;

Expand All @@ -12,4 +13,6 @@ public interface UserService {
UserOttListDTO getUserOTT(Long userId);
Long updateUserProfile(Long userId, UserProfileUpdateDTO updateRequestDTO);
Long updateUserOTT(Long userId, List<UserOttUpdateDTO> updateRequestDTO);
void update1stGenre(Long userId, GenreUpdateDTO updateRequestDTO);
void update2ndGenre(Long userId, GenreUpdateDTO updateRequestDTO);
}
36 changes: 36 additions & 0 deletions src/main/java/tavebalak/OTTify/user/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@

import java.util.*;
import java.util.stream.Collectors;
import tavebalak.OTTify.error.exception.NotFoundException;
import tavebalak.OTTify.genre.dto.request.GenreUpdateDTO;
import tavebalak.OTTify.genre.entity.Genre;
import tavebalak.OTTify.genre.entity.UserGenre;
import tavebalak.OTTify.genre.repository.GenreRepository;
import tavebalak.OTTify.genre.repository.UserGenreRepository;
import tavebalak.OTTify.user.repository.UserRepository;

@Service
@Transactional(readOnly = true)
Expand All @@ -39,6 +46,7 @@ public class UserServiceImpl implements UserService {
private final ReviewRepository reviewRepository;
private final LikedProgramRepository likedProgramRepository;
private final UninterestedProgramRepository uninterestedProgramRepository;
private final GenreRepository genreRepository;

@Override
public UserProfileDTO getUserProfile(Long userId) {
Expand Down Expand Up @@ -141,6 +149,34 @@ public UserOttListDTO getUserOTT(Long userId) {

@Override
@Transactional
public void update1stGenre(Long userId, GenreUpdateDTO updateRequestDTO) {
UserGenre userGenre = userGenreRepository.findByUserIdAndIsFirst(userId, true)
.orElseThrow(() -> new NotFoundException(ErrorCode.ENTITY_NOT_FOUND));
Genre genre = genreRepository.findById(updateRequestDTO.getId())
.orElseThrow(() -> new NotFoundException(ErrorCode.GENRE_NOT_FOUND));

userGenre.changeGenre(genre);
}

@Override
@Transactional
public void update2ndGenre(Long userId, GenreUpdateDTO updateRequestDTO) {
// req로 들어온 id 값이 유효한 장르 id인지 확인
Genre genre = genreRepository.findById(updateRequestDTO.getId())
.orElseThrow(() -> new NotFoundException(ErrorCode.GENRE_NOT_FOUND));

// 조회된 UserGenre가 있을 경우 삭제 & 없을 경우 저장
userGenreRepository.findByGenreIdAndUserIdAndIsFirst(genre.getId(), userId, false).ifPresentOrElse(
ug -> userGenreRepository.delete(ug),
() -> userGenreRepository.save(
UserGenre.builder()
.genre(genre)
.user(userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.ENTITY_NOT_FOUND)))
.build())
);
}

public Long updateUserProfile(Long userId, UserProfileUpdateDTO updateRequestDTO) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.ENTITY_NOT_FOUND));
Expand Down

0 comments on commit b23b190

Please sign in to comment.