-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 알림 등록 API Controller Request Body 수정
- Loading branch information
1 parent
5580e3a
commit 1560d1d
Showing
2 changed files
with
43 additions
and
43 deletions.
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
84 changes: 42 additions & 42 deletions
84
src/main/java/sopt/org/homepage/notification/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 |
---|---|---|
@@ -1,56 +1,56 @@ | ||
package sopt.org.homepage.notification; | ||
|
||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import java.sql.Timestamp; | ||
import java.util.List; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import sopt.org.homepage.notification.dto.GetNotificationListResponseDto; | ||
import sopt.org.homepage.notification.dto.RegisterNotificationRequestDto; | ||
import sopt.org.homepage.notification.dto.RegisterNotificationResponseDto; | ||
|
||
import java.sql.Timestamp; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class NotificationService { | ||
private final NotificationRepository notificationRepository; | ||
|
||
@Transactional | ||
public RegisterNotificationResponseDto registerNotification(RegisterNotificationRequestDto registerNotificationRequestDto) { | ||
NotificationEntity existingNotification = notificationRepository.findByEmailAndGeneration( | ||
registerNotificationRequestDto.getEmail(), registerNotificationRequestDto.getGeneration() | ||
); | ||
|
||
if (existingNotification != null) { | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, | ||
"Already Registered Email: " + registerNotificationRequestDto.getEmail()); | ||
} | ||
|
||
NotificationEntity notification = new NotificationEntity(); | ||
notification.setGeneration(registerNotificationRequestDto.getGeneration()); | ||
notification.setEmail(registerNotificationRequestDto.getEmail()); | ||
notification.setCreatedAt(new Timestamp(System.currentTimeMillis())); | ||
|
||
NotificationEntity savedNotification = notificationRepository.save(notification); | ||
|
||
return new RegisterNotificationResponseDto( | ||
savedNotification.getId(), | ||
savedNotification.getGeneration(), | ||
savedNotification.getEmail(), | ||
savedNotification.getCreatedAt() | ||
); | ||
} | ||
|
||
public GetNotificationListResponseDto getNotificationEmailList(int generation) { | ||
List<String> emailList = notificationRepository.findByGeneration(generation).stream() | ||
.map(NotificationEntity::getEmail) | ||
.toList(); | ||
|
||
return new GetNotificationListResponseDto(generation, emailList); | ||
} | ||
|
||
private final NotificationRepository notificationRepository; | ||
|
||
@Transactional | ||
public RegisterNotificationResponseDto registerNotification( | ||
RegisterNotificationRequestDto registerNotificationRequestDto) { | ||
NotificationEntity existingNotification = notificationRepository.findByEmailAndGeneration( | ||
registerNotificationRequestDto.getEmail(), registerNotificationRequestDto.getGeneration() | ||
); | ||
|
||
if (existingNotification != null) { | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, | ||
"Already Registered Email: " + registerNotificationRequestDto.getEmail()); | ||
} | ||
|
||
NotificationEntity notification = new NotificationEntity(); | ||
notification.setGeneration(registerNotificationRequestDto.getGeneration()); | ||
notification.setEmail(registerNotificationRequestDto.getEmail()); | ||
notification.setCreatedAt(new Timestamp(System.currentTimeMillis())); | ||
|
||
NotificationEntity savedNotification = notificationRepository.save(notification); | ||
|
||
return new RegisterNotificationResponseDto( | ||
savedNotification.getId(), | ||
savedNotification.getGeneration(), | ||
savedNotification.getEmail(), | ||
savedNotification.getCreatedAt() | ||
); | ||
} | ||
|
||
public GetNotificationListResponseDto getNotificationEmailList(int generation) { | ||
List<String> emailList = notificationRepository.findByGeneration(generation).stream() | ||
.map(NotificationEntity::getEmail) | ||
.toList(); | ||
|
||
return new GetNotificationListResponseDto(generation, emailList); | ||
} | ||
|
||
} |