Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import gotcha_domain.user.User;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand Down Expand Up @@ -31,15 +33,19 @@ public class Notification extends BaseTimeEntity {
@Column(columnDefinition = "TEXT")
private String content;

@Enumerated(EnumType.STRING)
private NotificationType type;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User writer;

@Builder
public Notification(String title, String content, User writer){
public Notification(String title, String content, User writer, NotificationType type){
this.title = title;
this.content = content;
this.writer = writer;
this.type = type;
}

public void update(NotificationReq req){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public Notification toEntity(User writer) {
title(title).
content(content).
writer(writer).
type(NotificationType.UPDATE).
build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package gotcha_domain.notification;

public enum NotificationType {
EVENT, UPDATE
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import Gotcha.domain.notification.dto.NotificationSortType;
import gotcha_domain.notification.NotificationType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
Expand All @@ -11,12 +12,21 @@
import jakarta.validation.constraints.Min;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.web.bind.annotation.RequestParam;

@Tag(name = "[공지사항 API]", description = "공지사항 관련 API")
public interface NotificationApi {

@Operation(summary = "공지사항 목록", description = "공지사항 목록을 조회하는 API. Keyword로 검색 및 정렬 가능.")
@Operation(summary = "공지사항 목록", description = "공지사항 목록을 조회하는 API. Keyword, Type으로 검색 및 정렬 가능.",
parameters = {
@Parameter(name = "keyword", description = "검색할 제목 키워드", example = "점검"),
@Parameter(name = "type", description = "알림 타입", schema = @Schema(implementation = NotificationType.class), example = "UPDATE"),
@Parameter(name = "page", description = "페이지 번호 (0부터 시작)", example = "0"),
@Parameter(name = "sort", description = "정렬 방식", schema = @Schema(implementation = NotificationSortType.class), example = "DATE_DESC")
}
)
@ApiResponses({
@ApiResponse(
responseCode = "200", description = "공지사항 목록 조회 성공",
Expand Down Expand Up @@ -74,9 +84,10 @@ public interface NotificationApi {
)

})
ResponseEntity<?> getNotifications(@RequestParam(value = "keyword", required = false) String keyword,
@RequestParam(value = "page",defaultValue = "0") @Min(0) Integer page,
@RequestParam(value = "sort", defaultValue = "DATE_DESC") NotificationSortType sort);
public ResponseEntity<?> getNotifications(@RequestParam(value = "keyword", required = false) String keyword,
@RequestParam(value = "type", required = false) NotificationType type,
@RequestParam(value = "page", defaultValue = "0") @Min(0) Integer page,
@RequestParam(value = "sort", defaultValue = "DATE_DESC") NotificationSortType sort);



Expand All @@ -89,6 +100,7 @@ ResponseEntity<?> getNotifications(@RequestParam(value = "keyword", required = f
{
"title": "걍 공지사항이다",
"content": "걍 공지사항이다 인마",
"type": "EVENT",
"createdAt": "2025-03-27T16:13:32",
"modifiedAt": "2025-11-16T16:13:32",
"writer": "묘묘"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
import Gotcha.domain.notification.dto.NotificationSortType;
import Gotcha.domain.notification.dto.NotificationSummaryRes;
import Gotcha.domain.notification.service.NotificationService;
import gotcha_domain.notification.NotificationType;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
Expand All @@ -23,9 +28,10 @@ public class NotificationController implements NotificationApi {
@Override
@GetMapping
public ResponseEntity<?> getNotifications(@RequestParam(value = "keyword", required = false) String keyword,
@RequestParam(value = "page", defaultValue = "0") @Min(0) Integer page,
@RequestParam(value = "sort", defaultValue = "DATE_DESC") NotificationSortType sort){
Page<NotificationSummaryRes> notifications = notificationService.getNotifications(keyword, page, sort);
@RequestParam(value = "type", required = false) NotificationType type,
@RequestParam(value = "page", defaultValue = "0") @Min(0) Integer page,
@RequestParam(value = "sort", defaultValue = "DATE_DESC") NotificationSortType sort) {
Page<NotificationSummaryRes> notifications = notificationService.getNotifications(keyword, type, page, sort);

return ResponseEntity.status(HttpStatus.OK).body(notifications);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@


import gotcha_domain.notification.Notification;
import gotcha_domain.notification.NotificationType;

import java.time.LocalDateTime;

public record NotificationRes(
String title,
String content,
NotificationType type,
LocalDateTime createdAt,
LocalDateTime modifiedAt,
String writer
Expand All @@ -16,6 +18,7 @@ public static NotificationRes fromEntity(Notification noti) {
return new NotificationRes(
noti.getTitle(),
noti.getContent(),
noti.getType(),
noti.getCreatedAt(),
noti.getModifiedAt(),
noti.getWriter().getNickname()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package Gotcha.domain.notification.repository;

import gotcha_domain.notification.Notification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.domain.Pageable;
import gotcha_domain.notification.NotificationType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface NotificationRepository extends JpaRepository<Notification, Long> {
Page<Notification> findByTitleContainingIgnoreCase(String keyword, Pageable pageable);
Page<Notification> findByTypeAndTitleContainingIgnoreCase(NotificationType type, String keyword, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import Gotcha.domain.notification.repository.NotificationRepository;
import gotcha_common.exception.CustomException;
import gotcha_domain.notification.Notification;
import gotcha_domain.notification.NotificationType;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -25,10 +26,18 @@ public class NotificationService {


@Transactional(readOnly = true)
public Page<NotificationSummaryRes> getNotifications(String keyword, Integer page, NotificationSortType sort){
public Page<NotificationSummaryRes> getNotifications(String keyword, NotificationType type, Integer page, NotificationSortType sort){
Pageable pageable = PageRequest.of(page, NOTIS_PER_PAGE, sort.getSort());

Page<Notification> notifications = notificationRepository.findByTitleContainingIgnoreCase(keyword, pageable);
Page<Notification> notifications;
if (keyword == null) keyword = "";
if (type != null) {
notifications = notificationRepository
.findByTypeAndTitleContainingIgnoreCase(type, keyword, pageable);
} else {
notifications = notificationRepository
.findByTitleContainingIgnoreCase(keyword, pageable);
}

return notifications.map(NotificationSummaryRes::fromEntity);
}
Expand Down
Loading