Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Gallery 관련 API 구현 완료 (#10) #64

Merged
merged 39 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0016766
feat: create gallery api 틀 세팅
yesjuhee Jul 25, 2024
3c1bc53
feat: Galley cotroller test 작성, restdocs 작성
yesjuhee Jul 25, 2024
d23284a
feat: createGallery 서비스 구현
yesjuhee Jul 25, 2024
53d7a48
feat: 로그에서 SQL 쿼리 확인할 수 있도록 수정
yesjuhee Jul 25, 2024
33a9179
feat: getGalleries api 구현
simta1 Jul 26, 2024
293a40a
fix: 도메인에서 DTO 사용하는 케이스 수정
yesjuhee Jul 26, 2024
c6e2690
feat: 트랜잭션 설정 추가
simta1 Jul 27, 2024
98038c4
feat: restdocs 작성
simta1 Jul 27, 2024
80a28df
Merge pull request #22 from SystemConsultantGroup/feat/10-gallery-api/1
yesjuhee Jul 28, 2024
846f3c4
Merge branch 'feat/10-gallery-api/base' into feat/10-gallery-api/2
yesjuhee Jul 28, 2024
0d7192a
Merge branch 'feat/10-gallery-api/base' into feat/10-gallery-api/2
simta1 Jul 30, 2024
8c07064
feat: RestDocumentationRequestBuilders 사용
simta1 Jul 30, 2024
0e49e0c
Merge branch 'feat/10-gallery-api/2' of https://github.com/SystemCons…
simta1 Jul 30, 2024
a0c040b
feat: restdocs에 페이지네이션 파라미터 추가
simta1 Jul 30, 2024
8bdd768
feat: getGallery api 구현
simta1 Jul 30, 2024
bf49dfc
feat: getGallery test, restdocs 추가
simta1 Jul 30, 2024
c670eac
feat: deleteGallery api 구현
simta1 Jul 30, 2024
c34ce31
feat: deleteGallery test, restDocs 추가
simta1 Jul 30, 2024
22b34d2
feat: updateGallery api 구현, test, restDocs 추가
simta1 Jul 30, 2024
cc9cf7b
fix: dto에서 원시 타입 대신
simta1 Jul 30, 2024
a2464bc
Merge pull request #27 from SystemConsultantGroup/feat/10-gallery-api/2
simta1 Jul 31, 2024
8b02863
Merge branch 'feat/10-gallery-api/base' into feat/10-gallery-api/3
simta1 Jul 31, 2024
e7fcab0
feat: ExceptionCode 추가
simta1 Jul 31, 2024
0c563ad
refactor: GalleryRequest dto 하나로 통일
simta1 Jul 31, 2024
fa7d200
feat: content필드 삭제, 조회수 필드 추가
simta1 Aug 1, 2024
08da776
feat: 조회수 증가 로직 구현
simta1 Aug 1, 2024
5ee88a1
fix: getGallery 조회수 변경 반영되도록 수정
simta1 Aug 2, 2024
9f706d1
refactor: dto 생성 메서드 galleryResponse내부로 이동
simta1 Aug 2, 2024
ef66c6b
fix: deleteGallery작동하도록 cascade옵션 추가
simta1 Aug 2, 2024
bdda0f0
fix: createGallery에서 안 쓰는 파일 제거하도록 수정
simta1 Aug 2, 2024
8f9061a
Merge pull request #33 from SystemConsultantGroup/feat/10-gallery-api/3
simta1 Aug 5, 2024
37bc1fd
Merge branch 'develop' into feat/10-gallery-api/base
simta1 Aug 9, 2024
1a22bf3
feat: @authUser 추가
simta1 Aug 9, 2024
9ab3f59
fix: 충돌 나는 부분 머지 잘못한 거 수정
simta1 Aug 9, 2024
d80579c
feat: test에 auth 설정 추가
simta1 Aug 9, 2024
a36fa8b
refactor: 폴더 위치 이동
simta1 Aug 9, 2024
ffb8e21
feat: restdocs에 requestCookies, requestHeaders추가
simta1 Aug 11, 2024
36ea002
chore: application-local.yml 수정
simta1 Aug 13, 2024
17ae210
Merge pull request #59 from SystemConsultantGroup/feat/10-gallery-api/4
simta1 Aug 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/docs/asciidoc/gallery.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
== 갤러리 API
:source-highlighter: highlightjs

---
=== 갤러리 게시글 생성 (POST /galleries)
====
operation::gallery-controller-test/create-gallery[snippets="http-request,request-fields,http-response,response-fields"]
====

=== 갤러리 게시글 목록 조회 (GET /galleries)
====
operation::gallery-controller-test/get-galleries[snippets="http-request,query-parameters,http-response,response-fields"]
====

=== 갤러리 게시글 조회 (GET /galleries/{galleryId})
====
operation::gallery-controller-test/get-gallery[snippets="http-request,path-parameters,http-response,response-fields"]
====

=== 갤러리 게시글 삭제 (DELETE /galleries/{galleryId})
====
operation::gallery-controller-test/delete-gallery[snippets="http-request,path-parameters"]
====

=== 갤러리 게시글 수정 (PUT /galleries/{galleryId})
====
operation::gallery-controller-test/update-gallery[snippets="http-request,path-parameters,request-fields,http-response,response-fields"]
====
1 change: 1 addition & 0 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ include::talk.adoc[]
include::notice.adoc[]
include::eventNotice.adoc[]
include::eventPeriod.adoc[]
include::gallery.adoc[]
6 changes: 5 additions & 1 deletion src/main/java/com/scg/stop/domain/file/domain/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import com.scg.stop.domain.gallery.domain.Gallery;
import com.scg.stop.gallery.domain.Gallery;
import com.scg.stop.notice.domain.Notice;
import com.scg.stop.event.domain.EventNotice;
import com.scg.stop.global.domain.BaseTimeEntity;
Expand Down Expand Up @@ -54,4 +54,8 @@ public void setNotice(Notice notice) {
public void setEventNotice(EventNotice eventNotice) {
this.eventNotice = eventNotice;
}

public void setGallery(Gallery gallery) {
this.gallery = gallery;
}
}
43 changes: 0 additions & 43 deletions src/main/java/com/scg/stop/domain/gallery/domain/Gallery.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.scg.stop.gallery.controller;

import com.scg.stop.auth.annotation.AuthUser;
import com.scg.stop.gallery.dto.request.GalleryRequest;
import com.scg.stop.gallery.dto.response.GalleryResponse;
import com.scg.stop.gallery.service.GalleryService;
import com.scg.stop.user.domain.AccessType;
import com.scg.stop.user.domain.User;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/galleries")
public class GalleryController {

private final GalleryService galleryService;

@PostMapping
public ResponseEntity<GalleryResponse> createGallery(
@RequestBody @Valid GalleryRequest galleryRequest,
@AuthUser(accessType = {AccessType.ADMIN}) User user
) {
GalleryResponse galleryResponse = galleryService.createGallery(galleryRequest);
return ResponseEntity.status(HttpStatus.CREATED).body(galleryResponse);
}

@GetMapping
public ResponseEntity<Page<GalleryResponse>> getGalleries(
@RequestParam(value = "year", required = false) Integer year,
@RequestParam(value = "month", required = false) Integer month,
@PageableDefault(page = 0, size = 10) Pageable pageable) {
Page<GalleryResponse> galleries = galleryService.getGalleries(year, month, pageable);
return ResponseEntity.ok(galleries);
}

@GetMapping("/{galleryId}")
public ResponseEntity<GalleryResponse> getGallery(@PathVariable("galleryId") Long galleryId) {
GalleryResponse galleryResponse = galleryService.getGallery(galleryId);
return ResponseEntity.ok(galleryResponse);
}

@PutMapping("/{galleryId}")
public ResponseEntity<GalleryResponse> updateGallery(
@PathVariable("galleryId") Long galleryId,
@RequestBody @Valid GalleryRequest galleryRequest,
@AuthUser(accessType = {AccessType.ADMIN}) User user
) {
GalleryResponse galleryResponse = galleryService.updateGallery(galleryId, galleryRequest);
return ResponseEntity.ok(galleryResponse);
}

@DeleteMapping("/{galleryId}")
public ResponseEntity<Void> deleteGallery(
@PathVariable("galleryId") Long galleryId,
@AuthUser(accessType = {AccessType.ADMIN}) User user
) {
galleryService.deleteGallery(galleryId);
return ResponseEntity.noContent().build();
}
}
64 changes: 64 additions & 0 deletions src/main/java/com/scg/stop/gallery/domain/Gallery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.scg.stop.gallery.domain;

import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import com.scg.stop.domain.file.domain.File;
import com.scg.stop.global.domain.BaseTimeEntity;
import jakarta.persistence.*;

import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = PROTECTED)
public class Gallery extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

