Skip to content

Commit

Permalink
feat: 공지사항, 이벤트 전체 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
jundonghyuk committed Mar 7, 2024
1 parent 12cd899 commit 660dd6e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Page<Announcement>> findAnnouncements(@RequestParam String type, Pageable pageable) {
AnnouncementType announcementType = AnnouncementType.findType(type);
Page<Announcement> announcements = announcementRepository.findAllByAnnouncementType(announcementType, pageable);
return ResponseEntity.ok(announcements);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Announcement, Long> {

Page<Announcement> findAllByAnnouncementType(AnnouncementType announcementType, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 660dd6e

Please sign in to comment.