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] #113 - 공연 삭제 DELETE API 구현 #116

Merged
merged 6 commits into from
Jul 17, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ Optional<Booking> findFirstByBookerNameAndBookerPhoneNumberAndBirthDateAndPasswo
);

List<Booking> findByUsersId(Long userId);

boolean existsBySchedulePerformanceId(Long performanceId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.beat.domain.performance.api;

import com.beat.domain.performance.application.PerformanceCreateService;
import com.beat.domain.performance.application.PerformanceManagementService;
import com.beat.domain.performance.application.dto.BookingPerformanceDetailResponse;
import com.beat.domain.performance.application.dto.MakerPerformanceResponse;
import com.beat.domain.performance.application.dto.PerformanceDetailResponse;
Expand All @@ -14,6 +14,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -27,14 +28,14 @@
public class PerformanceController {

private final PerformanceService performanceService;
private final PerformanceCreateService performanceCreateService;
private final PerformanceManagementService performanceManagementService;

@Operation(summary = "공연 생성 API", description = "공연을 생성하는 POST API입니다.")
@PostMapping
public ResponseEntity<SuccessResponse<PerformanceResponse>> createPerformance(
@CurrentMember Long userId,
@CurrentMember Long memberId,
@RequestBody PerformanceRequest performanceRequest) {
PerformanceResponse response = performanceCreateService.createPerformance(userId, performanceRequest);
PerformanceResponse response = performanceManagementService.createPerformance(memberId, performanceRequest);
return ResponseEntity.status(HttpStatus.CREATED)
.body(SuccessResponse.of(PerformanceSuccessCode.PERFORMANCE_CREATE_SUCCESS, response));
}
Expand All @@ -61,4 +62,13 @@ public ResponseEntity<SuccessResponse<MakerPerformanceResponse>> getUserPerforma
MakerPerformanceResponse response = performanceService.getMemberPerformances(memberId);
return ResponseEntity.ok(SuccessResponse.of(PerformanceSuccessCode.MAKER_PERFORMANCE_RETRIEVE_SUCCESS, response));
}
}

@Operation(summary = "공연 삭제 API", description = "공연을 삭제하는 DELETE API입니다.")
@DeleteMapping("/{performanceId}")
public ResponseEntity<SuccessResponse<Void>> deletePerformance(
@CurrentMember Long memberId,
@PathVariable Long performanceId) {
performanceManagementService.deletePerformance(memberId, performanceId);
return ResponseEntity.ok(SuccessResponse.of(PerformanceSuccessCode.PERFORMANCE_DELETE_SUCCESS, null));
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.beat.domain.performance.application;

import com.beat.domain.booking.dao.BookingRepository;
import com.beat.domain.cast.dao.CastRepository;
import com.beat.domain.cast.domain.Cast;
import com.beat.domain.member.dao.MemberRepository;
import com.beat.domain.member.domain.Member;
import com.beat.domain.member.exception.MemberErrorCode;
import com.beat.domain.performance.application.dto.create.CastResponse;
import com.beat.domain.performance.application.dto.create.PerformanceRequest;
import com.beat.domain.performance.application.dto.create.PerformanceResponse;
import com.beat.domain.performance.application.dto.create.ScheduleResponse;
import com.beat.domain.performance.application.dto.create.StaffResponse;
import com.beat.domain.performance.dao.PerformanceRepository;
import com.beat.domain.performance.domain.Performance;
import com.beat.domain.performance.exception.PerformanceErrorCode;
import com.beat.domain.schedule.dao.ScheduleRepository;
import com.beat.domain.schedule.domain.Schedule;
import com.beat.domain.staff.dao.StaffRepository;
import com.beat.domain.staff.domain.Staff;
import com.beat.domain.user.dao.UserRepository;
import com.beat.domain.user.domain.Users;
import com.beat.domain.user.exception.UserErrorCode;
import com.beat.global.common.exception.BadRequestException;
import com.beat.global.common.exception.NotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -28,18 +34,21 @@

@Service
@RequiredArgsConstructor
public class PerformanceCreateService {
public class PerformanceManagementService {

private final PerformanceRepository performanceRepository;
private final ScheduleRepository scheduleRepository;
private final UserRepository userRepository;
private final CastRepository castRepository;
private final StaffRepository staffRepository;
private final BookingRepository bookingRepository;
private final MemberRepository memberRepository;

@Transactional
public PerformanceResponse createPerformance(Long userId, PerformanceRequest request) {
Users user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(UserErrorCode.USER_NOT_FOUND));
public PerformanceResponse createPerformance(Long memberId, PerformanceRequest request) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundException(MemberErrorCode.MEMBER_NOT_FOUND));

Users user = member.getUser();

Performance performance = Performance.create(
request.performanceTitle(),
Expand Down Expand Up @@ -152,4 +161,19 @@ private PerformanceResponse mapToPerformanceResponse(Performance performance, Li
private int calculateDueDate(LocalDate performanceDate) {
return (int) ChronoUnit.DAYS.between(LocalDate.now(), performanceDate);
}

@Transactional
public void deletePerformance(Long memberId, Long performanceId) {
memberRepository.findById(memberId).orElseThrow(() -> new NotFoundException(UserErrorCode.USER_NOT_FOUND));

Performance performance = performanceRepository.findById(performanceId)
.orElseThrow(() -> new NotFoundException(PerformanceErrorCode.PERFORMANCE_NOT_FOUND));

boolean hasBookings = bookingRepository.existsBySchedulePerformanceId(performanceId);
if (hasBookings) {
throw new BadRequestException(PerformanceErrorCode.PERFORMANCE_DELETE_FAILED);
}

performanceRepository.delete(performance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum PerformanceErrorCode implements BaseErrorCode {
INVALID_DATA_FORMAT(400, "잘못된 데이터 형식입니다."),
INVALID_REQUEST_FORMAT(400, "잘못된 요청 형식입니다."),
NO_PERFORMANCE_FOUND(404, "공연을 찾을 수 없습니다."),
PERFORMANCE_DELETE_FAILED(400, "예매자가 1명 이상 있을 경우, 공연을 삭제할 수 없습니다."),
INTERNAL_SERVER_ERROR(500, "서버 내부 오류입니다.")
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
@Getter
@RequiredArgsConstructor
public enum PerformanceSuccessCode implements BaseSuccessCode {
PERFORMANCE_CREATE_SUCCESS(201, "공연이 성공정으로 생성되었습니다."),
PERFORMANCE_CREATE_SUCCESS(201, "공연이 성공적으로 생성되었습니다."),
PERFORMANCE_RETRIEVE_SUCCESS(200, "공연 상세 정보 조회가 성공적으로 완료되었습니다."),
PERFORMANCE_DELETE_SUCCESS(200, "공연이 성공적으로 삭제되었습니다."),
BOOKING_PERFORMANCE_RETRIEVE_SUCCESS(200, "예매 관련 공연 정보 조회가 성공적으로 완료되었습니다."),
HOME_PERFORMANCE_RETRIEVE_SUCCESS(200, "홈 화면 공연 목록 조회가 성공적으로 완료되었습니다."),
MAKER_PERFORMANCE_RETRIEVE_SUCCESS(200, "회원이 등록한 공연 목록의 조회가 성공적으로 완료되었습니다.")
Expand Down
Loading