Skip to content

Commit

Permalink
Merge pull request #4 from sopt-makers/feat/#3-notification-api-migra…
Browse files Browse the repository at this point in the history
…tion

[Feat]: Notification 모집알림 기능 v2 API 마이그레이션 작업
  • Loading branch information
Lim-Changi authored Oct 16, 2024
2 parents bc5538e + c73a078 commit 1dd0cb2
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.server.ResponseStatusException;

@Slf4j
@RestControllerAdvice
Expand All @@ -16,7 +17,7 @@ public class GlobalExceptionHandler {
public ResponseEntity<String> businessLogicException (BusinessLogicException ex) {
log.error(ex.getMessage());
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ex.getMessage());
}

Expand All @@ -32,7 +33,7 @@ public ResponseEntity<String> methodArgumentNotValidException (MethodArgumentNot
public ResponseEntity<String> entityNotfoundException (EntityNotFoundException ex) {
log.error(ex.getMessage());
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.status(HttpStatus.NOT_FOUND)
.body(ex.getMessage());
}

Expand Down Expand Up @@ -69,4 +70,11 @@ public ResponseEntity<String> unknownException (RuntimeException ex) {
.body(ex.getMessage());
}

@ExceptionHandler(ResponseStatusException.class)
public ResponseEntity<String> handleResponseStatusException(ResponseStatusException ex) {
log.error(ex.getMessage());
return ResponseEntity
.status(ex.getStatusCode())
.body(ex.getReason() != null ? ex.getReason() : "Unknown error occurred");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sopt.org.homepage.notification;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import sopt.org.homepage.notification.dto.GetNotificationListRequestDto;
import sopt.org.homepage.notification.dto.GetNotificationListResponseDto;
import sopt.org.homepage.notification.dto.RegisterNotificationRequestDto;
import sopt.org.homepage.notification.dto.RegisterNotificationResponseDto;

@RestController
@RequiredArgsConstructor
@RequestMapping("notification")
@Tag(name = "Notification")
public class NotificationController {
private final NotificationService notificationService;

@PostMapping("register")
public ResponseEntity<RegisterNotificationResponseDto> registerNotification (
@ParameterObject @ModelAttribute RegisterNotificationRequestDto registerNotificationRequestDto
) {
RegisterNotificationResponseDto result = notificationService.registerNotification(registerNotificationRequestDto);
return ResponseEntity.status(HttpStatus.CREATED).body(result);
}
@GetMapping("/list")
public ResponseEntity<GetNotificationListResponseDto> getAllProject (
@ParameterObject @ModelAttribute GetNotificationListRequestDto getNotificationListRequestDto
) {
GetNotificationListResponseDto result = notificationService.getNotificationEmailList(getNotificationListRequestDto.getGeneration());
return ResponseEntity.status(HttpStatus.OK).body(result);
}
}


Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package sopt.org.homepage.entity;
package sopt.org.homepage.notification;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

import java.sql.Timestamp;
import java.util.Objects;

@Entity
@Getter
@Setter
@Table(name = "\"Notification\"")
public class NotificationEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sopt.org.homepage.notification;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface NotificationRepository extends JpaRepository<NotificationEntity, Integer> {
NotificationEntity findByEmailAndGeneration(String email, int generation);

List<NotificationEntity> findByGeneration(int generation);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package sopt.org.homepage.notification;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
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)
.collect(Collectors.toList());

return new GetNotificationListResponseDto(generation, emailList);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sopt.org.homepage.notification.dto;

import io.swagger.v3.oas.annotations.Parameter;
import lombok.Getter;
import org.springframework.validation.annotation.Validated;

@Validated
@Getter
public class GetNotificationListRequestDto {

@Parameter(description = "기수")
private final Integer generation;

public GetNotificationListRequestDto(Integer generation) {
this.generation = generation;

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sopt.org.homepage.notification.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import org.springframework.validation.annotation.Validated;

import java.util.List;

@Validated
@Getter
public class GetNotificationListResponseDto {

@Schema(description = "기수", example = "34", requiredMode = Schema.RequiredMode.REQUIRED)
private final Integer generation;

@Schema(description = "모집알림 신청한 이메일 리스트", example = "[\"[email protected]\", \"[email protected]\", \"[email protected]\"]", required = true)
private final List<String> emailList;

public GetNotificationListResponseDto(Integer generation, List<String> emailList) {
this.generation = generation;
this.emailList = emailList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package sopt.org.homepage.notification.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Positive;
import lombok.Getter;
import org.springframework.validation.annotation.Validated;

@Validated
@Getter
public class RegisterNotificationRequestDto {

@Schema(description = "활동 기수", requiredMode = Schema.RequiredMode.REQUIRED, example = "34")
@NotEmpty(message = "Generation must not be empty")
@Positive(message = "Generation must be a positive number")
private final int generation;

@Schema(description = "이메일", requiredMode = Schema.RequiredMode.REQUIRED, example = "[email protected]")
@NotEmpty(message = "Email must not be empty")
@Email(message = "Email should be valid")
private final String email;

public RegisterNotificationRequestDto(int generation, String email) {
this.generation = generation;
this.email = email;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package sopt.org.homepage.notification.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Positive;
import lombok.Getter;
import org.springframework.validation.annotation.Validated;

import java.util.Date;

@Validated
@Getter
public class RegisterNotificationResponseDto {

@Schema(description = "Notification ID", requiredMode = Schema.RequiredMode.REQUIRED)
private final int id;

@Schema(description = "기수 (Generation)", requiredMode = Schema.RequiredMode.REQUIRED)
private final int generation;

@Schema(description = "이메일 (Email)", requiredMode = Schema.RequiredMode.REQUIRED)
private final String email;

@Schema(description = "생성일자 (Creation Date)", requiredMode = Schema.RequiredMode.REQUIRED)
private final Date createdAt;

public RegisterNotificationResponseDto(int id, int generation, String email, Date createdAt) {
this.id = id;
this.generation = generation;
this.email = email;
this.createdAt = createdAt;
}
}

0 comments on commit 1dd0cb2

Please sign in to comment.