Skip to content

Commit

Permalink
#153 목록 리스트
Browse files Browse the repository at this point in the history
목록 리스트
  • Loading branch information
geonoo committed Aug 3, 2022
1 parent a3cda92 commit 76b2f8e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,22 @@ public ResponseEntity<List<NotificationResponseDto>> getNotification() {
return ResponseEntity.status(HttpStatus.OK).body(responseDtoList);
}

@ApiOperation(value = "알림 전체 삭제")
@ApiImplicitParam(name = "Authorization", value = "Access Token", required = true, allowEmptyValue = false, paramType = "header", dataTypeClass = String.class, example = "access_token")
@DeleteMapping("/notification")
public ResponseEntity<String> deleteNotification() {
LoadUser.loginAndNickCheck();
return ResponseEntity.status(HttpStatus.OK)
.body(notificationService.deleteNotification(LoadUser.getEmail()));
}

@PostMapping("/publish/notification/{id}")
public void publish(
@PathVariable Long id,
@RequestBody NotificationRequestDto requestDto
) {
notificationService.sendNotification(id, requestDto);
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.backend.notification.domain;

import com.example.backend.common.domain.BaseTime;
import com.example.backend.notification.dto.NotificationRequestDto;
import com.example.backend.user.domain.User;
import lombok.Getter;
Expand All @@ -10,7 +11,7 @@
@Entity
@Getter
@NoArgsConstructor
public class Notification {
public class Notification extends BaseTime {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

import com.example.backend.notification.domain.Notification;
import com.example.backend.notification.domain.Type;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
public class NotificationResponseDto {

private Type type;
private String message;
private boolean readState;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss", timezone = "Asia/Seoul")
private LocalDateTime createdDate;

public NotificationResponseDto(Notification notification) {
this.type = notification.getType();
this.message = notification.getMessage();
this.readState = notification.isReadState();
this.createdDate = notification.getCreatedDate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.example.backend.notification.domain.Notification;
import com.example.backend.user.domain.User;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface NotificationRepository extends JpaRepository<Notification, Long> {

List<Notification> findByUser(User user);
List<Notification> findByUserOrderByCreatedDateDesc(User user);
void deleteByUser(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.transaction.Transactional;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -61,12 +63,23 @@ public List<NotificationResponseDto> getNotification(String email) {
() -> new CustomException(ErrorCode.USER_NOT_FOUND)
);
List<NotificationResponseDto> responseDtoList = new ArrayList<>();
List<Notification> notificationList = notificationRepository.findByUser(user);
List<Notification> notificationList = notificationRepository.findByUserOrderByCreatedDateDesc(user);
for (Notification n : notificationList) {
n.changeState();
responseDtoList.add(new NotificationResponseDto(n));
}
return responseDtoList;
}

@Transactional
public String deleteNotification(String email){
User user = userRepository.findByEmail(email).orElseThrow(
() -> new CustomException(ErrorCode.USER_NOT_FOUND)
);

notificationRepository.deleteByUser(user);

return "알림 삭제 완료";
}

}

0 comments on commit 76b2f8e

Please sign in to comment.