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

[refactor]: OFF 관련 API 수정 #60

Merged
merged 6 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.onnoff.onnoff.apiPayload.exception.handler;

import com.onnoff.onnoff.apiPayload.code.BaseErrorCode;
import com.onnoff.onnoff.apiPayload.exception.GeneralException;

public class FeedHandler extends GeneralException {
public FeedHandler(BaseErrorCode errorCode) {
super(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.onnoff.onnoff.apiPayload.exception.handler;

import com.onnoff.onnoff.apiPayload.code.BaseErrorCode;
import com.onnoff.onnoff.apiPayload.exception.GeneralException;

public class FeedImageHandler extends GeneralException {
public FeedImageHandler(BaseErrorCode errorCode) {
super(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.onnoff.onnoff.apiPayload.exception.handler;

import com.onnoff.onnoff.apiPayload.code.BaseErrorCode;
import com.onnoff.onnoff.apiPayload.exception.GeneralException;

public class MemoirHandler extends GeneralException {
public MemoirHandler(BaseErrorCode errorCode) {
super(errorCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ApiResponse<List<FeedResponseDTO.FeedDTO>> getFeed(@RequestParam(name = "
}

@PatchMapping("/feeds/{feedId}")
@Operation(summary = "워라벨 피드 수정 API", description = "워라벨 피드의 내용을 수정하는 API입니다.")
@Operation(summary = "워라벨 피드 내용 수정 API", description = "워라벨 피드의 내용을 수정하는 API입니다.")
public ApiResponse<FeedResponseDTO.FeedDTO> modifyFeed(@PathVariable(name = "feedId") Long feedId, @RequestBody @Valid FeedRequestDTO.ModifyFeedDTO request) {
Feed feed = feedService.modifyFeed(feedId, request);
return ApiResponse.onSuccess(FeedConverter.toFeedDTO(feed));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class FeedConverter {
public static Feed toFeed(FeedRequestDTO.AddFeedDTO request, User user) {
return Feed.builder()
.date(request.getDate())
.content(request.getContent())
.content(request.getContent().trim())
.isChecked(false)
.user(user)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.onnoff.onnoff.domain.off.feed.service;

import com.onnoff.onnoff.apiPayload.code.status.ErrorStatus;
import com.onnoff.onnoff.apiPayload.exception.GeneralException;
import com.onnoff.onnoff.apiPayload.exception.handler.FeedHandler;
import com.onnoff.onnoff.auth.UserContext;
import com.onnoff.onnoff.domain.off.feed.converter.FeedConverter;
import com.onnoff.onnoff.domain.off.feed.dto.FeedRequestDTO;
Expand Down Expand Up @@ -38,16 +38,16 @@ public List<Feed> getFeed(LocalDate date) {
@Override
@Transactional
public Feed modifyFeed(Long feedId, FeedRequestDTO.ModifyFeedDTO request) {
Feed feed = feedRepository.findById(feedId).orElseThrow(() -> new GeneralException(ErrorStatus.FEED_NOT_FOUND));
feed.setContent(request.getContent());
Feed feed = feedRepository.findById(feedId).orElseThrow(() -> new FeedHandler(ErrorStatus.FEED_NOT_FOUND));
feed.setContent(request.getContent().trim());

return feed;
}

@Override
@Transactional
public Feed delayFeed(Long feedId) {
Feed feed = feedRepository.findById(feedId).orElseThrow(() -> new GeneralException(ErrorStatus.FEED_NOT_FOUND));
Feed feed = feedRepository.findById(feedId).orElseThrow(() -> new FeedHandler(ErrorStatus.FEED_NOT_FOUND));
feed.setDate(LocalDate.now().plusDays(1));

return feed;
Expand All @@ -56,7 +56,7 @@ public Feed delayFeed(Long feedId) {
@Override
@Transactional
public Feed checkFeed(Long feedId) {
Feed feed = feedRepository.findById(feedId).orElseThrow(() -> new GeneralException(ErrorStatus.FEED_NOT_FOUND));
Feed feed = feedRepository.findById(feedId).orElseThrow(() -> new FeedHandler(ErrorStatus.FEED_NOT_FOUND));
feed.setIsChecked(feed.getIsChecked().equals(false));

return feed;
Expand All @@ -65,7 +65,7 @@ public Feed checkFeed(Long feedId) {
@Override
@Transactional
public Long deleteFeed(Long feedId) {
Feed feed = feedRepository.findById(feedId).orElseThrow(() -> new GeneralException(ErrorStatus.FEED_NOT_FOUND));
Feed feed = feedRepository.findById(feedId).orElseThrow(() -> new FeedHandler(ErrorStatus.FEED_NOT_FOUND));
feedRepository.delete(feed);

return feed.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ApiResponse<FeedImageResponseDTO.FeedImageDTO> uploadFeedImage(@RequestPa
}

@GetMapping("/feed-images")
@Operation(summary = "워라벨 피드 사진 조회 API",description = "워라벨 피드의 사진을 조회하는 API입니다. 생성 날짜 기준 오래된 순으로 정렬된 결과가 반환됩니다.")
@Operation(summary = "워라벨 피드 사진 조회 API",description = "워라벨 피드의 사진을 조회하는 API입니다. 업로드한 지 오래된 순으로 정렬된 결과가 반환됩니다.")
public ApiResponse<List<FeedImageResponseDTO.FeedImageDTO>> getFeedImage() {
return ApiResponse.onSuccess(feedImageService.getFeedImage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.onnoff.onnoff.apiPayload.code.status.ErrorStatus;
import com.onnoff.onnoff.apiPayload.exception.GeneralException;
import com.onnoff.onnoff.apiPayload.exception.handler.FeedImageHandler;
import com.onnoff.onnoff.auth.UserContext;
import com.onnoff.onnoff.domain.off.feedImage.converter.FeedImageConverter;
import com.onnoff.onnoff.domain.off.feedImage.dto.FeedImageResponseDTO;
Expand All @@ -32,6 +33,9 @@ public class FeedImageServiceImpl implements FeedImageService {

@Value("${cloud.aws.s3.bucket}")
private String bucket;
@Value("${cloud.aws.s3.path.feed-image}")
private String path;

private final AmazonS3Client amazonS3Client;
private final FeedImageRepository feedImageRepository;

Expand Down Expand Up @@ -64,7 +68,7 @@ public List<FeedImageResponseDTO.FeedImageDTO> getFeedImage() {
@Override
@Transactional
public Long deleteFeedImage(Long feedImageId) {
FeedImage feedImage = feedImageRepository.findById(feedImageId).orElseThrow(() -> new GeneralException(ErrorStatus.FEED_IMAGE_NOT_FOUND));
FeedImage feedImage = feedImageRepository.findById(feedImageId).orElseThrow(() -> new FeedImageHandler(ErrorStatus.FEED_IMAGE_NOT_FOUND));

deleteImage(feedImage.getImageKey());
feedImageRepository.delete(feedImage);
Expand All @@ -73,7 +77,7 @@ public Long deleteFeedImage(Long feedImageId) {
}

public String uploadImage(MultipartFile multipartFile) {
String fileName = createFileName(multipartFile.getOriginalFilename());
String fileName = path + "/" + createFileName(multipartFile.getOriginalFilename());

ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(multipartFile.getSize());
Expand All @@ -97,7 +101,7 @@ private String getFileExtension(String fileName) {
try {
return fileName.substring(fileName.lastIndexOf("."));
} catch (StringIndexOutOfBoundsException e) {
throw new GeneralException(ErrorStatus.FEED_IMAGE_BAD_REQUEST);
throw new FeedImageHandler(ErrorStatus.FEED_IMAGE_BAD_REQUEST);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ApiResponse<MemoirResponseDTO.BookmarkedMemoirListDTO> getBookmarkedMemoi
}

@PatchMapping("/memoirs/{memoirId}")
@Operation(summary = "회고 내용 수정 API", description = "기존의 회고 내용을 수정하는 API입니다.")
@Operation(summary = "회고 내용 수정 API", description = "회고의 이모티콘과 답변을 수정하는 API입니다.")
public ApiResponse<MemoirResponseDTO.MemoirDTO> modifyMemoir(@PathVariable(name = "memoirId") Long memoirId,
@RequestBody @Valid MemoirRequestDTO.MemoirUpdateDTO request) {
Memoir memoir = memoirService.modifyMemoir(memoirId, request);
Expand All @@ -67,7 +67,7 @@ public ApiResponse<MemoirResponseDTO.MemoirDTO> bookmarkMemoir(@PathVariable(nam
}

@DeleteMapping("/memoirs/{memoirId}")
@Operation(summary = "회고 삭제 API", description = "기존의 회고를 삭제하는 API입니다.")
@Operation(summary = "회고 삭제 API", description = "회고를 삭제하는 API입니다.")
public ApiResponse<Long> deleteMemoir(@PathVariable(name = "memoirId") Long memoirId) {
return ApiResponse.onSuccess(memoirService.deleteMemoir(memoirId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static class MemoirWriteDTO {
public static class MemoirWriteAnswerDTO {
@NotNull
Long questionId;
@NotNull
@Size(max = 500)
String answer;
}
Expand All @@ -40,6 +41,7 @@ public static class MemoirUpdateDTO {
public static class MemoirUpdateAnswerDTO {
@NotNull
Long answerId;
@NotNull
@Size(max = 500)
String answer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.onnoff.onnoff.domain.off.memoir.service;

import com.onnoff.onnoff.apiPayload.code.status.ErrorStatus;
import com.onnoff.onnoff.apiPayload.exception.GeneralException;
import com.onnoff.onnoff.apiPayload.exception.handler.MemoirHandler;
import com.onnoff.onnoff.auth.UserContext;
import com.onnoff.onnoff.domain.off.memoir.dto.MemoirRequestDTO;
import com.onnoff.onnoff.domain.off.memoir.entity.Memoir;
Expand Down Expand Up @@ -38,19 +38,19 @@ public class MemoirServiceImpl implements MemoirService {
public Memoir writeMemoir(MemoirRequestDTO.MemoirWriteDTO request) {
User user = UserContext.getUser();
if (memoirRepository.findByUserAndDate(user, request.getDate()).isPresent()) {
throw new GeneralException(ErrorStatus.MEMOIR_EXIST);
throw new MemoirHandler(ErrorStatus.MEMOIR_EXIST);
}

Memoir newMemoir = Memoir.builder()
.date(request.getDate())
.emoticon(emoticonRepository.findById(request.getEmoticonId()).orElseThrow(() -> new GeneralException(ErrorStatus.EMOTICON_NOT_FOUND)))
.emoticon(emoticonRepository.findById(request.getEmoticonId()).orElseThrow(() -> new MemoirHandler(ErrorStatus.EMOTICON_NOT_FOUND)))
.isBookmarked(false)
.build();

List<MemoirAnswer> newMemoirAnswerList = request.getMemoirAnswerList().stream()
.map(memoirAnswer -> MemoirAnswer.builder()
.answer(memoirAnswer.getAnswer())
.memoirQuestion(memoirQuestionRepository.findById(memoirAnswer.getQuestionId()).orElseThrow(() -> new GeneralException(ErrorStatus.QUESTION_NOT_FOUND)))
.answer(memoirAnswer.getAnswer().trim())
.memoirQuestion(memoirQuestionRepository.findById(memoirAnswer.getQuestionId()).orElseThrow(() -> new MemoirHandler(ErrorStatus.QUESTION_NOT_FOUND)))
.memoir(newMemoir)
.build())
.collect(Collectors.toList());
Expand All @@ -71,7 +71,7 @@ public Memoir getMemoirPreview(LocalDate date) {
@Override
@Transactional(readOnly = true)
public Memoir getMemoir(Long memoirId) {
return memoirRepository.findById(memoirId).orElseThrow(() -> new GeneralException(ErrorStatus.MEMOIR_NOT_FOUND));
return memoirRepository.findById(memoirId).orElseThrow(() -> new MemoirHandler(ErrorStatus.MEMOIR_NOT_FOUND));
}

@Override
Expand All @@ -84,16 +84,16 @@ public Page<Memoir> getBookmarkedMemoir(Integer pageNumber) {
@Override
@Transactional
public Memoir modifyMemoir(Long memoirId, MemoirRequestDTO.MemoirUpdateDTO request) {
Memoir memoir = memoirRepository.findById(memoirId).orElseThrow(() -> new GeneralException(ErrorStatus.MEMOIR_NOT_FOUND));
Memoir memoir = memoirRepository.findById(memoirId).orElseThrow(() -> new MemoirHandler(ErrorStatus.MEMOIR_NOT_FOUND));

memoir.setEmoticon(emoticonRepository.findById(request.getEmoticonId()).orElseThrow(() -> new GeneralException(ErrorStatus.EMOTICON_NOT_FOUND)));
memoir.setEmoticon(emoticonRepository.findById(request.getEmoticonId()).orElseThrow(() -> new MemoirHandler(ErrorStatus.EMOTICON_NOT_FOUND)));

for (MemoirRequestDTO.MemoirUpdateAnswerDTO memoirAnswer : request.getMemoirAnswerList()) {
MemoirAnswer findMemoirAnswer = memoirAnswerRepository.findById(memoirAnswer.getAnswerId()).orElseThrow(() -> new GeneralException(ErrorStatus.ANSWER_NOT_FOUND));
MemoirAnswer findMemoirAnswer = memoirAnswerRepository.findById(memoirAnswer.getAnswerId()).orElseThrow(() -> new MemoirHandler(ErrorStatus.ANSWER_NOT_FOUND));
if (findMemoirAnswer.getMemoir() != memoir) {
throw new GeneralException(ErrorStatus.ANSWER_BAD_MATCH);
throw new MemoirHandler(ErrorStatus.ANSWER_BAD_MATCH);
}
findMemoirAnswer.setAnswer(memoirAnswer.getAnswer());
findMemoirAnswer.setAnswer(memoirAnswer.getAnswer().trim());
}

return memoir;
Expand All @@ -102,7 +102,7 @@ public Memoir modifyMemoir(Long memoirId, MemoirRequestDTO.MemoirUpdateDTO reque
@Override
@Transactional
public Memoir bookmarkMemoir(Long memoirId) {
Memoir memoir = memoirRepository.findById(memoirId).orElseThrow(() -> new GeneralException(ErrorStatus.MEMOIR_NOT_FOUND));
Memoir memoir = memoirRepository.findById(memoirId).orElseThrow(() -> new MemoirHandler(ErrorStatus.MEMOIR_NOT_FOUND));
memoir.setIsBookmarked(memoir.getIsBookmarked().equals(false));

return memoir;
Expand All @@ -111,7 +111,7 @@ public Memoir bookmarkMemoir(Long memoirId) {
@Override
@Transactional
public Long deleteMemoir(Long memoirId) {
Memoir memoir = memoirRepository.findById(memoirId).orElseThrow(() -> new GeneralException(ErrorStatus.MEMOIR_NOT_FOUND));
Memoir memoir = memoirRepository.findById(memoirId).orElseThrow(() -> new MemoirHandler(ErrorStatus.MEMOIR_NOT_FOUND));
memoirRepository.delete(memoir);

return memoir.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ public class WeekdayController {

@GetMapping("/weekdays/init")
@Operation(summary = "초기 요일 조회 API", description = "오늘 날짜를 기준으로 일주일을 조회하는 API입니다.")
public ApiResponse<WeekdayResponseDTO.WeekdayResultDTO> getInitWeekday() {
public ApiResponse<WeekdayResponseDTO.WeekdayDTO> getInitWeekday() {
return ApiResponse.onSuccess(weekdayService.getWeekday(LocalDate.now()));
}

@GetMapping("/weekdays/prev")
@Operation(summary = "이전 주 요일 조회 API", description = "입력된 날짜를 기준으로 이전 일주일을 조회하는 API입니다. Query String으로 날짜를 입력해 주세요.")
public ApiResponse<WeekdayResponseDTO.WeekdayResultDTO> getPrevWeekday(@RequestParam(name = "date") LocalDate date) {
public ApiResponse<WeekdayResponseDTO.WeekdayDTO> getPrevWeekday(@RequestParam(name = "date") LocalDate date) {
return ApiResponse.onSuccess(weekdayService.getWeekday(date.minusDays(7)));
}

@GetMapping("/weekdays/next")
@Operation(summary = "다음 주 요일 조회 API", description = "입력된 날짜를 기준으로 다음 일주일을 조회하는 API입니다. Query String으로 날짜를 입력해 주세요.")
public ApiResponse<WeekdayResponseDTO.WeekdayResultDTO> getNextWeekday(@RequestParam(name = "date") LocalDate date) {
public ApiResponse<WeekdayResponseDTO.WeekdayDTO> getNextWeekday(@RequestParam(name = "date") LocalDate date) {
return ApiResponse.onSuccess(weekdayService.getWeekday(date.plusDays(7)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class WeekdayResponseDTO {
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class WeekdayResultDTO {
public static class WeekdayDTO {
LocalDate monday;
LocalDate tuesday;
LocalDate wednesday;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

public interface WeekdayService {

WeekdayResponseDTO.WeekdayResultDTO getWeekday(LocalDate date);
WeekdayResponseDTO.WeekdayDTO getWeekday(LocalDate date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
public class WeekdayServiceImpl implements WeekdayService {

@Override
public WeekdayResponseDTO.WeekdayResultDTO getWeekday(LocalDate date) {
public WeekdayResponseDTO.WeekdayDTO getWeekday(LocalDate date) {
LocalDate monday = date.minusDays(date.getDayOfWeek().getValue() - 1);

return WeekdayResponseDTO.WeekdayResultDTO.builder()
return WeekdayResponseDTO.WeekdayDTO.builder()
.monday(monday)
.tuesday(monday.plusDays(1))
.wednesday(monday.plusDays(2))
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ cloud:
aws:
s3:
bucket: ${S3_BUCKET}
path:
feed-image: feedImages
credentials:
access-key: ${S3_ACCESS_KEY}
secret-key: ${S3_SECRET_KEY}
Expand Down
Loading