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 7f5d395 commit 04c05f8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ public class CulturalEventController {
public ResponseEntity getCulturalEventList(
@RequestParam() CategoryTitle type,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size
) {
@RequestParam(defaultValue = "10") int size,
@RequestParam(required = false) String ordering
) {
// Member
Member member = memberJwtService.getMember();

// Response
List<CulturalEvent> culturalEventList = culturalEventService.getCulturalEventList(type, page, size);
List<CulturalEvent> culturalEventList = culturalEventService.getCulturalEventList(type, page, size, ordering);
List<CulturalEventListDto> culturalEventResponseDtoList = culturalEventMapper
.culturalEventToCulturalEventListDtos(culturalEventList);
culturalEventResponseDtoList.forEach(dto -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class CulturalEvent extends BaseEntity {

public Integer visitCount = 0;

public Integer point = 0;

@Column(columnDefinition = "TEXT")
public String information;

Expand Down Expand Up @@ -137,13 +139,16 @@ public Optional<CulturalEventVisit> findMemberVisit(Member member) {

public void increaseLikeCount() {
this.likeCount = this.likeCount + 1;
this.point = this.point + 2;
}

public void decreaseLikeCount() {
this.likeCount = this.likeCount - 1;
this.point = this.point - 2;
}

public void increaseVisitCount() {
this.visitCount = this.visitCount + 1;
this.point = this.point + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ public interface CulturalEventRepository extends JpaRepository<CulturalEvent, Lo
Page<CulturalEvent> findAll(Pageable pageable);

Page<CulturalEvent> findAllByCulturalEventCategory(Pageable pageable, CulturalEventCategory culturalEventCategory);


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import project.backend.domain.culturalevent.entity.CulturalEvent;
Expand All @@ -24,7 +25,6 @@
import java.util.List;
import java.util.Optional;

import static org.springframework.data.jpa.domain.AbstractAuditable_.createdDate;

@Service
@RequiredArgsConstructor
Expand All @@ -36,8 +36,8 @@ public class CulturalEventService {
private final MemberJwtService memberJwtService;
private final CulturalEventVisitRepository culturalEventVisitRepository;

public List<CulturalEvent> getCulturalEventList(CategoryTitle type, int page, int size) {
Pageable pageable = PageRequest.of(page, size);
public List<CulturalEvent> getCulturalEventList(CategoryTitle type, int page, int size, String ordering) {
Pageable pageable = PageRequest.of(page, size, createSort(ordering));
if (type == CategoryTitle.ALL || type == null) {
return culturalEventRepository.findAll(pageable).getContent();
} else {
Expand Down Expand Up @@ -99,4 +99,11 @@ public void visit(CulturalEvent culturalEvent) {
private CulturalEvent verifiedCulturalEvent(Long id) {
return culturalEventRepository.findById(id).orElseThrow(() -> new BusinessException(ErrorCode.CULTURAL_EVENT));
}

private Sort createSort(String ordering) {
if (ordering == null || ordering.isEmpty()) {
return Sort.by(Sort.Direction.ASC, "id");
}
return Sort.by(Sort.Direction.DESC, ordering);
}
}

0 comments on commit 04c05f8

Please sign in to comment.