Skip to content

Commit

Permalink
[Weekly/11] Image/delete (#114)
Browse files Browse the repository at this point in the history
* feat: 해시태그 네이밍 통일

* feat: 이벤트 응답 수정

* feat: testData 수정
  • Loading branch information
Daolove0323 authored Nov 12, 2024
1 parent 8016c91 commit b0e9977
Show file tree
Hide file tree
Showing 33 changed files with 716 additions and 433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected String getChildPath() {
@Override
protected AdvertisementImage toEntity(ImageRequest imageRequest) {
return AdvertisementImage.builder()
.name(imageRequest.getUrl())
.url(imageRequest.getUrl())
.size(imageRequest.getSize())
.extension(imageRequest.getExtension())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ public T getById(Long id) {
}

protected ImageResponse create(ImageRequest imageRequest) {
return ImageResponse.from(getImageRepository().save(toEntity(imageRequest)), UriUtil.assembleFullUrl(apiUrl, getChildPath()));
T image = toEntity(imageRequest);
return ImageResponse.from(getImageRepository().save(image), getImageUrl(image));
}

public String getImageUrl(T image) {
return UriUtil.assembleFullUrl(apiUrl, getChildPath(), image.getName());
}

protected void delete(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class AdvertisementImage extends Image {
private Advertisement advertisement;

@Builder
public AdvertisementImage(String name, Long size, String extension, Advertisement advertisement) {
super(name, size, extension);
public AdvertisementImage(String url, Long size, String extension, Advertisement advertisement) {
super(url, size, extension);
this.advertisement = advertisement;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public enum ErrorCode {

URL_PARSING_FAILED(HttpStatus.BAD_REQUEST.value(), "-20400", "URL 파싱에 실패했습니다."),

INVALID_IMAGE_DOMAIN(HttpStatus.BAD_REQUEST.value(), "-20400", "해당 이미지 도메인에 대한 서비스가 존재하지 않습니다."),

CURRENT_LOCATION_EMPTY(HttpStatus.BAD_REQUEST.value(), "-20400", "현재 위치 정보를 찾을 수 없습니다.");

private final Integer status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import java.util.stream.Collectors;
import org.ktc2.cokaen.wouldyouin.Image.api.ImageDomain;
import org.ktc2.cokaen.wouldyouin._common.api.ApiResponse;
import org.ktc2.cokaen.wouldyouin._common.api.ApiResponseBody;
import org.ktc2.cokaen.wouldyouin._common.exception.BusinessException;
Expand All @@ -10,6 +11,7 @@
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

@ControllerAdvice
public class GlobalExceptionHandler {
Expand All @@ -19,6 +21,14 @@ public ResponseEntity<ApiResponseBody<Void>> handleBusinessException(BusinessExc
return ApiResponse.error(e.getErrorCode(), e.getMessage());
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<ApiResponseBody<Void>> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) {
if (ex.getRequiredType() == ImageDomain.class) {
return ApiResponse.error(ErrorCode.INVALID_IMAGE_DOMAIN, "해당 이미지 도메인에 대한 서비스가 존재하지 않습니다.");
}
return ApiResponse.error(ErrorCode.INVALID_INPUT_VALUE, "잘못된 요청 값입니다.");
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ApiResponseBody<Void>> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
return ApiResponse.error(ErrorCode.INVALID_INPUT_VALUE, e.getBindingResult().getFieldErrors().stream()
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/org/ktc2/cokaen/wouldyouin/_common/vo/Location.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.ktc2.cokaen.wouldyouin._common.vo;

import jakarta.persistence.Embeddable;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -12,7 +16,17 @@
@AllArgsConstructor
@Embeddable
public class Location {
Double longitude;
Double latitude;

@NotNull(message = "위도 값은 필수입니다.")
@Min(value = -90, message = "위도는 -90 이상이어야 합니다.")
@Max(value = 90, message = "위도는 90 이하여야 합니다.")
private Double latitude;

@NotNull(message = "경도 값은 필수입니다.")
@Min(value = -180, message = "경도는 -180 이상이어야 합니다.")
@Max(value = 180, message = "경도는 180 이하여야 합니다.")
private Double longitude;

@NotEmpty(message = "상세 주소는 필수입니다.")
String detailAddress;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CurationCreateRequest {
@NotNull(message = "지역은 필수입니다.")
private Area area;

private List<String> hashTags;
private List<String> hashtags;

private List<Long> eventIds;

Expand All @@ -49,7 +49,7 @@ public Curation toEntity(Curator curator, List<CurationCard> curationCards, List
.content(this.content)
.curationCards(curationCards)
.area(this.area)
.hashTags(this.hashTags)
.hashtags(this.hashtags)
.events(events)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CurationEditRequest {
@NotNull(message = "지역은 필수입니다.")
private Area area;

private List<String> hashTags;
private List<String> hashtags;

private List<Long> eventIds;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class CurationResponse {
private final Area area;
private final List<String> hashTag;
private final List<CurationEventResponse> eventsInfo;
private LocalDateTime modifiedDate;
private final LocalDateTime createdTime;
private final LocalDateTime modifiedDate;

public static CurationResponse from(Curation curation) {
return CurationResponse.builder()
Expand All @@ -33,7 +33,7 @@ public static CurationResponse from(Curation curation) {
.curationCards(curation.getCurationCards().stream()
.map(CurationCardResponse::from).toList())
.area(curation.getArea())
.hashTag(curation.getHashTags())
.hashTag(curation.getHashtags())
.eventsInfo(curation.getEvents().stream()
.map(CurationEventResponse::from).toList())
.createdTime(curation.getCreatedDate())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class Curation {
private String content;

@OneToMany(mappedBy = "curation", fetch = FetchType.LAZY)
private List<CurationCard> curationCards = new ArrayList<>();
private List<CurationCard> curationCards;

@NotNull
@Enumerated(EnumType.STRING)
Expand All @@ -69,15 +69,15 @@ public class Curation {

@Column(name = "hashtag")
@Convert(converter = HashtagConverter.class)
private List<String> hashTags = new ArrayList<>();
private List<String> hashtags;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "curation_event_relation",
joinColumns = @JoinColumn(name = "curation_id"),
inverseJoinColumns = @JoinColumn(name = "event_id")
)
private List<Event> events = new ArrayList<>();
private List<Event> events;

@CreatedDate
@Column(name = "created_date")
Expand All @@ -88,22 +88,22 @@ public class Curation {
private LocalDateTime modifiedDate;

@Builder
public Curation(Curator curator, String title, String content, List<CurationCard> curationCards, Area area, List<String> hashTags,
public Curation(Curator curator, String title, String content, List<CurationCard> curationCards, Area area, List<String> hashtags,
List<Event> events) {
this.curator = curator;
this.title = title;
this.content = content;
Optional.ofNullable(curationCards).ifPresent(this::setCurationCards);
this.area = area;
Optional.ofNullable(hashTags).ifPresent(this::setHashTags);
Optional.ofNullable(hashtags).ifPresent(this::setHashtags);
Optional.ofNullable(events).ifPresent(this::setEvents);
}

public void updateFrom(CurationEditRequest curationEditRequest, List<CurationCard> curationCards, List<Event> events) {
Optional.ofNullable(curationEditRequest.getTitle()).ifPresent(this::setTitle);
Optional.ofNullable(curationEditRequest.getContent()).ifPresent(this::setContent);
Optional.ofNullable(curationEditRequest.getArea()).ifPresent(this::setArea);
Optional.ofNullable(curationEditRequest.getHashTags()).ifPresent(this::setHashTags);
Optional.ofNullable(curationEditRequest.getHashtags()).ifPresent(this::setHashtags);
Optional.ofNullable(curationCards).ifPresent(this::setCurationCards);
Optional.ofNullable(events).ifPresent(this::setEvents);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ktc2.cokaen.wouldyouin.event.api.dto;

import jakarta.validation.Valid;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.FutureOrPresent;
import jakarta.validation.constraints.Max;
Expand Down Expand Up @@ -33,6 +34,7 @@ public class EventCreateRequest {
@NotNull(message = "지역는 필수입니다.")
private Area area;

@Valid
@NotNull(message = "장소는 필수입니다.")
private Location location;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ktc2.cokaen.wouldyouin.event.api.dto;

import jakarta.validation.Valid;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.FutureOrPresent;
import jakarta.validation.constraints.Max;
Expand Down Expand Up @@ -30,6 +31,7 @@ public class EventEditRequest {
@NotNull(message = "지역는 필수입니다.")
private Area area;

@Valid
@NotNull(message = "장소는 필수입니다.")
private Location location;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.ktc2.cokaen.wouldyouin.event.api.dto;

import java.time.LocalDateTime;
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageResponse;
import org.ktc2.cokaen.wouldyouin._common.vo.Area;
import org.ktc2.cokaen.wouldyouin._common.vo.Category;
import org.ktc2.cokaen.wouldyouin._common.vo.Location;
Expand All @@ -17,6 +19,7 @@ public class EventResponse {
private Long id;
private String title;
private String content;
private List<String> images;
private EventHostResponse host;
private Area area;
private Location location;
Expand All @@ -28,12 +31,13 @@ public class EventResponse {
private Category category;
private Boolean expired;

public static EventResponse from(Event event) {
public static EventResponse from(Event event, List<String> imageUrls) {
Host host = event.getHost();
return EventResponse.builder()
.id(event.getId())
.title(event.getTitle())
.content(event.getContent())
.images(imageUrls)
.host(EventHostResponse.from(host))
.area(event.getArea())
.location(event.getLocation())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageResponse;
import org.ktc2.cokaen.wouldyouin._common.api.SliceInfo;
import org.ktc2.cokaen.wouldyouin.event.persist.Event;
import org.springframework.data.domain.Slice;
Expand All @@ -14,10 +15,9 @@ public class EventSliceResponse {
private List<EventResponse> events;
private SliceInfo sliceInfo;

public static EventSliceResponse from(Slice<Event> reservations, int size, Long lastId) {
public static EventSliceResponse from(List<EventResponse> events, int size, Long lastId) {
return EventSliceResponse.builder()
.events(reservations.stream()
.map(EventResponse::from).toList())
.events(events)
.sliceInfo(SliceInfo.builder()
.sliceSize(size)
.lastId(lastId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.ktc2.cokaen.wouldyouin.Image.api.dto.ImageResponse;
import org.ktc2.cokaen.wouldyouin.Image.application.EventImageService;
import org.ktc2.cokaen.wouldyouin.Image.persist.EventImage;
import org.ktc2.cokaen.wouldyouin._common.exception.EntityNotFoundException;
Expand Down Expand Up @@ -39,7 +40,8 @@ public Event getByIdOrThrow(Long id) throws EntityNotFoundException {

@Transactional(readOnly = true)
public EventResponse getById(Long id) {
return EventResponse.from(getByIdOrThrow(id));
Event event = getByIdOrThrow(id);
return EventResponse.from(event, getImageUrl(event));
}

@Transactional(readOnly = true)
Expand All @@ -51,14 +53,16 @@ public EventSliceResponse getAllByFilterOrderByDistanceAsc(LocationFilter locati
title, category, area, pageable
);
Long newLastId = getLastId(events, beforeLastId);
return EventSliceResponse.from(events, events.getSize(), newLastId);
List<EventResponse> responses = events.stream().map(this::getEventResponse).toList();
return EventSliceResponse.from(responses, events.getSize(), newLastId);
}

@Transactional(readOnly = true)
public EventSliceResponse getAllByHostIdOrderByCreatedDateDesc(Long hostId, Pageable pageable, Long beforeLastId) {
Slice<Event> events = eventRepository.findAllByHostIdOrderByEventIdDesc(hostId, beforeLastId, pageable);
Long newLastId = getLastId(events, beforeLastId);
return EventSliceResponse.from(events, events.getSize(), newLastId);
List<EventResponse> responses = events.stream().map(this::getEventResponse).toList();
return EventSliceResponse.from(responses, events.getSize(), newLastId);
}

@Transactional
Expand All @@ -68,7 +72,7 @@ public EventResponse create(Long hostId, EventCreateRequest eventCreateRequest)
.map(eventImageService::getById).toList();
Event event = eventRepository.save(eventCreateRequest.toEntity(host, images));
images.forEach(image -> eventImageService.setEvent(image, event));
return EventResponse.from(event);
return getEventResponse(event);
}

@Transactional
Expand All @@ -80,7 +84,7 @@ public EventResponse update(Long hostId, Long eventId, EventEditRequest eventEdi
.map(eventImageService::getById).toList();
event.updateFrom(eventEditRequest, images);
images.forEach(image -> eventImageService.setEvent(image, event));
return EventResponse.from(event);
return getEventResponse(event);
}

@Transactional
Expand Down Expand Up @@ -110,4 +114,14 @@ public void validateHostId(Long hostId, Event event) {
throw new UnauthorizedException("호스트 ID가 행사의 호스트 ID와 일치하지 않습니다.");
}
}

private EventResponse getEventResponse(Event event) {
return EventResponse.from(event, getImageUrl(event));
}

private List<String> getImageUrl(Event event) {
return event.getImages().stream()
.map(eventImageService::getImageUrl)
.toList();
}
}
Loading

0 comments on commit b0e9977

Please sign in to comment.