Skip to content

Commit

Permalink
#90 [feat] : 유저는 마이페이지에서 고정 스케줄을 삭제할 수 있다
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbang105 committed Oct 23, 2024
1 parent a54a5ac commit c356b78
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/main/java/side/onetime/controller/FixedController.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,15 @@ public ResponseEntity<ApiResponse<Object>> modifyFixedEvent(

return ApiResponse.onSuccess(SuccessStatus._MODIFY_FIXED_SCHEDULE);
}

// 고정 이벤트 & 스케줄 삭제 API
@DeleteMapping("/{id}")
public ResponseEntity<ApiResponse<Object>> removeFixedEvent(
@RequestHeader("Authorization") String authorizationHeader,
@PathVariable("id") Long fixedEventId) {

fixedEventService.removeFixedEvent(authorizationHeader, fixedEventId);

return ApiResponse.onSuccess(SuccessStatus._REMOVE_FIXED_SCHEDULE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public enum SuccessStatus implements BaseCode {
_GET_ALL_FIXED_SCHEDULES(HttpStatus.OK, "200", "전체 고정 스케줄 조회에 성공했습니다."),
_GET_FIXED_SCHEDULE_DETAIL(HttpStatus.OK, "200", "특정 고정 스케줄 상세 조회에 성공했습니다."),
_MODIFY_FIXED_SCHEDULE(HttpStatus.OK, "200", "고정 스케줄 수정에 성공했습니다."),
_REMOVE_FIXED_SCHEDULE(HttpStatus.OK, "200", "고정 스케줄 삭제에 성공했습니다."),
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import side.onetime.domain.User;
import side.onetime.repository.custom.FixedEventRepositoryCustom;

import java.util.Optional;

public interface FixedEventRepository extends JpaRepository<FixedEvent, Long>, FixedEventRepositoryCustom {
FixedEvent findByUserAndId(User user, Long id);
Optional<FixedEvent> findByUserAndId(User user, Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
public interface FixedEventRepositoryCustom {
List<FixedEvent> findAllByUser(User user);
FixedEvent findByUserAndFixedEventIdCustom(User user, Long fixedEventId);
void deleteFixedEventAndSelections(User user, Long fixedEventId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import side.onetime.domain.FixedEvent;
import side.onetime.domain.User;

Expand All @@ -18,6 +19,7 @@ public class FixedEventRepositoryImpl implements FixedEventRepositoryCustom {

// 유저 고정 스케줄 목록 조회
@Override
@Transactional(readOnly = true)
public List<FixedEvent> findAllByUser(User user) {
return queryFactory.selectFrom(fixedEvent)
.leftJoin(fixedEvent.fixedSelections, fixedSelection)
Expand All @@ -30,6 +32,7 @@ public List<FixedEvent> findAllByUser(User user) {

// 특정 고정 스케줄 상세 조회
@Override
@Transactional(readOnly = true)
public FixedEvent findByUserAndFixedEventIdCustom(User user, Long fixedEventId) {
return queryFactory.selectFrom(fixedEvent)
.leftJoin(fixedEvent.fixedSelections, fixedSelection)
Expand All @@ -40,4 +43,21 @@ public FixedEvent findByUserAndFixedEventIdCustom(User user, Long fixedEventId)
.and(fixedEvent.id.eq(fixedEventId)))
.fetchOne();
}

// 고정 이벤트 & 스케줄 삭제
@Override
@Transactional
public void deleteFixedEventAndSelections(User user, Long fixedEventId) {
// fixed_selections 삭제
queryFactory.delete(fixedSelection)
.where(fixedSelection.fixedEvent.id.eq(fixedEventId)
.and(fixedSelection.fixedEvent.user.eq(user)))
.execute();

// fixed_events 삭제
queryFactory.delete(fixedEvent)
.where(fixedEvent.id.eq(fixedEventId)
.and(fixedEvent.user.eq(user)))
.execute();
}
}
14 changes: 13 additions & 1 deletion src/main/java/side/onetime/service/FixedEventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import side.onetime.domain.User;
import side.onetime.dto.fixed.request.CreateFixedEventRequest;
import side.onetime.dto.fixed.request.ModifyFixedEventRequest;
import side.onetime.exception.CustomException;
import side.onetime.exception.status.FixedErrorStatus;
import side.onetime.repository.FixedEventRepository;
import side.onetime.util.JwtUtil;

Expand Down Expand Up @@ -36,8 +38,18 @@ public void createFixedEvent(String authorizationHeader, CreateFixedEventRequest
@Transactional
public void modifyFixedEvent(String authorizationHeader, Long fixedEventId, ModifyFixedEventRequest modifyFixedEventRequest) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId);
FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId)
.orElseThrow(() -> new CustomException(FixedErrorStatus._NOT_FOUND_FIXED_EVENT));
fixedEvent.updateTitle(modifyFixedEventRequest.title());
fixedEventRepository.save(fixedEvent);
}

// 고정 이벤트 & 스케줄 삭제 메서드
@Transactional
public void removeFixedEvent(String authorizationHeader, Long fixedEventId) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId)
.orElseThrow(() -> new CustomException(FixedErrorStatus._NOT_FOUND_FIXED_EVENT));
fixedEventRepository.deleteFixedEventAndSelections(user, fixedEventId);
}
}
3 changes: 2 additions & 1 deletion src/main/java/side/onetime/service/FixedScheduleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public FixedEventDetailResponse getFixedScheduleDetail(String authorizationHeade
@Transactional
public void modifyFixedSchedule(String authorizationHeader, Long fixedEventId, ModifyFixedEventRequest modifyFixedEventRequest) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId);
FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId)
.orElseThrow(() -> new CustomException(FixedErrorStatus._NOT_FOUND_FIXED_EVENT));

List<String> times = modifyFixedEventRequest.schedules().get(0).times();
String startTime = times.get(0);
Expand Down

0 comments on commit c356b78

Please sign in to comment.