Skip to content

Commit

Permalink
fix: 알림 등록 API Controller Request Body 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Lim-Changi committed Jan 25, 2025
1 parent 5580e3a commit 1560d1d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class NotificationController {

@PostMapping("register")
public ResponseEntity<RegisterNotificationResponseDto> registerNotification(
@Valid @RequestBody @ModelAttribute RegisterNotificationRequestDto registerNotificationRequestDto
@Valid @RequestBody RegisterNotificationRequestDto registerNotificationRequestDto
) {
RegisterNotificationResponseDto result = notificationService.registerNotification(
registerNotificationRequestDto);
Expand Down
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);
}

}

0 comments on commit 1560d1d

Please sign in to comment.