@Column(nullable = false)
private String title;

@Column(nullable = false)
private Integer year;

@Column(nullable = false)
private Integer month;

@Column(nullable = false)
private Integer hitCount = 0;

@OneToMany(fetch = LAZY, mappedBy = "gallery", cascade = CascadeType.ALL, orphanRemoval = true)
private List<File> files = new ArrayList<>();

private Gallery(String title, Integer year, Integer month, List<File> files) {
this.title = title;
this.year = year;
this.month = month;
this.files = files;
files.forEach(file -> file.setGallery(this));
}

public static Gallery of(String title, Integer year, Integer month, List<File> files) {
return new Gallery(title, year, month, files);
}

public void update(String title, Integer year, Integer month, List<File> files) {
this.title = title;
this.year = year;
this.month = month;
this.files.clear();
this.files.addAll(files);
files.forEach(file -> file.setGallery(this));
}

public void increaseHitCount() {
this.hitCount += 1;
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/scg/stop/gallery/dto/request/GalleryRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.scg.stop.gallery.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

import static lombok.AccessLevel.PRIVATE;

@Getter
@NoArgsConstructor(access = PRIVATE)
@AllArgsConstructor
public class GalleryRequest {

@NotBlank(message = "제목을 입력해주세요.")
private String title;

@NotNull(message = "연도를 입력해주세요.")
private Integer year;

@NotNull(message = "월을 입력해주세요.")
private Integer month;

@Size(min = 1, message = "1개 이상의 파일을 첨부해야 합니다.")
@NotNull(message = "파일을 첨부해주세요.")
private List<Long> fileIds;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.scg.stop.gallery.dto.response;

import static lombok.AccessLevel.PRIVATE;

import com.scg.stop.domain.file.dto.response.FileResponse;
import com.scg.stop.gallery.domain.Gallery;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = PRIVATE)
@AllArgsConstructor
public class GalleryResponse {

private Long id;
private String title;
private int year;
private int month;
private int hitCount;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private List<FileResponse> files;

public static GalleryResponse of(Gallery gallery, List<FileResponse> fileResponses) {
return new GalleryResponse(
gallery.getId(),
gallery.getTitle(),
gallery.getYear(),
gallery.getMonth(),
gallery.getHitCount(),
gallery.getCreatedAt(),
gallery.getUpdatedAt(),
fileResponses
);
}

public static GalleryResponse from(Gallery gallery) {
List<FileResponse> fileResponses = gallery.getFiles().stream()
.map(FileResponse::from)
.collect(Collectors.toList());
return GalleryResponse.of(gallery, fileResponses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.scg.stop.gallery.repository;

import com.scg.stop.gallery.domain.Gallery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface GalleryRepository extends JpaRepository<Gallery, Long> {

@Query("SELECT g FROM Gallery g " +
"WHERE (:year IS NULL OR g.year = :year) " +
"AND (:month IS NULL OR g.month = :month)")
Page<Gallery> findGalleries(
@Param("year") Integer year,
@Param("month") Integer month,
Pageable pageable
);
}
Loading
Loading