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/29] 꿈 일기 삭제 API 구현 #30

Merged
merged 2 commits into from
Jul 4, 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
13 changes: 10 additions & 3 deletions src/main/java/com/umc/dream/controller/DreamController.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,26 @@ public ApiResponse<AddDreamResponseDto> createDream(@RequestBody DreamRequestDto
@Parameters({
@Parameter(name = "user_id", description = "꿈 일기 목록을 조회하고자하는 사용자의 pk"),
})
private ApiResponse<List<GetDreamResponseDto>> GetDream(@RequestParam Long user_id) {
private ApiResponse<List<GetDreamResponseDto>> GetDream(@PathVariable Long user_id) {
List<GetDreamResponseDto> getDreamResponseDtos = dreamService.getDream(user_id);
return ApiResponse.onSuccess(getDreamResponseDtos);
}

@GetMapping("/diary/{user_id}/{diary_id}")
@GetMapping("/diary/{user_id}/{dream_id}")
@Operation(summary = "내 꿈 일기 상세 조회", description = "내 꿈 일기 상세 조회 API입니다")
@Parameters({
@Parameter(name = "user_id", description = "자신의 pk"),
@Parameter(name = "dream_id", description = "꿈 일기의 pk")
})
private ApiResponse<ViewDreamResponseDto> ViewDream(@RequestParam Long user_id, @RequestParam Long dream_id) {
private ApiResponse<ViewDreamResponseDto> ViewDream(@PathVariable Long user_id, @PathVariable Long dream_id) {
ViewDreamResponseDto viewDreamResponseDto = dreamService.viewDream(user_id, dream_id);
return ApiResponse.onSuccess(viewDreamResponseDto);
}

@DeleteMapping("/diary/{user_id}/{dream_id}")
@Operation(summary = "꿈 일기 삭제 API", description = "꿈 일기 삭제 API입니다")
public ApiResponse<Void> deleteDream(@PathVariable Long user_id, @PathVariable Long dream_id) {
dreamService.deleteDream(user_id, dream_id);
return ApiResponse.onSuccess(null);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/umc/dream/controller/TestController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.umc.dream.controller;

import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

@Hidden
@GetMapping("/health")
@Operation(summary = "health check API",description = "인스턴스의 health check하는 API입니다")
public String healthCheck() {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/umc/dream/domain/Dream.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
Expand Down Expand Up @@ -38,6 +40,21 @@ public class Dream extends BaseEntity {
@Enumerated(EnumType.STRING)
private DreamCategory category;

@OneToMany(mappedBy = "dream", cascade = CascadeType.ALL, orphanRemoval = true)
private List<People> characters = new ArrayList<>();

@OneToMany(mappedBy = "dream", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Place> places = new ArrayList<>();

@OneToMany(mappedBy = "dream", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Feeling> feelings = new ArrayList<>();

@OneToMany(mappedBy = "dream", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Hashtag> hashtags = new ArrayList<>();

@OneToMany(mappedBy = "dream", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Post> posts = new ArrayList<>();

@Builder
public Dream(User user, LocalDate date, LocalDateTime sleepTime, LocalDateTime wakeUpTime, String title, String content, DreamCategory category) {
this.user = user;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/umc/dream/service/DreamService.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,17 @@ public ViewDreamResponseDto viewDream(Long user_id, Long dream_id) {
}
return DreamConverter.toViewDreamResponse(dream, feels, name, location, tag);
}

@Transactional
public void deleteDream(Long user_id, Long dream_id) {
Dream dream = dreamRepository.findById(dream_id)
.orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST));

// 작성자가 아닌 경우
if (!dream.getUser().getId().equals(user_id)) {
throw new GeneralException(ErrorStatus._FORBIDDEN);
}

dreamRepository.delete(dream);
}
}
Loading