Skip to content

Commit

Permalink
Merge pull request #54 from Mu-necting/refactor/#30
Browse files Browse the repository at this point in the history
Comment, UploadedMusic에 인증 및 user 정보 리팩토링 완료
  • Loading branch information
mingmingmon authored Nov 3, 2024
2 parents 6190baf + 769036d commit 9e1d43f
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 43 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ dependencies {
//aws
implementation 'io.awspring.cloud:spring-cloud-starter-aws:2.4.4'

//json
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'

// Etc
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.munecting.api.domain.comment.dto.response.CommentIdResponseDto;
import com.munecting.api.domain.comment.dto.response.CommentResponseDto;
import com.munecting.api.domain.comment.service.CommentService;
import com.munecting.api.global.auth.user.UserId;
import com.munecting.api.global.common.dto.response.ApiResponse;
import com.munecting.api.global.common.dto.response.PagedResponseDto;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -29,43 +30,46 @@
public class CommentController {

private final CommentService commentService;

@PostMapping("/comments")
@Operation(summary = "댓글 등록하기")
public ApiResponse<?> createComment(
@UserId Long userId,
@RequestBody CommentRequestDto commentRequestDto
) {
CommentIdResponseDto commentIdResponseDto = commentService.createComment(commentRequestDto);
CommentIdResponseDto commentIdResponseDto = commentService.createComment(userId, commentRequestDto);
return ApiResponse.created(commentIdResponseDto);
}

@DeleteMapping("/{commentId}")
@Operation(summary = "댓글 삭제하기")
public ApiResponse<?> deleteComment(
@UserId Long userId,
@PathVariable Long commentId
) {
CommentIdResponseDto commentIdResponseDto = commentService.deleteCommentById(commentId);
CommentIdResponseDto commentIdResponseDto = commentService.deleteCommentById(userId, commentId);
return ApiResponse.ok(commentIdResponseDto);
}

@PatchMapping("/comments/{commentId}")
@Operation(summary = "댓글 수정하기")
public ApiResponse<?> updateComment(
@UserId Long userId,
@PathVariable (name = "commentId") Long commentId,
@RequestBody CommentRequestDto commentRequestDto
) {
CommentIdResponseDto commentIdResponseDto = commentService.updateComment(commentId, commentRequestDto);
CommentIdResponseDto commentIdResponseDto = commentService.updateComment(userId, commentId, commentRequestDto);
return ApiResponse.ok(commentIdResponseDto);
}

@GetMapping("/{trackId}/comments")
@Operation(summary = "댓글 조회하기")
public ApiResponse<?> getCommentsByTrackId(
@UserId Long userId,
@PathVariable (name = "trackId") String trackId,
@RequestParam (name = "cursor") LocalDateTime cursor,
@RequestParam (name = "limit") int limit
) {
PagedResponseDto<CommentResponseDto> commentResponseDtoList = commentService.getCommentsByTrackId(trackId, cursor, limit);
PagedResponseDto<CommentResponseDto> commentResponseDtoList = commentService.getCommentsByTrackId(userId, trackId, cursor, limit);
return ApiResponse.ok(commentResponseDtoList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

public record CommentRequestDto(

@NotNull(message = "사용자 id는 필수값입니다.")
Long userId,

@NotNull(message = "trackId는 필수값입니다.")
String trackId,

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
package com.munecting.api.domain.comment.dto.response;

import com.munecting.api.domain.comment.entity.Comment;
import com.munecting.api.domain.user.dto.response.UserResponseDto;
import com.munecting.api.domain.user.entity.User;
import java.time.LocalDateTime;
import lombok.Builder;

@Builder
public record CommentResponseDto (
Long userId,
UserResponseDto userDto,
String trackId,
String content,
LocalDateTime createdAt,
LocalDateTime updatedAt
// 내가 쓴 댓글인지 판별하는 필드 필요 -> 인증 부분 완성되면 추가 예정
LocalDateTime updatedAt,
Boolean isOwner
) {

public static CommentResponseDto of(Comment comment) {
public static CommentResponseDto of(User user, Comment comment, Boolean isOwner) {
UserResponseDto userDto = UserResponseDto.of(
user.getId(), user.getNickname(), user.getProfileImageUrl());

return CommentResponseDto.builder()
.userId(comment.getUserId())
.userDto(userDto)
.trackId(comment.getTrackId())
.content(comment.getContent())
.createdAt(comment.getCreatedAt())
.updatedAt(comment.getUpdatedAt())
.isOwner(isOwner)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public void updateContent(String content) {
this.content = content;
}

public static Comment toEntity(CommentRequestDto commentRequestDto) {
public static Comment toEntity(Long userId, CommentRequestDto commentRequestDto) {
return Comment.builder()
.userId(commentRequestDto.userId())
.userId(userId)
.trackId(commentRequestDto.trackId())
.content(commentRequestDto.content())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
import com.munecting.api.domain.comment.dto.response.CommentResponseDto;
import com.munecting.api.domain.comment.entity.Comment;
import com.munecting.api.domain.spotify.service.SpotifyService;
import com.munecting.api.domain.user.dto.response.UserResponseDto;
import com.munecting.api.domain.user.entity.User;
import com.munecting.api.domain.user.service.UserService;
import com.munecting.api.global.error.exception.EntityNotFoundException;
import com.munecting.api.global.common.dto.response.PagedResponseDto;
import com.munecting.api.global.common.dto.response.Status;

import com.munecting.api.global.error.exception.UnauthorizedException;
import java.time.LocalDateTime;

import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -27,12 +34,14 @@ public class CommentService {

private final CommentRepository commentRepository;
private final SpotifyService spotifyService;
private final UserService userService;

@Transactional
public CommentIdResponseDto createComment(CommentRequestDto commentRequestDto) {
public CommentIdResponseDto createComment(Long userId, CommentRequestDto commentRequestDto) {
userService.validateUserExists(userId);
String trackId = commentRequestDto.trackId();
spotifyService.validateTrackExists(trackId);
Comment comment = Comment.toEntity(commentRequestDto);
Comment comment = Comment.toEntity(userId, commentRequestDto);
Long id = saveComment(comment);
return CommentIdResponseDto.of(id);
}
Expand All @@ -42,23 +51,30 @@ private Long saveComment(Comment comment) {
}

@Transactional(readOnly = true)
public PagedResponseDto<CommentResponseDto> getCommentsByTrackId(String trackId, LocalDateTime cursor, int limit) {
public PagedResponseDto<CommentResponseDto> getCommentsByTrackId(Long userId, String trackId, LocalDateTime cursor, int limit) {
Pageable pageable = PageRequest.of(0, limit);
//Timestamp cursorTimestamp = LocalDateTimeUtil.toTimestamp(cursor);
Page<Comment> pagedComment = getCommentsByTrackIdWithCursor(trackId, cursor, pageable);
Page<CommentResponseDto> pagedCommentResponseDto = pagedComment.map(CommentResponseDto::of);
List<CommentResponseDto> commentResponseDtos = pagedComment.stream()
.map(comment -> {
User commentWriter = userService.findUserByIdOrThrow(comment.getUserId());
Boolean isOwner = userId.equals(commentWriter.getId());
return CommentResponseDto.of(commentWriter, comment, isOwner);
})
.collect(Collectors.toList());

Page<CommentResponseDto> pagedCommentResponseDto = new PageImpl<>(commentResponseDtos, pageable, pagedComment.getTotalElements());
return new PagedResponseDto<>(pagedCommentResponseDto);
}

@Transactional(readOnly = true)
public Page<Comment> getCommentsByTrackIdWithCursor(String trackId, LocalDateTime cursor, Pageable pageable) {
private Page<Comment> getCommentsByTrackIdWithCursor(String trackId, LocalDateTime cursor, Pageable pageable) {
log.info(String.valueOf(cursor));
return commentRepository.findCommentsByTrackIdWithCursor(trackId, cursor.toString(), pageable);
}

@Transactional
public CommentIdResponseDto updateComment(Long commentId, CommentRequestDto commentRequestDto) {
public CommentIdResponseDto updateComment(Long userId, Long commentId, CommentRequestDto commentRequestDto) {
Comment comment = getCommentById(commentId);
validateCommentWriter(userId, comment);
comment.updateContent(commentRequestDto.content());
return CommentIdResponseDto.of(commentId);
}
Expand All @@ -70,13 +86,20 @@ public Comment getCommentById(Long id) {
}

@Transactional
public CommentIdResponseDto deleteCommentById(Long commentId) {
public CommentIdResponseDto deleteCommentById(Long userId, Long commentId) {
Comment comment = getCommentById(commentId);
validateCommentWriter(userId, comment);
deleteComment(comment);
return CommentIdResponseDto.of(commentId);
}

private void deleteComment(Comment comment) {
commentRepository.delete(comment);
}

private void validateCommentWriter(Long userId, Comment comment) {
if (!userId.equals(comment.getUserId())) {
throw new UnauthorizedException(Status.NOT_COMMENT_WRITER);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
public class LikeController {

private final LikeService likeService;

@PostMapping("/{trackId}/likes")
@Operation(summary = "좋아요 누르기")
public ApiResponse<?> addTrackLike(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.munecting.api.domain.uploadedMusic.dto.response.UploadedMusicIdResponseDto;
import com.munecting.api.domain.uploadedMusic.dto.response.UploadedMusicResponseDto;
import com.munecting.api.domain.uploadedMusic.service.MusicService;
import com.munecting.api.global.auth.user.UserId;
import com.munecting.api.global.common.dto.response.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -26,13 +27,13 @@
public class MusicController {

private final MusicService musicService;

@PostMapping("")
@Operation(summary = "음악 업로드 하기")
public ApiResponse<?> uploadMusic(
@Valid @RequestBody MusicRequestDto musicRequestDto
@UserId Long userId,
@Valid @RequestBody MusicRequestDto musicRequestDto
) {
UploadedMusicIdResponseDto uploadedMusicIdResponseDto = musicService.uploadMusic(musicRequestDto);
UploadedMusicIdResponseDto uploadedMusicIdResponseDto = musicService.uploadMusic(userId, musicRequestDto);
return ApiResponse.created(uploadedMusicIdResponseDto);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public record MusicRequestDto (
@NotNull(message = "경도는 필수값입니다.")
Double longitude,

Long userId, // 인증 작업 리팩토링 끝나면 연동 할 예정

@NotNull(message = "노래 아이디는 필수값입니다.")
String trackId,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

import com.munecting.api.domain.spotify.dto.response.MusicResponseDto;
import com.munecting.api.domain.uploadedMusic.entity.UploadedMusic;
import com.munecting.api.domain.user.dto.response.UserResponseDto;
import com.munecting.api.domain.user.entity.User;
import lombok.Builder;

@Builder
public record UploadedMusicResponseDto(
Long id,
MusicResponseDto musicResponseDto,
Double latitude,
Double longitude
//인증 작업 완료 후 User 정보 dto로 반환하기
Double longitude,
UserResponseDto userResponseDto
) {

public static UploadedMusicResponseDto of(UploadedMusic uploadedMusic, MusicResponseDto musicResponseDto) {
public static UploadedMusicResponseDto of(UploadedMusic uploadedMusic, MusicResponseDto musicResponseDto, User user) {
UserResponseDto userDto = UserResponseDto.of(
user.getId(), user.getNickname(), user.getProfileImageUrl());

return UploadedMusicResponseDto.builder()
.id(uploadedMusic.getId())
.musicResponseDto(musicResponseDto)
.latitude(uploadedMusic.getLatitude())
.longitude(uploadedMusic.getLongitude())
.userResponseDto(userDto)
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public class UploadedMusic extends BaseEntity {
@NotNull
private Integer uploadDuration;

public static UploadedMusic toEntity (MusicRequestDto musicRequestDto) {
public static UploadedMusic toEntity (Long userId, MusicRequestDto musicRequestDto) {
return UploadedMusic.builder().
userId(musicRequestDto.userId())
userId(userId)
.trackId(musicRequestDto.trackId())
.latitude(musicRequestDto.latitude())
.longitude(musicRequestDto.longitude())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.munecting.api.domain.uploadedMusic.dto.response.UploadedMusicIdResponseDto;
import com.munecting.api.domain.uploadedMusic.dto.response.UploadedMusicResponseDto;
import com.munecting.api.domain.uploadedMusic.entity.UploadedMusic;
import com.munecting.api.domain.user.dto.response.UserResponseDto;
import com.munecting.api.domain.user.service.UserService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -23,11 +25,12 @@ public class MusicService {

private final UploadedMusicRepository uploadedMusicRepository;
private final SpotifyService spotifyService;
private final UserService userService;

@Transactional
public UploadedMusicIdResponseDto uploadMusic(MusicRequestDto musicRequestDto) {
public UploadedMusicIdResponseDto uploadMusic(Long userId, MusicRequestDto musicRequestDto) {
spotifyService.getTrack(musicRequestDto.trackId());
UploadedMusic uploadedMusic = UploadedMusic.toEntity(musicRequestDto);
UploadedMusic uploadedMusic = UploadedMusic.toEntity(userId, musicRequestDto);
return saveUploadMusicEntity(uploadedMusic);
}

Expand All @@ -47,8 +50,8 @@ public List<UploadedMusicResponseDto> getUploadedMusics(Double latitude, Double
= uploadedMusics.stream()
.map(uploadedMusic -> {
MusicResponseDto musicInfo = musicInfoByTrackId.get(uploadedMusic.getTrackId());
User user = null; // 인증 부분 완료되면 수정 예정
return UploadedMusicResponseDto.of(uploadedMusic, musicInfo);
User user = userService.findUserByIdOrThrow(uploadedMusic.getUserId());
return UploadedMusicResponseDto.of(uploadedMusic, musicInfo, user);
})
.collect(Collectors.toList());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.munecting.api.domain.user.dto.response;

import lombok.Builder;

@Builder
public record UserResponseDto(
Long id,
String nickname,
String imageUrl
) {
public static UserResponseDto of(Long id, String nickname, String imgUrl) {
return UserResponseDto.builder()
.id(id)
.nickname(nickname)
.imageUrl(imgUrl)
.build();
}
}
Loading

0 comments on commit 9e1d43f

Please sign in to comment.