diff --git a/src/main/java/modernfarmer/server/farmususer/FarmusUserApplication.java b/src/main/java/modernfarmer/server/farmususer/FarmusUserApplication.java index ecfda07..29944ad 100644 --- a/src/main/java/modernfarmer/server/farmususer/FarmusUserApplication.java +++ b/src/main/java/modernfarmer/server/farmususer/FarmusUserApplication.java @@ -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 { diff --git a/src/main/java/modernfarmer/server/farmususer/user/entity/BaseEntity.java b/src/main/java/modernfarmer/server/farmususer/user/entity/BaseEntity.java new file mode 100644 index 0000000..f3fa8b1 --- /dev/null +++ b/src/main/java/modernfarmer/server/farmususer/user/entity/BaseEntity.java @@ -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; +} \ No newline at end of file diff --git a/src/main/java/modernfarmer/server/farmususer/user/entity/User.java b/src/main/java/modernfarmer/server/farmususer/user/entity/User.java index 5893b64..d87f53f 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/entity/User.java +++ b/src/main/java/modernfarmer/server/farmususer/user/entity/User.java @@ -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 @@ -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) @@ -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; @@ -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 userFirebaseTokens = new LinkedHashSet<>(); + + + } \ No newline at end of file diff --git a/src/main/java/modernfarmer/server/farmususer/user/entity/UserFirebaseToken.java b/src/main/java/modernfarmer/server/farmususer/user/entity/UserFirebaseToken.java new file mode 100644 index 0000000..5c97fb4 --- /dev/null +++ b/src/main/java/modernfarmer/server/farmususer/user/entity/UserFirebaseToken.java @@ -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; + + +} \ No newline at end of file diff --git a/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java b/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java index 33c6fc2..6bbbf0d 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java +++ b/src/main/java/modernfarmer/server/farmususer/user/repository/UserRepository.java @@ -12,8 +12,8 @@ @Repository public interface UserRepository extends JpaRepository { - Optional findByUsernumber(String usernumber); - @Query("SELECT a.role FROM User AS a WHERE a.id = :userId") + Optional findByUserNumber(String usernumber); + @Query("SELECT a.roles FROM User AS a WHERE a.id = :userId") String findUserRole(@Param("userId") Long userId); @Modifying diff --git a/src/main/java/modernfarmer/server/farmususer/user/service/AuthService.java b/src/main/java/modernfarmer/server/farmususer/user/service/AuthService.java index 9978c2a..c465e16 100644 --- a/src/main/java/modernfarmer/server/farmususer/user/service/AuthService.java +++ b/src/main/java/modernfarmer/server/farmususer/user/service/AuthService.java @@ -47,7 +47,7 @@ public TokenResponseDto googleLogin(String accessToken) { GoogleUserResponseDto userInfo = userInfoMono.block(); - Optional userData = userRepository.findByUsernumber(String.valueOf(userInfo.getId())); + Optional userData = userRepository.findByUserNumber(String.valueOf(userInfo.getId())); log.info(String.valueOf(userInfo.getEmail())); log.info(String.valueOf(userInfo.getPicture())); @@ -55,8 +55,8 @@ public TokenResponseDto googleLogin(String accessToken) { 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; @@ -64,7 +64,7 @@ public TokenResponseDto googleLogin(String accessToken) { userRepository.save(user); } - Optional userLoginData = userRepository.findByUsernumber(String.valueOf(userInfo.getId())); + Optional userLoginData = userRepository.findByUserNumber(String.valueOf(userInfo.getId())); String refreshToken = jwtTokenProvider.createRefreshToken(userLoginData.get().getId()); @@ -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(); @@ -99,12 +99,12 @@ public TokenResponseDto kakaoLogin(String accessToken) { log.info(String.valueOf(userInfo.getKakao_account().getProfile().getNickname())); - Optional userData = userRepository.findByUsernumber(String.valueOf(userInfo.getId())); + Optional 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(); @@ -113,7 +113,7 @@ public TokenResponseDto kakaoLogin(String accessToken) { userRepository.save(user); } - Optional userLoginData = userRepository.findByUsernumber(String.valueOf(userInfo.getId())); + Optional userLoginData = userRepository.findByUserNumber(String.valueOf(userInfo.getId())); String refreshToken = jwtTokenProvider.createRefreshToken(userLoginData.get().getId()); @@ -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();