Skip to content

Commit

Permalink
Merge pull request #38 from syyling/feat/33
Browse files Browse the repository at this point in the history
[feat] 홈화면 다른 사용자가 기록한 영화 조회 기능 추가
  • Loading branch information
syyling authored May 27, 2024
2 parents baef90a + fd88e51 commit 7c6899a
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,29 @@ public RecordCalendarResponse(String rating, String review, String title, String

@Getter
@NoArgsConstructor
public static class RecordHomeResponse{
public static class RecordHomeByUserResponse{
private String poster;
private String tmdbId;

@Builder
public RecordHomeResponse(String poster, String tmdbId) {
public RecordHomeByUserResponse(String poster, String tmdbId) {
this.poster = poster;
this.tmdbId = tmdbId;
}
}


@Getter
@NoArgsConstructor
public static class RecordHomeResponse {
private String tmdbId;
private String title;
private String poster;

@Builder
public RecordHomeResponse(String tmdbId, String title, String poster) {
this.tmdbId = tmdbId;
this.title = title;
this.poster = poster;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,16 @@ public static RecordResponse.RecordCalendarResponse mapToRecordCalendarResponse(
.build();
}

public static RecordResponse.RecordHomeByUserResponse mapToRecordHomeByUserResponse(Movie movie) {
return RecordResponse.RecordHomeByUserResponse.builder()
.poster(movie.getPoster())
.tmdbId(movie.getTmdbId())
.build();
}

public static RecordResponse.RecordHomeResponse mapToRecordHomeResponse(Movie movie) {
return RecordResponse.RecordHomeResponse.builder()
.title(movie.getTitle())
.poster(movie.getPoster())
.tmdbId(movie.getTmdbId())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import com.mookive.mookive_backend.record.application.dto.response.RecordResponse;
import com.mookive.mookive_backend.record.application.mapper.RecordMapper;
import com.mookive.mookive_backend.record.domain.entity.Record;
import com.mookive.mookive_backend.record.domain.repository.MovieMapping;
import com.mookive.mookive_backend.record.domain.service.RecordQueryService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.*;

@Service
@RequiredArgsConstructor
Expand All @@ -25,7 +25,9 @@ public class RecordGetService {
private final RecordQueryService recordQueryService;
private final KeywordQueryService keywordQueryService;
private final MovieQueryService movieQueryService;
private static final int HOME_RECORDS = 5;

private static final int HOME_RECORDS_BY_USER = 5;
private static final int HOME_RECORDS = 10;

public RecordResponse.RecordDetailResponse getRecord(Long userId, Long movieId) {
Optional<Record> record = recordQueryService.findByUserIdAndMovieId(userId, movieId);
Expand All @@ -47,7 +49,7 @@ public RecordResponse.RecordDetailResponse getRecord(Long userId, Long movieId)
}
}

public List<List<RecordResponse.RecordCalendarResponse>> getRecordByCalendar(String year, String month) {
public List<List<RecordResponse.RecordCalendarResponse>> getRecordOfCalender(String year, String month) {
String start = "01"; String end = "31";
LocalDate startDate = getLocalDate(year, month, start);
LocalDate endDate = getLocalDate(year, month, end);
Expand Down Expand Up @@ -86,15 +88,47 @@ private static LocalDate getLocalDate(String year, String month, String day) {
return LocalDate.parse(localDate, formatter);
}

public List<RecordResponse.RecordHomeResponse> getRecordByHome(Long userId) {
List<RecordResponse.RecordHomeResponse> recordHomeResponseList = new ArrayList<>(HOME_RECORDS);
List<Record> records = recordQueryService.findByUserIdOrderByUpdatedAtDesc(userId);
public List<RecordResponse.RecordHomeByUserResponse> getRecordOfHomeByUser(Long userId) {
List<RecordResponse.RecordHomeByUserResponse> recordHomeResponseList = new ArrayList<>(HOME_RECORDS_BY_USER);
List<Record> records = recordQueryService.findTop5ByUserIdOrderByUpdatedAtDesc(userId);
for(int i=0; i<records.size(); i++) {
Long movieId = records.get(i).getMovie().getId();
Movie movie = movieQueryService.findById(movieId);
RecordResponse.RecordHomeResponse recordHomeResponse = RecordMapper.mapToRecordHomeResponse(movie);
RecordResponse.RecordHomeByUserResponse recordHomeResponse = RecordMapper.mapToRecordHomeByUserResponse(movie);
recordHomeResponseList.add(recordHomeResponse);
}
return recordHomeResponseList;
}

/**
* Todo: 성능개선
*/
public List<RecordResponse.RecordHomeResponse> getRecordOfHome(Long userId) {
List<RecordResponse.RecordHomeResponse> recordHomeResponseList = new ArrayList<>();
HashMap<Long, Integer> movieIdMap = new HashMap<>();

List<Record> records = recordQueryService.findTop20ByOrderByUpdatedAtDesc();
List<MovieMapping> recordByUserList = recordQueryService.findAllByUserId(userId);
List<Movie> recordByUserMovieList = new ArrayList<>();
for(MovieMapping movieMapping : recordByUserList) {
recordByUserMovieList.add(movieMapping.getMovie());
}

for(Record record : records) {
if(recordHomeResponseList.size() == HOME_RECORDS) break;
if(!Objects.equals(record.getUser().getId(), userId)) {
if(movieIdMap.containsKey(record.getMovie().getId()) || recordByUserMovieList.contains(record.getMovie())) {
continue;
}
else {
Long movieId = record.getMovie().getId();
movieIdMap.put(movieId, 1);
Movie movie = movieQueryService.findById(movieId);
RecordResponse.RecordHomeResponse recordHomeResponse = RecordMapper.mapToRecordHomeResponse(movie);
recordHomeResponseList.add(recordHomeResponse);
}
}
}
return recordHomeResponseList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mookive.mookive_backend.record.domain.repository;

import com.mookive.mookive_backend.movie.domain.entity.Movie;

public interface MovieMapping {

Movie getMovie();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ public interface RecordRepository extends JpaRepository<Record, Long> {

List<Record> findByDateBetween(LocalDate startDate, LocalDate endDate);

List<Record> findByUserIdOrderByUpdatedAtDesc(Long userId);
List<Record> findTop5ByUserIdOrderByUpdatedAtDesc(Long userId);

List<Record> findTop20ByOrderByUpdatedAtDesc();

List<MovieMapping> findAllByUserId(Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mookive.mookive_backend.record.domain.service;

import com.mookive.mookive_backend.record.domain.entity.Record;
import com.mookive.mookive_backend.record.domain.repository.MovieMapping;
import com.mookive.mookive_backend.record.domain.repository.RecordRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -30,7 +31,15 @@ public List<Record> findByDateBetween(LocalDate startDate, LocalDate endDate) {
return recordRepository.findByDateBetween(startDate, endDate);
}

public List<Record> findByUserIdOrderByUpdatedAtDesc(Long userId) {
return recordRepository.findByUserIdOrderByUpdatedAtDesc(userId);
public List<Record> findTop5ByUserIdOrderByUpdatedAtDesc(Long userId) {
return recordRepository.findTop5ByUserIdOrderByUpdatedAtDesc(userId);
}

public List<Record> findTop20ByOrderByUpdatedAtDesc() {
return recordRepository.findTop20ByOrderByUpdatedAtDesc();
}

public List<MovieMapping> findAllByUserId(Long userId) {
return recordRepository.findAllByUserId(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mookive.mookive_backend.record.application.service.RecordGetService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
Expand All @@ -33,13 +34,18 @@ public void deleteRecord(@RequestParam Long userId, @RequestParam Long movieId)
}

@GetMapping("/record/calendar")
public List<List<RecordResponse.RecordCalendarResponse>> getRecordByCalendar(@RequestParam String year, @RequestParam String month) {
return recordGetService.getRecordByCalendar(year, month);
public List<List<RecordResponse.RecordCalendarResponse>> getRecordOfCalender(@RequestParam String year, @RequestParam String month) {
return recordGetService.getRecordOfCalender(year, month);
}

@GetMapping("/record/home/recent/{userId}")
public List<RecordResponse.RecordHomeResponse> getRecordByHome(@PathVariable Long userId) {
return recordGetService.getRecordByHome(userId);
public List<RecordResponse.RecordHomeByUserResponse> getRecordOfHomeByUser(@PathVariable Long userId) {
return recordGetService.getRecordOfHomeByUser(userId);
}

@GetMapping("/record/home/recent/exclusion/{userId}")
public List<RecordResponse.RecordHomeResponse> getRecordOfHome(@PathVariable Long userId) {
return recordGetService.getRecordOfHome(userId);
}

}

0 comments on commit 7c6899a

Please sign in to comment.