Skip to content

Commit

Permalink
refactor: user keyword 변경
Browse files Browse the repository at this point in the history
Refactor/user keyword
  • Loading branch information
yj-leez authored Nov 20, 2023
2 parents 42514cc + fc310f2 commit bac6f3f
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.bapMate.bapMateServer.domain.keyword.controller;

import com.bapMate.bapMateServer.domain.keyword.dto.request.EatingRequestDto;
import com.bapMate.bapMateServer.domain.keyword.dto.request.HobbyRequestDto;
import com.bapMate.bapMateServer.domain.keyword.dto.request.HobbyRequestDtoRe;
import com.bapMate.bapMateServer.domain.keyword.dto.request.PersonalityRequestDto;
import com.bapMate.bapMateServer.domain.keyword.service.UserKeywordService;
import com.bapMate.bapMateServer.domain.user.entity.User;
Expand All @@ -26,12 +26,22 @@ public class UserKeywordController {
private final UserKeywordService userKeywordService;
private final AuthentiatedUserUtils authentiatedUserUtils;

// @Operation(summary = "사용자의 취미 키워드를 받습니다.")
// @PostMapping("/userHobby")
// public SuccessResponse<Object> setUserHobby(@Valid @RequestBody HobbyRequestDto requestDto) {
//
// User user = authentiatedUserUtils.getCurrentUser(); // 유저 정보 가져오기
// userKeywordService.setUserHobby(user, requestDto);
//
// SuccessResponse<Object> successResponse = SuccessResponse.onSuccess(200);
// return successResponse;
// }

@Operation(summary = "사용자의 취미 키워드를 받습니다.")
@PostMapping("/userHobby")
public SuccessResponse<Object> setUserHobby(@Valid @RequestBody HobbyRequestDto requestDto) {

User user = authentiatedUserUtils.getCurrentUser(); // 유저 정보 가져오기
userKeywordService.setUserHobby(user, requestDto);
public SuccessResponse<Object> setUserHobby(@Valid @RequestBody HobbyRequestDtoRe requestDto) {
User user = authentiatedUserUtils.getCurrentUser();
userKeywordService.setUserHobbyRe(user, requestDto);

SuccessResponse<Object> successResponse = SuccessResponse.onSuccess(200);
return successResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,55 @@
public class HobbyRequestDto {

@NotNull
private Boolean running;
private int running;
@NotNull
private Boolean climbing;
private int climbing;
@NotNull
private Boolean tennis;
private int tennis;
@NotNull
private Boolean hiking;
private int hiking;
@NotNull
private Boolean activities;
private int activities;

@NotNull
private Boolean traveling;
private int traveling;
@NotNull
private Boolean new_restaurants;
private int new_restaurants;
@NotNull
private Boolean visiting_hot_spots;
private int visiting_hot_spots;
@NotNull
private Boolean exchange_student;
private int exchange_student;

@NotNull
private Boolean idol;
private int idol;
@NotNull
private Boolean anime;
private int anime;
@NotNull
private Boolean pets;
private int pets;
@NotNull
private Boolean electronic_gadgets;
private int electronic_gadgets;
@NotNull
private Boolean collector;
private int collector;

@NotNull
private Boolean music;
private int music;
@NotNull
private Boolean exhibitions;
private int exhibitions;
@NotNull
private Boolean movies;
private int movies;
@NotNull
private Boolean drawing;
private int drawing;
@NotNull
private Boolean musicals; // 연극 or 뮤지컬
private int musicals; // 연극 or 뮤지컬

@NotNull
private Boolean foreign_languages;
private int foreign_languages;
@NotNull
private Boolean cooking;
private int cooking;
@NotNull
private Boolean stocks;
private int stocks;
@NotNull
private Boolean job_preparation;
private int job_preparation;

@Builder
public Hobby toEntity(){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.bapMate.bapMateServer.domain.keyword.dto.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
public class HobbyRequestDtoRe {
private List<String> strings;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,49 @@
@NoArgsConstructor
public class PersonalityRequestDto {
@NotNull
private Boolean humorous;
private int humorous;
@NotNull
private Boolean lively;
private int lively;
@NotNull
private Boolean talkative;
private int talkative;
@NotNull
private Boolean high_energy;
private int high_energy;

@NotNull
private Boolean constructive;
private int constructive;
@NotNull
private Boolean self_improving;
private int self_improving;
@NotNull
private Boolean passionate;
private int passionate;
@NotNull
private Boolean ambitious;
private int ambitious;

@NotNull
private Boolean empathetic;
private int empathetic;
@NotNull
private Boolean sensible;
private int sensible;
@NotNull
private Boolean approachable;
private int approachable;
@NotNull
private Boolean good_listener;
private int good_listener;

@NotNull
private Boolean shy;
private int shy;
@NotNull
private Boolean reserved;
private int reserved;
@NotNull
private Boolean quiet;
private int quiet;
@NotNull
private Boolean timid;
private int timid;

@NotNull
private Boolean spontaneous;
private int spontaneous;
@NotNull
private Boolean adventurous;
private int adventurous;
@NotNull
private Boolean creative;
private int creative;
@NotNull
private Boolean good_under_pressure;
private int good_under_pressure;

@Builder
public Personality toEntity(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import jakarta.persistence.*;
import lombok.*;

import java.lang.reflect.Field;
import java.util.List;

@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder // 엔티티에 builder 붙이는게 최선?
@Builder
public class Hobby {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -20,32 +23,91 @@ public class Hobby {
@JoinColumn(name = "user_id")
private User user;

private Boolean running;
private Boolean climbing;
private Boolean tennis;
private Boolean hiking;
private Boolean activities;

private Boolean traveling;
private Boolean new_restaurants;
private Boolean visiting_hot_spots;
private Boolean exchange_student;

private Boolean idol;
private Boolean anime;
private Boolean pets;
private Boolean electronic_gadgets;
private Boolean collector;

private Boolean music;
private Boolean exhibitions;
private Boolean movies;
private Boolean drawing;
private Boolean musicals; // 연극 or 뮤지컬

private Boolean foreign_languages;
private Boolean cooking;
private Boolean stocks;
private Boolean job_preparation;
private int running;
private int climbing;
private int tennis;
private int hiking;
private int activities;

private int traveling;
private int new_restaurants;
private int visiting_hot_spots;
private int exchange_student;

private int idol;
private int anime;
private int pets;
private int electronic_gadgets;
private int collector;

private int music;
private int exhibitions;
private int movies;
private int drawing;
private int musicals;

private int foreign_languages;
private int cooking;
private int stocks;
private int job_preparation;

/**
* String 리스트로 받아와서 존재하는 필드명을 true로 바꿔주게끔
* 1차 refactoring
*/
// 새로운 생성자
public Hobby(User user, List<String> hobbyDescriptions) {
this.user = user;

initializeHobbies(); // 모든 취미 필드를 false로 초기화

// DTO에서 받은 취미 설명으로 필드 설정
for (String description : hobbyDescriptions) {
HobbyType hobbyType = HobbyType.fromDescription(description);
if (hobbyType != null) {
setHobbyField(this, hobbyType.name().toLowerCase(), 1);
}
}
}

private void initializeHobbies() {
this.running = 0;
this.climbing = 0;
this.tennis = 0;
this.hiking = 0;
this.activities = 0;

this.traveling = 0;
this.new_restaurants = 0;
this.visiting_hot_spots = 0;
this.exchange_student = 0;

this.idol = 0;
this.anime = 0;
this.pets = 0;
this.electronic_gadgets = 0;
this.collector = 0;

this.music = 0;
this.exhibitions = 0;
this.movies = 0;
this.drawing = 0;
this.musicals = 0;

this.foreign_languages = 0;
this.cooking = 0;
this.stocks = 0;
this.job_preparation = 0;
}

private void setHobbyField(Hobby hobby, String fieldName, int value) {
try {
Field field = hobby.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(hobby, value);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.bapMate.bapMateServer.domain.keyword.entity;

public enum HobbyType {
RUNNING("#러닝"),
CLIMBING("#클라이밍"),
TENNIS("#테니스"),
HIKING("#등산"),
ACTIVITIES("#액티비티"),
TRAVELING("#여행"),
NEW_RESTAURANTS("#맛집탐방"),
VISITING_HOT_SPOTS("#핫플레이스탐방"),
EXCHANGE_STUDENT("#교환학생"),
IDOL("#아이돌"),
ANIME("#애니"),
PETS("#애완동물"),
ELECTRONIC_GADGETS("#전자기기"),
COLLECTOR("#컬렉터"),
MUSIC("#음악듣기"),
EXHIBITIONS("#전시회"),
MOVIES("#영화보기"),
DRAWING("#그림그리기"),
MUSICALS("#뮤지컬"),
FOREIGN_LANGUAGES("#외국어"),
COOKING("#요리"),
STOCKS("#주식"),
JOB_PREPARATION("#취업준비");

private final String description;

HobbyType(String description) {
this.description = description;
}

public String getDescription() {
return description;
}

// 문자열 값을 받아 해당하는 HobbyType 반환
public static HobbyType fromDescription(String description) {
for (HobbyType hobbyType : HobbyType.values()) {
if (hobbyType.getDescription().equals(description)) {
return hobbyType;
}
}
return null; // 예외 처리로 수정 예정
}

}
Loading

0 comments on commit bac6f3f

Please sign in to comment.