diff --git a/src/main/java/com/example/parking/api/announcement/AnnouncementController.java b/src/main/java/com/example/parking/api/announcement/AnnouncementController.java new file mode 100644 index 00000000..19f1db26 --- /dev/null +++ b/src/main/java/com/example/parking/api/announcement/AnnouncementController.java @@ -0,0 +1,28 @@ +package com.example.parking.api.announcement; + +import com.example.parking.domain.announcement.Announcement; +import com.example.parking.domain.announcement.AnnouncementRepository; +import com.example.parking.domain.announcement.AnnouncementType; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class AnnouncementController { + + private final AnnouncementRepository announcementRepository; + + public AnnouncementController(AnnouncementRepository announcementRepository) { + this.announcementRepository = announcementRepository; + } + + @GetMapping("/announcements") + public ResponseEntity> findAnnouncements(@RequestParam String type, Pageable pageable) { + AnnouncementType announcementType = AnnouncementType.findType(type); + Page announcements = announcementRepository.findAllByAnnouncementType(announcementType, pageable); + return ResponseEntity.ok(announcements); + } +} diff --git a/src/main/java/com/example/parking/domain/announcement/Announcement.java b/src/main/java/com/example/parking/domain/announcement/Announcement.java new file mode 100644 index 00000000..9f4ece97 --- /dev/null +++ b/src/main/java/com/example/parking/domain/announcement/Announcement.java @@ -0,0 +1,37 @@ +package com.example.parking.domain.announcement; + +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Getter +@Entity +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Announcement { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String title; + private String fileName; + private LocalDateTime createdAt; + + @Enumerated(EnumType.STRING) + private AnnouncementType announcementType; + + public Announcement(String title, String fileName, LocalDateTime createdAt, AnnouncementType announcementType) { + this.title = title; + this.fileName = fileName; + this.createdAt = createdAt; + this.announcementType = announcementType; + } +} diff --git a/src/main/java/com/example/parking/domain/announcement/AnnouncementRepository.java b/src/main/java/com/example/parking/domain/announcement/AnnouncementRepository.java new file mode 100644 index 00000000..4191cf9e --- /dev/null +++ b/src/main/java/com/example/parking/domain/announcement/AnnouncementRepository.java @@ -0,0 +1,10 @@ +package com.example.parking.domain.announcement; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface AnnouncementRepository extends JpaRepository { + + Page findAllByAnnouncementType(AnnouncementType announcementType, Pageable pageable); +} diff --git a/src/main/java/com/example/parking/domain/announcement/AnnouncementType.java b/src/main/java/com/example/parking/domain/announcement/AnnouncementType.java new file mode 100644 index 00000000..a466a510 --- /dev/null +++ b/src/main/java/com/example/parking/domain/announcement/AnnouncementType.java @@ -0,0 +1,28 @@ +package com.example.parking.domain.announcement; + +import com.example.parking.application.member.dto.MemberNotFoundException; + +import java.util.Arrays; + +public enum AnnouncementType { + + NOTICE("notice"), + EVENT("event"); + + private final String type; + + AnnouncementType(String type) { + this.type = type; + } + + public static AnnouncementType findType(String type) { + return Arrays.stream(AnnouncementType.values()) + .filter(announcementType -> announcementType.getType().equals(type)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("찾을 수 없는 타입")); + } + + public String getType() { + return type; + } +}