Skip to content

Commit

Permalink
Implement #18 - 어드민 수정 기능 구현
Browse files Browse the repository at this point in the history
수정과 생성을 한 url 로 원활하게 하기 위해
upsert 패턴을 도입
데이터가 생성인지 수정인지를 알아내는 방법으로는
request dto에 `id` 를 추가하여 사용
  • Loading branch information
bmcho committed Aug 18, 2022
1 parent bf9bad9 commit ce4530d
Show file tree
Hide file tree
Showing 14 changed files with 222 additions and 62 deletions.
11 changes: 7 additions & 4 deletions src/main/java/com/bm/getin/controller/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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.*;
Expand Down Expand Up @@ -70,13 +71,14 @@ public String newPlace(Model model) {

@ResponseStatus(HttpStatus.SEE_OTHER)
@PostMapping("/places")
public String createPlace(
public String upsertPlace(
@Valid PlaceRequest placeRequest,
RedirectAttributes redirectAttributes
) {
placeService.createPlace(placeRequest.toDto());
AdminOperationStatus status = placeRequest.id() != null ? AdminOperationStatus.MODIFY : AdminOperationStatus.CREATE;
placeService.upsertPlace(placeRequest.toDto());

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

return "redirect:/admin/confirm";
Expand All @@ -102,9 +104,10 @@ public String createEvent(
@PathVariable Long placeId,
RedirectAttributes redirectAttributes
) {
AdminOperationStatus status = eventRequest.id() != null ? AdminOperationStatus.MODIFY : AdminOperationStatus.CREATE;
eventService.createEvent(eventRequest.toDto(PlaceDto.idOnly(placeId)));

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

return "redirect:/admin/confirm";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import java.time.LocalDateTime;
import java.util.List;

//@RestController
//@RequestMapping("/api")
@RestController
@RequestMapping("/api")
public class ApiPlaceController {

@GetMapping("/places")
Expand All @@ -36,8 +36,8 @@ public ApiDataResponse<Void> createPlace(@RequestBody PlaceRequest placeRequest)

@GetMapping("/places/{placeId}")
public ApiDataResponse<PlaceDto> getPlace(@PathVariable Long placeId) {
if (placeId.equals(2)) {
return ApiDataResponse.of(null);
if (placeId.equals(2L)) {
return ApiDataResponse.empty();
}


Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/bm/getin/dto/EventRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.time.LocalDateTime;

public record EventRequest(
Long id,
@NotBlank String eventName,
@NotNull EventStatus eventStatus,
@NotNull @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime eventStartDatetime,
Expand All @@ -19,6 +20,7 @@ public record EventRequest(
String memo
) {
public static EventRequest of(
Long id,
String eventName,
EventStatus eventStatus,
LocalDateTime eventStartDatetime,
Expand All @@ -28,6 +30,7 @@ public static EventRequest of(
String memo
) {
return new EventRequest(
id,
eventName,
eventStatus,
eventStartDatetime,
Expand All @@ -40,7 +43,7 @@ public static EventRequest of(

public EventDto toDto(PlaceDto placeDto) {
return EventDto.of(
null,
this.id(),
placeDto,
this.eventName(),
this.eventStatus(),
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/bm/getin/dto/PlaceRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.bm.getin.constant.PlaceType;

public record PlaceRequest(
Long id,
PlaceType placeType,
String placeName,
String address,
Expand All @@ -11,19 +12,20 @@ public record PlaceRequest(
String memo
) {
public static PlaceRequest of(
Long id,
PlaceType placeType,
String placeName,
String address,
String phoneNumber,
Integer capacity,
String memo
) {
return new PlaceRequest(placeType, placeName, address, phoneNumber, capacity, memo);
return new PlaceRequest(id, placeType, placeName, address, phoneNumber, capacity, memo);
}

public PlaceDto toDto() {
return PlaceDto.of(
null,
this.id(),
this.placeType(),
this.placeName(),
this.address(),
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/bm/getin/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

@RequiredArgsConstructor
@Transactional
@Service
public class EventService {

private final EventRepository eventRepository;
private final PlaceRepository placeRepository;

@Transactional(readOnly = true)
public List<EventDto> getEvents(Predicate predicate) {
try {
return StreamSupport.stream(eventRepository.findAll(predicate).spliterator(), false)
Expand All @@ -36,6 +38,7 @@ public List<EventDto> getEvents(Predicate predicate) {
}
}

@Transactional(readOnly = true)
public Page<EventViewResponse> getEventViewResponse(
String placeName,
String eventName,
Expand All @@ -58,6 +61,7 @@ public Page<EventViewResponse> getEventViewResponse(
}
}

@Transactional(readOnly = true)
public Optional<EventDto> getEvent(Long eventId) {
try {
return eventRepository.findById(eventId).map(EventDto::of);
Expand All @@ -66,6 +70,18 @@ public Optional<EventDto> getEvent(Long eventId) {
}
}

public boolean upsertEvent(EventDto eventDto) {
try {
if (eventDto.id() != null) {
return modifyEvent(eventDto.id(), eventDto);
} else {
return createEvent(eventDto);
}
} catch (Exception e) {
throw new GeneralException(ErrorCode.DATA_ACCESS_ERROR, e);
}
}

public boolean createEvent(EventDto eventDto) {
try {
if (eventDto == null) {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/bm/getin/service/PlaceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
import com.querydsl.core.types.Predicate;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

@RequiredArgsConstructor
@Transactional
@Service
public class PlaceService {

private final PlaceRepository placeRepository;

@Transactional(readOnly = true)
public List<PlaceDto> getPlaces(Predicate predicate) {
try {
return StreamSupport.stream(placeRepository.findAll(predicate).spliterator(), false)
Expand All @@ -28,6 +31,7 @@ public List<PlaceDto> getPlaces(Predicate predicate) {
}
}

@Transactional(readOnly = true)
public Optional<PlaceDto> getPlace(Long placeId) {
try {
return placeRepository.findById(placeId).map(PlaceDto::of);
Expand All @@ -36,6 +40,18 @@ public Optional<PlaceDto> getPlace(Long placeId) {
}
}

public boolean upsertPlace(PlaceDto placeDto) {
try {
if (placeDto.id() != null) {
return modifyPlace(placeDto.id(), placeDto);
} else {
return createPlace(placeDto);
}
} catch (Exception e) {
throw new GeneralException(ErrorCode.DATA_ACCESS_ERROR, e);
}
}

public boolean createPlace(PlaceDto placeDto) {
try {
if (placeDto == null) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/templates/admin/event-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@
</tr>
<tr>
<td><label for="memo">메모</label></td>
<td><textarea id="memo"></textarea></td>
<td><textarea id="memo" name="memo"></textarea></td>
</tr>
</tbody>
</table>
<input type="hidden" id="eventId" name="id">
</form>
<button id="saveEvent" type="submit">저장</button>
<button id="backToEvents" type="button">취소</button>
Expand Down
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 @@ -13,6 +13,7 @@
<attr sel="#capacity" th:value="*{event?.capacity}" />
<attr sel="#memo" th:text="*{event?.memo}" />
</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="#backToEvents" th:onclick="'location.href=\'' + @{/admin/events} + '\''" />
</thlogic>
3 changes: 2 additions & 1 deletion src/main/resources/templates/admin/place-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@
</tr>
<tr>
<td><label for="memo">메모</label></td>
<td><textarea id="memo"></textarea></td>
<td><textarea id="memo" name="memo"></textarea></td>
</tr>
</tbody>
</table>
<input type="hidden" id="placeId" name="id">
</form>
<button id="savePlace" type="submit">저장</button>
<button id="backToPlaces" type="button">취소</button>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/templates/admin/place-detail.th.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<attr sel="#capacity" th:value="${place?.capacity}" th:min="0" />
<attr sel="#memo" th:text="${place?.memo}" />
</attr>
<attr sel="#placeId" th:value="${place?.id}" />
<attr sel="#savePlace" th:form="placeForm" th:formaction="@{/admin/places}" th:formmethod="post" />
<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})} + '\''" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ void givenNothing_whenRequestingNewPlacePage_thenReturnsNewPlacePage() throws Ex
void givenNewPlace_whenSavingPlace_thenSavesPlaceAndReturnsToListPage() throws Exception {
// Given
PlaceRequest placeRequest = PlaceRequest.of(
null,
PlaceType.SPORTS,
"강남 배드민턴장",
"서울시 강남구 강남동",
Expand Down Expand Up @@ -246,7 +247,7 @@ void givenNothing_whenRequestingNewEventPage_thenReturnsNewEventPage() throws Ex
void givenNewEvent_whenSavingEvent_thenSavesEventAndReturnsToListPage() throws Exception {
// Given
long placeId = 1L;
EventRequest eventRequest = EventRequest.of("test event", EventStatus.OPENED, LocalDateTime.now(), LocalDateTime.now(), 10, 10, null);
EventRequest eventRequest = EventRequest.of(null,"test event", EventStatus.OPENED, LocalDateTime.now(), LocalDateTime.now(), 10, 10, null);
given(eventService.createEvent(eventRequest.toDto(PlaceDto.idOnly(placeId)))).willReturn(true);

// When & Then
Expand Down Expand Up @@ -287,7 +288,7 @@ void given_whenAfterOperation_thenRedirectsToPage() throws Exception {
@Test
void givenPlaceObject_whenConverting_thenReturnsFormData() {
// Given
PlaceRequest placeRequest = PlaceRequest.of(PlaceType.SPORTS, "강남 배드민턴장", "서울시 강남구 강남동", "010-1231-2312", 10, null);
PlaceRequest placeRequest = PlaceRequest.of(null,PlaceType.SPORTS, "강남 배드민턴장", "서울시 강남구 강남동", "010-1231-2312", 10, null);

// When
String result = objectToFormData(placeRequest);
Expand Down
Loading

0 comments on commit ce4530d

Please sign in to comment.