Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

조회시 데이터 뻥튀기되는 문제 수정 #53

Merged
merged 2 commits into from
Oct 7, 2024
Merged
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
Expand Up @@ -65,7 +65,7 @@ RegionResponse map(
@AuthenticationCommonResponse
void memo(
@Parameter(hidden = true) AuthenticationMember authenticationMember,
@Parameter(description = "사진", schema = @Schema(type = "array", format = "binary")) List<MultipartFile> photos,
@Parameter(description = "수정요청한 사진(jpg, jpeg, png)", required = false) List<MultipartFile> photos,
@Parameter(description = "기록 요청 정보", required = true) RecordRequest recordRequest
);

Expand Down Expand Up @@ -109,7 +109,7 @@ void updateRecord(
AuthenticationMember authenticationMember,
@Parameter(description = "방문기록 id값", required = true)
long recordId,
@Parameter(description = "수정요청한 사진", required = false)
@Parameter(description = "수정요청한 사진(jpg, jpeg, png)", required = false)
List<MultipartFile> photos,
@Parameter(description = "수정요청 값", required = true)
UpdateRecordRequest updateRecordRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public interface MemberAttractionRepositoryCustom {
Long maxCursor(long memberId);

List<RecordProjection> findAttractionRecords(long memberId, long cursorNo, int displayPerPage);
List<RecordProjection> findAttractionRecords(long cursorNo, int displayPerPage);

Long entireVisitCount(long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Long maxCursor(long memberId) {
*/

@Override
public List<RecordProjection> findAttractionRecords(long memberId, long cursorNo, int displayPerPage) {
public List<RecordProjection> findAttractionRecords(long cursorNo, int displayPerPage) {
return jpaQueryFactory.select(Projections.constructor(RecordProjection.class,
memberAttraction.id,
memberAttraction.memo,
Expand All @@ -46,7 +46,7 @@ public List<RecordProjection> findAttractionRecords(long memberId, long cursorNo
attraction
))
.from(memberAttraction)
.join(member).on(memberAttraction.member.id.eq(memberId))
.join(member).on(memberAttraction.member.id.eq(member.id))
.join(attraction).on(memberAttraction.attraction.id.eq(attraction.id))
.where(memberAttraction.id.lt(cursorNo))
.orderBy(memberAttraction.id.desc())
Expand Down
33 changes: 16 additions & 17 deletions src/main/java/com/dnd/dndtravel/map/service/MapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,17 @@ public void recordAttraction(RecordDto recordDto, long memberId) {
// 모든 기록 조회
@Transactional(readOnly = true)
public AttractionRecordsResponse allRecords(long memberId, long cursorNo, int displayPerPage) {
//validation
Member member = memberRepository.findById(memberId).orElseThrow(() -> new MemberNotFoundException(memberId));
// 회원의 방문기록 조회
List<MemberAttraction> memberAttractions = memberAttractionRepository.findByMemberId(memberId);
if (memberAttractions.isEmpty()) {
return new AttractionRecordsResponse(0, List.of());
}

// 첫 커서값인 경우
if (cursorNo <= 0) {
cursorNo = memberAttractionRepository.maxCursor(member.getId());
cursorNo = memberAttractionRepository.maxCursor(memberId) + 1;
}
List<RecordProjection> attractionRecords = memberAttractionRepository.findAttractionRecords(memberId, cursorNo, displayPerPage);
List<RecordProjection> attractionRecords = memberAttractionRepository.findAttractionRecords(cursorNo, displayPerPage);
Long visitCount = memberAttractionRepository.entireVisitCount(memberId);

// 명소명 저장
Expand Down Expand Up @@ -131,6 +130,19 @@ public void updateVisitRecord(RecordDto dto, long memberId, long memberAttractio
updatePhotos(memberAttraction, dto.photos());
}

// 방문기록 삭제
@Transactional
public void deleteRecord(long memberId, long memberAttractionId) {
//validation
MemberAttraction memberAttraction = memberAttractionRepository.findByIdAndMemberId(memberAttractionId, memberId)
.orElseThrow(() -> new MemberAttractionNotFoundException(memberAttractionId, memberId));

List<Photo> photos = photoRepository.findByMemberAttractionId(memberAttraction.getId());
photoRepository.deleteAll(photos);
memberAttractionRepository.delete(memberAttraction);
attractionRepository.deleteById(memberAttraction.getAttraction().getId());
}

/**
* 기록에 저장된 사진이 없는경우, 수정요청엔 사진이 있는경우 => s3에 업로드, DB에 새로 사진추가
* 기록에 저장된 사진이 없는경우, 수정요청엔 사진이 없는경우 => 아무작업도 안해도 됨
Expand Down Expand Up @@ -168,19 +180,6 @@ private void updatePhotos(MemberAttraction memberAttraction, List<MultipartFile>
}
}

// 방문기록 삭제
@Transactional
public void deleteRecord(long memberId, long memberAttractionId) {
//validation
MemberAttraction memberAttraction = memberAttractionRepository.findByIdAndMemberId(memberAttractionId, memberId)
.orElseThrow(() -> new MemberAttractionNotFoundException(memberAttractionId, memberId));

List<Photo> photos = photoRepository.findByMemberAttractionId(memberAttraction.getId());
photoRepository.deleteAll(photos);
memberAttractionRepository.delete(memberAttraction);
attractionRepository.deleteById(memberAttraction.getAttraction().getId());
}

private void setPhotoUrlsWithJoin(List<RecordProjection> attractionRecords) {
List<AttractionPhotoProjection> attractionPhotos = photoRepository.findByRecordDtos(
attractionRecords);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import com.dnd.dndtravel.config.AuthenticationMember;
import com.dnd.dndtravel.member.service.MemberService;
import com.dnd.dndtravel.member.service.response.MyPageResponse;
import com.dnd.dndtravel.member.swagger.MemberControllerSwagger;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
public class MemberController {
public class MemberController implements MemberControllerSwagger {

private final MemberService memberService;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dnd.dndtravel.member.service;

import com.dnd.dndtravel.auth.repository.RefreshTokenRepository;
import com.dnd.dndtravel.map.exception.MemberNotFoundException;
import com.dnd.dndtravel.map.repository.MemberAttractionRepository;
import com.dnd.dndtravel.map.repository.MemberRegionRepository;
import com.dnd.dndtravel.auth.service.MemberNameGenerator;
Expand Down Expand Up @@ -31,8 +32,7 @@ public Member saveMember(String email, String selectedColor) {

@Transactional
public void withdrawMember(long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new RuntimeException("Member not found"));
Member member = memberRepository.findById(memberId).orElseThrow(() -> new MemberNotFoundException(memberId));

memberRepository.delete(member);
memberAttractionRepository.deleteById(memberId);
Expand All @@ -42,7 +42,7 @@ public void withdrawMember(long memberId) {

@Transactional(readOnly = true)
public MyPageResponse myPageInfo(long memberId) {
Member member = memberRepository.findById(memberId).orElseThrow(() -> new RuntimeException());
Member member = memberRepository.findById(memberId).orElseThrow(() -> new MemberNotFoundException(memberId));
return new MyPageResponse(member.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.dnd.dndtravel.member.swagger;

import org.springframework.http.MediaType;

import com.dnd.dndtravel.config.AuthenticationMember;
import com.dnd.dndtravel.map.controller.swagger.AuthenticationCommonResponse;
import com.dnd.dndtravel.member.service.response.MyPageResponse;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

public interface MemberControllerSwagger {

String STATUS_CODE_400_BODY_MESSAGE = "{\"message\":\"잘못된 요청입니다\"}";

@Operation(
summary = "인증된 사용자의 마이페이지"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "정상 조회",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = MyPageResponse.class)
)
),
@ApiResponse(responseCode = "400", description = "유저가 존재하지 않는경우",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(example = STATUS_CODE_400_BODY_MESSAGE)
)
),
})
@AuthenticationCommonResponse
MyPageResponse myPage(
@Parameter(hidden = true)
AuthenticationMember authenticationMember
);
}
Loading