-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
dateroad-api/src/main/java/org/dateroad/advertisment/dto/response/AdvGetDetailRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.dateroad.advertisment.dto.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
|
||
@Builder(access = AccessLevel.PROTECTED) | ||
public record AdvGetDetailRes( | ||
List<AdvImagesRes> images, | ||
String title, | ||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy.MM.dd", timezone = "Asia/Seoul") | ||
LocalDate createAt, | ||
String description | ||
) { | ||
public static AdvGetDetailRes of(List<AdvImagesRes> images, String title, LocalDate createAt, String description) { | ||
return AdvGetDetailRes.builder() | ||
.images(images) | ||
.title(title) | ||
.createAt(createAt) | ||
.description(description) | ||
.build(); | ||
} | ||
|
||
@Builder(access = AccessLevel.PROTECTED) | ||
public record AdvImagesRes( | ||
String imagesUrl, | ||
int sequence | ||
) { | ||
public static AdvImagesRes of(String imagesUrl, int sequence) { | ||
return AdvImagesRes.builder() | ||
.imagesUrl(imagesUrl) | ||
.sequence(sequence) | ||
.build(); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
dateroad-api/src/main/java/org/dateroad/advertisment/service/AdvertismentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,57 @@ | ||
package org.dateroad.advertisment.service; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.dateroad.adImage.domain.AdImage; | ||
import org.dateroad.adImage.repository.AdImageRepository; | ||
import org.dateroad.advertisement.domain.Advertisment; | ||
import org.dateroad.advertisement.repository.AdvertismentRepository; | ||
import org.dateroad.advertisment.dto.response.AdvGetAllRes; | ||
import org.dateroad.advertisment.dto.response.AdvGetAllRes.AdvertismentDtoRes; | ||
import org.dateroad.advertisment.dto.response.AdvGetDetailRes; | ||
import org.dateroad.advertisment.dto.response.AdvGetDetailRes.AdvImagesRes; | ||
import org.dateroad.code.FailureCode; | ||
import org.dateroad.exception.EntityNotFoundException; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class AdvertismentService { | ||
private final AdvertismentRepository advertismentRepository; | ||
private final AdImageRepository adImageRepository; | ||
|
||
private static List<AdvImagesRes> getImages(final List<AdImage> adImages) { | ||
return adImages.stream().map( | ||
adImage -> AdvImagesRes.of(adImage.getImageUrl(), adImage.getSequence()) | ||
).toList(); | ||
} | ||
|
||
public AdvGetAllRes getAllAdvertisments() { | ||
Pageable topFive = PageRequest.of(0, 5); | ||
return AdvGetAllRes.of(advertismentRepository.findTop5ByOrderByCreatedDateDesc(topFive). | ||
stream() | ||
.map(AdvertismentDtoRes::of) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
public AdvGetDetailRes getAdvertismentsDetail(final Long advId) { | ||
Advertisment advertisment = getAdvertisment(advId); | ||
List<AdImage> adImages = adImageRepository.findAllById(advId); | ||
return AdvGetDetailRes.of( | ||
getImages(adImages), advertisment.getTitle(), advertisment.getCreatedAt().toLocalDate(), | ||
advertisment.getTitle() | ||
); | ||
} | ||
|
||
private Advertisment getAdvertisment(final Long advId) { | ||
return advertismentRepository.findById(advId).orElseThrow( | ||
() -> new EntityNotFoundException(FailureCode.ADVERTISMENT_NOT_FOUND) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters