-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from cvs-go/feature#85
공지사항 조회 기능 추가
- Loading branch information
Showing
15 changed files
with
554 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.cvsgo.controller; | ||
|
||
import com.cvsgo.dto.SuccessResponse; | ||
import com.cvsgo.dto.notice.ReadNoticeDetailResponseDto; | ||
import com.cvsgo.dto.notice.ReadNoticeResponseDto; | ||
import com.cvsgo.service.NoticeService; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
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.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/notices") | ||
public class NoticeController { | ||
|
||
private final NoticeService noticeService; | ||
|
||
@GetMapping | ||
public SuccessResponse<Page<ReadNoticeResponseDto>> readNoticeList(Pageable pageable) { | ||
return SuccessResponse.from(noticeService.readNoticeList(pageable)); | ||
} | ||
|
||
@GetMapping("/{noticeId}") | ||
public SuccessResponse<ReadNoticeDetailResponseDto> readNotice(@PathVariable Long noticeId) { | ||
return SuccessResponse.from(noticeService.readNotice(noticeId)); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/cvsgo/dto/notice/ReadNoticeDetailResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.cvsgo.dto.notice; | ||
|
||
import com.cvsgo.entity.Notice; | ||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ReadNoticeDetailResponseDto { | ||
|
||
private final Long id; | ||
|
||
private final String title; | ||
|
||
private final String content; | ||
|
||
private final List<String> noticeImageUrls; | ||
|
||
@JsonFormat(pattern = "yy.MM.dd", timezone = "Asia/Seoul") | ||
private final LocalDateTime createdAt; | ||
|
||
public ReadNoticeDetailResponseDto(Notice notice, List<String> noticeImageUrls) { | ||
this.id = notice.getId(); | ||
this.title = notice.getTitle(); | ||
this.content = notice.getContent(); | ||
this.noticeImageUrls = noticeImageUrls; | ||
this.createdAt = notice.getCreatedAt(); | ||
} | ||
|
||
public static ReadNoticeDetailResponseDto of(Notice notice, List<String> noticeImageUrls) { | ||
return new ReadNoticeDetailResponseDto(notice, noticeImageUrls); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/cvsgo/dto/notice/ReadNoticeResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.cvsgo.dto.notice; | ||
|
||
import com.cvsgo.entity.Notice; | ||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.temporal.ChronoUnit; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ReadNoticeResponseDto { | ||
|
||
private final Long id; | ||
|
||
private final String title; | ||
|
||
@JsonFormat(pattern = "yy.MM.dd", timezone = "Asia/Seoul") | ||
private final LocalDateTime createdAt; | ||
|
||
private final boolean isNew; | ||
|
||
public ReadNoticeResponseDto(Notice notice) { | ||
this.id = notice.getId(); | ||
this.title = notice.getTitle(); | ||
this.createdAt = notice.getCreatedAt(); | ||
this.isNew = notice.getCreatedAt() != null | ||
&& ChronoUnit.DAYS.between(notice.getCreatedAt().toLocalDate(), LocalDate.now()) <= 7; | ||
} | ||
|
||
public static ReadNoticeResponseDto from(Notice notice) { | ||
return new ReadNoticeResponseDto(notice); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.cvsgo.entity; | ||
|
||
import jakarta.persistence.CascadeType; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.OneToMany; | ||
import jakarta.validation.constraints.NotNull; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Entity | ||
public class Notice extends BaseTimeEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@NotNull | ||
private String title; | ||
|
||
@NotNull | ||
private String content; | ||
|
||
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL, orphanRemoval = true) | ||
private final List<NoticeImage> noticeImages = new ArrayList<>(); | ||
|
||
public void addImage(String imageUrl) { | ||
NoticeImage noticeImage = NoticeImage.builder() | ||
.notice(this) | ||
.imageUrl(imageUrl) | ||
.build(); | ||
noticeImages.add(noticeImage); | ||
} | ||
|
||
@Builder | ||
public Notice(Long id, String title, String content, List<String> imageUrls) { | ||
this.id = id; | ||
this.title = title; | ||
this.content = content; | ||
if (imageUrls != null) { | ||
for (String imageUrl : imageUrls) { | ||
addImage(imageUrl); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.cvsgo.entity; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Entity | ||
public class NoticeImage { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String imageUrl; | ||
|
||
@NotNull | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "notice_id") | ||
private Notice notice; | ||
|
||
@Builder | ||
public NoticeImage(Long id, String imageUrl, Notice notice) { | ||
this.id = id; | ||
this.imageUrl = imageUrl; | ||
this.notice = notice; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/cvsgo/repository/NoticeImageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.cvsgo.repository; | ||
|
||
import com.cvsgo.entity.Notice; | ||
import com.cvsgo.entity.NoticeImage; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface NoticeImageRepository extends JpaRepository<NoticeImage, Long> { | ||
|
||
List<NoticeImage> findByNotice(Notice notice); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.cvsgo.repository; | ||
|
||
import com.cvsgo.entity.Notice; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface NoticeRepository extends JpaRepository<Notice, Long> { | ||
|
||
Page<Notice> findAllByOrderByCreatedAtDesc(Pageable pageable); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.cvsgo.service; | ||
|
||
import static com.cvsgo.exception.ExceptionConstants.NOT_FOUND_NOTICE; | ||
|
||
import com.cvsgo.dto.notice.ReadNoticeDetailResponseDto; | ||
import com.cvsgo.dto.notice.ReadNoticeResponseDto; | ||
import com.cvsgo.entity.Notice; | ||
import com.cvsgo.entity.NoticeImage; | ||
import com.cvsgo.exception.NotFoundException; | ||
import com.cvsgo.repository.NoticeImageRepository; | ||
import com.cvsgo.repository.NoticeRepository; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class NoticeService { | ||
|
||
private final NoticeRepository noticeRepository; | ||
private final NoticeImageRepository noticeImageRepository; | ||
|
||
/** | ||
* 공지사항 목록을 조회한다. | ||
* | ||
* @return 공지사항 목록 | ||
*/ | ||
@Transactional(readOnly = true) | ||
public Page<ReadNoticeResponseDto> readNoticeList(Pageable pageable) { | ||
return noticeRepository.findAllByOrderByCreatedAtDesc(pageable) | ||
.map(ReadNoticeResponseDto::from); | ||
} | ||
|
||
/** | ||
* 공지사항을 상세 조회한다. | ||
* | ||
* @param noticeId 공지사항 ID | ||
* @return 공지사항 상세 정보 | ||
* @throws NotFoundException 해당하는 아이디를 가진 공지사항이 없는 경우 | ||
*/ | ||
@Transactional(readOnly = true) | ||
public ReadNoticeDetailResponseDto readNotice(Long noticeId) { | ||
Notice notice = noticeRepository.findById(noticeId).orElseThrow(() -> NOT_FOUND_NOTICE); | ||
List<String> noticeImages = noticeImageRepository.findByNotice(notice).stream() | ||
.map(NoticeImage::getImageUrl).toList(); | ||
|
||
return ReadNoticeDetailResponseDto.of(notice, noticeImages); | ||
} | ||
|
||
} |
Oops, something went wrong.