Skip to content

Commit

Permalink
Implement #18 - 어드민 삭제 기능 구현
Browse files Browse the repository at this point in the history
삭제는 평범한 get 링크 호출로 구현
추후 리팩토링을 할 수 있도록
설계 부분에서 개선의 여지를 남겨둠

on delete cascade 설정을 admin - place 간에 추가
이 매핑 데이터는 삭제에 민감하지 않은 편으로 판단
event - place 간에는 룰을 추가하지 않음
연결된 이벤트가 있어서 함부로 지우면 안되는 장소를
삭제할 경우 에러가 나도록.
  • Loading branch information
bmcho committed Aug 18, 2022
1 parent ce4530d commit 71c2cec
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
42 changes: 27 additions & 15 deletions src/main/java/com/bm/getin/controller/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.bm.getin.constant.ErrorCode;
import com.bm.getin.constant.EventStatus;
import com.bm.getin.constant.PlaceType;
import com.bm.getin.domain.Admin;
import com.bm.getin.domain.Event;
import com.bm.getin.domain.Place;
import com.bm.getin.dto.*;
import com.bm.getin.exception.GeneralException;
Expand Down Expand Up @@ -84,6 +82,21 @@ public String upsertPlace(
return "redirect:/admin/confirm";
}

@ResponseStatus(HttpStatus.SEE_OTHER)
@PostMapping("/places/{placeId}/delete")
public String deletePlace(
@PathVariable Long placeId,
RedirectAttributes redirectAttributes
) {
placeService.removePlace(placeId);

redirectAttributes.addFlashAttribute("adminOperationStatus", AdminOperationStatus.DELETE);
redirectAttributes.addFlashAttribute("redirectUrl", "/admin/places");

return "redirect:/admin/confirm";
}


@GetMapping("/places/{placeId}/newEvent")
public String newEvent(@PathVariable Long placeId, Model model) {
EventResponse event = placeService.getPlace(placeId)
Expand All @@ -99,33 +112,32 @@ public String newEvent(@PathVariable Long placeId, Model model) {

@ResponseStatus(HttpStatus.SEE_OTHER)
@PostMapping("/places/{placeId}/events")
public String createEvent(
public String upsertEvent(
@Valid EventRequest eventRequest,
@PathVariable Long placeId,
RedirectAttributes redirectAttributes
) {
AdminOperationStatus status = eventRequest.id() != null ? AdminOperationStatus.MODIFY : AdminOperationStatus.CREATE;
eventService.createEvent(eventRequest.toDto(PlaceDto.idOnly(placeId)));
eventService.upsertEvent(eventRequest.toDto(PlaceDto.idOnly(placeId)));

redirectAttributes.addFlashAttribute("adminOperationStatus", status);
redirectAttributes.addFlashAttribute("redirectUrl", "/admin/places/" + placeId);

return "redirect:/admin/confirm";
}

@ResponseStatus(HttpStatus.SEE_OTHER)
@GetMapping("/events/{eventId}/delete")
public String deleteEvent(
@PathVariable Long eventId,
RedirectAttributes redirectAttributes
) {
eventService.removeEvent(eventId);

@GetMapping("/events")
public ModelAndView adminEvents(@QuerydslPredicate(root = Event.class) Predicate predicate) {

List<EventResponse> events = eventService.getEvents(predicate)
.stream()
.map(EventResponse::from)
.toList();
redirectAttributes.addFlashAttribute("adminOperationStatus", AdminOperationStatus.DELETE);
redirectAttributes.addFlashAttribute("redirectUrl", "/admin/events");

return new ModelAndView("admin/events", Map.of(
"events", events,
"eventStatusOption", EventStatus.values()
));
return "redirect:/admin/confirm";
}

@GetMapping("/events/{eventId}")
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/bm/getin/domain/Place.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class Place {

@ToString.Exclude
@OrderBy("id")
@OneToMany(mappedBy = "place")
@OneToMany(mappedBy = "place", cascade = CascadeType.REMOVE)
private final Set<AdminPlaceMap> adminPlaceMaps = new LinkedHashSet<>();

protected Place() {}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/templates/admin/event-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<input type="hidden" id="eventId" name="id">
</form>
<button id="saveEvent" type="submit">저장</button>
<button id="removeEvent" type="button">삭제</button>
<button id="backToEvents" type="button">취소</button>
</body>
</html>
1 change: 1 addition & 0 deletions src/main/resources/templates/admin/event-detail.th.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
</attr>
<attr sel="#eventId" th:value="${event?.id}" />
<attr sel="#saveEvent" th:form="eventForm" th:formaction="@{/admin/places/{placeId}/events(placeId=${event?.place.id})}" th:formmethod="post" />
<attr sel="#removeEvent" th:onclick="'location.href=\'' + @{/admin/events/{eventId}/delete(eventId=${event?.id})} + '\''" />
<attr sel="#backToEvents" th:onclick="'location.href=\'' + @{/admin/events} + '\''" />
</thlogic>
1 change: 1 addition & 0 deletions src/main/resources/templates/admin/place-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<input type="hidden" id="placeId" name="id">
</form>
<button id="savePlace" type="submit">저장</button>
<button id="removePlace" type="button">삭제</button>
<button id="backToPlaces" type="button">취소</button>
<hr>
<!-- TODO: 여기에 장소와 연관된 이벤트들이 리스트로 보이면 좋을 듯 -->
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/templates/admin/place-detail.th.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</attr>
<attr sel="#placeId" th:value="${place?.id}" />
<attr sel="#savePlace" th:form="placeForm" th:formaction="@{/admin/places}" th:formmethod="post" />
<attr sel="#removePlace" th:onclick="'location.href=\'' + @{/admin/places/{placeId}/delete(placeId=${place?.id})} + '\''" />
<attr sel="#backToPlaces" th:onclick="'location.href=\'' + @{/admin/places} + '\''" />
<attr sel="#newEvent" th:if="${place} != null" th:onclick="'location.href=\'' + @{/admin/places/{placeId}/newEvent(placeId=${place.id})} + '\''" />
<attr sel="#newEvent" th:if="${place} != null" th:onclick="'location.href=\'' + @{/admin/places/{placeId}/newEvent(placeId=${place?.id})} + '\''" />
</thlogic>

0 comments on commit 71c2cec

Please sign in to comment.