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

Feat : 소셜링 수정 #44

Merged
merged 22 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5804a39
feat : 소셜링 수정 dto 작성
hd0rable Aug 1, 2024
f93dcf4
feat : CAPTAIN이 아닌 member 들어왔을때 BaseExceptionResponseStatus 작성
hd0rable Aug 1, 2024
3f4ceb8
feat : socialring 관련 BaseExceptionResponseStatus 작성
hd0rable Aug 1, 2024
7cf6b7d
feat : membersocialring 관련 BaseExceptionResponseStatus 작성
hd0rable Aug 1, 2024
eb891a7
feat : membersocialring 예외 작성
hd0rable Aug 1, 2024
b5daf27
feat : membersocialringRepository 작성
hd0rable Aug 1, 2024
440fac8
feat : Socialring Setter 추가
hd0rable Aug 1, 2024
1385a19
feat : SocialringRepository 작성
hd0rable Aug 1, 2024
a09633d
feat : MemberSocialringService의ConfirmCaptainMemberSocialring 작성
hd0rable Aug 1, 2024
508d3b3
feat : PatchSocialringModifyRequest ado 작성
hd0rable Aug 1, 2024
763b0b9
feat : SocialringController.modifySocialring 작성
hd0rable Aug 1, 2024
c2ff0fa
feat : 소셜링 수정 구현
hd0rable Aug 1, 2024
9c48265
refator : 안쓰는 import 삭제
hd0rable Aug 1, 2024
5a58207
feat : respond 추가
hd0rable Aug 1, 2024
c943065
feat : UpdateSocialringInfo 작성
hd0rable Aug 1, 2024
1ecfa79
fix : 소셜링 수정 부분 수정
hd0rable Aug 1, 2024
78e971f
Merge branch 'develop' into 13-be-소셜링-수정
jsilver01 Aug 2, 2024
21c75fd
refator : 주석 추가
hd0rable Aug 2, 2024
5de88fe
feat : 레포지토리에서 name으로 찾는 find 작성
hd0rable Aug 2, 2024
8d1e887
fix : 리다이렉트 url 수정
hd0rable Aug 2, 2024
2ce4cd6
fix : activityRegion,facility,exercise id값이아닌 name값으로 변경
hd0rable Aug 2, 2024
981c31d
Merge branch 'develop' into 13-be-소셜링-수정
jsilver01 Aug 2, 2024
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,19 @@
package com.example.likelion12.common.exception;

