-
Notifications
You must be signed in to change notification settings - Fork 1
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] 기록하기(Album) API 구현 #135
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9a0c020
[CHORE] 이미지 버킷명 환경변수 추가 #133
jun02160 c748518
[FEAT] Album 엔티티 설계, 새로운 기록 등록하기 API #133
jun02160 142e461
[FIX] JPA Entity 연관관계 에러 해결 #133
jun02160 126dfa4
[FEAT] AWS S3 설정, PreSigned Url 발급 로직 구현 #133
jun02160 0ba13af
[FEAT] PreSigned Url 발급 로직 구현 #133
jun02160 2f4f1bd
[FIX] @RequestParam -> @RequestBody로 수정 #133
jun02160 39926b3
[FEAT] 앨범 글 삭제 API 구현 #133
jun02160 6434a2a
[FEAT] 앨범 목록 조회 API 구현 #133
jun02160 9dd657c
[DEL] 버킷 이미지 삭제 테스트 후 삭제 #133
jun02160 674cf96
[CHORE] 응답값 ID 필드 추가, 성공 응답 메세지 일부 수정 #133
jun02160 67a875d
[CHORE] User의 Parentchild null 여부에 대한 예외처리 #133
jun02160 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
72 changes: 72 additions & 0 deletions
72
umbba-api/src/main/java/sopt/org/umbba/api/controller/album/AlbumController.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,72 @@ | ||
package sopt.org.umbba.api.controller.album; | ||
|
||
import static sopt.org.umbba.api.config.jwt.JwtProvider.*; | ||
import static sopt.org.umbba.common.exception.SuccessType.*; | ||
import static sopt.org.umbba.external.s3.S3BucketPrefix.*; | ||
|
||
import java.security.Principal; | ||
import java.util.List; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import javax.validation.Valid; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PatchMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
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.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import sopt.org.umbba.api.controller.album.dto.request.AlbumImgUrlRequestDto; | ||
import sopt.org.umbba.api.controller.album.dto.request.CreateAlbumRequestDto; | ||
import sopt.org.umbba.api.controller.album.dto.response.AlbumResponseDto; | ||
import sopt.org.umbba.api.service.album.AlbumService; | ||
import sopt.org.umbba.common.exception.dto.ApiResponse; | ||
import sopt.org.umbba.external.s3.PreSignedUrlDto; | ||
import sopt.org.umbba.external.s3.S3BucketPrefix; | ||
import sopt.org.umbba.external.s3.S3Service; | ||
|
||
@RestController | ||
@RequestMapping("/album") | ||
@RequiredArgsConstructor | ||
public class AlbumController { | ||
|
||
private final AlbumService albumService; | ||
private final S3Service s3Service; | ||
|
||
@PostMapping | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public ApiResponse createAlbum(@Valid @RequestBody final CreateAlbumRequestDto request, final Principal principal, HttpServletResponse response) { | ||
String imgUrl = s3Service.getS3ImgUrl(ALBUM_PREFIX.getValue(), request.getImgFileName()); | ||
Long albumId = albumService.createAlbum(request, imgUrl, getUserFromPrincial(principal)); | ||
response.setHeader("Location", "/album/" + albumId); | ||
return ApiResponse.success(CREATE_ALBUM_SUCCESS); | ||
} | ||
|
||
// PreSigned Url 이용 (클라이언트에서 해당 URL로 업로드) | ||
@PatchMapping("/image") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분 PATCH인것 확인후에 명세서에 반영해뒀습니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 헉 감사합니다 !! |
||
@ResponseStatus(HttpStatus.OK) | ||
public ApiResponse<PreSignedUrlDto> getImgPreSignedUrl(@RequestBody final AlbumImgUrlRequestDto request) { | ||
return ApiResponse.success(GET_PRE_SIGNED_URL_SUCCESS, s3Service.getPreSignedUrl(S3BucketPrefix.of(request.getImgPrefix()))); | ||
} | ||
|
||
@DeleteMapping("/{albumId}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public ApiResponse deleteAlbum(@PathVariable final Long albumId, final Principal principal) { | ||
String imgUrl = albumService.deleteAlbum(albumId, getUserFromPrincial(principal)); | ||
s3Service.deleteS3Image(imgUrl); | ||
return ApiResponse.success(DELETE_ALBUM_SUCCESS); | ||
} | ||
|
||
@GetMapping | ||
@ResponseStatus(HttpStatus.OK) | ||
public ApiResponse<List<AlbumResponseDto>> getAlbumList(final Principal principal) { | ||
return ApiResponse.success(GET_ALBUM_LIST_SUCCESS, albumService.getAlbumList(getUserFromPrincial(principal))); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
.../src/main/java/sopt/org/umbba/api/controller/album/dto/request/AlbumImgUrlRequestDto.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,16 @@ | ||
package sopt.org.umbba.api.controller.album.dto.request; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
public class AlbumImgUrlRequestDto { | ||
|
||
private String imgPrefix; | ||
} |
28 changes: 28 additions & 0 deletions
28
.../src/main/java/sopt/org/umbba/api/controller/album/dto/request/CreateAlbumRequestDto.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,28 @@ | ||
package sopt.org.umbba.api.controller.album.dto.request; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.Size; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
public class CreateAlbumRequestDto { | ||
|
||
@NotBlank(message = "제목은 필수 입력 값입니다.") | ||
@Size(max = 15) | ||
private String title; | ||
|
||
@NotBlank(message = "소개글은 필수 입력 값입니다.") | ||
@Size(max = 32) | ||
private String content; | ||
|
||
@NotBlank(message = "이미지 파일명은 필수 입력 값입니다.") | ||
private String imgFileName; | ||
} |
30 changes: 30 additions & 0 deletions
30
...-api/src/main/java/sopt/org/umbba/api/controller/album/dto/response/AlbumResponseDto.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,30 @@ | ||
package sopt.org.umbba.api.controller.album.dto.response; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import sopt.org.umbba.domain.domain.album.Album; | ||
|
||
@Getter | ||
@Builder | ||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
public class AlbumResponseDto { | ||
|
||
private Long albumId; | ||
private String title; | ||
private String content; | ||
private String writer; | ||
private String imgUrl; | ||
|
||
public static AlbumResponseDto of(Album album) { | ||
return AlbumResponseDto.builder() | ||
.albumId(album.getId()) | ||
.title(album.getTitle()) | ||
.content(album.getContent()) | ||
.writer(album.getWriter()) | ||
.imgUrl(album.getImgUrl()) | ||
.build(); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
umbba-api/src/main/java/sopt/org/umbba/api/service/album/AlbumService.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,93 @@ | ||
package sopt.org.umbba.api.service.album; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import sopt.org.umbba.api.controller.album.dto.request.CreateAlbumRequestDto; | ||
import sopt.org.umbba.api.controller.album.dto.response.AlbumResponseDto; | ||
import sopt.org.umbba.common.exception.ErrorType; | ||
import sopt.org.umbba.common.exception.model.CustomException; | ||
import sopt.org.umbba.domain.domain.album.Album; | ||
import sopt.org.umbba.domain.domain.album.repository.AlbumRepository; | ||
import sopt.org.umbba.domain.domain.parentchild.Parentchild; | ||
import sopt.org.umbba.domain.domain.user.User; | ||
import sopt.org.umbba.domain.domain.user.repository.UserRepository; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class AlbumService { | ||
|
||
private final AlbumRepository albumRepository; | ||
private final UserRepository userRepository; | ||
|
||
@Transactional | ||
public Long createAlbum(final CreateAlbumRequestDto request, final String imgUrl, final Long userId) { | ||
|
||
User user = getUserById(userId); | ||
Parentchild parentchild = getParentchildByUser(user); | ||
|
||
Album album = Album.builder() | ||
.title(request.getTitle()) | ||
.content(request.getContent()) | ||
.imgUrl(imgUrl) | ||
.writer(user.getUsername()) | ||
.parentchild(parentchild) | ||
.build(); | ||
albumRepository.save(album); | ||
album.setParentchild(parentchild); | ||
parentchild.addAlbum(album); | ||
|
||
return album.getId(); | ||
} | ||
|
||
@Transactional | ||
public String deleteAlbum(final Long albumId, final Long userId) { | ||
|
||
User user = getUserById(userId); | ||
Parentchild parentchild = getParentchildByUser(user); | ||
Album album = getAlbumById(albumId); | ||
|
||
album.deleteParentchild(); | ||
parentchild.deleteAlbum(album); | ||
albumRepository.delete(album); | ||
|
||
return album.getImgUrl(); | ||
} | ||
|
||
public List<AlbumResponseDto> getAlbumList(final Long userId) { | ||
User user = getUserById(userId); | ||
Parentchild parentchild = getParentchildByUser(user); | ||
List<Album> albumList = albumRepository.findAllByParentchildOrderByCreatedAtDesc( | ||
parentchild); | ||
|
||
return albumList.stream() | ||
.map(AlbumResponseDto::of) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private User getUserById(Long userId) { // TODO userId -> Parentchild 한번에 가져오기 | ||
return userRepository.findById(userId).orElseThrow( | ||
() -> new CustomException(ErrorType.INVALID_USER) | ||
); | ||
} | ||
|
||
private Album getAlbumById(Long albumId) { | ||
return albumRepository.findById(albumId).orElseThrow( | ||
() -> new CustomException(ErrorType.NOT_FOUND_ALBUM) | ||
); | ||
} | ||
|
||
private Parentchild getParentchildByUser(User user) { | ||
Parentchild parentchild = user.getParentChild(); | ||
if (parentchild == null) { | ||
throw new CustomException(ErrorType.USER_HAVE_NO_PARENTCHILD); | ||
} | ||
|
||
return parentchild; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q) 혹시 header를 지정함으로써 해서 얻는 효과가 무엇일까요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTTP 통신 규약에 따르면 201 Created로 응답하는 경우에 생성된 리소스의 값을 응답에 포함시키는 게 원칙이라고 하더라구요! 그래서 응답 헤더(Location)에 생성된 리소스의 식별자를 요청 url에 붙여서 반환하도록 HttpServleResponse를 인자로 받아 넣어 주었습니다!!
이 부분은 다른 API에도 추후 적용해보면 좋을 것 같네요 :)
*HTTP 201 Created에 관한 규약 아래 내용 참고해 주세요!!
https://developer.mozilla.org/ko/docs/Web/HTTP/Status/201