Skip to content

Commit

Permalink
[BYOB-136] 목록 조회 버그 해결 (#31)
Browse files Browse the repository at this point in the history
* fix: region null 문제 해결을 위해 스프링데이터JPA로 변경
  • Loading branch information
yangdongsuk authored Aug 2, 2024
1 parent 6702702 commit e267c0a
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 105 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ dependencies {


//jackson
// implementation 'com.fasterxml.jackson.core:jackson-databind'
// implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'


testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingDto;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingListResponseDto;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingResDto;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingListResDto;

@Tag(name = "모임 API", description = "모임에 대한 CRUD를 수행하는 API입니다.")
public interface MeetingApi {

@Operation(summary = "id에 해당하는 모임 상세정보를 응답하는 API입니다.")
@ApiResponse(
responseCode = "200",
description = "모임 조회 성공",
content = @Content(schema = @Schema(implementation = MeetingDto.class))
responseCode = "200",
description = "모임 조회 성공",
content = @Content(schema = @Schema(implementation = MeetingResDto.class))
)
public MeetingDto getMeetingById(Long id);
public MeetingResDto getMeetingById(Long id);

@Operation(summary = "모임 목록에 대한 pagination API입니다. 정렬방식, 개수, 커서를 지정할 수 있습니다.")
@ApiResponse(
responseCode = "200",
description = "모임 목록 조회 성공",
content = @Content(schema = @Schema(implementation = MeetingListResponseDto.class))
content = @Content(schema = @Schema(implementation = MeetingListResDto.class))
)
public MeetingListResponseDto getLatestMeetingList(String sort, int limit, Long cursor);
public MeetingListResDto getLatestMeetingList(String sort, int limit, Long cursor);
}

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package team_alcoholic.jumo_server.domain.meeting.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingDto;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingListResponseDto;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingResDto;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingListResDto;
import team_alcoholic.jumo_server.domain.meeting.service.MeetingService;

@RestController
Expand All @@ -15,12 +14,12 @@ public class MeetingController implements MeetingApi {
private final MeetingService meetingService;

@GetMapping("{id}")
public MeetingDto getMeetingById(@PathVariable("id") Long id) {
public MeetingResDto getMeetingById(@PathVariable("id") Long id) {
return meetingService.findMeetingById(id);
}

@GetMapping()
public MeetingListResponseDto getLatestMeetingList(
public MeetingListResDto getLatestMeetingList(
@RequestParam(required = false, defaultValue = "latest") String sort,
@RequestParam(required = false, defaultValue = "30") int limit,
@RequestParam(required = false, defaultValue = "0") Long cursor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

import jakarta.persistence.*;
import lombok.Getter;
import team_alcoholic.jumo_server.domain.region.domain.Region;
import team_alcoholic.jumo_server.global.common.domain.BaseTimeEntity;

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

@Entity
@Getter
public class Meeting extends BaseTimeEntity {

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String uuid;
Expand All @@ -20,7 +22,11 @@ public class Meeting extends BaseTimeEntity {
private String status;
private LocalDateTime meetingAt;
private LocalDateTime fixAt;
private String region;

@ManyToOne
@JoinColumn(name = "region", referencedColumnName = "admcd")
private Region region;

private String place;
private String liquors;
private Integer participatesMin;
Expand All @@ -34,4 +40,7 @@ public class Meeting extends BaseTimeEntity {
private String thumbnailImage;
private String externalService;
private String externalLink;

@OneToMany(mappedBy = "meeting")
private List<MeetingImage> images;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package team_alcoholic.jumo_server.domain.meeting.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.Getter;
import team_alcoholic.jumo_server.global.common.domain.BaseTimeEntity;

Expand All @@ -14,6 +11,8 @@ public class MeetingImage extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long meeting;
@ManyToOne
@JoinColumn(name = "meeting", nullable = false)
private Meeting meeting;
private String url;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;
import lombok.Setter;
import team_alcoholic.jumo_server.domain.meeting.domain.Meeting;
import team_alcoholic.jumo_server.domain.region.domain.Region;

import java.time.LocalDateTime;

Expand All @@ -14,20 +15,20 @@ public class MeetingListDto {

private Long id;
private String uuid;
// private String host;
// private String host;
private String name;
private String status;
private LocalDateTime meetingAt;
private LocalDateTime fixAt;
private String region;
// private String place;
// private String place;
private String liquors;
private Integer participatesMin;
private Integer participatesMax;
private Integer payment;
// private String paymentMethod;
// private String paymentMethod;
private boolean byob;
// private Integer byobMin;
// private Integer byobMin;
// private Integer byobMax;
// private String description;
private String thumbnail;
Expand All @@ -41,7 +42,10 @@ public MeetingListDto(Meeting meeting) {
this.status = meeting.getStatus();
this.meetingAt = meeting.getMeetingAt();
this.fixAt = meeting.getFixAt();
this.region = meeting.getRegion();
if (meeting.getRegion() != null) {
this.region = meeting.getRegion().getAdmnm();
}

this.liquors = meeting.getLiquors();
this.participatesMin = meeting.getParticipatesMin();
this.participatesMax = meeting.getParticipatesMax();
Expand All @@ -50,4 +54,6 @@ public MeetingListDto(Meeting meeting) {
this.thumbnail = meeting.getThumbnailImage();
this.externalService = meeting.getExternalService();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Getter
@Setter
@AllArgsConstructor
public class MeetingListResponseDto {
public class MeetingListResDto {
private List<MeetingListDto> meetings;
private Long lastId;
private boolean eof;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import lombok.Getter;
import lombok.Setter;
import team_alcoholic.jumo_server.domain.meeting.domain.Meeting;
import team_alcoholic.jumo_server.domain.meeting.domain.MeetingImage;

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

@Getter
@Setter
public class MeetingDto {
public class MeetingResDto {

private Long id;
private String uuid;
Expand All @@ -34,15 +35,18 @@ public class MeetingDto {
private String externalLink;
private List<String> images;

public MeetingDto(Meeting meeting, List<String> images) {

public MeetingResDto(Meeting meeting) {
this.id = meeting.getId();
this.uuid = meeting.getUuid();
this.host = meeting.getHost();
this.name = meeting.getName();
this.status = meeting.getStatus();
this.meetingAt = meeting.getMeetingAt();
this.fixAt = meeting.getFixAt();
this.region = meeting.getRegion();
if (meeting.getRegion() != null) {
this.region = meeting.getRegion().getAdmnm();
}
this.place = meeting.getPlace();
this.liquors = meeting.getLiquors();
this.participatesMin = meeting.getParticipatesMin();
Expand All @@ -56,6 +60,6 @@ public MeetingDto(Meeting meeting, List<String> images) {
this.thumbnail = meeting.getThumbnailImage();
this.externalService = meeting.getExternalService();
this.externalLink = meeting.getExternalLink();
this.images = images;
this.images = meeting.getImages().stream().map(MeetingImage::getUrl).toList();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package team_alcoholic.jumo_server.domain.meeting.repository;

import team_alcoholic.jumo_server.domain.meeting.dto.MeetingDto;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingListDto;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import team_alcoholic.jumo_server.domain.meeting.domain.Meeting;


import java.util.List;

public interface MeetingRepository {
MeetingDto findMeetingById(Long id);
List<MeetingListDto> findLatestMeetingList(int limit);
List<MeetingListDto> findLatestMeetingListById(int limit, Long cursor);
public interface MeetingRepository extends JpaRepository<Meeting, Long> {

// 특정 ID의 Meeting 조회
@EntityGraph(attributePaths = "images")
Meeting findMeetingById(Long id);

// 특정 ID보다 작은 Meeting 목록 조회
// n+1 문제 해결을 위해 EntityGraph 사용
@EntityGraph(attributePaths = "region")
List<Meeting> findByIdLessThanOrderByIdDesc(Long cursor, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package team_alcoholic.jumo_server.domain.meeting.service;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingDto;
import org.springframework.transaction.annotation.Transactional;
import team_alcoholic.jumo_server.domain.meeting.domain.Meeting;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingResDto;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingListDto;
import team_alcoholic.jumo_server.domain.meeting.repository.MeetingRepository;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingListResponseDto;
import team_alcoholic.jumo_server.domain.region.domain.Region;
import team_alcoholic.jumo_server.domain.region.repository.RegionRepository;
import team_alcoholic.jumo_server.domain.meeting.dto.MeetingListResDto;

import java.util.List;

Expand All @@ -17,21 +18,31 @@
public class MeetingService {

private final MeetingRepository meetingRepository;
private final RegionRepository regionRepository;

public MeetingDto findMeetingById(Long id) {
MeetingDto meeting = meetingRepository.findMeetingById(id);
Region region = regionRepository.findByAdmcd(meeting.getRegion());
meeting.setRegion(region.getAdmnm());
return meeting;
public MeetingResDto findMeetingById(Long id) {
Meeting meeting = meetingRepository.findMeetingById(id);
return new MeetingResDto(meeting);
}

public MeetingListResponseDto findLatestMeetingList(int limit, Long cursor) {
List<MeetingListDto> meetings;
if (cursor==0) meetings = meetingRepository.findLatestMeetingList(limit+1);
else meetings = meetingRepository.findLatestMeetingListById(limit+1, cursor);
boolean eof = (meetings.size()<limit+1);
if (!eof) meetings.remove(meetings.size()-1);
return new MeetingListResponseDto(meetings, meetings.get(meetings.size()-1).getId(), eof);
@Transactional(readOnly = true)
public MeetingListResDto findLatestMeetingList(int limit, Long cursor) {
List<Meeting> meetings;
Pageable pageable = PageRequest.of(0, limit + 1);

// cursor가 0이면 Long.MAX_VALUE를 사용
Long effectiveCursor = (cursor == 0) ? Long.MAX_VALUE : cursor;

meetings = meetingRepository.findByIdLessThanOrderByIdDesc(effectiveCursor, pageable);

boolean eof = (meetings.size() < limit + 1);
if (!eof) {
meetings.remove(meetings.size() - 1);
}

List<MeetingListDto> meetingList = meetings.stream().map(MeetingListDto::new).toList();
return new MeetingListResDto(meetingList, meetings.get(meetings.size() - 1).getId(), eof);
}

}


Loading

0 comments on commit e267c0a

Please sign in to comment.