Skip to content

Commit

Permalink
54 redis 사용하여 조회수 좋아요 카운팅 및 인기 포폴 (#76)
Browse files Browse the repository at this point in the history
* refactor: api 스펙 변경 및 코드 리팩터링

* feat: Redis를 활용한 좋아요 누적 후 db 반영

* feat: 좋아요 조회수 누적 합 Map에 저장

* feat: 누적 합 zset에 점수 기록

* feat: zset 인기 포폴 Redis 캐시를 사용하여 조회 완료

* feat: 인기포폴 캐싱 완료

* fix: 좋아요 카운팅 버그 수정

* feat: 중복 조회수 적용

* feat:Redis  중복 조회수 리셋

* feat:Redis 좋아요 취소 Redis에 없을시 DB에서 취소

* feat: 변경사항 저장

* refactor : 리팩터링

* refactor : url 수정 및 security pattern 수정

* fix : 인기순위 score 반영

* refactor : 주석으로 코드 설명

* refactor : test 주석

* refactor : cors config 수정

* fix : Map -> linkedMap으로 변경

* fix : 게더링 쪽 noofffset 수정사항 반영

* fix : 코드리뷰 반영
  • Loading branch information
kcsc2217 authored Dec 4, 2024
1 parent eeab51b commit 7f6b2c3
Show file tree
Hide file tree
Showing 35 changed files with 1,193 additions and 161 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/palettee/PaletteApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableJpaAuditing
@SpringBootApplication
@EnableScheduling
@EnableCaching
public class PaletteApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.palettee.gathering.controller.dto.Response.GatheringCommonResponse;
import com.palettee.gathering.controller.dto.Response.GatheringDetailsResponse;
import com.palettee.gathering.controller.dto.Response.GatheringLikeResponse;
import com.palettee.gathering.controller.dto.Response.GatheringResponse;
import com.palettee.gathering.service.GatheringService;
import com.palettee.global.security.validation.UserUtils;
import com.palettee.portfolio.controller.dto.response.CustomSliceResponse;
Expand All @@ -13,7 +12,6 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -34,15 +32,16 @@ public GatheringCommonResponse create(@RequestBody @Valid GatheringCommonRequest
}

@GetMapping()
public Slice<GatheringResponse> findAll(
public CustomSliceResponse findAll(
@RequestParam(required = false) String sort,
@RequestParam(required = false) String subject,
@RequestParam(required = false) String period,
@RequestParam(required = false) String position,
@RequestParam(required = false) String status,
@RequestParam(required = false) Long gatheringId,
Pageable pageable
) {
return gatheringService.findAll(sort, period, position, status, gatheringId, pageable);
return gatheringService.findAll(sort, subject, period, position, status, gatheringId, pageable);
}

@GetMapping("/{gatheringId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public static GatheringDetailsResponse toDto(Gathering gathering) {

return new GatheringDetailsResponse(
gathering.getUser().getId(),
gathering.getSort().name(),
gathering.getSort().getSort(),
gathering.getUser().getName(),
createTime,
gathering.getSubject().name(),
gathering.getContact().name(),
gathering.getSubject().getSubject(),
gathering.getContact().getContact(),
gathering.getPersonnel(),
gathering.getPeriod(),
deadLine,
gathering.getPosition().name(),
gathering.getPosition().getPosition(),
list,
gathering.getUrl(),
gathering.getTitle(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public record GatheringResponse(
Long gatheringId,
Long userId,
String sort,
String subject,
String title,
String deadLine,
String username,
Expand All @@ -17,12 +18,31 @@ public record GatheringResponse(
) {

public static GatheringResponse toDto(Gathering gathering) {
List<String> list = gathering.getGatheringTagList().stream()
.map(gatheringTag -> gathering.getContent()).toList();

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm");
String deadLine = gathering.getDeadLine().format(formatter);

return new GatheringResponse(gathering.getId(),gathering.getUser().getId(), gathering.getSort().name(), gathering.getTitle(), deadLine, gathering.getUser().getName(), list);
List<String> gatheringTagList = checkGatheringTag(gathering);


return new GatheringResponse(
gathering.getId(),
gathering.getUser().getId(),
gathering.getSort().getSort(),
gathering.getSubject().getSubject(),
gathering.getTitle(),
deadLine,
gathering.getUser().getName(),
gatheringTagList);
}


private static List<String> checkGatheringTag(Gathering gathering) {
if(gathering.getGatheringTagList() != null && !gathering.getGatheringTagList().isEmpty()){
return gathering.getGatheringTagList().stream()
.map(gatheringTag -> gathering.getContent()).toList();
}
return null;
}

}
4 changes: 4 additions & 0 deletions src/main/java/com/palettee/gathering/domain/Contact.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public enum Contact {

private final String contact;

public String getContact(){
return contact;
}


public static Contact findContact(final String input) {
return Arrays.stream(Contact.values())
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/palettee/gathering/domain/Position.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.palettee.gathering.domain;

import com.palettee.user.domain.MajorJobGroup;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;
Expand All @@ -13,6 +12,9 @@ public enum Position {

private final String position;

public String getPosition() {
return position;
}


public static Position findPosition(String input) {
Expand All @@ -23,6 +25,4 @@ public static Position findPosition(String input) {
}




}
4 changes: 4 additions & 0 deletions src/main/java/com/palettee/gathering/domain/Sort.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public enum Sort {

private final String sort;

public String getSort(){
return sort;
}

public static Sort findSort(final String input) {
return Arrays.stream(Sort.values())
.filter(it -> it.sort.equals(input))
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/palettee/gathering/domain/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public enum Status {

private final String status;

public String getStatus(){
return status;
}

public static Status findsStatus(final String input) {
return Arrays.stream(Status.values())
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/palettee/gathering/domain/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public enum Subject {

private final String subject;

public String getSubject(){
return subject;
}

public static Subject finSubject(final String input) {
return Arrays.stream(Subject.values())
.filter(it -> it.subject.equals(input))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

public interface GatheringRepositoryCustom {

Slice<GatheringResponse> pageGathering(
CustomSliceResponse pageGathering(
String sort,
String subject,
String period,
String position,
String status,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package com.palettee.gathering.repository;


import static com.palettee.gathering.domain.QGathering.*;
import static com.palettee.likes.domain.QLikes.*;
import static com.palettee.user.domain.QUser.*;

import com.palettee.gathering.controller.dto.Response.*;
import com.palettee.gathering.domain.Sort;
import com.palettee.gathering.controller.dto.Response.GatheringResponse;
import com.palettee.gathering.domain.*;
import com.palettee.likes.domain.*;
import com.palettee.portfolio.controller.dto.response.*;
import com.palettee.user.controller.dto.response.users.*;
import com.querydsl.core.*;
import com.querydsl.core.types.dsl.*;
import com.querydsl.jpa.impl.*;
import java.util.*;
import java.util.stream.*;
import org.springframework.data.domain.*;
import org.springframework.stereotype.*;
import com.palettee.likes.domain.LikeType;
import com.palettee.portfolio.controller.dto.response.CustomSliceResponse;
import com.palettee.user.controller.dto.response.users.GetUserGatheringResponse;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static com.palettee.gathering.domain.QGathering.gathering;
import static com.palettee.likes.domain.QLikes.likes;
import static com.palettee.user.domain.QUser.user;

@Repository
public class GatheringRepositoryImpl implements GatheringRepositoryCustom {
Expand All @@ -32,8 +33,9 @@ public GatheringRepositoryImpl(JPAQueryFactory queryFactory) {
NoOffSet 방식울 활용한 포트폴리오 전체 조회
*/
@Override
public Slice<GatheringResponse> pageGathering(
public CustomSliceResponse pageGathering(
String sort,
String subject,
String period,
String position,
String status,
Expand All @@ -43,18 +45,20 @@ public Slice<GatheringResponse> pageGathering(
List<Gathering> result = queryFactory
.selectFrom(gathering)
.join(gathering.user, user).fetchJoin()
.where(sortEq(sort), periodEq(period), positionEq(position), statusEq(status), pageIdLt(gatheringId))
.where(sortEq(sort),subjectEq(subject), periodEq(period), positionEq(position), statusEq(status), pageIdLoe(gatheringId))
.orderBy(gathering.id.desc())
.limit(pageable.getPageSize() + 1)
.fetch();

boolean hasNext = hasNextPage(pageable, result);

Long nextId = hasNext ? result.get(result.size() - 1).getId() : null;

List<GatheringResponse> list = result.stream()
.map(GatheringResponse::toDto)
.toList();

return new SliceImpl<>(list, pageable, hasNext);
return new CustomSliceResponse(list, hasNext, nextId);
}

@Override
Expand All @@ -80,7 +84,7 @@ public CustomSliceResponse PageFindLikeGathering(Pageable pageable, Long userId,

boolean hasNext = hasNextPage(pageable, targetIds);

Long nextId = hasNext ? results.get(results.size() -1).get(likes.likeId) : null;
Long nextId = hasNext ? results.get(results.size() - 1).get(likes.likeId) : null;

List<GatheringResponse> list = queryFactory
.selectFrom(gathering)
Expand Down Expand Up @@ -136,6 +140,10 @@ private BooleanExpression sortEq(String sort) {
return sort != null ? gathering.sort.eq(Sort.findSort(sort)) : null;
}

private BooleanExpression subjectEq(String subject){
return subject != null ? gathering.subject.eq(Subject.finSubject(subject)) : null;
}

private BooleanExpression periodEq(String period) {
return period != null ? gathering.period.eq(period) : null;
}
Expand All @@ -148,8 +156,8 @@ private BooleanExpression statusEq(String status) {
return status != null ? gathering.status.eq(Status.findsStatus(status)) : null;
}

private BooleanExpression pageIdLt(Long pageId) {
return pageId != null ? gathering.id.lt(pageId) : null;
private BooleanExpression pageIdLoe(Long pageId) {
return pageId != null ? gathering.id.loe(pageId) : null;
}

private BooleanExpression likeIdLoe(Long likeId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.palettee.gathering.controller.dto.Response.GatheringCommonResponse;
import com.palettee.gathering.controller.dto.Response.GatheringDetailsResponse;
import com.palettee.gathering.controller.dto.Response.GatheringLikeResponse;
import com.palettee.gathering.controller.dto.Response.GatheringResponse;
import com.palettee.gathering.domain.*;
import com.palettee.gathering.repository.GatheringRepository;
import com.palettee.global.s3.service.ImageService;
Expand All @@ -22,7 +21,6 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -65,8 +63,9 @@ public GatheringCommonResponse createGathering(GatheringCommonRequest request, U
return GatheringCommonResponse.toDTO(gatheringRepository.save(gathering));
}

public Slice<GatheringResponse> findAll(
public CustomSliceResponse findAll(
String sort,
String subject,
String period,
String position,
String status,
Expand All @@ -75,6 +74,7 @@ public Slice<GatheringResponse> findAll(
) {
return gatheringRepository.pageGathering(
sort,
subject,
period,
position,
status,
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/palettee/global/Const.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.palettee.global;

public class Const {

public static final String VIEW_PREFIX = "View_";

public static final String LIKE_PREFIX = "Like_";

}
Loading

0 comments on commit 7f6b2c3

Please sign in to comment.