Skip to content

Commit

Permalink
Merge pull request #41 from Likelion12/11-be-소셜링-등록
Browse files Browse the repository at this point in the history
Feat : 소셜링 등록
  • Loading branch information
jsilver01 authored Aug 1, 2024
2 parents 0deabcb + 8c56a6d commit 9bbac4c
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 2 deletions.
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 SocialringException extends RuntimeException{
private final ResponseStatus exceptionStatus;

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

public SocialringException(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.SocialringException;
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 SocialringExceptionControllerAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(SocialringException.class)
public BaseErrorResponse handle_SocialringException(SocialringException e) {
log.error("[handle_SocialringException]", e);
return new BaseErrorResponse(e.getExceptionStatus(), e.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.likelion12.controller;

import com.example.likelion12.common.response.BaseResponse;
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.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/socialring")

public class SocialringController {

private final SocialringService socialringService;
private final JwtProvider jwtProvider;

@PostMapping("")
public BaseResponse<PostSocialringResponse> createSocialring(@RequestHeader("Authorization") String authorization , @RequestBody PostSocialringRequest postSocialringRequest)
{ //헤더에서 소셜링 등록하는 멤버아이디 찾기
Long memberId = jwtProvider.extractIdFromHeader(authorization);
return new BaseResponse<>(socialringService.createSocialring(memberId,postSocialringRequest));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ public class MemberSocialring extends BaseTime {
@JoinColumn(name = "member_id")
private Member member;

public MemberSocialring(BaseRole baseRole, Socialring socialring, Member member, BaseStatus baseStatus) {
this.role = baseRole;
this.socialring = socialring;
this.member = member;
this.status = baseStatus;
}
}
22 changes: 20 additions & 2 deletions src/main/java/com/example/likelion12/domain/Socialring.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

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

Expand All @@ -36,7 +36,7 @@ public class Socialring extends BaseTime {
private int totalRecruits;

@Column(nullable = false)
private LocalDateTime socialringDate;
private LocalDate socialringDate;

@Column(nullable = false)
private int socialringCost;
Expand Down Expand Up @@ -77,4 +77,22 @@ public class Socialring extends BaseTime {
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "exercise_id")
private Exercise exercise;

public Socialring(String socialringName, String socialringImg, int totalRecruits, LocalDate socialringDate,
int socialringCost, String comment, String commentSimple, BaseGender gender, BaseLevel level,
ActivityRegion activityRegion, Facility facility, Exercise exercise, BaseStatus baseStatus) {
this.socialringName = socialringName;
this.socialringImg = socialringImg;
this.totalRecruits = totalRecruits;
this.socialringDate = socialringDate;
this.socialringCost = socialringCost;
this.commentSimple = commentSimple;
this.comment = comment;
this.gender = gender;
this.level = level;
this.activityRegion = activityRegion;
this.facility = facility;
this.exercise = exercise;
this.status = baseStatus;
}
}
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 PostSocialringRequest {
private String socialringName;
private String socialringImg;
private long activityRegionId;
private long facilityId;
private long exerciseId;
private int totalRecruits;
private LocalDate socialringDate;
private int socialringCost;
private String comment;
private String commentSimple;
private BaseGender gender;
private BaseLevel level;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.likelion12.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class PostSocialringResponse {
private long socialringId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.likelion12.repository;

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

public interface MemberSocialringRepository extends JpaRepository<MemberSocialring, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.likelion12.repository;


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

public interface SocialringRepository extends JpaRepository<Socialring, Long> {
//Optional<Socialring> findBySocialringIdAndStatus(Long socialringId, BaseStatus status);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.likelion12.service;

import com.example.likelion12.domain.*;
import com.example.likelion12.domain.base.BaseRole;
import com.example.likelion12.domain.base.BaseStatus;
import com.example.likelion12.repository.MemberSocialringRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MemberSocialringService {

private final MemberSocialringRepository memberSocialringRepository;

/**
* 소셜링 등록 시 소셜링 만든 사람이 CAPTAIN 이 되도록
*/
@Transactional
public void createMemberSocialring(Member member, Socialring socialring){
log.info("[MemberSocialringService.createMemberSocialring]");
MemberSocialring memberSocialring = new MemberSocialring(BaseRole.CAPTAIN,socialring,member,BaseStatus.ACTIVE);
memberSocialringRepository.save(memberSocialring);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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.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.PostSocialringRequest;
import com.example.likelion12.dto.PostSocialringResponse;
import com.example.likelion12.repository.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

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

@Slf4j
@Service
@RequiredArgsConstructor
public class SocialringService {
private final SocialringRepository socialringRepository;
private final MemberRepository memberRepository;
private final FacilityRepository facilityRepository;
private final ExerciseRepository exerciseRepository;
private final MemberSocialringService memberSocialringService;
private final ActivityRegionRepository activityRegionRepository;

//소셜링등록
@Transactional
public PostSocialringResponse createSocialring(Long memberId, PostSocialringRequest postSocialringRequest) {
log.info("[SocialringService.createSocialring]");
Member member = memberRepository.findByMemberIdAndStatus(memberId, BaseStatus.ACTIVE)
.orElseThrow(() -> new MemberException(CANNOT_FOUND_MEMBER));

String socialringName = postSocialringRequest.getSocialringName();
String socialringImg = postSocialringRequest.getSocialringImg();
long activityRegionId = postSocialringRequest.getActivityRegionId();
long facilityId = postSocialringRequest.getFacilityId();
long exerciseId = postSocialringRequest.getExerciseId();
int totalRecruits = postSocialringRequest.getTotalRecruits();
LocalDate socialringDate = postSocialringRequest.getSocialringDate();
int socialringCost = postSocialringRequest.getSocialringCost();
String comment = postSocialringRequest.getComment();
String commentSimple = postSocialringRequest.getCommentSimple();
BaseGender gender = postSocialringRequest.getGender();
BaseLevel level = postSocialringRequest.getLevel();

ActivityRegion activityRegion = activityRegionRepository.findByActivityRegionIdAndStatus(activityRegionId, BaseStatus.ACTIVE)
.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));

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);
return new PostSocialringResponse(socialring.getSocialringId());
}
}

0 comments on commit 9bbac4c

Please sign in to comment.