-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FEAT] 기록하기(Album) API 구현
- Loading branch information
Showing
22 changed files
with
578 additions
and
2 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
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") | ||
@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.