Skip to content

Commit

Permalink
[feat] 유저 도메인 db, 엔티티 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryeolee committed Nov 1, 2023
1 parent 13f00d4 commit a066a36
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;


@EnableJpaAuditing
@SpringBootApplication
public class FarmusUserApplication {

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

import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;

@Data
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity {

@CreatedDate
@Column
private LocalDateTime createdAt;

@LastModifiedDate
private LocalDateTime updateAt;
}
44 changes: 32 additions & 12 deletions src/main/java/modernfarmer/server/farmususer/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.Instant;
import java.util.LinkedHashSet;
import java.util.Set;

@Getter
@Setter
Expand All @@ -14,7 +17,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "users")
public class User {
public class User extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", nullable = false)
Expand All @@ -24,17 +27,6 @@ public class User {
@Column(name = "username", length = 45)
private String username;

@Size(max = 45)
@NotNull
@Column(name = "usernumber", nullable = false, length = 45)
private String usernumber;

@Size(max = 45)
@NotNull
@Column(name = "role", nullable = false, length = 45)
private String role;


@Size(max = 45)
@Column(name = "nickname", length = 45)
private String nickname;
Expand All @@ -43,4 +35,32 @@ public class User {
@Column(name = "profile_image")
private String profileImage;

@Size(max = 6)
@NotNull
@Column(name = "roles", nullable = false, length = 6)
private String roles;

@Size(max = 30)
@NotNull
@Column(name = "user_number", nullable = false, length = 30)
private String userNumber;

@Size(max = 100)
@Column(name = "firebase_token", length = 100)
private String firebaseToken;

@Size(max = 40)
@Column(name = "motivation", length = 40)
private String motivation;

@Size(max = 10)
@Column(name = "level", length = 10)
private String level;


@OneToMany(mappedBy = "user")
private Set<UserFirebaseToken> userFirebaseTokens = new LinkedHashSet<>();



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 javax.validation.constraints.Size;
import java.time.Instant;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "user_firebase_token")
public class UserFirebaseToken extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "firebase_token_id", nullable = false)
private Integer id;

@Size(max = 100)
@NotNull
@Column(name = "token", nullable = false, length = 100)
private String token;

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


}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
@Repository
public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findByUsernumber(String usernumber);
@Query("SELECT a.role FROM User AS a WHERE a.id = :userId")
Optional<User> findByUserNumber(String usernumber);
@Query("SELECT a.roles FROM User AS a WHERE a.id = :userId")
String findUserRole(@Param("userId") Long userId);

@Modifying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,24 @@ public TokenResponseDto googleLogin(String accessToken) {
GoogleUserResponseDto userInfo = userInfoMono.block();


Optional<User> userData = userRepository.findByUsernumber(String.valueOf(userInfo.getId()));
Optional<User> userData = userRepository.findByUserNumber(String.valueOf(userInfo.getId()));

log.info(String.valueOf(userInfo.getEmail()));
log.info(String.valueOf(userInfo.getPicture()));
log.info(String.valueOf(userInfo.getId()));

if(userData.isEmpty()){
user = User.builder()
.usernumber(String.valueOf(userInfo.getId()))
.role("USER")
.userNumber(String.valueOf(userInfo.getId()))
.roles("USER")
.profileImage(userInfo.getPicture())
.build();
early = true;

userRepository.save(user);
}

Optional<User> userLoginData = userRepository.findByUsernumber(String.valueOf(userInfo.getId()));
Optional<User> userLoginData = userRepository.findByUserNumber(String.valueOf(userInfo.getId()));


String refreshToken = jwtTokenProvider.createRefreshToken(userLoginData.get().getId());
Expand All @@ -75,7 +75,7 @@ public TokenResponseDto googleLogin(String accessToken) {
.early(early)
.accessToken(jwtTokenProvider.createAccessToken(
userLoginData.get().getId(),
String.valueOf(userLoginData.get().getRole())))
String.valueOf(userLoginData.get().getRoles())))
.refreshToken(refreshToken)
.build();

Expand All @@ -99,12 +99,12 @@ public TokenResponseDto kakaoLogin(String accessToken) {
log.info(String.valueOf(userInfo.getKakao_account().getProfile().getNickname()));


Optional<User> userData = userRepository.findByUsernumber(String.valueOf(userInfo.getId()));
Optional<User> userData = userRepository.findByUserNumber(String.valueOf(userInfo.getId()));

if(userData.isEmpty()){
user = User.builder()
.usernumber(String.valueOf(userInfo.getId()))
.role("USER")
.userNumber(String.valueOf(userInfo.getId()))
.roles("USER")
.profileImage(userInfo.getKakao_account().getProfile().getProfile_image_url())
.build();

Expand All @@ -113,7 +113,7 @@ public TokenResponseDto kakaoLogin(String accessToken) {
userRepository.save(user);
}

Optional<User> userLoginData = userRepository.findByUsernumber(String.valueOf(userInfo.getId()));
Optional<User> userLoginData = userRepository.findByUserNumber(String.valueOf(userInfo.getId()));


String refreshToken = jwtTokenProvider.createRefreshToken(userLoginData.get().getId());
Expand All @@ -124,7 +124,7 @@ public TokenResponseDto kakaoLogin(String accessToken) {
.early(early)
.accessToken(jwtTokenProvider.createAccessToken(
userLoginData.get().getId(),
String.valueOf(userLoginData.get().getRole())))
String.valueOf(userLoginData.get().getRoles())))
.refreshToken(refreshToken)
.build();

Expand Down

0 comments on commit a066a36

Please sign in to comment.