Skip to content

Commit

Permalink
feat: 유저 생성 및 수정시간을 추가한다
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb committed Dec 27, 2023
1 parent d8704da commit 363a012
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/net/teumteum/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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

@EnableJpaAuditing
@SpringBootApplication
public class Application {

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/net/teumteum/core/entity/TimeBaseEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.teumteum.core.entity;

import jakarta.persistence.Column;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PreUpdate;
import java.time.Instant;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@MappedSuperclass
public abstract class TimeBaseEntity {

@Column(name = "created_at", columnDefinition = "TIMESTAMP(6)", nullable = false, updatable = false)
protected Instant createdAt;

@Column(name = "updated_at", columnDefinition = "TIMESTAMP(6)", nullable = false)
protected Instant updatedAt;

@PrePersist
void prePersist() {
var now = Instant.now();

createdAt = createdAt != null ? createdAt : now;
updatedAt = updatedAt != null ? updatedAt : now;
}

@PreUpdate
void preUpdate() {
updatedAt = updatedAt != null ? updatedAt : Instant.now();
}

}
3 changes: 2 additions & 1 deletion src/main/java/net/teumteum/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import net.teumteum.core.entity.TimeBaseEntity;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.Assert;

@Getter
@Entity(name = "users")
@NoArgsConstructor
@AllArgsConstructor
public class User {
public class User extends TimeBaseEntity {

@Id
@Column(name = "id")
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/db/migration/V1__create_users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ create table if not exists users(
status enum('직장인','학생','취업준비생'),
terms_of_service boolean not null,
privacy_policy boolean not null,
created_at timestamp(6) not null,
updated_at timestamp(6) not null,
primary key (id)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void Find_success_if_exists_user_id_input() {
Assertions.assertThat(result)
.isPresent()
.usingRecursiveComparison()
.ignoringFields("value.createdAt", "value.updatedAt")
.isEqualTo(Optional.of(existsUser));
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ create table if not exists users(
status enum('직장인','학생','취업준비생'),
terms_of_service boolean not null,
privacy_policy boolean not null,
created_at timestamp(6) not null,
updated_at timestamp(6) not null,
primary key (id)
);

Expand Down

0 comments on commit 363a012

Please sign in to comment.