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

[Refactor] 댓글,태그,알림 기능 수정 #144

Merged
merged 6 commits into from
Sep 13, 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
Empty file.
Empty file.
40 changes: 15 additions & 25 deletions src/main/java/me/snaptime/alarm/controller/AlarmController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import me.snaptime.alarm.common.AlarmType;
import me.snaptime.alarm.dto.res.AlarmFindAllResDto;
import me.snaptime.alarm.service.AlarmService;
import me.snaptime.common.CommonResponseDto;
import me.snaptime.reply.dto.res.ParentReplyPagingResDto;
import me.snaptime.snap.dto.res.SnapDetailInfoResDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand All @@ -31,45 +28,39 @@ public class AlarmController {
private final AlarmService alarmService;

@GetMapping("/follow/{followAlarmId}")
@Operation(summary = "팔로우요청 알림에 대해 수락or거절을 합니다.", description =
"수락 or 거절할 follow알림 id와 수락여부를 보내주세요.<br>" +
"친구요청 수락(sender(수락자)의 팔로잉 +1, receiver의 팔로워 +1)<br>" +
"친구요청 거절(sender(수락자)의 팔로워 -1, receiver의 팔로잉 -1) ")
@Parameters({
@Parameter(name = "followAlarmId" , description = "followAlarmId를 입력해주세요", required = true,example = "1"),
@Parameter(name = "isAccept", description = "수락여부를 입력해주세요", required = true, example = "true"),
})
@Operation(summary = "팔로우알림 조회", description = "팔로우알림을 읽음처리합니다.")
@Parameter(name = "followAlarmId" , description = "followAlarmId를 입력해주세요", required = true,example = "1")
public ResponseEntity<CommonResponseDto<Void>> acceptFollowReq(
@AuthenticationPrincipal UserDetails userDetails,
@PathVariable Long followAlarmId,
@RequestParam @NotNull(message = "수락여부를 보내주세요.") boolean isAccept) {
@PathVariable Long followAlarmId) {

String msg = alarmService.readFollowAlarm(userDetails.getUsername(),followAlarmId,isAccept);
return ResponseEntity.status(HttpStatus.OK).body(new CommonResponseDto(msg, null));
alarmService.readFollowAlarm(userDetails.getUsername(),followAlarmId);
return ResponseEntity.status(HttpStatus.OK)
.body(new CommonResponseDto("팔로우 알림조회 성공", null));
}

@GetMapping("/snaps/{snapAlarmId}")
@Operation(summary = "스냅알림 조회", description = "스냅알림을 읽음처리 후 해당스냅페이지로 이동합니다.")
@Operation(summary = "스냅알림 조회", description = "스냅알림을 읽음처리합니다.")
@Parameter(name = "snapAlarmId" , description = "snapAlarmId를 입력해주세요", required = true,example = "1")
public ResponseEntity<CommonResponseDto<SnapDetailInfoResDto>> readSnapAlarm(
public ResponseEntity<CommonResponseDto<Void>> readSnapAlarm(
@AuthenticationPrincipal UserDetails userDetails,
@PathVariable Long snapAlarmId) {

alarmService.readSnapAlarm(userDetails.getUsername(), snapAlarmId);
return ResponseEntity.status(HttpStatus.OK)
.body(new CommonResponseDto("스냅알림 조회 성공",
alarmService.readSnapAlarm(userDetails.getUsername(), snapAlarmId)));
.body(new CommonResponseDto("스냅알림 조회 성공", null));
}

@GetMapping("/replies/{replyAlarmId}")
@Operation(summary = "댓글알림 조회", description = "댓글알림을 읽음처리 후 해당 댓글페이지 1번으로 이동합니다.")
@Operation(summary = "댓글알림 조회", description = "댓글알림을 읽음처리합니다.")
@Parameter(name = "replyAlarmId" , description = "replyAlarmId를 입력해주세요", required = true,example = "1")
public ResponseEntity<CommonResponseDto<ParentReplyPagingResDto>> readReplyAlarm(
public ResponseEntity<CommonResponseDto<Void>> readReplyAlarm(
@AuthenticationPrincipal UserDetails userDetails,
@PathVariable Long replyAlarmId) {

alarmService.readReplyAlarm(userDetails.getUsername(), replyAlarmId);
return ResponseEntity.status(HttpStatus.OK)
.body(new CommonResponseDto("댓글알림 조회 성공",
alarmService.readReplyAlarm(userDetails.getUsername(), replyAlarmId)));
.body(new CommonResponseDto("댓글알림 조회 성공", null));
}

@GetMapping("/count/not-read")
Expand All @@ -85,8 +76,7 @@ public ResponseEntity<CommonResponseDto<Long>> findNotReadAlarmCnt(
@GetMapping
@Operation(summary = "알림리스트 조회", description = "자신에게 온 알림리스트를 조회합니다.<br>"+
"읽지않은 알림을 먼저 보여주며 시간순으로 정렬하여 반환합니다.<br>"+
"알림타입별로 반환되는 데이터가 다릅니다. 팔로우알림에는 snapUrl정보가 없으며 "+
"댓글알림에만 댓글내용을 보여주는 previewText값이 있습니다.<br>"+
"알림타입별로 반환되는 데이터가 다릅니다.<br>" +
"각 알림타입별로 alarmId값이 부여되기 때문에 타입이 다른 알림의 경우 id값이 중복될 수 있습니다.")
public ResponseEntity<CommonResponseDto<AlarmFindAllResDto>> findAlarms(
@AuthenticationPrincipal UserDetails userDetails) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/me/snaptime/alarm/dto/res/AlarmInfoResDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public record AlarmInfoResDto(
String timeAgo,
String previewText,
AlarmType alarmType,
Long snapId,
String senderLoginId,
@Getter
LocalDateTime createdDate

Expand All @@ -33,6 +35,8 @@ public static AlarmInfoResDto toDtoByFollowAlarm(String senderProfilePhotoURL, S
.previewText(null)
.alarmType(followAlarm.getAlarmType())
.createdDate(followAlarm.getCreatedDate())
.snapId(null)
.senderLoginId(followAlarm.getSender().getLoginId())
.build();
}

Expand All @@ -48,6 +52,8 @@ public static AlarmInfoResDto toDtoBySnapAlarm(String senderProfilePhotoURL, Str
.previewText(null)
.alarmType(snapAlarm.getAlarmType())
.createdDate(snapAlarm.getCreatedDate())
.snapId(snapAlarm.getSnap().getId())
.senderLoginId(snapAlarm.getSender().getLoginId())
.build();
}

Expand All @@ -63,6 +69,8 @@ public static AlarmInfoResDto toDtoByReplyAlarm(String senderProfilePhotoURL, St
.previewText(replyAlarm.getReplyMessage())
.alarmType(replyAlarm.getAlarmType())
.createdDate(replyAlarm.getCreatedDate())
.snapId(replyAlarm.getSnap().getId())
.senderLoginId(replyAlarm.getSender().getLoginId())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import me.snaptime.snap.domain.Snap;
import me.snaptime.user.domain.User;

public interface CreateAlarmService {
public interface AlarmAddService {

/*
알림을 생성합니다.
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/me/snaptime/alarm/service/AlarmService.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
package me.snaptime.alarm.service;

import me.snaptime.alarm.common.AlarmType;
import me.snaptime.reply.dto.res.ParentReplyPagingResDto;
import me.snaptime.snap.dto.res.SnapDetailInfoResDto;


public interface AlarmService {

/*
snapAlarm을 읽음처리합니다.
읽음 처리 후 해당 스냅을 조회합니다.
*/
SnapDetailInfoResDto readSnapAlarm(String reqLoginId, Long snapAlarmId);
void readSnapAlarm(String reqLoginId, Long snapAlarmId);

//팔로우요청을 수락 or거절한 뒤 FollowAlarm을 읽음처리합니다.
String readFollowAlarm(String reqLoginId, Long followAlarmId, boolean isAccept);
//팔로우알림을 읽음처리합니다.
void readFollowAlarm(String reqLoginId, Long followAlarmId);

/*
ReplyAlarm을 읽음처리합니다.
읽음처리 후 해당 댓글의 1페이지로 이동합니다.
*/
ParentReplyPagingResDto readReplyAlarm(String reqLoginId, Long replyAlarmId);
void readReplyAlarm(String reqLoginId, Long replyAlarmId);

//유저의 모든 알림을 불러옵니다.
Object findAlarms(String reqLoginId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import me.snaptime.alarm.repository.FollowAlarmRepository;
import me.snaptime.alarm.repository.ReplyAlarmRepository;
import me.snaptime.alarm.repository.SnapAlarmRepository;
import me.snaptime.alarm.service.CreateAlarmService;
import me.snaptime.alarm.service.AlarmAddService;
import me.snaptime.snap.domain.Snap;
import me.snaptime.user.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class CreateAlarmServiceImpl implements CreateAlarmService {
public class AlarmAddServiceImpl implements AlarmAddService {

private final SnapAlarmRepository snapAlarmRepository;
private final FollowAlarmRepository followAlarmRepository;
Expand All @@ -25,6 +25,9 @@ public class CreateAlarmServiceImpl implements CreateAlarmService {
@Override
@Transactional
public void createSnapAlarm(User sender, User receiver, Snap snap, AlarmType alarmType) {
if(sender.getUserId() == receiver.getUserId())
return ;

SnapAlarm snapAlarm = SnapAlarm.builder()
.sender(sender)
.receiver(receiver)
Expand All @@ -50,6 +53,9 @@ public void createFollowAlarm(User sender, User receiver) {
@Override
@Transactional
public void createReplyAlarm(User sender, User receiver, Snap snap, String replyMessage) {
if(sender.getUserId() == receiver.getUserId())
return ;

ReplyAlarm replyAlarm = ReplyAlarm.builder()
.sender(sender)
.receiver(receiver)
Expand Down
34 changes: 10 additions & 24 deletions src/main/java/me/snaptime/alarm/service/impl/AlarmServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
import me.snaptime.component.url.UrlComponent;
import me.snaptime.exception.CustomException;
import me.snaptime.exception.ExceptionCode;
import me.snaptime.friend.service.FriendService;
import me.snaptime.reply.dto.res.ParentReplyPagingResDto;
import me.snaptime.reply.service.ReplyService;
import me.snaptime.snap.dto.res.SnapDetailInfoResDto;
import me.snaptime.snap.service.SnapService;
import me.snaptime.user.domain.User;
import me.snaptime.user.repository.UserRepository;
import me.snaptime.util.TimeAgoCalculator;
Expand All @@ -38,15 +33,12 @@ public class AlarmServiceImpl implements AlarmService {
private final FollowAlarmRepository followAlarmRepository;
private final ReplyAlarmRepository replyAlarmRepository;
private final UserRepository userRepository;
private final FriendService friendService;
private final SnapService snapService;
private final ReplyService replyService;
private final UrlComponent urlComponent;


@Override
@Transactional
public SnapDetailInfoResDto readSnapAlarm(String reqLoginId, Long snapAlarmId) {
public void readSnapAlarm(String reqLoginId, Long snapAlarmId) {

SnapAlarm snapAlarm = snapAlarmRepository.findById(snapAlarmId)
.orElseThrow(() -> new CustomException(ExceptionCode.ALARM_NOT_EXIST));
Expand All @@ -56,29 +48,25 @@ public SnapDetailInfoResDto readSnapAlarm(String reqLoginId, Long snapAlarmId) {

snapAlarm.readAlarm();
snapAlarmRepository.save(snapAlarm);

return snapService.findSnap(snapAlarm.getSnap().getId(), reqLoginId);
}

@Override
@Transactional
public String readFollowAlarm(String reqLoginId, Long followAlarmId, boolean isAccept) {
public void readFollowAlarm(String reqLoginId, Long followAlarmId) {
FollowAlarm followAlarm = followAlarmRepository.findById(followAlarmId)
.orElseThrow(() -> new CustomException(ExceptionCode.ALARM_NOT_EXIST));

// 자신한테 온 알림인지 여부체크
isMyAlarm(reqLoginId, followAlarm.getReceiver().getLoginId());

String msg = friendService.acceptFollow(followAlarm.getSender(), followAlarm.getReceiver(), isAccept);

followAlarm.readAlarm();
followAlarmRepository.save(followAlarm);

return msg;
}

@Override
@Transactional
public ParentReplyPagingResDto readReplyAlarm(String reqLoginId, Long replyAlarmId) {
public void readReplyAlarm(String reqLoginId, Long replyAlarmId) {
ReplyAlarm replyAlarm = replyAlarmRepository.findById(replyAlarmId)
.orElseThrow(() -> new CustomException(ExceptionCode.ALARM_NOT_EXIST));

Expand All @@ -87,8 +75,6 @@ public ParentReplyPagingResDto readReplyAlarm(String reqLoginId, Long replyAlarm

replyAlarm.readAlarm();
replyAlarmRepository.save(replyAlarm);

return replyService.findParentReplyPage(replyAlarm.getSnap().getId(), 1L);
}

@Override
Expand All @@ -113,23 +99,23 @@ public Long findNotReadAlarmCnt(String reqLoginId) {
@Transactional
public void deleteAlarm(String reqLoginId, Long alarmId, AlarmType alarmType) {

// 팔로우 알림일 경우 거절처리 후 삭제
// 팔로우 알림일 경우
if(alarmType == AlarmType.FOLLOW){
FollowAlarm followAlarm = followAlarmRepository.findById(alarmId)
.orElseThrow(() -> new CustomException(ExceptionCode.ALARM_NOT_EXIST));

isMyAlarm(reqLoginId, followAlarm.getReceiver().getLoginId());
friendService.acceptFollow(followAlarm.getSender(), followAlarm.getReceiver(), false);
followAlarmRepository.delete(followAlarm);
}
// 댓글알림일 경우 바로삭제
// 댓글알림일 경우
else if(alarmType == AlarmType.REPLY){
ReplyAlarm replyAlarm = replyAlarmRepository.findById(alarmId)
.orElseThrow(() -> new CustomException(ExceptionCode.ALARM_NOT_EXIST));

isMyAlarm(reqLoginId, replyAlarm.getReceiver().getLoginId());
replyAlarmRepository.delete(replyAlarm);
}
// 스냅(스냅태그, 좋아요)에 대한 알림일 경우 바로 삭제
// 스냅(스냅태그, 좋아요)에 대한 알림일 경우
else{
SnapAlarm snapAlarm = snapAlarmRepository.findById(alarmId)
.orElseThrow(() -> new CustomException(ExceptionCode.ALARM_NOT_EXIST));
Expand Down Expand Up @@ -170,7 +156,7 @@ private List<AlarmInfoResDto> findSortedAlarms(User reqUser, boolean isRead){

User sender = replyAlarm.getSender();
String profilePhotoURL = urlComponent.makeProfileURL(sender.getProfilePhoto().getProfilePhotoId());
String snapPhotoURL = urlComponent.makePhotoURL(replyAlarm.getSnap().getFileName(),false);
String snapPhotoURL = urlComponent.makePhotoURL(replyAlarm.getSnap().getFileName(),replyAlarm.getSnap().isPrivate());
String timeAgo = TimeAgoCalculator.findTimeAgo(replyAlarm.getCreatedDate());

AlarmInfoResDto alarmInfoResDto = AlarmInfoResDto.toDtoByReplyAlarm(profilePhotoURL, snapPhotoURL, timeAgo, replyAlarm);
Expand All @@ -181,7 +167,7 @@ private List<AlarmInfoResDto> findSortedAlarms(User reqUser, boolean isRead){

User sender = snapAlarm.getSender();
String profilePhotoURL = urlComponent.makeProfileURL(sender.getProfilePhoto().getProfilePhotoId());
String snapPhotoURL = urlComponent.makePhotoURL(snapAlarm.getSnap().getFileName(),false);
String snapPhotoURL = urlComponent.makePhotoURL(snapAlarm.getSnap().getFileName(),snapAlarm.getSnap().isPrivate());
String timeAgo = TimeAgoCalculator.findTimeAgo(snapAlarm.getCreatedDate());

AlarmInfoResDto alarmInfoResDto = AlarmInfoResDto.toDtoBySnapAlarm(profilePhotoURL, snapPhotoURL, timeAgo, snapAlarm);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/me/snaptime/exception/ExceptionCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public enum ExceptionCode {
SNAP_IS_PRIVATE(HttpStatus.BAD_REQUEST, "사용자가 이 스냅을 비공개로 설정했습니다."),
SNAP_USER_IS_NOT_THE_SAME(HttpStatus.BAD_REQUEST, "스냅을 저장한 유저와 스냅을 요청한 유저가 일치하지 않습니다."),
SNAP_MODIFY_ERROR(HttpStatus.BAD_REQUEST, "스냅을 수정하던 중 문제가 발생했습니다."),
CAN_NOT_SELF_TAG(HttpStatus.BAD_REQUEST, "자기 자신을 태그할 수 없습니다."),

// Photo Exception
PHOTO_NOT_EXIST(HttpStatus.BAD_REQUEST, "사진을 찾을 수 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.querydsl.core.Tuple;
import lombok.RequiredArgsConstructor;
import me.snaptime.alarm.service.CreateAlarmService;
import me.snaptime.alarm.service.AlarmAddService;
import me.snaptime.component.url.UrlComponent;
import me.snaptime.exception.CustomException;
import me.snaptime.exception.ExceptionCode;
Expand Down Expand Up @@ -34,7 +34,7 @@ public class FriendServiceImpl implements FriendService {
private final FriendRepository friendRepository;
private final UserRepository userRepository;
private final UrlComponent urlComponent;
private final CreateAlarmService createAlarmService;
private final AlarmAddService alarmAddService;

@Override
@Transactional
Expand All @@ -58,7 +58,7 @@ public void sendFollow(String senderLoginId, String receiverLoginId){
.receiver(receiver)
.build());

createAlarmService.createFollowAlarm(sender,receiver);
alarmAddService.createFollowAlarm(sender,receiver);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/snaptime/reply/domain/ChildReply.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public class ChildReply extends BaseTimeEntity {
@Column(nullable = false)
private String content;

@ManyToOne
@ManyToOne(fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name = "user_id",nullable = false)
private User user;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name = "parent_reply_id",nullable = false)
private ParentReply parentReply;
Expand Down
Loading
Loading