Skip to content

Commit b069f0a

Browse files
authored
feat: 공지사항 타입 추가 (#117)
* feat: 공지사항 타입 추가 * feat: 공지사항 타입 추가 * feat: 공지사항 타입 별 조회 로직 추가 * docs: 공지사항 조회 Swagger 수정 * feat: 임시 로직 추가 * feat: 키워드 Null 처리 추가
1 parent d164fa0 commit b069f0a

8 files changed

Lines changed: 57 additions & 13 deletions

File tree

gotcha-domain/src/main/java/gotcha_domain/notification/Notification.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import gotcha_domain.user.User;
55
import jakarta.persistence.Column;
66
import jakarta.persistence.Entity;
7+
import jakarta.persistence.EnumType;
8+
import jakarta.persistence.Enumerated;
79
import jakarta.persistence.FetchType;
810
import jakarta.persistence.GeneratedValue;
911
import jakarta.persistence.GenerationType;
@@ -31,15 +33,19 @@ public class Notification extends BaseTimeEntity {
3133
@Column(columnDefinition = "TEXT")
3234
private String content;
3335

36+
@Enumerated(EnumType.STRING)
37+
private NotificationType type;
38+
3439
@ManyToOne(fetch = FetchType.LAZY)
3540
@JoinColumn(name = "user_id")
3641
private User writer;
3742

3843
@Builder
39-
public Notification(String title, String content, User writer){
44+
public Notification(String title, String content, User writer, NotificationType type){
4045
this.title = title;
4146
this.content = content;
4247
this.writer = writer;
48+
this.type = type;
4349
}
4450

4551
public void update(NotificationReq req){

gotcha-domain/src/main/java/gotcha_domain/notification/NotificationReq.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public Notification toEntity(User writer) {
1818
title(title).
1919
content(content).
2020
writer(writer).
21+
type(NotificationType.UPDATE).
2122
build();
2223
}
2324
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package gotcha_domain.notification;
2+
3+
public enum NotificationType {
4+
EVENT, UPDATE
5+
}

gotcha/src/main/java/Gotcha/domain/notification/api/NotificationApi.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import Gotcha.domain.notification.dto.NotificationSortType;
5+
import gotcha_domain.notification.NotificationType;
56
import io.swagger.v3.oas.annotations.Operation;
67
import io.swagger.v3.oas.annotations.media.Content;
78
import io.swagger.v3.oas.annotations.media.ExampleObject;
@@ -11,12 +12,21 @@
1112
import jakarta.validation.constraints.Min;
1213
import org.springframework.http.ResponseEntity;
1314
import org.springframework.web.bind.annotation.PathVariable;
15+
import io.swagger.v3.oas.annotations.Parameter;
16+
import io.swagger.v3.oas.annotations.media.Schema;
1417
import org.springframework.web.bind.annotation.RequestParam;
1518

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

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

7686
})
77-
ResponseEntity<?> getNotifications(@RequestParam(value = "keyword", required = false) String keyword,
78-
@RequestParam(value = "page",defaultValue = "0") @Min(0) Integer page,
79-
@RequestParam(value = "sort", defaultValue = "DATE_DESC") NotificationSortType sort);
87+
public ResponseEntity<?> getNotifications(@RequestParam(value = "keyword", required = false) String keyword,
88+
@RequestParam(value = "type", required = false) NotificationType type,
89+
@RequestParam(value = "page", defaultValue = "0") @Min(0) Integer page,
90+
@RequestParam(value = "sort", defaultValue = "DATE_DESC") NotificationSortType sort);
8091

8192

8293

@@ -89,6 +100,7 @@ ResponseEntity<?> getNotifications(@RequestParam(value = "keyword", required = f
89100
{
90101
"title": "걍 공지사항이다",
91102
"content": "걍 공지사항이다 인마",
103+
"type": "EVENT",
92104
"createdAt": "2025-03-27T16:13:32",
93105
"modifiedAt": "2025-11-16T16:13:32",
94106
"writer": "묘묘"

gotcha/src/main/java/Gotcha/domain/notification/controller/NotificationController.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
import Gotcha.domain.notification.dto.NotificationSortType;
66
import Gotcha.domain.notification.dto.NotificationSummaryRes;
77
import Gotcha.domain.notification.service.NotificationService;
8+
import gotcha_domain.notification.NotificationType;
89
import jakarta.validation.constraints.Min;
910
import lombok.RequiredArgsConstructor;
1011
import org.springframework.data.domain.Page;
1112
import org.springframework.http.HttpStatus;
1213
import org.springframework.http.ResponseEntity;
13-
import org.springframework.web.bind.annotation.*;
14+
import org.springframework.web.bind.annotation.GetMapping;
15+
import org.springframework.web.bind.annotation.PathVariable;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RequestParam;
18+
import org.springframework.web.bind.annotation.RestController;
1419

1520
@RestController
1621
@RequiredArgsConstructor
@@ -23,9 +28,10 @@ public class NotificationController implements NotificationApi {
2328
@Override
2429
@GetMapping
2530
public ResponseEntity<?> getNotifications(@RequestParam(value = "keyword", required = false) String keyword,
26-
@RequestParam(value = "page", defaultValue = "0") @Min(0) Integer page,
27-
@RequestParam(value = "sort", defaultValue = "DATE_DESC") NotificationSortType sort){
28-
Page<NotificationSummaryRes> notifications = notificationService.getNotifications(keyword, page, sort);
31+
@RequestParam(value = "type", required = false) NotificationType type,
32+
@RequestParam(value = "page", defaultValue = "0") @Min(0) Integer page,
33+
@RequestParam(value = "sort", defaultValue = "DATE_DESC") NotificationSortType sort) {
34+
Page<NotificationSummaryRes> notifications = notificationService.getNotifications(keyword, type, page, sort);
2935

3036
return ResponseEntity.status(HttpStatus.OK).body(notifications);
3137
}

gotcha/src/main/java/Gotcha/domain/notification/dto/NotificationRes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33

44
import gotcha_domain.notification.Notification;
5+
import gotcha_domain.notification.NotificationType;
56

67
import java.time.LocalDateTime;
78

89
public record NotificationRes(
910
String title,
1011
String content,
12+
NotificationType type,
1113
LocalDateTime createdAt,
1214
LocalDateTime modifiedAt,
1315
String writer
@@ -16,6 +18,7 @@ public static NotificationRes fromEntity(Notification noti) {
1618
return new NotificationRes(
1719
noti.getTitle(),
1820
noti.getContent(),
21+
noti.getType(),
1922
noti.getCreatedAt(),
2023
noti.getModifiedAt(),
2124
noti.getWriter().getNickname()
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package Gotcha.domain.notification.repository;
22

33
import gotcha_domain.notification.Notification;
4-
import org.springframework.data.jpa.repository.JpaRepository;
5-
import org.springframework.data.domain.Pageable;
4+
import gotcha_domain.notification.NotificationType;
65
import org.springframework.data.domain.Page;
6+
import org.springframework.data.domain.Pageable;
7+
import org.springframework.data.jpa.repository.JpaRepository;
78

89
public interface NotificationRepository extends JpaRepository<Notification, Long> {
910
Page<Notification> findByTitleContainingIgnoreCase(String keyword, Pageable pageable);
11+
Page<Notification> findByTypeAndTitleContainingIgnoreCase(NotificationType type, String keyword, Pageable pageable);
1012
}

gotcha/src/main/java/Gotcha/domain/notification/service/NotificationService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import Gotcha.domain.notification.repository.NotificationRepository;
88
import gotcha_common.exception.CustomException;
99
import gotcha_domain.notification.Notification;
10+
import gotcha_domain.notification.NotificationType;
1011
import lombok.RequiredArgsConstructor;
1112
import org.springframework.data.domain.Page;
1213
import org.springframework.data.domain.PageRequest;
@@ -25,10 +26,18 @@ public class NotificationService {
2526

2627

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

31-
Page<Notification> notifications = notificationRepository.findByTitleContainingIgnoreCase(keyword, pageable);
32+
Page<Notification> notifications;
33+
if (keyword == null) keyword = "";
34+
if (type != null) {
35+
notifications = notificationRepository
36+
.findByTypeAndTitleContainingIgnoreCase(type, keyword, pageable);
37+
} else {
38+
notifications = notificationRepository
39+
.findByTitleContainingIgnoreCase(keyword, pageable);
40+
}
3241

3342
return notifications.map(NotificationSummaryRes::fromEntity);
3443
}

0 commit comments

Comments
 (0)