Skip to content

Commit

Permalink
feat: Event 삭제 API
Browse files Browse the repository at this point in the history
  • Loading branch information
goldentrash committed Aug 19, 2024
1 parent 3db1a6e commit 6dc8709
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import gdsc.konkuk.platformcore.application.event.dtos.EventBrief;
import gdsc.konkuk.platformcore.domain.event.entity.Event;
import gdsc.konkuk.platformcore.external.s3.StorageClient;
import java.util.List;

public class EventMapper {

public static List<EventBrief> mapEventListToEventBriefList(
List<Event> events, StorageClient storageClient) {
List<Event> events) {
return events.stream()
.map(
event ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gdsc.konkuk.platformcore.application.event;

import gdsc.konkuk.platformcore.application.attendance.AttendanceService;
import gdsc.konkuk.platformcore.application.event.dtos.EventBrief;
import gdsc.konkuk.platformcore.application.event.dtos.EventWithAttendance;
import gdsc.konkuk.platformcore.application.event.exceptions.EventErrorCode;
Expand All @@ -8,7 +9,9 @@
import gdsc.konkuk.platformcore.controller.event.dtos.EventDetailResponse;
import gdsc.konkuk.platformcore.controller.event.dtos.EventRegisterRequest;
import gdsc.konkuk.platformcore.controller.event.dtos.EventUpdateRequest;
import gdsc.konkuk.platformcore.domain.attendance.repository.AttendanceRepository;
import gdsc.konkuk.platformcore.domain.event.entity.Event;
import gdsc.konkuk.platformcore.domain.event.entity.EventImage;
import gdsc.konkuk.platformcore.domain.event.repository.EventRepository;
import gdsc.konkuk.platformcore.external.s3.StorageClient;
import gdsc.konkuk.platformcore.global.utils.FileValidator;
Expand All @@ -30,6 +33,8 @@
public class EventService {
private final EventRepository eventRepository;
private final StorageClient storageClient;
private final AttendanceRepository attendanceRepository;
private final AttendanceService attendanceService;

public EventDetailResponse getEvent(Long eventId) {
Event event = findById(eventId);
Expand All @@ -38,7 +43,7 @@ public EventDetailResponse getEvent(Long eventId) {

public EventBriefResponse getAllBriefs() {
List<Event> events = eventRepository.findAll();
List<EventBrief> eventBriefs = EventMapper.mapEventListToEventBriefList(events, storageClient);
List<EventBrief> eventBriefs = EventMapper.mapEventListToEventBriefList(events);
return EventBriefResponse.builder().eventBriefs(eventBriefs).build();
}

Expand Down Expand Up @@ -96,6 +101,18 @@ public void updateRetrospect(Long eventId, String content) {
event.updateRetrospectContent(content);
}

@Transactional
public void delete(Long eventId) {
Event event = findById(eventId);
attendanceRepository.findByEventId(eventId)
.ifPresent(attendance -> attendanceService.deleteAttendance(attendance.getId()));
eventRepository.delete(event);

List<EventImage> eventImages = event.getEventImageList();
List<URL> imageUrls = eventImages.stream().map(EventImage::getUrl).toList();
storageClient.deleteFiles(imageUrls);
}

private Event findById(Long eventId) {
return eventRepository
.findById(eventId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import lombok.RequiredArgsConstructor;

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.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -72,6 +73,12 @@ public ResponseEntity<SuccessResponse> updateRetrospect(
return ResponseEntity.ok(SuccessResponse.messageOnly());
}

@DeleteMapping("/{eventId}")
public ResponseEntity<SuccessResponse> delete(@PathVariable Long eventId) {
eventService.delete(eventId);
return ResponseEntity.noContent().build();
}

private URI getCreatedURI(Long memberId) {
return ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,33 @@ void should_update_retrospect_when_pass_content() throws Exception {
fieldWithPath("data").description("null"))
.build())));
}

@Test
@WithMockUser
@DisplayName("이벤트 삭제 성공")
void should_delete_event_when_pass_event_id() throws Exception {
// given
doNothing().when(eventService).delete(any(Long.class));

// when
ResultActions result =
mockMvc.perform(
RestDocumentationRequestBuilders.delete("/api/v1/events/{eventId}", 1L).with(csrf()));

// then
result
.andDo(print())
.andExpect(status().isNoContent())
.andDo(
document(
"deleteEvent",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
resource(
ResourceSnippetParameters.builder()
.description("이벤트를 삭제할 수 있다")
.tag("events")
.pathParameters(parameterWithName("eventId").description("이벤트 ID"))
.build())));
}
}

0 comments on commit 6dc8709

Please sign in to comment.