Skip to content

Commit

Permalink
Merge pull request #50 from Project-Catcher/feat-cw-delete-draft-sche…
Browse files Browse the repository at this point in the history
…dule

Feat: 작성 중인 일정을 삭제 처리한다
  • Loading branch information
cheolwon1994 authored Dec 17, 2023
2 parents daa5cca + 3796c4c commit bb805f2
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum BaseResponseStatus {
*/
DATABASE_ERROR(4000, "데이터베이스 연결에 실패하였습니다."),
REDIS_ERROR(4002, "redis 연결에 실패하였습니다."),

FAIL_DELETE_DRAFT_SCHEDULE(4003, "작성 중인 일정을 삭제하는 데 실패하였습니다."),
/**
* 5000: AWS Error
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public interface ScheduleRepository {
List<Schedule> appliedScheduleList(Long userId);

void saveAll(List<Schedule> scheduleList);

void deleteDraftSchedule(Long userId, Long scheduleId);
}
5 changes: 5 additions & 0 deletions src/main/java/com/catcher/core/service/ScheduleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,9 @@ public MyListResponse myList(Long userId) {
scheduleRepository.appliedScheduleList(userId) //참여 신청
);
}

@Transactional
public void deleteDraftSchedule(Long userId, Long scheduleId) {
scheduleRepository.deleteDraftSchedule(userId, scheduleId);
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/catcher/datasource/ScheduleRepositoryImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.catcher.datasource;

import com.catcher.common.exception.BaseException;
import com.catcher.common.exception.BaseResponseStatus;
import com.catcher.core.database.ScheduleRepository;
import com.catcher.core.domain.entity.Schedule;
import com.catcher.core.domain.entity.ScheduleParticipant;
Expand Down Expand Up @@ -116,4 +118,13 @@ public List<Schedule> appliedScheduleList(Long userId) {
public void saveAll(List<Schedule> scheduleList) {
scheduleJpaRepository.saveAll(scheduleList);
}

@Override
public void deleteDraftSchedule(Long userId, Long scheduleId) {
boolean isUpdated = scheduleJpaRepository.updateScheduleToDeleted(userId, scheduleId) == 1;

if(!isUpdated){
throw new BaseException(BaseResponseStatus.FAIL_DELETE_DRAFT_SCHEDULE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.catcher.core.domain.entity.User;
import com.catcher.core.domain.entity.enums.ScheduleStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

Expand All @@ -26,4 +27,9 @@ public interface ScheduleJpaRepository extends JpaRepository<Schedule, Long> {
List<Schedule> findByScheduleStatusAndParticipationPeriod(
@Param("status") ScheduleStatus status,
@Param("currentDateTime") LocalDateTime currentDateTime);

@Modifying
@Query("UPDATE Schedule s SET s.scheduleStatus = 'DELETED' WHERE s.id = :id AND s.user.id = :userId")
int updateScheduleToDeleted(@Param("userId") Long userId, @Param("id") Long id);

}
12 changes: 12 additions & 0 deletions src/main/java/com/catcher/resource/ScheduleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,16 @@ public CommonResponse<MyListResponse> mySchedule(@CurrentUser User user) {
MyListResponse myList = scheduleService.myList(user.getId());
return CommonResponse.success(200, myList);
}

@Operation(summary = "작성 중인 일정 삭제하기")
@DeleteMapping("/draft/{scheduleId}")
@AuthorizationRequired(value = UserRole.USER)
public CommonResponse<Object> deleteDraftSchedule(
@CurrentUser User user,
@PathVariable Long scheduleId
) {
scheduleService.deleteDraftSchedule(user.getId(), scheduleId);
return CommonResponse.success();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.catcher.datasource;

import com.catcher.common.exception.BaseException;
import com.catcher.core.database.ScheduleRepository;
import com.catcher.core.db.UserRepository;
import com.catcher.core.domain.entity.*;
Expand All @@ -23,13 +24,11 @@
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.*;

import static com.catcher.core.domain.entity.enums.UserProvider.CATCHER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

@Transactional
@ActiveProfiles("test")
Expand Down Expand Up @@ -117,6 +116,7 @@ void upcomingScheduleList_will_return_satisfied_top_7() {
@Test
void empty_upcoming_schedule_list() {
//given
setShouldSkipSetup(true);

//when
List<Schedule> upcomingScheduleList = scheduleRepository.upcomingScheduleList(userList.get(0).getId());
Expand Down Expand Up @@ -374,6 +374,53 @@ void empty_applied_schedule_list() {
assertThat(openScheduleList.size()).isEqualTo(0);
}

@DisplayName("작성 중인 일정을 삭제하는 데 성공하면 상태가 DELETED 이다")
@Test
void success_deleting_draft_schedule() {
//given
Long userId = userList.get(0).getId();
Schedule draftSchedule = generateSchedule(
userList.get(0),
ScheduleStatus.DRAFT,
LocalDateTime.now(),
LocalDateTime.now()
);
scheduleRepository.save(draftSchedule);

//when
scheduleRepository.deleteDraftSchedule(userId, draftSchedule.getId());

flushAndClearPersistence();

Optional<Schedule> updatedSchedule = scheduleRepository.findByIdAndUser(draftSchedule.getId(), userList.get(0));

//then
assertThat(updatedSchedule.get().getScheduleStatus()).isEqualTo(ScheduleStatus.DELETED);

}

@DisplayName("작성 중인 일정을 삭제하는 데 실패하면 예외를 던진다")
@Test
void fail_deleting_draft_schedule() {
//given
Long userId = userList.get(0).getId();
Schedule draftSchedule = generateSchedule(
userList.get(0),
ScheduleStatus.DRAFT,
LocalDateTime.now(),
LocalDateTime.now()
);
scheduleRepository.save(draftSchedule);
flushAndClearPersistence();

//when

//then
assertThrows(BaseException.class, () ->
scheduleRepository.deleteDraftSchedule(userId + 1L, draftSchedule.getId())
);
}

private void setShouldSkipSetup(boolean shouldSkipSetup) {
this.shouldSkipSetup = shouldSkipSetup;
}
Expand Down

0 comments on commit bb805f2

Please sign in to comment.