Skip to content

Commit

Permalink
Merge pull request #121 from 9uttery/feat/notice-#120
Browse files Browse the repository at this point in the history
[Feature] 공지사항 API 구현
  • Loading branch information
mingeun0507 authored Feb 20, 2024
2 parents 7800ca8 + 4a09606 commit 4338dca
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public class SecurityConstant {
"/users/id-check/**",
"/users/refresh/**",
"/joy/today/**",
"/albums/all/**"
"/albums/all/**",
"/notices"

// TODO: 인증 필요 없는 API 엔드포인트 추가
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.guttery.madii.domain.notice.application.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;

@Schema(description = "공지사항 생성 요청")
public record CreateNoticeRequest(
@NotBlank
@Schema(description = "공지사항 제목", example = "공지사항 제목")
String title,
@NotBlank
@Schema(description = "공지사항 내용", example = "공지사항 내용")
String contents
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.guttery.madii.domain.notice.application.dto;

import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDateTime;

@Schema(description = "공지사항 정보")
public record NoticeInfo(
@Schema(description = "공지사항 ID", example = "1")
Long id,
@Schema(description = "공지사항 제목", example = "공지사항 제목")
String title,
@Schema(description = "공지사항 내용", example = "공지사항 내용")
String contents,
@Schema(description = "공지사항 작성일자", example = "2021-10-01")
LocalDateTime createdAt
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.guttery.madii.domain.notice.application.dto;

import java.util.List;

public record RecentNoticesResponse(
List<NoticeInfo> notices
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.guttery.madii.domain.notice.application.service;

import com.guttery.madii.domain.notice.application.dto.CreateNoticeRequest;
import com.guttery.madii.domain.notice.application.dto.RecentNoticesResponse;
import com.guttery.madii.domain.notice.domain.model.Notice;
import com.guttery.madii.domain.notice.domain.repository.NoticeRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@RequiredArgsConstructor
@Slf4j
@Service
public class NoticeService {
private final NoticeRepository noticeRepository;

public RecentNoticesResponse getRecentNotices() {
return noticeRepository.findRecent30DaysNotices(LocalDateTime.now());
}

public void createNotice(final CreateNoticeRequest createNoticeRequest) {
final Notice newNotice = Notice.create(createNoticeRequest.title(), createNoticeRequest.contents());
noticeRepository.save(newNotice);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.guttery.madii.domain.notice.domain.model;

import com.guttery.madii.common.domain.model.BaseTimeEntity;
import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "t_notice")
@Access(AccessType.FIELD)
public class Notice extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long noticeId;
private String title;
@Column(name = "contents", columnDefinition = "TEXT")
private String contents;

public Notice(String title, String contents) {
this.title = title;
this.contents = contents;
}

public static Notice create(String title, String contents) {
return new Notice(title, contents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.guttery.madii.domain.notice.domain.repository;

import com.guttery.madii.domain.notice.application.dto.RecentNoticesResponse;

import java.time.LocalDateTime;

public interface NoticeQuerydslRepository {
RecentNoticesResponse findRecent30DaysNotices(LocalDateTime date);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.guttery.madii.domain.notice.domain.repository;

import com.guttery.madii.domain.notice.domain.model.Notice;
import org.springframework.data.jpa.repository.JpaRepository;

public interface NoticeRepository extends JpaRepository<Notice, Long>, NoticeQuerydslRepository {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.guttery.madii.domain.notice.infrastructure;

import com.guttery.madii.common.domain.repository.BaseQueryDslRepository;
import com.guttery.madii.domain.notice.application.dto.NoticeInfo;
import com.guttery.madii.domain.notice.application.dto.RecentNoticesResponse;
import com.guttery.madii.domain.notice.domain.model.Notice;
import com.guttery.madii.domain.notice.domain.repository.NoticeQuerydslRepository;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

import static com.guttery.madii.domain.notice.domain.model.QNotice.notice;

@Repository
public class NoticeRepositoryImpl extends BaseQueryDslRepository<Notice> implements NoticeQuerydslRepository {
public NoticeRepositoryImpl(JPAQueryFactory queryFactory) {
super(queryFactory);
}

@Override
public RecentNoticesResponse findRecent30DaysNotices(final LocalDateTime date) {
final LocalDateTime startOfDay = date.toLocalDate().atStartOfDay();
final List<NoticeInfo> foundNotices = select(NoticeInfo.class, notice.noticeId, notice.title, notice.contents, notice.createdAt)
.from(notice)
.where(notice.createdAt.after(startOfDay.minusDays(30)))
.fetch();

return new RecentNoticesResponse(foundNotices);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.guttery.madii.domain.notice.presentation;

import com.guttery.madii.domain.notice.application.dto.CreateNoticeRequest;
import com.guttery.madii.domain.notice.application.dto.RecentNoticesResponse;
import com.guttery.madii.domain.notice.application.service.NoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
@RequestMapping("/notices")
@Validated
@Tag(name = "Notice", description = "공지사항 관련 API")
public class NoticeController {
private final NoticeService noticeService;

@GetMapping
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "최근 30일간 공지사항 조회 성공",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "최근 30일간 공지사항 조회 API", description = "최근 30일간 공지사항 조회 API입니다.")
public RecentNoticesResponse getRecentNotices() {
return noticeService.getRecentNotices();
}

@PostMapping
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "공지사항 생성 성공",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "공지사항 생성 API", description = "테스트용 공지사항 생성 API입니다.")
public void createNotice(@Valid @RequestBody CreateNoticeRequest createNoticeRequest) {
noticeService.createNotice(createNoticeRequest);
}
}

0 comments on commit 4338dca

Please sign in to comment.