Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.gam.api.domain.magazine.dto.query;

public record MagazineWithScrapQueryDTO(
Long magazineId,
String thumbnail,
String magazineTitle,
String interviewPerson,
Long viewCount,
boolean isScraped
) {
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package com.gam.api.domain.magazine.dto.response;

import com.gam.api.domain.magazine.entity.Magazine;
import com.gam.api.domain.magazine.dto.query.MagazineWithScrapQueryDTO;
import lombok.Builder;
import lombok.val;

import java.util.List;

@Builder
public record MagazineResponseDTO(
List<MagazineResponseVO> magazineList
) {
public static MagazineResponseDTO of(List<Magazine> magazineList, List<Long> userMagazineScraps, String magazineBaseUrl) {
public static MagazineResponseDTO of(List<MagazineWithScrapQueryDTO> magazineList, String magazineBaseUrl) {
return MagazineResponseDTO.builder()
.magazineList(magazineList.stream().map(magazine -> {
val isScraped = userMagazineScraps.contains(magazine.getId()) ? true : false;
return MagazineResponseVO.of(magazine, isScraped, magazineBaseUrl);
}).toList())
.magazineList(magazineList.stream().map(magazineWithScrap -> MagazineResponseVO.of(
magazineWithScrap.magazineId(),
magazineWithScrap.thumbnail(),
magazineWithScrap.magazineTitle(),
magazineWithScrap.interviewPerson(),
magazineWithScrap.viewCount(),
magazineWithScrap.isScraped(),
magazineBaseUrl
)).toList()
)
.build();
}
}


@Builder
record MagazineResponseVO(
Long magazineId,
Expand All @@ -30,14 +36,22 @@ record MagazineResponseVO(
Long view,
boolean isScraped
) {
public static MagazineResponseVO of(Magazine magazine, boolean isScraped, String magazineBaseUrl) {
public static MagazineResponseVO of(
Long magazineId,
String thumbnail,
String magazineTitle,
String interviewPerson,
Long viewCount,
boolean isScraped,
String magazineBaseUrl
) {
return MagazineResponseVO.builder()
.magazineId(magazine.getId())
.thumbNail(magazine.getThumbNail())
.title(magazine.getMagazineTitle())
.interviewPerson(magazine.getInterviewPerson())
.magazineUrl(magazineBaseUrl + magazine.getId())
.view(magazine.getViewCount())
.magazineId(magazineId)
.thumbNail(thumbnail)
.title(magazineTitle)
.interviewPerson(interviewPerson)
.magazineUrl(magazineBaseUrl + magazineId)
.view(viewCount)
.isScraped(isScraped)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.gam.api.domain.magazine.repository;

import com.gam.api.domain.magazine.dto.query.MagazineWithScrapQueryDTO;
import com.gam.api.domain.magazine.entity.Magazine;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -10,8 +12,27 @@

public interface MagazineRepository extends JpaRepository<Magazine, Long> {
Optional<Magazine> getMagazineById(Long magazineId);
List<Magazine> findMagazinesByOrderByCreatedAtDesc();
List<Magazine> findTop3ByOrderByViewCountDesc();

@Query("SELECT new com.gam.api.domain.magazine.dto.query.MagazineWithScrapQueryDTO(" +
"m.id, " +
"m.thumbNail, " +
"m.magazineTitle, " +
"m.interviewPerson, " +
"m.viewCount, " +
"EXISTS (SELECT 1 FROM MagazineScrap ms WHERE ms.magazine = m AND ms.user.id = :userId AND ms.status = true)) " +
"FROM Magazine m ORDER BY m.createdAt DESC")
List<MagazineWithScrapQueryDTO> findMagazinesByOrderByCreatedAtDesc(@Param("userId") Long userId);

@Query("SELECT new com.gam.api.domain.magazine.dto.query.MagazineWithScrapQueryDTO(" +
"m.id, " +
"m.thumbNail, " +
"m.magazineTitle, " +
"m.interviewPerson, " +
"m.viewCount, " +
"EXISTS (SELECT 1 FROM MagazineScrap ms WHERE ms.magazine = m AND ms.user.id = :userId AND ms.status = true)) " +
"FROM Magazine m ORDER BY m.viewCount DESC")
List<MagazineWithScrapQueryDTO> findTopMagazinesWithScrapStatus(@Param("userId") Long userId, Pageable pageable);

List<Magazine> findAllByOrderByModifiedAtDescCreatedAtDesc();
@Query("SELECT m FROM Magazine m WHERE LOWER(m.interviewPerson) LIKE LOWER(CONCAT('%', :interviewPersonKeyword, '%')) OR LOWER(m.magazineTitle) LIKE LOWER(CONCAT('%', :magazineTitleKeyword, '%')) ORDER BY m.createdAt DESC")
List<Magazine> finAllByKeyword(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public interface MagazineService {
MagazineResponseDTO getPopularMagazines(Long userId);
MagazineScrapResponseDTO scrapMagazine(Long userId, MagazineScrapRequestDTO magazineScrapRequestDTO);
List<MagazineSearchResponseDTO> searchMagazine(String keyword);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.gam.api.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -38,11 +39,9 @@ public class MagazineServiceImpl implements MagazineService {

@Override
public MagazineResponseDTO getMagazines(Long userId) {
val user = findUser(userId);
val magazineScrapList = getMagazineScrapList(user);
val magazineList = magazineRepository.findMagazinesByOrderByCreatedAtDesc();
val magazineList = magazineRepository.findMagazinesByOrderByCreatedAtDesc(userId);

return MagazineResponseDTO.of(magazineList, magazineScrapList, gamConfig.getMagaineBaseUrl());
return MagazineResponseDTO.of(magazineList, gamConfig.getMagaineBaseUrl());
}

@Transactional
Expand Down Expand Up @@ -79,12 +78,11 @@ public MagazineScrapsResponseDTO getMagazineScraps(Long userId) {
}

@Override
@Transactional
public MagazineResponseDTO getPopularMagazines(Long userId) {
val user = findUser(userId);
val magazineScrapList = getMagazineScrapList(user);
val magazineList = magazineRepository.findTop3ByOrderByViewCountDesc();
val magazineList = magazineRepository.findTopMagazinesWithScrapStatus(userId, PageRequest.of(0, 3));

return MagazineResponseDTO.of(magazineList, magazineScrapList, gamConfig.getMagaineBaseUrl());
return MagazineResponseDTO.of(magazineList, gamConfig.getMagaineBaseUrl());
}

@Transactional
Expand Down Expand Up @@ -132,12 +130,6 @@ public List<MagazineSearchResponseDTO> searchMagazine(String keyword) {
.collect(Collectors.toList());
}

private List<Long> getMagazineScrapList(User user) {
return user.getMagazineScraps().stream()
.map(MagazineScrap::getMagazineId)
.toList();
}

private User findUser(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException(ExceptionMessage.NOT_FOUND_USER.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,12 @@ public UserDetails loadUserByUsername(String userId) {
val userRole = user.getRole();
val userStatus = user.getUserStatus();

val authUser = authProviderRepository.searchAuthProviderByUser(user)
.orElseThrow(() -> new EntityNotFoundException(ExceptionMessage.TOKEN_USER_EXCEPTION.getMessage()));

val authUserId = String.valueOf(authUser.getId());

authorities.add(new SimpleGrantedAuthority("ROLE_"+userRole.toString()));
authorities.add(new SimpleGrantedAuthority(userStatus.toString()));

return GamUserDetails.builder()
.id(user.getId())
.user(user)
.authUserId(authUserId)
.username(user.getUserName())
.authorities(authorities)
.build();
Expand Down