Skip to content

Commit

Permalink
Merge pull request #126 from YogitTeam/feat/user-profile
Browse files Browse the repository at this point in the history
#8 #95 유저 프로필 필수, 추가 정보 입력 API 통합
  • Loading branch information
shinhn authored Jan 26, 2023
2 parents c5bcca8 + 30f3a61 commit 7244277
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class UserController {
private final UserService userService;

/**
* 유저 필수 정보 입력 및 수정
* 유저 프로필 정보 입력 및 수정
* @author 강신현
*/
@ApiOperation(value = "유저 필수 정보 입력 및 수정")
@ApiOperation(value = "유저 프로필 정보 입력 및 수정")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", required = true, dataTypeClass = Long.class, example = "1"),
@ApiImplicitParam(name = "userName", dataTypeClass = String.class, example = "강신현"),
Expand All @@ -42,18 +42,23 @@ public class UserController {
@ApiImplicitParam(name = "nationality", dataTypeClass = String.class, example = "Korea"),
@ApiImplicitParam(name = "languageNames", dataType = "list", value = "언어 이름을 순서대로 넣어주세요"),
@ApiImplicitParam(name = "languageLevels", dataType = "list", value = "언어 레벨을 순서대로 넣어주세요"),
@ApiImplicitParam(name = "latitude", dataTypeClass = Float.class, example = "10.23"),
@ApiImplicitParam(name = "longitude", dataTypeClass = Float.class, example = "10.23"),
@ApiImplicitParam(name = "aboutMe", dataTypeClass = String.class, example = "캠핑 모임에 관심이 많습니다."),
@ApiImplicitParam(name = "job", dataTypeClass = String.class, example = "학생"),
@ApiImplicitParam(name = "cityName", dataTypeClass = String.class, example = "SEOUL"),
@ApiImplicitParam(name = "refreshToken", required = true, dataTypeClass = String.class, example = "reb5085c395164587b84ac583d023011f.0.sryrq.IDLsECw-rsTozfsX0Yz-CA")
})
@PatchMapping("/essential-profile")
public ApplicationResponse<UserEssentialProfileRes> enterEssentialProfile(@ModelAttribute CreateUserEssentialProfileReq createUserEssentialProfileReq){
return userService.enterEssentialProfile(createUserEssentialProfileReq);
@PatchMapping("/profile")
public ApplicationResponse<UserProfileRes> enterProfile(@ModelAttribute CreateUserProfileReq createUserProfileReq){
return userService.enterProfile(createUserProfileReq);
}

/**
* 유저 Profile 조회
* 유저 프로필 조회
* @author 강신현
*/
@ApiOperation(value = "유저 Profile 조회")
@ApiOperation(value = "유저 프로필 조회")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", required = true, dataTypeClass = Long.class, example = "1"),
@ApiImplicitParam(name = "refreshTokenUserId", required = true, dataTypeClass = Long.class, example = "21"),
Expand All @@ -65,10 +70,10 @@ public ApplicationResponse<UserProfileRes> getProfile(@ModelAttribute GetUserPro
}

/**
* 유저 Profile 삭제
* 유저 프로필 삭제
* @author 강신현
*/
@ApiOperation(value = "유저 Profile 삭제")
@ApiOperation(value = "유저 프로필 삭제")
@ApiImplicitParam(name = "userId", required = true, dataTypeClass = Long.class, example = "0")
@PatchMapping("/{userId}")
public ApplicationResponse<Void> delProfile(@PathVariable Long userId){
Expand Down Expand Up @@ -106,25 +111,6 @@ public ApplicationResponse<UserImagesRes> AddAndDeleteUserImage(@ModelAttribute
return userService.AddAndDeleteUserImage(addAndDeleteUserImageReq);
}

/**
* 유저 추가 정보 입력 및 수정
* @author 강신현
*/
@ApiOperation(value = "유저 추가 정보 입력 및 수정")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", required = true, dataTypeClass = Long.class, example = "1"),
@ApiImplicitParam(name = "latitude", dataTypeClass = Float.class, example = "10.23"),
@ApiImplicitParam(name = "longitude", dataTypeClass = Float.class, example = "10.23"),
@ApiImplicitParam(name = "aboutMe", dataTypeClass = String.class, example = "캠핑 모임에 관심이 많습니다."),
@ApiImplicitParam(name = "job", dataTypeClass = String.class, example = "학생"),
@ApiImplicitParam(name = "cityName", dataTypeClass = String.class, example = "SEOUL"),
@ApiImplicitParam(name = "refreshToken", required = true, dataTypeClass = String.class, example = "reb5085c395164587b84ac583d023011f.0.sryrq.IDLsECw-rsTozfsX0Yz-CA")
})
@PatchMapping("/additional-profile")
public ApplicationResponse<UserAdditionalProfileRes> enterAdditionalProfile(@ModelAttribute AddUserAdditionalProfileReq addUserAdditionalProfileReq){
return userService.enterAdditionalProfile(addUserAdditionalProfileReq);
}

