Skip to content

Commit

Permalink
Feat: 좋아요 상태값 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosiee committed May 18, 2024
1 parent 40eb4aa commit 15d1e5c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import project.backend.domain.culturalevent.service.CulturalEventService;
import project.backend.domain.culturalevnetcategory.entity.CategoryTitle;
import project.backend.domain.culturalevnetinfo.service.CulturalEventInfoService;
import project.backend.domain.member.entity.Member;
import project.backend.domain.ticketingsite.mapper.TicketingSiteMapper;
import project.backend.domain.member.service.MemberJwtService;

Expand All @@ -29,6 +30,7 @@ public class CulturalEventController {
private final CulturalEventMapper culturalEventMapper;
private final CulturalEventInfoService culturalEventInfoService;
private final TicketingSiteMapper ticketingSiteMapper;
private final MemberJwtService memberJwtService;

@ApiOperation(value = "문화생활 리스트 조회")
@GetMapping
Expand All @@ -37,16 +39,34 @@ public ResponseEntity getCulturalEventList(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size
) {
List<CulturalEventListDto> CulturalEventResponseDtoList = culturalEventMapper
.culturalEventToCulturalEventListDtos(culturalEventService.getCulturalEventList(type, page, size));
return ResponseEntity.status(HttpStatus.OK).body(CulturalEventResponseDtoList);
// Member
Member member = memberJwtService.getMember();

// Response
List<CulturalEvent> culturalEventList = culturalEventService.getCulturalEventList(type, page, size);
List<CulturalEventListDto> culturalEventResponseDtoList = culturalEventMapper
.culturalEventToCulturalEventListDtos(culturalEventList);
culturalEventResponseDtoList.forEach(dto -> {
dto.setIsOpened();
dto.setIsLiked(member);
});
return ResponseEntity.status(HttpStatus.OK).body(culturalEventResponseDtoList);
}

@ApiOperation(value = "문화생활 객체 조회")
@GetMapping("/{id}")
public ResponseEntity getCulturalEvent(@Positive @PathVariable Long id) {

// Member
Member member = memberJwtService.getMember();

CulturalEvent culturalEvent = culturalEventService.getCulturalEvent(id);
CulturalEventRetrieveDto culturalEventRetrieveDto = culturalEventMapper.culturalEventToCulturalEventRetrieveDto(culturalEvent);

// Like와 Open 정보
culturalEventRetrieveDto.setIsLiked(member);
culturalEventRetrieveDto.setIsOpened();

// information 추가
culturalEventRetrieveDto.setInformationList(culturalEventInfoService.getImageUrlList(culturalEvent));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package project.backend.domain.culturalevent.dto;

import lombok.*;
import project.backend.domain.culturalevent.entity.CulturalEvent;
import project.backend.domain.culturalevent.entity.CulturalEventStatus;
import project.backend.domain.culturaleventlike.entity.CulturalEventLike;
import project.backend.domain.member.entity.Member;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CulturalEventListDto {

private Long id;
private String thumbnailImageUrl;
private String categoryName;
Expand All @@ -20,6 +26,19 @@ public class CulturalEventListDto {
private Date startDate;
private Date endDate;
private LocalDateTime ticketOpenDate;
private Boolean isLiked; // TODO : 구현 이전
private Boolean isLiked;
private Boolean isOpened;

public void setIsLiked(Member member) {
if (member == null) {
this.isLiked = false;
} else {
this.isLiked = member.getCulturalEventLikeList().stream().anyMatch(like -> Objects.equals(like.culturalEvent.id, this.id));
}
}

public void setIsOpened() {
LocalDateTime now = LocalDateTime.now();
this.isOpened = !now.isBefore(this.ticketOpenDate);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package project.backend.domain.culturalevent.dto;

import lombok.*;
import project.backend.domain.member.entity.Member;
import project.backend.domain.place.dto.PlaceRetrieveDto;
import project.backend.domain.ticketingsite.dto.TicketingSiteListDto;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Getter
@Setter
Expand All @@ -28,5 +30,20 @@ public class CulturalEventRetrieveDto {
private String viewRateName;
private Integer likeCount;
private Integer visitCount;
private Boolean isLiked;
private Boolean isOpened;
private List<String> informationList;

public void setIsLiked(Member member) {
if (member == null) {
this.isLiked = false;
} else {
this.isLiked = member.getCulturalEventLikeList().stream().anyMatch(like -> Objects.equals(like.culturalEvent.id, this.id));
}
}

public void setIsOpened() {
LocalDateTime now = LocalDateTime.now();
this.isOpened = !now.isBefore(this.ticketOpenDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import project.backend.domain.culturaleventlike.entity.CulturalEventLike;
import project.backend.domain.culturalevnetcategory.entity.CulturalEventCategory;
import project.backend.domain.culturalevnetinfo.entity.CulturalEventInfo;
import project.backend.domain.member.entity.Member;
import project.backend.domain.place.entity.Place;
import project.backend.domain.ticketingsite.entity.TicketingSite;

Expand Down Expand Up @@ -122,6 +123,10 @@ public void setCulturalEventCategory(CulturalEventCategory culturalEventCategory
}
}

public Optional<CulturalEventLike> findMemberLike(Member member) {
return this.getCulturalEventLikeList().stream().filter(culturalEventLike -> culturalEventLike.member == member).findFirst();
}

public void increaseLikeCount() {
this.likeCount = this.likeCount + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@

import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;
import project.backend.domain.culturalevent.dto.CulturalEventCreateDto;
import project.backend.domain.culturalevent.dto.CulturalEventListDto;
import project.backend.domain.culturalevent.dto.CulturalEventRetrieveDto;
import project.backend.domain.culturalevent.entity.CulturalEvent;
import project.backend.domain.culturalevnetinfo.entity.CulturalEventInfo;
import project.backend.domain.member.entity.Member;

import java.time.LocalDateTime;
import java.util.List;

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface CulturalEventMapper {



CulturalEventMapper INSTANCE = Mappers.getMapper(CulturalEventMapper.class);

@Mapping(source = "culturalEventCategory.title.name", target = "categoryName")
Expand All @@ -29,18 +26,9 @@ public interface CulturalEventMapper {

@Mapping(source = "culturalEventCategory.title.name", target = "categoryName")
@Mapping(source = "place.name", target = "placeName")
@Mapping(target = "isOpened", expression = "java(calculateIsOpened(culturalEvent.getTicketOpenDate()))")
CulturalEventListDto culturalEventToCulturalEventListDto(CulturalEvent culturalEvent);

List<CulturalEventListDto> culturalEventToCulturalEventListDtos(List<CulturalEvent> culturalEventList);

default Boolean calculateIsOpened(LocalDateTime ticketOpenDate) {
LocalDateTime now = LocalDateTime.now();

if (now.isBefore(ticketOpenDate)) {
return false;
} else {
return true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,11 @@ public CulturalEvent getCulturalEvent(Long id) {
return verifiedCulturalEvent(id);
}

public Optional<CulturalEventLike> findMemberLike(Member member, CulturalEvent culturalEvent) {
return culturalEvent.getCulturalEventLikeList().stream().filter(culturalEventLike -> culturalEventLike.member == member).findFirst();
}

public void like(Long id) {
Member member = memberJwtService.getMember();
CulturalEvent culturalEvent = getCulturalEvent(id);

if (findMemberLike(member, culturalEvent).isPresent()) {
if (culturalEvent.findMemberLike(member).isPresent()) {
return;
}

Expand All @@ -63,7 +59,7 @@ public void like(Long id) {
public void unLike(Long id) {
Member member = memberJwtService.getMember();
CulturalEvent culturalEvent = getCulturalEvent(id);
Optional<CulturalEventLike> culturalEventLike = findMemberLike(member, culturalEvent);
Optional<CulturalEventLike> culturalEventLike = culturalEvent.findMemberLike(member);

if (culturalEventLike.isEmpty()) {
return;
Expand Down

0 comments on commit 15d1e5c

Please sign in to comment.