Skip to content

Commit

Permalink
Merge pull request #5 from Mojacknong/feature_4/온보딩-마이페이지-개발
Browse files Browse the repository at this point in the history
Feature 4/온보딩 마이페이지 개발
  • Loading branch information
Ryeolee authored Apr 1, 2024
2 parents af393f3 + e162194 commit 4c820e6
Show file tree
Hide file tree
Showing 22 changed files with 761 additions and 272 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ dependencies {
// redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
testImplementation 'it.ozimov:embedded-redis:0.7.2'

// 유효성 검사
implementation 'org.springframework.boot:spring-boot-starter-validation'

}

tasks.named('bootBuildImage') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;


Expand All @@ -40,14 +42,15 @@ public class AuthService {

private final KakaoLogin kakaoLogin;

@Transactional
public BaseResponseDto<LoginResponseDto> googleLogin(String googleAccessToken) {

return BaseResponseDto.of(SuccessCode.SUCCESS,
googleLogin.loginMethod(googleAccessToken)

);
}

@Transactional
public BaseResponseDto<LoginResponseDto> kakaoLogin(String kakaoAccessToken) {

return BaseResponseDto.of(SuccessCode.SUCCESS,
Expand All @@ -56,32 +59,45 @@ public BaseResponseDto<LoginResponseDto> kakaoLogin(String kakaoAccessToken) {


}

@Transactional
public BaseResponseDto<Void> logout(Long userId) {

redisManager.deleteValueByKey(String.valueOf(userId));
deleteredisToken(userId);
log.info("로그아웃 완료");
return BaseResponseDto.of(SuccessCode.SUCCESS,null);
}

@Transactional
public BaseResponseDto<TokenResponseDto> reissueToken(Long userId, String refreshToken) {

validateRefreshToken(userId, refreshToken);
User user = userRepository.findUserData(Long.valueOf(userId));
User user = findUser(userId);
return BaseResponseDto.of(SuccessCode.SUCCESS,
TokenResponseDto.of(
jwtTokenProvider.createAccessToken(Long.valueOf(userId), user.getRole()),
refreshToken
));
}

private void deleteredisToken(Long userId){
redisManager.deleteValueByKey(String.valueOf(userId));
}
private String getRedisToken(Long key){return redisManager.getValueByKey(key);}


private User findUser(Long userId) {
return userRepository.findUserData(Long.valueOf(userId));
}


private void validateRefreshToken(Long userId, String refreshToken) {

String redisRefreshToken = redisManager.getValueByKey(userId);
String redisRefreshToken = getRedisToken(userId);
if (!refreshToken.equals(redisRefreshToken)) {

throw new AuthRefreshTokenValidateException("일치하지 않는 토큰입니다.");
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.modernfarmer.farmusspring.domain.user.controller;

import com.modernfarmer.farmusspring.domain.auth.dto.LoginResponseDto;
import com.modernfarmer.farmusspring.domain.auth.entity.CustomUser;
import com.modernfarmer.farmusspring.domain.user.dto.request.SetLevelRequest;
import com.modernfarmer.farmusspring.domain.user.dto.request.SetMotivationRequest;
import com.modernfarmer.farmusspring.domain.user.dto.response.SetLevelResponse;
import com.modernfarmer.farmusspring.domain.user.entity.User;
import com.modernfarmer.farmusspring.domain.user.service.OnBoardingService;
import com.modernfarmer.farmusspring.global.response.BaseResponseDto;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user")

public class OnBoardingController {

private final OnBoardingService onBoardingService;


@PostMapping(value = "/on-boarding/motivation")
public BaseResponseDto<Void> settingMotivation(@AuthenticationPrincipal CustomUser user,
@Validated @RequestBody SetMotivationRequest setMotivationRequest
) {
return onBoardingService.settingMotivation(
User.createUserObject(user.getUserId()),
setMotivationRequest);
}

@PostMapping(value = "/on-boarding/level")
public BaseResponseDto<SetLevelResponse> settingLevel(@AuthenticationPrincipal CustomUser user,
@Validated @RequestBody SetLevelRequest setLevelRequest
) {

return onBoardingService.settingLevel(user.getUserId(), setLevelRequest);
}


@PatchMapping (value = "/on-boarding/complete")
public BaseResponseDto<Void> completeOnBoarding(@AuthenticationPrincipal CustomUser user) {

return onBoardingService.completeOnBoarding(user.getUserId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.modernfarmer.farmusspring.domain.user.controller;

import com.modernfarmer.farmusspring.domain.auth.entity.CustomUser;
import com.modernfarmer.farmusspring.domain.user.dto.request.SetLevelRequest;
import com.modernfarmer.farmusspring.domain.user.dto.response.SetLevelResponse;
import com.modernfarmer.farmusspring.domain.user.dto.response.UserProfileResponse;
import com.modernfarmer.farmusspring.domain.user.service.UserService;
import com.modernfarmer.farmusspring.global.response.BaseResponseDto;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user")
public class UserController {

private final UserService userService;

@GetMapping(value = "")
public BaseResponseDto<UserProfileResponse> selectUserProfile(@AuthenticationPrincipal CustomUser user) {

return userService.selectUserProfile(user.getUserId());
}


@PatchMapping(value = "/profile-image")
public BaseResponseDto<Void> deleteProfleImage(@AuthenticationPrincipal CustomUser user) {

return userService.deleteProfleImage(user.getUserId());
}

@PostMapping(value = "/profile", produces = MediaType.APPLICATION_JSON_VALUE)
public BaseResponseDto<Void> settingProfile(
@AuthenticationPrincipal CustomUser user,
@RequestPart(value = "file", required = false) MultipartFile multipartFile,
@RequestParam("nickName") String nickName) throws IOException {

return userService.settingProfile(user.getUserId(), multipartFile,nickName);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.modernfarmer.farmusspring.domain.user.dto.request;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;


@Getter
@AllArgsConstructor
@NoArgsConstructor
public class SetLevelRequest {

@NotNull(message = "null 값을 가지면 안됩니다.")
private int time;

@NotNull(message = "null 값을 가지면 안됩니다.")
private String skill;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.modernfarmer.farmusspring.domain.user.dto.request;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;


import java.util.ArrayList;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class SetMotivationRequest {

@NotNull(message = "null 값을 가지면 안됩니다.")
private ArrayList<String> motivation;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.modernfarmer.farmusspring.domain.user.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
@Getter
public class SetLevelResponse {

private String level;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.modernfarmer.farmusspring.domain.user.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
@Getter
public class UserProfileResponse {


private String nickName;
private String userImageUrl;
private long dDay;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.experimental.SuperBuilder;


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

Expand Down Expand Up @@ -45,23 +46,24 @@ public class User extends BaseEntity {
@Column(nullable = true)
private String level;

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
@Builder.Default
private List<UserFirebaseToken> userFirebaseTokens = new ArrayList<>();

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Builder.Default
private List<UserMotivation> userMotivations = new ArrayList<>();

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
@Builder.Default
private List<MyVeggie> myVeggies = new ArrayList<>();

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
@Builder.Default
private List<DiaryComment> diaryComments = new ArrayList<>();

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
@Builder.Default
private List<DiaryLike> diaryLikes = new ArrayList<>();

Expand All @@ -82,6 +84,13 @@ public static User createUser(String role, String userNumber, boolean early){

return newUser;

}
public static User createUserObject(Long userId){
User userObject = User.builder()
.id(userId)
.build();
return userObject;

}

public void addUserFirebaseToken(UserFirebaseToken userFirebaseToken) {
Expand Down Expand Up @@ -111,4 +120,6 @@ public void addMissionPostComment(MissionPostComment missionPostComment) {
public void addMissionPostLike(MissionPostLike missionPostLike) {
missionPostLikes.add(missionPostLike);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;


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


@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -29,8 +29,8 @@ public class UserFirebaseToken extends BaseEntity {
@Column(name = "token")
private String token;

@OnDelete(action = OnDeleteAction.CASCADE)
@ManyToOne(fetch = FetchType.LAZY)
@MapsId
@JoinColumn(name = "user_id")
private User user;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;


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


@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -29,8 +29,8 @@ public class UserMotivation extends BaseEntity {
@Column(name = "motivation")
private String motivation;

@ManyToOne(fetch = FetchType.LAZY)
@MapsId
@OnDelete(action = OnDeleteAction.CASCADE)
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id")
private User user;

Expand All @@ -46,4 +46,17 @@ public static UserMotivation createUserMotivation(String motivation, User user){

}

public static UserMotivation createUserMotivationWithId(Long id, String motivation, User user){
UserMotivation newUserMotivation = UserMotivation.builder()
.id(id)
.motivation(motivation)
.user(user)
.build();

user.addUserMotivation(newUserMotivation);

return newUserMotivation;

}

}
Loading

0 comments on commit 4c820e6

Please sign in to comment.