/**
* 유저 회원가입 (일반)
* @author 강신현
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.yogit.server.user.dto.request;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class CreateUserProfileReq {
Long userId;

String userName;
int userAge;
String gender;
String nationality;

List<String> languageNames;
List<String> languageLevels;

float latitude;
float longitude;
String aboutMe;
String job;

String cityName;
List<String> interests;

String refreshToken;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ public class UserProfileRes {
String aboutMe;
UserStatus userStatus;
String phone;
String job;

List<String> languageNames = new ArrayList<>();
List<String> languageLevels = new ArrayList<>();

// 유저 추가 정보
String administrativeArea;
float longtitude;
float latitude;

CityName city;
String city;

List<String> interests = new ArrayList<>();

Expand All @@ -57,6 +57,7 @@ public static UserProfileRes create(User user){
userProfileRes.aboutMe = user.getAboutMe();
userProfileRes.userStatus = user.getUserStatus();
userProfileRes.phone = user.getPhoneNum();
userProfileRes.job = user.getJob();

// 유저 추가 정보
if(user.getLongtitude() != null)userProfileRes.longtitude = user.getLongtitude();
Expand All @@ -65,10 +66,6 @@ public static UserProfileRes create(User user){
return userProfileRes;
}

public void addCity(CityName city){
this.city = city;
}

public void addLanguage(String languageName, String languageLevel){
this.languageNames.add(languageName);
this.languageLevels.add(languageLevel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public interface UserService {
Void validateRefreshToken(Long userId, String refreshToken);

ApplicationResponse<UserEssentialProfileRes> enterEssentialProfile(CreateUserEssentialProfileReq createUserEssentialProfileReq);
ApplicationResponse<UserProfileRes> enterProfile(CreateUserProfileReq createUserProfileReq);

ApplicationResponse<UserProfileRes> getProfile(GetUserProfileReq getUserProfileReq);

Expand All @@ -18,8 +18,6 @@ public interface UserService {

ApplicationResponse<UserImagesRes> AddAndDeleteUserImage(AddAndDeleteUserImageReq addAndDeleteUserImageReq);

ApplicationResponse<UserAdditionalProfileRes> enterAdditionalProfile(AddUserAdditionalProfileReq addUserAdditionalProfileReq);

ApplicationResponse<Void> createUser(CreateUserReq createUserReq);

User createUserApple(CreateUserAppleReq createUserAppleReq);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
import com.yogit.server.global.dto.ApplicationResponse;
import com.yogit.server.s3.AwsS3Service;
import com.yogit.server.user.dto.request.*;
import com.yogit.server.user.dto.response.*;
import com.yogit.server.user.dto.response.LogoutRes;
import com.yogit.server.user.dto.response.UserDeviceTokenRes;
import com.yogit.server.user.dto.response.UserImagesRes;
import com.yogit.server.user.dto.response.UserProfileRes;
import com.yogit.server.user.entity.*;
import com.yogit.server.user.exception.*;
import com.yogit.server.user.exception.NotFoundUserException;
import com.yogit.server.user.exception.NotFoundUserImageException;
import com.yogit.server.user.exception.UserDuplicationLoginId;
import com.yogit.server.user.exception.UserGenderException;
import com.yogit.server.user.repository.*;
import lombok.RequiredArgsConstructor;
import net.minidev.json.JSONArray;
Expand Down Expand Up @@ -56,36 +62,99 @@ public Void validateRefreshToken(Long userId, String refreshToken){
return null;
}

// 국가 정보 조회 Open Api
JSONObject getNation(String nationality) {

JSONObject nation = null;
try {
URL url = new URL("http://apis.data.go.kr/1262000/CountryFlagService2/getCountryFlagList2?ServiceKey=Os%2B%2Fa%2BWGJPptb5Rf1U850JQo11XO0fCA5cL3YND%2BxoxUm8B38IDZjHKlrpV0gj496%2Br53Rg61EdzI9KDuILDrg%3D%3D" + "&cond[country_iso_alp2::EQ]=" + nationality);

BufferedReader bf;
bf = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
String result = bf.readLine();

JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(result);
JSONArray data = (JSONArray) jsonObject.get("data");
nation = (JSONObject) data.get(0);
} catch (Exception e) {
e.printStackTrace();
}

return nation;
}

@Transactional
@Override
public ApplicationResponse<UserEssentialProfileRes> enterEssentialProfile(CreateUserEssentialProfileReq createUserEssentialProfileReq){
public ApplicationResponse<UserProfileRes> enterProfile(CreateUserProfileReq createUserProfileReq){

validateRefreshToken(createUserEssentialProfileReq.getUserId(), createUserEssentialProfileReq.getRefreshToken());
validateRefreshToken(createUserProfileReq.getUserId(), createUserProfileReq.getRefreshToken());

if(!createUserEssentialProfileReq.getGender().equals("Prefer not to say") && !createUserEssentialProfileReq.getGender().equals("Male") && !createUserEssentialProfileReq.getGender().equals("Female")) throw new UserGenderException();
if(!createUserProfileReq.getGender().equals("Prefer not to say") && !createUserProfileReq.getGender().equals("Male") && !createUserProfileReq.getGender().equals("Female")) throw new UserGenderException();

User user = userRepository.findByUserId(createUserEssentialProfileReq.getUserId()).orElseThrow(NotFoundUserException::new);
user.changeUserInfo(createUserEssentialProfileReq.getUserName(), createUserEssentialProfileReq.getUserAge(), createUserEssentialProfileReq.getGender(), createUserEssentialProfileReq.getNationality());
User user = userRepository.findByUserId(createUserProfileReq.getUserId()).orElseThrow(NotFoundUserException::new);
user.changeUserInfo(createUserProfileReq.getUserName(), createUserProfileReq.getUserAge(), createUserProfileReq.getGender(), createUserProfileReq.getNationality());

UserEssentialProfileRes userEssentialProfileRes = UserEssentialProfileRes.create(createUserEssentialProfileReq.getUserId(), createUserEssentialProfileReq.getUserName(), createUserEssentialProfileReq.getUserAge(), createUserEssentialProfileReq.getGender(), createUserEssentialProfileReq.getNationality());
UserProfileRes userProfileRes = UserProfileRes.create(user);

if(createUserEssentialProfileReq.getLanguageNames() != null) {
if(createUserProfileReq.getLanguageNames() != null) {
// 기존 languages 삭제
languageRepository.deleteAllByUserId(createUserEssentialProfileReq.getUserId());
languageRepository.deleteAllByUserId(createUserProfileReq.getUserId());
// 새로운 languages 추가
for(int i=0;i < createUserEssentialProfileReq.getLanguageNames().size(); i++){
for(int i=0;i < createUserProfileReq.getLanguageNames().size(); i++){
Language language = Language.builder()
.user(user)
.name(createUserEssentialProfileReq.getLanguageNames().get(i))
.level(createUserEssentialProfileReq.getLanguageLevels().get(i))
.name(createUserProfileReq.getLanguageNames().get(i))
.level(createUserProfileReq.getLanguageLevels().get(i))
.build();
languageRepository.save(language);

userEssentialProfileRes.addLanguage(createUserEssentialProfileReq.getLanguageNames().get(i), createUserEssentialProfileReq.getLanguageLevels().get(i));
userProfileRes.addLanguage(createUserProfileReq.getLanguageNames().get(i), createUserProfileReq.getLanguageLevels().get(i));
}
}

return ApplicationResponse.ok(userEssentialProfileRes);
user.addAdditionalProfile(createUserProfileReq.getLatitude(), createUserProfileReq.getLongitude(), createUserProfileReq.getAboutMe(), createUserProfileReq.getJob());

// 기존에 존재하는 city인 경우
if(cityRepository.existsByCityName(createUserProfileReq.getCityName())){
City city = cityRepository.findByCityName(createUserProfileReq.getCityName());
city.addUser(user);
}
else{ // 기존에 존재하지 않는 city인 경우
City city = City.builder()
.user(user)
.cityName(createUserProfileReq.getCityName())
.build();
cityRepository.save(city);
city.addUser(user);
}

userProfileRes.setCity(createUserProfileReq.getCityName());

for(String interestName : createUserProfileReq.getInterests()){
Interest interest = Interest.builder()
.name(interestName)
.build();
interestRepository.save(interest);

UserInterest userInterest = UserInterest.builder()
.user(user)
.interest(interest)
.build();
userInterestRepository.save(userInterest);

userProfileRes.getInterests().add(interestName);
}

// 국가 이름 및 국기 이미지 조회
JSONObject nation = getNation(user.getNationality());
String country_eng_nm = nation.get("country_eng_nm").toString();
String download_url = nation.get("download_url").toString();

userProfileRes.setCountry_eng_nm(country_eng_nm);
userProfileRes.setDownload_url(download_url);

return ApplicationResponse.ok(userProfileRes);
}

@Override
Expand Down Expand Up @@ -121,33 +190,12 @@ public ApplicationResponse<UserProfileRes> getProfile(GetUserProfileReq getUserP
userProfileRes.addImage(awsS3Service.makeUrlOfFilename(userImage.getImgUUid()));
}

try {

URL url = new URL("http://apis.data.go.kr/1262000/CountryFlagService2/getCountryFlagList2?ServiceKey=Os%2B%2Fa%2BWGJPptb5Rf1U850JQo11XO0fCA5cL3YND%2BxoxUm8B38IDZjHKlrpV0gj496%2Br53Rg61EdzI9KDuILDrg%3D%3D" + "&cond[country_iso_alp2::EQ]=" + user.getNationality());
JSONObject nation = getNation(user.getNationality());
String country_eng_nm = nation.get("country_eng_nm").toString();
String download_url = nation.get("download_url").toString();

BufferedReader bf;

bf = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));

String result = bf.readLine();

JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(result);

JSONArray data = (JSONArray) jsonObject.get("data");


JSONObject nation = (JSONObject) data.get(0);

String country_eng_nm = nation.get("country_eng_nm").toString();
String download_url = nation.get("download_url").toString();

userProfileRes.setCountry_eng_nm(country_eng_nm);
userProfileRes.setDownload_url(download_url);

} catch (Exception e) {
e.printStackTrace();
}
userProfileRes.setCountry_eng_nm(country_eng_nm);
userProfileRes.setDownload_url(download_url);

return ApplicationResponse.ok(userProfileRes);
}
Expand Down Expand Up @@ -249,53 +297,6 @@ public Void deleteUserImage(List<Long> deleteUserImageIds){
return null;
}


@Override
@Transactional
public ApplicationResponse<UserAdditionalProfileRes> enterAdditionalProfile(AddUserAdditionalProfileReq addUserAdditionalProfileReq){

validateRefreshToken(addUserAdditionalProfileReq.getUserId(), addUserAdditionalProfileReq.getRefreshToken());

User user = userRepository.findByUserId(addUserAdditionalProfileReq.getUserId()).orElseThrow(NotFoundUserException::new);

user.addAdditionalProfile(addUserAdditionalProfileReq.getLatitude(), addUserAdditionalProfileReq.getLongitude(), addUserAdditionalProfileReq.getAboutMe(), addUserAdditionalProfileReq.getJob());

UserAdditionalProfileRes userAdditionalProfileRes = UserAdditionalProfileRes.create(user);

// 기존에 존재하는 city인 경우
if(cityRepository.existsByCityName(addUserAdditionalProfileReq.getCityName())){
City city = cityRepository.findByCityName(addUserAdditionalProfileReq.getCityName());
city.addUser(user);
}
else{ // 기존에 존재하지 않는 city인 경우
City city = City.builder()
.user(user)
.cityName(addUserAdditionalProfileReq.getCityName())
.build();
cityRepository.save(city);
city.addUser(user);
}

userAdditionalProfileRes.setCityName(addUserAdditionalProfileReq.getCityName());

for(String interestName : addUserAdditionalProfileReq.getInterests()){
Interest interest = Interest.builder()
.name(interestName)
.build();
interestRepository.save(interest);

UserInterest userInterest = UserInterest.builder()
.user(user)
.interest(interest)
.build();
userInterestRepository.save(userInterest);

userAdditionalProfileRes.getInterests().add(interestName);
}

return ApplicationResponse.ok(userAdditionalProfileRes);
}

@Override
@Transactional
public ApplicationResponse<Void> createUser(CreateUserReq createUserReq){
Expand Down

0 comments on commit 7244277

Please sign in to comment.