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]: 워라벨 피드 관련 API 수정 #48

Merged
merged 4 commits into from
Feb 3, 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
Expand Up @@ -40,7 +40,6 @@ public enum ErrorStatus implements BaseErrorCode {

// 피드 관련 에러
FEED_NOT_FOUND(HttpStatus.BAD_REQUEST, "FEED4001", "해당하는 워라벨 피드가 없습니다."),
FEED_NOT_BLANK(HttpStatus.BAD_REQUEST, "FEED4002", "워라벨 피드 내용은 공백일 수 없습니다."),

// 피드 사진 관련 에러
FEED_IMAGE_EXIST(HttpStatus.BAD_REQUEST, "FEEDIMAGE4001", "이미 해당 위치에 업로드된 워라벨 피드 사진이 있습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,42 @@ public class FeedController {

@PostMapping("/feeds")
@Operation(summary = "워라벨 피드 추가 API", description = "새로운 워라벨 피드를 추가하는 API입니다.")
public ApiResponse<FeedResponseDTO.FeedResultDTO> addFeed(@RequestBody @Valid FeedRequestDTO.AddFeedDTO request) {
public ApiResponse<FeedResponseDTO.FeedDTO> addFeed(@RequestBody @Valid FeedRequestDTO.AddFeedDTO request) {
Feed feed = feedService.addFeed(request);
return ApiResponse.onSuccess(FeedConverter.toFeedResultDTO(feed));
return ApiResponse.onSuccess(FeedConverter.toFeedDTO(feed));
}

@GetMapping("/feeds")
@Operation(summary = "워라벨 피드 조회 API",description = "특정한 날짜의 워라벨 피드를 조회하는 API입니다. Query String으로 사용자 아이디와 날짜를 입력해 주세요.")
public ApiResponse<List<FeedResponseDTO.FeedResultDTO>> getFeed(@RequestParam(name = "userId") Long userId, @RequestParam(name = "date") LocalDate date){
List<Feed> feedList = feedService.getFeed(userId, date);
return ApiResponse.onSuccess(feedList.stream().map(FeedConverter::toFeedResultDTO).toList());
@Operation(summary = "워라벨 피드 조회 API", description = "특정한 날짜의 워라벨 피드를 조회하는 API입니다. Query String으로 날짜를 입력해 주세요.")
public ApiResponse<List<FeedResponseDTO.FeedDTO>> getFeed(@RequestParam(name = "date") LocalDate date) {
List<Feed> feedList = feedService.getFeed(date);
return ApiResponse.onSuccess(feedList.stream().map(FeedConverter::toFeedDTO).toList());
}

@PatchMapping("/feeds")
@Operation(summary = "워라벨 피드 수정 API", description = "기존의 워라벨 피드를 수정하는 API입니다.")
public ApiResponse<FeedResponseDTO.FeedResultDTO> modifyFeed(@RequestBody @Valid FeedRequestDTO.ModifyFeedDTO request) {
Feed feed = feedService.modifyFeed(request);
return ApiResponse.onSuccess(FeedConverter.toFeedResultDTO(feed));
@PatchMapping("/feeds/{feedId}")
@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));
}

@PatchMapping("/feeds/{feedId}/delay")
@Operation(summary = "워라벨 피드 내일로 미루기 API", description = "워라벨 피드의 날짜를 현재 기준 내일로 변경하는 API입니다.")
public ApiResponse<FeedResponseDTO.FeedDTO> delayFeed(@PathVariable(name = "feedId") Long feedId) {
Feed feed = feedService.delayFeed(feedId);
return ApiResponse.onSuccess(FeedConverter.toFeedDTO(feed));
}

@PatchMapping("/feeds/{feedId}/check")
@Operation(summary = "워라벨 피드 체크 및 해제 API", description = "워라벨 피드를 체크하거나 체크 해제하는 API입니다.")
public ApiResponse<FeedResponseDTO.FeedDTO> checkFeed(@PathVariable(name = "feedId") Long feedId) {
Feed feed = feedService.checkFeed(feedId);
return ApiResponse.onSuccess(FeedConverter.toFeedDTO(feed));
}

@DeleteMapping("/feeds/{feedId}")
@Operation(summary = "워라벨 피드 삭제 API",description = "기존의 워라벨 피드를 삭제하는 API입니다.")
public ApiResponse<?> deleteFeed(@PathVariable(name = "feedId") Long feedId){
feedService.deleteFeed(feedId);
return ApiResponse.onSuccess(null);
@Operation(summary = "워라벨 피드 삭제 API", description = "기존의 워라벨 피드를 삭제하는 API입니다.")
public ApiResponse<Long> deleteFeed(@PathVariable(name = "feedId") Long feedId) {
return ApiResponse.onSuccess(feedService.deleteFeed(feedId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public static Feed toFeed(FeedRequestDTO.AddFeedDTO request, User user) {
.build();
}

public static FeedResponseDTO.FeedResultDTO toFeedResultDTO(Feed feed) {
return FeedResponseDTO.FeedResultDTO.builder()
public static FeedResponseDTO.FeedDTO toFeedDTO(Feed feed) {
return FeedResponseDTO.FeedDTO.builder()
.feedId(feed.getId())
.date(feed.getDate())
.content(feed.getContent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public class FeedRequestDTO {

@Getter
public static class AddFeedDTO {
@NotNull
Long userId;
@NotNull
@PastOrPresent
LocalDate date;
Expand All @@ -24,11 +22,8 @@ public static class AddFeedDTO {

@Getter
public static class ModifyFeedDTO {
@NotNull
Long feedId;
LocalDate date;
@NotBlank
@Size(max = 30)
String content;
Boolean isChecked;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class FeedResponseDTO {
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class FeedResultDTO {
public static class FeedDTO {
Long feedId;
LocalDate date;
String content;
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/onnoff/onnoff/domain/off/feed/entity/Feed.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public class Feed extends BaseEntity {
@JoinColumn(name = "user_id")
private User user;

public void updateFeed(LocalDate date, String content, Boolean isChecked) {
if (date != null) {
this.date = date;
}
if (content != null) {
this.content = content;
}
if (isChecked != null) {
this.isChecked = isChecked;
}
public void setContent(String content) {
this.content = content;
}

public void setDate(LocalDate date) {
this.date = date;
}

public void setIsChecked(Boolean isChecked) {
this.isChecked = isChecked;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ public interface FeedService {

Feed addFeed(FeedRequestDTO.AddFeedDTO request);

List<Feed> getFeed(Long userId, LocalDate date);
List<Feed> getFeed(LocalDate date);

Feed modifyFeed(FeedRequestDTO.ModifyFeedDTO request);
Feed modifyFeed(Long feedId, FeedRequestDTO.ModifyFeedDTO request);

void deleteFeed(Long feedId);
Feed delayFeed(Long feedId);

Feed checkFeed(Long feedId);

Long deleteFeed(Long feedId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.onnoff.onnoff.apiPayload.code.status.ErrorStatus;
import com.onnoff.onnoff.apiPayload.exception.GeneralException;
import com.onnoff.onnoff.auth.UserContext;
import com.onnoff.onnoff.domain.off.feed.converter.FeedConverter;
import com.onnoff.onnoff.domain.off.feed.dto.FeedRequestDTO;
import com.onnoff.onnoff.domain.off.feed.entity.Feed;
import com.onnoff.onnoff.domain.off.feed.repository.FeedRepository;
import com.onnoff.onnoff.domain.user.User;
import com.onnoff.onnoff.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -20,37 +20,54 @@
public class FeedServiceImpl implements FeedService {

private final FeedRepository feedRepository;
private final UserRepository userRepository;

@Override
@Transactional
public Feed addFeed(FeedRequestDTO.AddFeedDTO request) {
User user = userRepository.findById(request.getUserId()).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
User user = UserContext.getUser();
return feedRepository.save(FeedConverter.toFeed(request, user));
}

@Override
@Transactional(readOnly = true)
public List<Feed> getFeed(Long userId, LocalDate date) {
User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
public List<Feed> getFeed(LocalDate date) {
User user = UserContext.getUser();
return feedRepository.findAllByUserAndDateOrderByCreatedAtAsc(user, date);
}

@Override
@Transactional
public Feed modifyFeed(FeedRequestDTO.ModifyFeedDTO request) {
Feed feed = feedRepository.findById(request.getFeedId()).orElseThrow(() -> new GeneralException(ErrorStatus.FEED_NOT_FOUND));
if (request.getContent() != null && request.getContent().trim().isEmpty()) {
throw new GeneralException(ErrorStatus.FEED_NOT_BLANK);
}
feed.updateFeed(request.getDate(), request.getContent(), request.getIsChecked());
public Feed modifyFeed(Long feedId, FeedRequestDTO.ModifyFeedDTO request) {
Feed feed = feedRepository.findById(feedId).orElseThrow(() -> new GeneralException(ErrorStatus.FEED_NOT_FOUND));
feed.setContent(request.getContent());

return feed;
}

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

return feed;
}

@Override
@Transactional
public void deleteFeed(Long feedId) {
public Feed checkFeed(Long feedId) {
Feed feed = feedRepository.findById(feedId).orElseThrow(() -> new GeneralException(ErrorStatus.FEED_NOT_FOUND));
feed.setIsChecked(feed.getIsChecked().equals(false));

return feed;
}

@Override
@Transactional
public Long deleteFeed(Long feedId) {
Feed feed = feedRepository.findById(feedId).orElseThrow(() -> new GeneralException(ErrorStatus.FEED_NOT_FOUND));
feedRepository.delete(feed);

return feed.getId();
}
}
Loading