Skip to content

Commit

Permalink
๐Ÿš€ :: v0.0.5
Browse files Browse the repository at this point in the history
๐Ÿš€ :: v0.0.5
  • Loading branch information
ImNM authored Dec 18, 2022
2 parents 7b08553 + e8add21 commit c9df07e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,16 @@ public class Notification extends BaseTimeEntity {
@OneToMany(mappedBy = "notification", fetch = FetchType.LAZY)
private Set<NotificationReaction> notificationReactions = new HashSet<>();

private boolean deleted;

public void addReceivers(List<NotificationReceiver> receivers) {
this.receivers.addAll(receivers);
}

public void deleteNotification() {
this.deleted = true;
}

public static Notification of(Long notificationId) {
return Notification.builder().id(notificationId).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public interface NotificationRepository
extends JpaRepository<Notification, Long>, CustomNotificationRepository {

@EntityGraph(attributePaths = {"group"})
Slice<Notification> findAllByGroupId(Long groupId, Pageable pageable);
Slice<Notification> findAllByGroupIdAndDeleted(
Long groupId, boolean deleted, Pageable pageable);

@EntityGraph(attributePaths = {"group"})
Optional<Notification> findById(Long notificationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@SecurityRequirement(name = "access-token")
@Tag(name = "ํ‘ธ์‰ฌ์•Œ๋ฆผ ๊ด€๋ จ ์ปจํŠธ๋กค๋Ÿฌ", description = "")
Expand Down Expand Up @@ -60,4 +54,12 @@ public QueryNotificationListResponse queryListByGroupId(
@PathVariable(value = "group_id") Long groupId) {
return notificationService.queryListByGroupId(pageable, groupId);
}

@Operation(summary = "ํ‘ธ์‰ฌ์•Œ๋ฆผ ์‚ญ์ œ")
@ResponseStatus(HttpStatus.OK)
@DeleteMapping("{notification_id}")
public void deleteByNotificationId(
@PathVariable(value = "notification_id") Long notificationId) {
notificationService.deleteByNotificationId(notificationId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import io.github.depromeet.knockknockbackend.domain.notification.domain.repository.NotificationRepository;
import io.github.depromeet.knockknockbackend.domain.notification.domain.vo.NotificationReactionCountInfoVo;
import io.github.depromeet.knockknockbackend.domain.notification.exception.FcmResponseException;
import io.github.depromeet.knockknockbackend.domain.notification.exception.NotificationForbiddenException;
import io.github.depromeet.knockknockbackend.domain.notification.exception.NotificationNotFoundException;
import io.github.depromeet.knockknockbackend.domain.notification.presentation.dto.request.RegisterFcmTokenRequest;
import io.github.depromeet.knockknockbackend.domain.notification.presentation.dto.request.SendInstanceRequest;
import io.github.depromeet.knockknockbackend.domain.notification.presentation.dto.response.MyNotificationReactionResponseElement;
Expand Down Expand Up @@ -42,6 +44,7 @@
@Service
public class NotificationService {

private static final boolean CREATED_DELETED_STATUS = false;
private final NotificationRepository notificationRepository;
private final DeviceTokenRepository deviceTokenRepository;
private final NotificationReactionRepository notificationReactionRepository;
Expand All @@ -55,7 +58,8 @@ public QueryNotificationListResponse queryAlarmHistoryByUserId(Pageable pageable
@Transactional(readOnly = true)
public QueryNotificationListResponse queryListByGroupId(Pageable pageable, Long groupId) {
Slice<Notification> notifications =
notificationRepository.findAllByGroupId(groupId, pageable);
notificationRepository.findAllByGroupIdAndDeleted(
groupId, CREATED_DELETED_STATUS, pageable);

Slice<QueryNotificationListResponseElement> notificationListResponseElements =
getNotificationListResponseElements(notifications);
Expand Down Expand Up @@ -133,6 +137,13 @@ public void sendInstance(SendInstanceRequest request) {
notificationRepository.save(notification);
}

public void deleteByNotificationId(Long notificationId) {
Notification notification = queryNotificationById(notificationId);
validateDeletePermission(notification);
notification.deleteNotification();
notificationRepository.save(notification);
}

public Slice<QueryNotificationListResponseElement> getNotificationListResponseElements(
Slice<Notification> notifications) {

Expand Down Expand Up @@ -236,4 +247,16 @@ private Slice<NotificationReaction> retrieveNotificationReactions(
return notificationReactionRepository.findByUserIdAndNotificationIn(
SecurityUtils.getCurrentUserId(), notifications.getContent());
}

private void validateDeletePermission(Notification notification) {
if (!SecurityUtils.getCurrentUserId().equals(notification.getSendUser().getId())) {
throw NotificationForbiddenException.EXCEPTION;
}
}

private Notification queryNotificationById(Long notificationId) {
return notificationRepository
.findById(notificationId)
.orElseThrow(() -> NotificationNotFoundException.EXCEPTION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@ public class ReactionController {

private final ReactionService reactionService;

@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "์•Œ๋ฆผ์— ๋ฆฌ์•ก์…˜ ๋“ฑ๋ก")
@ResponseStatus(HttpStatus.OK)
@PostMapping
public void registerReaction(@RequestBody RegisterReactionRequest request) {
reactionService.registerReaction(request);
}

@Operation(summary = "์•Œ๋ฆผ ๋ฆฌ์•ก์…˜ ์ˆ˜์ •")
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "์•Œ๋ฆผ์— ๋ฆฌ์•ก์…˜ ์ˆ˜์ •")
@PatchMapping("{notification_reaction_id}")
public void changeReaction(
@PathVariable("notification_reaction_id") Long notificationReactionId,
@RequestBody RegisterReactionRequest request) {
reactionService.changeReaction(notificationReactionId, request);
}

@Operation(summary = "์•Œ๋ฆผ ๋ฆฌ์•ก์…˜ ์‚ญ์ œ")
@ResponseStatus(HttpStatus.OK)
@DeleteMapping("{notification_reaction_id}")
public void deleteReaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@RestControllerAdvice
@RestControllerAdvice(basePackages = "io.github.depromeet")
public class SuccessResponseAdvice implements ResponseBodyAdvice {

@Override
Expand Down

0 comments on commit c9df07e

Please sign in to comment.