-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] 알림 (단일 조회, 목록 조회, 삭제) api 구현 (#21)
- Loading branch information
Showing
10 changed files
with
339 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/com/nadoyagsa/pillaroid/dto/NotificationDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.nadoyagsa.pillaroid.dto; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class NotificationDto { | ||
private long userIdx; | ||
|
||
@NotNull(message = "medicineIdx 필수") | ||
private int medicineIdx; | ||
|
||
@NotNull(message = "name 필수") | ||
private String name; | ||
|
||
@NotNull(message = "period 필수") | ||
private int period; | ||
|
||
private String dosage; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/nadoyagsa/pillaroid/dto/NotificationResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.nadoyagsa.pillaroid.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class NotificationResponse { | ||
private long notificationIdx; | ||
private String name; | ||
private int period; | ||
private String dosage; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/nadoyagsa/pillaroid/dto/NotificationTimeDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.nadoyagsa.pillaroid.dto; | ||
|
||
import java.time.LocalTime; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class NotificationTimeDto { | ||
private long notificationTimeIdx; | ||
private LocalTime time; | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/nadoyagsa/pillaroid/dto/NotificationTimeResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.nadoyagsa.pillaroid.dto; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.nadoyagsa.pillaroid.entity.Notification; | ||
import com.nadoyagsa.pillaroid.entity.NotificationTime; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@JsonInclude(JsonInclude.Include.NON_DEFAULT) | ||
public class NotificationTimeResponse { | ||
private long notificationIdx; | ||
private String name; | ||
private long period; | ||
private String dosage; | ||
private List<NotificationTimeDto> notificationTimes; | ||
|
||
public NotificationTimeResponse(Notification notification, List<NotificationTime> notificationTimes) { | ||
this.notificationIdx = notification.getNotificationIdx(); | ||
this.name = notification.getName(); | ||
this.period = notification.getPeriod(); | ||
this.dosage = notification.getDosage(); | ||
this.notificationTimes = notificationTimes.stream() | ||
.map(NotificationTime::toNotificationTimeDto) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/nadoyagsa/pillaroid/entity/Notification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.nadoyagsa.pillaroid.entity; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.Table; | ||
|
||
import com.nadoyagsa.pillaroid.dto.NotificationResponse; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Builder | ||
@Table(name = "notification") | ||
@Entity | ||
public class Notification { | ||
@Id | ||
@Column(name = "notification_idx") | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long notificationIdx; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "user_idx") | ||
private User user; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "medicine_idx") | ||
private Medicine medicine; | ||
|
||
@Column(name = "name") | ||
private String name; | ||
|
||
@Column | ||
private int period; | ||
|
||
@Column | ||
private String dosage; | ||
|
||
public NotificationResponse toNotificationResponse() { | ||
return NotificationResponse.builder() | ||
.notificationIdx(this.notificationIdx) | ||
.name(this.name) | ||
.period(this.period) | ||
.dosage(this.dosage) | ||
.build(); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/nadoyagsa/pillaroid/entity/NotificationTime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.nadoyagsa.pillaroid.entity; | ||
|
||
import java.time.LocalTime; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.MapsId; | ||
import javax.persistence.OneToOne; | ||
import javax.persistence.Table; | ||
|
||
import com.nadoyagsa.pillaroid.dto.NotificationTimeDto; | ||
import com.nadoyagsa.pillaroid.dto.NotificationTimeResponse; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Builder | ||
@Table(name = "notification_time") | ||
@Entity | ||
public class NotificationTime { | ||
@Id | ||
@Column(name = "notification_time_idx") | ||
private Long notificationTimeIdx; | ||
|
||
@OneToOne | ||
@JoinColumn(name = "notification_idx") | ||
private Notification notification; | ||
|
||
@Column | ||
private LocalTime time; | ||
|
||
public NotificationTimeDto toNotificationTimeDto() { | ||
return NotificationTimeDto.builder() | ||
.notificationTimeIdx(this.notificationTimeIdx) | ||
.time(this.time) | ||
.build(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/nadoyagsa/pillaroid/repository/NotificationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.nadoyagsa.pillaroid.repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import com.nadoyagsa.pillaroid.entity.Medicine; | ||
import com.nadoyagsa.pillaroid.entity.Notification; | ||
import com.nadoyagsa.pillaroid.entity.User; | ||
|
||
public interface NotificationRepository extends JpaRepository<Notification, Long> { | ||
Optional<Notification> findByUserAndMedicine(User user, Medicine medicine); | ||
|
||
@Query("SELECT f FROM Notification f WHERE f.user.userIdx = :userIdx AND f.medicine.medicineIdx = :medicineIdx") | ||
Optional<Notification> findByUserIdxAndMedicineIdx(@Param("userIdx") long userIdx, @Param("medicineIdx") int medicineIdx); | ||
|
||
List<Notification> findByUser(User user); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/nadoyagsa/pillaroid/repository/NotificationTimeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.nadoyagsa.pillaroid.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.nadoyagsa.pillaroid.entity.Notification; | ||
import com.nadoyagsa.pillaroid.entity.NotificationTime; | ||
|
||
public interface NotificationTimeRepository extends JpaRepository<NotificationTime, Long> { | ||
List<NotificationTime> findByNotification(Notification notification); | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/nadoyagsa/pillaroid/service/NotificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.nadoyagsa.pillaroid.service; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.nadoyagsa.pillaroid.common.exception.BadRequestException; | ||
import com.nadoyagsa.pillaroid.common.exception.ForbiddenException; | ||
import com.nadoyagsa.pillaroid.common.exception.NotFoundException; | ||
import com.nadoyagsa.pillaroid.dto.NotificationResponse; | ||
import com.nadoyagsa.pillaroid.dto.NotificationTimeDto; | ||
import com.nadoyagsa.pillaroid.dto.NotificationTimeResponse; | ||
import com.nadoyagsa.pillaroid.entity.Medicine; | ||
import com.nadoyagsa.pillaroid.entity.Notification; | ||
import com.nadoyagsa.pillaroid.entity.NotificationTime; | ||
import com.nadoyagsa.pillaroid.entity.User; | ||
import com.nadoyagsa.pillaroid.repository.MedicineRepository; | ||
import com.nadoyagsa.pillaroid.repository.NotificationRepository; | ||
import com.nadoyagsa.pillaroid.repository.NotificationTimeRepository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class NotificationService { | ||
|
||
private final NotificationRepository notificationRepository; | ||
private final NotificationTimeRepository notificationTimeRepository; | ||
private final MedicineRepository medicineRepository; | ||
|
||
// 의약품에 해당하는 사용자 알림 조회 | ||
public NotificationResponse findNotificationByUserAndMedicineIdx(User user, int medicineIdx) { | ||
Medicine medicine = medicineRepository.findById(medicineIdx) | ||
.orElseThrow(() -> BadRequestException.BAD_PARAMETER); | ||
Notification notification = notificationRepository.findByUserAndMedicine(user, medicine) | ||
.orElseThrow(() -> NotFoundException.DATA_NOT_FOUND); | ||
return notification.toNotificationResponse(); | ||
} | ||
|
||
// 사용자 관련 알림 목록 조회 | ||
public List<NotificationResponse> findNotificationByUser(User user) { | ||
List<Notification> notifications = notificationRepository.findByUser(user); | ||
return notifications.stream() | ||
.map(Notification::toNotificationResponse) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
// 의약품에 해당하는 사용자 알림 삭제 | ||
@Transactional | ||
public NotificationTimeResponse deleteNotification(User user, long notificationIdx) throws NotFoundException, ForbiddenException { | ||
// 알림 데이터가 있는지 검사 | ||
Notification notification = notificationRepository.findById(notificationIdx) | ||
.orElseThrow(() -> NotFoundException.DATA_NOT_FOUND); | ||
|
||
// 사용자 본인이 데이터를 지우는 것인지 검사 | ||
if (!Objects.equals(notification.getUser().getUserIdx(), user.getUserIdx())) { | ||
throw ForbiddenException.deleteForbidden; | ||
} | ||
|
||
List<NotificationTime> notificationTimes = notificationTimeRepository.findByNotification(notification); // 삭제할 알림과 관련된 시간 데이터 조회 | ||
notificationRepository.deleteById(notification.getNotificationIdx()); // 데이터 삭제 | ||
|
||
// 전달할 Response 생성 | ||
List<NotificationTimeDto> notificationTimeDtos = notificationTimes.stream() | ||
.map(NotificationTime::toNotificationTimeDto) | ||
.collect(Collectors.toList()); | ||
|
||
return NotificationTimeResponse.builder() | ||
.notificationIdx(notificationIdx) | ||
.notificationTimes(notificationTimeDtos) | ||
.build(); | ||
} | ||
} |