Skip to content

Commit

Permalink
[fix] 동기 다대다 db 수정, 동기 api 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryeolee committed Nov 11, 2023
1 parent 811a21c commit 70809d8
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
public enum ErrorMessage {

INTERVAL_SERVER_ERROR(1001, "요청을 처리하는 과정에서 서버가 예상하지 못한 오류가 발생하였습니다."),
REFRESH_NOTIFICATION_ERROR(4017, "Refresh Token 인증 오류"),
NO_USER_DATA(1002, "유저에 대한 정보가 없습니다.");
REFRESH_NOTIFICATION_ERROR(1002, "Refresh Token 인증 오류"),
NO_USER_DATA(1003, "유저에 대한 정보가 없습니다."),
NO_MOTIVATION_DATA(1004, "동기에 대한 정보가 없습니다.");



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package modernfarmer.server.farmususer.user.entity;

import lombok.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.LinkedHashSet;
import java.util.Set;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Entity
@Table(name = "motivation")
public class Motivation extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "motivation_id", nullable = false)
private Long id;


@Column(name = "motivation_reason", nullable = false, length = 40)
private String motivationReason;

@OneToMany(mappedBy = "motivation", fetch = FetchType.LAZY)
private Set<UserMotivation> userMotivations = new LinkedHashSet<>();



}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.Instant;

@Builder
@AllArgsConstructor
Expand All @@ -28,10 +26,18 @@ public class UserMotivation extends BaseEntity{
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Size(max = 40)
@NotNull
@Column(name = "user_motivation", nullable = false, length = 40)
private String userMotivation;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name = "motivation_id", nullable = false)
private Motivation motivation;










Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package modernfarmer.server.farmususer.user.repository;


import modernfarmer.server.farmususer.user.entity.Motivation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface MotivationRepository extends JpaRepository<Motivation, Long> {

@Query("select m.id from Motivation as m where m.motivationReason = :motivationReason")
Long getMotivationId(@Param("motivationReason") String motivationReason);


Optional<Motivation> findByMotivationReason(String motivationReason);



}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
import modernfarmer.server.farmususer.user.dto.request.OnBoardingMotivationRequest;
import modernfarmer.server.farmususer.user.dto.response.BaseResponseDto;
import modernfarmer.server.farmususer.user.dto.response.OnBoardingLevelResponse;
import modernfarmer.server.farmususer.user.entity.Motivation;
import modernfarmer.server.farmususer.user.entity.User;
import modernfarmer.server.farmususer.user.entity.UserMotivation;
import modernfarmer.server.farmususer.user.repository.MotivationRepository;
import modernfarmer.server.farmususer.user.repository.UserMotivationRepository;
import modernfarmer.server.farmususer.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Optional;


@RequiredArgsConstructor
Expand All @@ -31,16 +32,24 @@ public class OnBoardingService {

private final UserMotivationRepository userMotivationRepository;

private final MotivationRepository motivationRepository;

public BaseResponseDto onBoardingMotivation(Long userId, OnBoardingMotivationRequest onBoardingMotivationRequest){

User user = User.builder().id(userId).build();

for(String motivations : onBoardingMotivationRequest.getMotivation()){

Optional<Motivation> motivation = motivationRepository.findByMotivationReason(motivations);

if(motivation.isEmpty()){
return BaseResponseDto.of(ErrorMessage.NO_MOTIVATION_DATA);
}

UserMotivation userMotivation = UserMotivation
.builder()
.user(user)
.userMotivation(motivations)
.motivation(motivation.get())
.build();

userMotivationRepository.save(userMotivation);
Expand Down

0 comments on commit 70809d8

Please sign in to comment.