import com.example.likelion12.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class MemberSocialringException extends RuntimeException{
private final ResponseStatus exceptionStatus;

public MemberSocialringException(ResponseStatus exceptionStatus) {
super(exceptionStatus.getMessage());
this.exceptionStatus = exceptionStatus;
}

public MemberSocialringException(ResponseStatus exceptionStatus, String message) {
super(message);
this.exceptionStatus = exceptionStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.likelion12.common.exception_handler;

import com.example.likelion12.common.exception.MemberSocialringException;
import com.example.likelion12.common.response.BaseErrorResponse;
import jakarta.annotation.Priority;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@Priority(0)
@RestControllerAdvice
public class MemberSocialringExceptionControllerAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MemberSocialringException.class)
public BaseErrorResponse handle_MemberSocialringException(MemberSocialringException e) {
log.error("[handle_MemberSocialringException]", e);
return new BaseErrorResponse(e.getExceptionStatus(), e.getMessage());
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 잘 넣으셨네요!! 👍

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,18 @@ public enum BaseExceptionResponseStatus implements ResponseStatus{
* 6000: member 관련
*/
CANNOT_FOUND_MEMBER(6000, HttpStatus.BAD_REQUEST.value(), "유저를 찾을 수 없습니다."),
ALREADY_EXIST_EMAIL(6001, HttpStatus.BAD_REQUEST.value(), "이미 존재하는 이메일입니다.");
ALREADY_EXIST_EMAIL(6001, HttpStatus.BAD_REQUEST.value(), "이미 존재하는 이메일입니다."),

/**
* 7000: socialring 관련
*/
CANNOT_FOUND_SOCIALRING(7000, HttpStatus.BAD_REQUEST.value(), "소셜링을 찾을 수 없습니다."),

/**
* 8000: membersocialring 관련
*/
CANNOT_FOUND_MEMBERSOCIALRING(8000, HttpStatus.BAD_REQUEST.value(), "해당하는 멤버소셜링을 찾을 수 없습니다."),
CANNOT_MEMBERSOCIALRING_CAPTAIN(8001, HttpStatus.BAD_REQUEST.value(), "소셜링 수정,삭제에 접근할수없는 권한입니다.");


private final int code;
Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주석 달아주신거 너무 좋습니다~

Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.example.likelion12.controller;

import com.example.likelion12.common.response.BaseResponse;
import com.example.likelion12.common.response.status.BaseExceptionResponseStatus;
import com.example.likelion12.dto.PatchSocialringModifyRequest;
import com.example.likelion12.dto.PostSocialringRequest;
import com.example.likelion12.dto.PostSocialringResponse;
import com.example.likelion12.service.SocialringService;
import com.example.likelion12.util.JwtProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@Slf4j
Expand All @@ -19,10 +22,23 @@ public class SocialringController {
private final SocialringService socialringService;
private final JwtProvider jwtProvider;

//소셜링 등록
@PostMapping("")
public BaseResponse<PostSocialringResponse> createSocialring(@RequestHeader("Authorization") String authorization , @RequestBody PostSocialringRequest postSocialringRequest)
public BaseResponse<PostSocialringResponse> createSocialring(@RequestHeader("Authorization") String authorization ,
@RequestBody PostSocialringRequest postSocialringRequest)
{ //헤더에서 소셜링 등록하는 멤버아이디 찾기
Long memberId = jwtProvider.extractIdFromHeader(authorization);
return new BaseResponse<>(socialringService.createSocialring(memberId,postSocialringRequest));
}

//소셜링 수정
@PatchMapping("")
public BaseResponse<Void> modifySocialring(@RequestHeader("Authorization") String authorization, @RequestParam long socialringId,
@RequestBody PatchSocialringModifyRequest patchSocialringModifyRequest)
{
Long memberId = jwtProvider.extractIdFromHeader(authorization);
socialringService.modifySocialring(memberId,socialringId,patchSocialringModifyRequest);
return new BaseResponse<>(BaseExceptionResponseStatus.SUCCESS, null);
}

}
19 changes: 19 additions & 0 deletions src/main/java/com/example/likelion12/domain/Socialring.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static jakarta.persistence.FetchType.LAZY;

Expand Down Expand Up @@ -95,4 +96,22 @@ public Socialring(String socialringName, String socialringImg, int totalRecruits
this.exercise = exercise;
this.status = baseStatus;
}

public void UpdateSocialringInfo(String newSocialringName, String newSocialringImg, Integer newTotalRecruits, LocalDate newSocialringDate,
Integer newSocialringCost, String newComment, String newCommentSimple, BaseGender newGender,
BaseLevel newLevel, ActivityRegion activityRegion, Facility facility,Exercise exercise) {
this.socialringName = newSocialringName;
this.socialringImg = newSocialringImg;
this.totalRecruits = newTotalRecruits;
this.socialringDate = newSocialringDate;
this.socialringCost = newSocialringCost;
this.commentSimple = newCommentSimple;
this.comment = newComment;
this.gender = newGender;
this.level = newLevel;
this.activityRegion = activityRegion;
this.facility = facility;
this.exercise = exercise;

}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 이거 명진님이랑만 말한거 같은데 저기서 activityRegionId 랑 facilityId, exerciseId 로 넘겨주지 말고 (저 3개는 id 로 요청할 일이 거의 없으니까요!) 차라리 그냥 프론트에서 바로 사용할 수 있도록 String 인 이름값으로 넘겨주면 어떨까요??? activityRegionId -> activityRegionName / facilityId -> facilityName / exerciseId -> exerciseName 이렇게요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다! 참고하여 수정하겠습니다!!

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.likelion12.dto;

import com.example.likelion12.domain.base.BaseGender;
import com.example.likelion12.domain.base.BaseLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.time.LocalDate;

@Getter
@AllArgsConstructor
public class PatchSocialringModifyRequest {
private String socialringName;
private String socialringImg;
private Long activityRegionId;
private Long facilityId;
private Long exerciseId;
private Integer totalRecruits;
private LocalDate socialringDate;
private Integer socialringCost;
private String comment;
private String commentSimple;
private BaseGender gender;
private BaseLevel level;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.example.likelion12.repository;

import com.example.likelion12.domain.MemberSocialring;
import com.example.likelion12.domain.base.BaseStatus;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface MemberSocialringRepository extends JpaRepository<MemberSocialring, Long> {
//Optional<MemberSocialring> findByMemberIdAndSocialringIdAndStatus(Long memberId, Long socialringId, BaseStatus status);
Optional<MemberSocialring> findByMember_MemberIdAndSocialring_SocialringIdAndStatus(Long memberId, Long socialringId, BaseStatus status);
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@


import com.example.likelion12.domain.Socialring;
import com.example.likelion12.domain.base.BaseStatus;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface SocialringRepository extends JpaRepository<Socialring, Long> {
//Optional<Socialring> findBySocialringIdAndStatus(Long socialringId, BaseStatus status);

Optional<Socialring> findBySocialringIdAndStatus(Long socialringId, BaseStatus status);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.likelion12.service;

import com.example.likelion12.common.exception.MemberSocialringException;
import com.example.likelion12.domain.*;
import com.example.likelion12.domain.base.BaseRole;
import com.example.likelion12.domain.base.BaseStatus;
Expand All @@ -9,6 +10,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.example.likelion12.common.response.status.BaseExceptionResponseStatus.CANNOT_MEMBERSOCIALRING_CAPTAIN;

@Slf4j
@Service
@RequiredArgsConstructor
Expand All @@ -26,4 +29,17 @@ public void createMemberSocialring(Member member, Socialring socialring){
MemberSocialring memberSocialring = new MemberSocialring(BaseRole.CAPTAIN,socialring,member,BaseStatus.ACTIVE);
memberSocialringRepository.save(memberSocialring);
}

/**
* 소셜링 수정,삭제 시 접근하는 member가 CAPTAIN 인지 확인
*/
@Transactional
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기 메소드 단순히 CAPTAIN 인지 아닌지 조회하는 부분 맞나요?? 맞다면 단순 조회고 데이터베이스에는 아무런 영향을 주지 않기 때문에 트랜잭션 어노테이션은 사용하지 않으셔도 무방합니다~

public void ConfirmCaptainMemberSocialring(MemberSocialring memberSocialring){
log.info("[MemberSocialringService.ConfirmCaptainMemberSocialring]");

if (!BaseRole.CAPTAIN.equals(memberSocialring.getRole())) {
throw new MemberSocialringException(CANNOT_MEMBERSOCIALRING_CAPTAIN);
}
}

}
97 changes: 85 additions & 12 deletions src/main/java/com/example/likelion12/service/SocialringService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.example.likelion12.service;

import com.example.likelion12.common.exception.ActivityRegionException;
import com.example.likelion12.common.exception.ExerciseException;
import com.example.likelion12.common.exception.FacilityException;
import com.example.likelion12.common.exception.MemberException;
import com.example.likelion12.common.exception.*;
import com.example.likelion12.domain.*;
import com.example.likelion12.domain.base.BaseGender;
import com.example.likelion12.domain.base.BaseLevel;
import com.example.likelion12.domain.base.BaseStatus;
import com.example.likelion12.dto.PatchSocialringModifyRequest;
import com.example.likelion12.dto.PostSocialringRequest;
import com.example.likelion12.dto.PostSocialringResponse;
import com.example.likelion12.repository.*;
Expand All @@ -30,6 +28,7 @@ public class SocialringService {
private final ExerciseRepository exerciseRepository;
private final MemberSocialringService memberSocialringService;
private final ActivityRegionRepository activityRegionRepository;
private final MemberSocialringRepository memberSocialringRepository;

//소셜링등록
@Transactional
Expand All @@ -38,7 +37,7 @@ public PostSocialringResponse createSocialring(Long memberId, PostSocialringRequ
Member member = memberRepository.findByMemberIdAndStatus(memberId, BaseStatus.ACTIVE)
.orElseThrow(() -> new MemberException(CANNOT_FOUND_MEMBER));

String socialringName = postSocialringRequest.getSocialringName();
String socialringName = postSocialringRequest.getSocialringName();
String socialringImg = postSocialringRequest.getSocialringImg();
long activityRegionId = postSocialringRequest.getActivityRegionId();
long facilityId = postSocialringRequest.getFacilityId();
Expand All @@ -52,18 +51,92 @@ public PostSocialringResponse createSocialring(Long memberId, PostSocialringRequ
BaseLevel level = postSocialringRequest.getLevel();

ActivityRegion activityRegion = activityRegionRepository.findByActivityRegionIdAndStatus(activityRegionId, BaseStatus.ACTIVE)
.orElseThrow(()-> new ActivityRegionException(CANNOT_FOUND_ACTIVITYREGION));
.orElseThrow(() -> new ActivityRegionException(CANNOT_FOUND_ACTIVITYREGION));
Facility facility = facilityRepository.findByFacilityIdAndStatus(facilityId, BaseStatus.ACTIVE)
.orElseThrow(()-> new FacilityException(CANOOT_FOUND_FACILITY));
Exercise exercise = exerciseRepository.findByExerciseIdAndStatus(exerciseId,BaseStatus.ACTIVE)
.orElseThrow(()-> new ExerciseException(CANNOT_FOUND_EXERCISE));
.orElseThrow(() -> new FacilityException(CANOOT_FOUND_FACILITY));
Exercise exercise = exerciseRepository.findByExerciseIdAndStatus(exerciseId, BaseStatus.ACTIVE)
.orElseThrow(() -> new ExerciseException(CANNOT_FOUND_EXERCISE));

Socialring socialring = new Socialring(socialringName,socialringImg,totalRecruits,socialringDate,socialringCost,comment,commentSimple,
gender,level,activityRegion,facility, exercise, BaseStatus.ACTIVE);
Socialring socialring = new Socialring(socialringName, socialringImg, totalRecruits, socialringDate, socialringCost, comment, commentSimple,
gender, level, activityRegion, facility, exercise, BaseStatus.ACTIVE);
socialringRepository.save(socialring);

//소셜링을 만든 사람이 CAPTAIN 이 되도록
memberSocialringService.createMemberSocialring(member,socialring);
memberSocialringService.createMemberSocialring(member, socialring);
return new PostSocialringResponse(socialring.getSocialringId());
}

//소셜링 수정
@Transactional
public void modifySocialring(Long memberId, Long socialringId, PatchSocialringModifyRequest patchSocialringModifyRequest) {
log.info("[SocialringService.modifySocialring]");

//소셜링을 수정하고자 하는 멤버
Member member = memberRepository.findByMemberIdAndStatus(memberId, BaseStatus.ACTIVE)
.orElseThrow(() -> new MemberException(CANNOT_FOUND_MEMBER));

//수정하고자 하는 소셜링
Socialring socialring = socialringRepository.findBySocialringIdAndStatus(socialringId, BaseStatus.ACTIVE)
.orElseThrow(() -> new SocialringException(CANNOT_FOUND_SOCIALRING));

//수정하고자 하는 멤버의 멤버소셜링
MemberSocialring memberSocialring = memberSocialringRepository.findByMember_MemberIdAndSocialring_SocialringIdAndStatus(memberId,
socialringId, BaseStatus.ACTIVE).orElseThrow(() -> new MemberSocialringException(CANNOT_FOUND_MEMBERSOCIALRING));

//접근 멤버가 captain인지 유효성 검사
memberSocialringService.ConfirmCaptainMemberSocialring(memberSocialring);

String newSocialringName = patchSocialringModifyRequest.getSocialringName()
== null ? socialring.getSocialringName() : patchSocialringModifyRequest.getSocialringName();
String newSocialringImg = patchSocialringModifyRequest.getSocialringImg()
== null ? socialring.getSocialringImg() : patchSocialringModifyRequest.getSocialringImg();
Integer newTotalRecruits = patchSocialringModifyRequest.getTotalRecruits()
== null ? socialring.getTotalRecruits() : patchSocialringModifyRequest.getTotalRecruits();
LocalDate newSocialringDate = patchSocialringModifyRequest.getSocialringDate()
== null ? socialring.getSocialringDate() : patchSocialringModifyRequest.getSocialringDate();
Integer newSocialringCost = patchSocialringModifyRequest.getSocialringCost()
== null ? socialring.getSocialringCost() : patchSocialringModifyRequest.getSocialringCost();
String newComment = patchSocialringModifyRequest.getComment()
== null ? socialring.getComment() : patchSocialringModifyRequest.getComment() ;
String newCommentSimple = patchSocialringModifyRequest.getCommentSimple()
== null ? socialring.getCommentSimple() : patchSocialringModifyRequest.getCommentSimple() ;
BaseGender newGender = patchSocialringModifyRequest.getGender()
== null ? socialring.getGender() : patchSocialringModifyRequest.getGender();
BaseLevel newLevel = patchSocialringModifyRequest.getLevel()
== null ? socialring.getLevel() : patchSocialringModifyRequest.getLevel();

long newActivityRegionId = patchSocialringModifyRequest.getActivityRegionId()
== null ? socialring.getActivityRegion().getActivityRegionId() :
//리퀘스트로 들어온값이 존재하는 값인지
activityRegionRepository.findByActivityRegionIdAndStatus(
patchSocialringModifyRequest.getActivityRegionId(), BaseStatus.ACTIVE)
.orElseThrow(() -> new ActivityRegionException(CANNOT_FOUND_ACTIVITYREGION)).getActivityRegionId();
long newFacilityId = patchSocialringModifyRequest.getFacilityId()
== null ? socialring.getFacility().getFacilityId() :
facilityRepository.findByFacilityIdAndStatus(
patchSocialringModifyRequest.getFacilityId(), BaseStatus.ACTIVE)
.orElseThrow(() -> new ActivityRegionException(CANOOT_FOUND_FACILITY)).getFacilityId();
long newExerciseId = patchSocialringModifyRequest.getExerciseId()
== null ? socialring.getExercise().getExerciseId() :
exerciseRepository.findByExerciseIdAndStatus
(patchSocialringModifyRequest.getExerciseId(), BaseStatus.ACTIVE)
.orElseThrow(() -> new ExerciseException(CANNOT_FOUND_EXERCISE)).getExerciseId();

ActivityRegion activityRegion = activityRegionRepository.findByActivityRegionIdAndStatus(newActivityRegionId, BaseStatus.ACTIVE)
.orElseThrow(() -> new ActivityRegionException(CANNOT_FOUND_ACTIVITYREGION));
Facility facility = facilityRepository.findByFacilityIdAndStatus(newFacilityId, BaseStatus.ACTIVE)
.orElseThrow(() -> new FacilityException(CANOOT_FOUND_FACILITY));
Exercise exercise = exerciseRepository.findByExerciseIdAndStatus(newExerciseId, BaseStatus.ACTIVE)
.orElseThrow(() -> new ExerciseException(CANNOT_FOUND_EXERCISE));


//변경사항 업데이트 및 저장
socialring.UpdateSocialringInfo(newSocialringName, newSocialringImg, newTotalRecruits, newSocialringDate, newSocialringCost,
newComment, newCommentSimple, newGender, newLevel, activityRegion, facility, exercise);
socialringRepository.save(socialring);

}



}