Skip to content

Commit

Permalink
Fix: ColumnDefault 설정으로 인해 PrePersist 설정 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
BYEONGRYEOL committed Jun 16, 2024
1 parent 710336b commit 2a8a30f
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 18 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.0'
id 'org.springframework.boot' version '3.3.0'
id 'io.spring.dependency-management' version '1.1.4'
}

Expand Down Expand Up @@ -67,8 +67,8 @@ dependencies {
// test && test h2
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'com.h2database:h2:1.4.200'

// testRuntimeOnly 'com.h2database:h2'
runtimeOnly 'com.h2database:h2'
// swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
implementation 'org.springframework.boot:spring-boot-devtools'
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/gt/genti/domain/Creator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import com.gt.genti.domain.common.BaseTimeEntity;
import com.gt.genti.domain.enums.BankType;
import com.gt.genti.domain.enums.Sex;
import com.gt.genti.domain.enums.UserStatus;
import com.gt.genti.domain.enums.converter.db.BankTypeConverter;

import jakarta.persistence.Column;
Expand All @@ -17,6 +19,7 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
Expand Down Expand Up @@ -50,7 +53,6 @@ public class Creator extends BaseTimeEntity {

@Convert(converter = BankTypeConverter.class)
@Column(name = "bank_type", nullable = false)
@ColumnDefault("NONE")
BankType bankType;

@Column(name = "account_number", nullable = false)
Expand All @@ -64,6 +66,16 @@ public class Creator extends BaseTimeEntity {
@Column(name = "completed_task_count", nullable = false)
int completedTaskCount;

@PrePersist
public void prePersist() {
if (this.bankType == null) {
this.bankType = BankType.NONE;
}
if(this.workable == null){
this.workable = true;
}
}

public Creator(User user) {
this.workable = true;
this.user = user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Table(name = "directory_traversal_attack")
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/com/gt/genti/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Table(name = "users") // h2 예약어 해결법 못찾음
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -57,7 +60,7 @@ public class User extends BaseTimeEntity {
@Column(name = "email")
String email;

@Column(name = "sex")
@Column(name = "sex", nullable = false)
@Convert(converter = SexConverter.class)
Sex sex;

Expand Down Expand Up @@ -103,6 +106,7 @@ public class User extends BaseTimeEntity {
LocalDateTime lastLoginDate;

@Column(name = "login", nullable = false)
@ColumnDefault("false")
Boolean login;

// user hard delete시 deposit도 삭제
Expand All @@ -116,6 +120,19 @@ public class User extends BaseTimeEntity {
@Column(name = "birth_date")
LocalDate birthDate;

@PrePersist
public void prePersist() {
if (this.userStatus == null) {
this.userStatus = UserStatus.ACTIVATED; // 기본값 설정
}
if (this.login == null) {
this.login = true;
}
if (this.sex == null){
this.sex = Sex.NONE;
}
}

public static User createNewSocialUser(OAuthAttributes oauthAttributes) {
String email = oauthAttributes.getEmail();
String username = oauthAttributes.getUsername();
Expand Down Expand Up @@ -184,7 +201,7 @@ public Boolean isLogin() {
public User(List<PictureProfile> pictureProfileList, List<PictureUserFace> pictureUserFaceList, String email,
String introduction,
String username, String nickname, UserStatus userStatus, Boolean emailVerified, String loginId, String password,
Creator creator, UserRole userRole, OauthType lastLoginSocialPlatform, LocalDateTime deletedAt) {
Creator creator, UserRole userRole, OauthType lastLoginSocialPlatform, LocalDateTime deletedAt, LocalDateTime lastLoginDate) {
this.pictureProfileList = pictureProfileList;
this.pictureUserFaceList = pictureUserFaceList;
this.email = email;
Expand All @@ -199,6 +216,7 @@ public User(List<PictureProfile> pictureProfileList, List<PictureUserFace> pictu
this.userRole = userRole;
this.lastLoginSocialPlatform = lastLoginSocialPlatform;
this.deletedAt = deletedAt;
this.lastLoginDate = lastLoginDate;
}

public void addRequestCount() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/gt/genti/other/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public class SecurityConfig {
"/swagger-ui/**",
"/swagger-resources/**",
"/v3/api-docs/**",
"/api-docs/**"
"/api-docs/**",
"/h2-console/**",
"/h2-console"
};

public static String[] COMMON_RESOURCE_AND_ALLOWED_URL;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/gt/genti/repository/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import com.gt.genti.domain.User;
import com.gt.genti.domain.enums.UserRole;

import io.lettuce.core.Value;

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ spring:
group:
local:
deploy:
test:
include:
- secret
- common
Expand Down
15 changes: 9 additions & 6 deletions src/test/java/com/gt/genti/service/MatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

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

import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -33,7 +34,7 @@
import com.gt.genti.repository.UserRepository;
import com.gt.genti.service.config.TestConfig;

@ActiveProfiles("test")
@ActiveProfiles({"test", "common", "secret"})
@SpringBootTest(classes = TestConfig.class)
public class MatchTest {
@Autowired
Expand All @@ -43,10 +44,10 @@ public class MatchTest {
PictureService pictureService;

@Autowired
UserRepository uploaderRepository;
UserRepository userRepository;

@Autowired
UserService uploaderService;
UserService userService;

@Autowired
AdminService adminService;
Expand All @@ -58,12 +59,12 @@ public class MatchTest {
public void oneRequestWithAdminStrategy() {
// given
User requester = getTestUser();
User savedUser = uploaderRepository.save(requester);
User savedUser = userRepository.save(requester);

User adminUser = getTestAdminUser();
User savedAdmin = uploaderRepository.save(adminUser);
User savedAdmin = userRepository.save(adminUser);
// admin으로 uploaderrole 변경시 admin creator 생성됨
uploaderService.updateUserRole(savedAdmin.getId(),
userService.updateUserRole(savedAdmin.getId(),
UserRoleUpdateRequestDto.builder().userRole(UserRole.ADMIN).build());

CreatePicturePoseCommand command = getCreatePicturePoseCommand(savedUser);
Expand Down Expand Up @@ -101,6 +102,7 @@ private User getTestAdminUser() {
.userStatus(UserStatus.ACTIVATED)
.lastLoginSocialPlatform(OauthType.GOOGLE)
.username("adminusername")
.lastLoginDate(LocalDateTime.now())
.build();
}

Expand Down Expand Up @@ -146,6 +148,7 @@ private static User getTestUser() {
.nickname("nickname")
.pictureUserFaceList(null)
.username("username")
.lastLoginDate(LocalDateTime.now())
.build();
}
}
6 changes: 3 additions & 3 deletions src/test/resources/application-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ spring:
settings:
web-allow-others: true
datasource:
url: jdbc:h2:mem:test;MODE=MySQL
url: jdbc:h2:mem:test;MODE=MySQL;DATABASE_TO_UPPER=false;CASE_INSENSITIVE_IDENTIFIERS=true;DB_CLOSE_DELAY=-1
username: sa
password:
driver-class-name: org.h2.Driver
jpa:
hibernate:
ddl-auto: create
ddl-auto: update
properties:
hibernate:
format_sql: 'true'
use_sql_comments: 'true'
show_sql: 'true'
dialect: org.hibernate.dialect.MySQL8Dialect
dialect: org.hibernate.dialect.MySQLDialect
redis:
host: localhost

Expand Down

0 comments on commit 2a8a30f

Please sign in to comment.