#29 pass: create entity
This commit is contained in:
@@ -21,12 +21,17 @@ repositories {
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-batch'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
runtimeOnly 'com.h2database:h2'
|
||||
runtimeOnly 'com.mysql:mysql-connector-j'
|
||||
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
testImplementation 'org.springframework.batch:spring-batch-test'
|
||||
|
||||
testCompileOnly 'org.projectlombok:lombok'
|
||||
testAnnotationProcessor 'org.projectlombok:lombok'
|
||||
}
|
||||
|
||||
tasks.named('test') {
|
||||
|
||||
@@ -14,7 +14,6 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@EnableBatchProcessing
|
||||
@SpringBootApplication
|
||||
@RequiredArgsConstructor
|
||||
public class PassBatchApplication {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.passbatch.config;
|
||||
|
||||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@EnableBatchProcessing
|
||||
@Configuration
|
||||
public class BatchConfig {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.passbatch.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
|
||||
@EnableJpaAuditing
|
||||
@Configuration
|
||||
public class JpaConfig {
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.passbatch.repository;
|
||||
|
||||
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;
|
||||
|
||||
@MappedSuperclass
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
public abstract class BaseEntity {
|
||||
|
||||
@CreatedDate
|
||||
@Column(updatable = false, nullable = false)
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
@LastModifiedDate
|
||||
private LocalDateTime modifiedAt;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.example.passbatch.repository.booking;
|
||||
|
||||
import com.example.passbatch.repository.BaseEntity;
|
||||
import com.example.passbatch.repository.pass.PassEntity;
|
||||
import com.example.passbatch.repository.user.UserEntity;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@Entity
|
||||
@Table(name = "booking")
|
||||
public class BookingEntity extends BaseEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer bookingSeq;
|
||||
private Integer passSeq;
|
||||
private String userId;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private BookingStatus status;
|
||||
private boolean usedPass;
|
||||
private boolean attended;
|
||||
|
||||
private LocalDateTime startedAt;
|
||||
private LocalDateTime endedAt;
|
||||
private LocalDateTime cancelledAt;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "userId", insertable = false, updatable = false)
|
||||
private UserEntity userEntity;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "passSeq", insertable = false, updatable = false)
|
||||
private PassEntity passEntity;
|
||||
|
||||
// endedAt 기준, yyyy-MM-HH 00:00:00
|
||||
public LocalDateTime getStatisticsAt() {
|
||||
return this.endedAt.withHour(0).withMinute(0).withSecond(0).withNano(0);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.passbatch.repository.booking;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
public interface BookingRepository extends JpaRepository<BookingEntity, Integer> {
|
||||
@Transactional
|
||||
@Modifying
|
||||
@Query(value = "UPDATE BookingEntity b" +
|
||||
" SET b.usedPass = :usedPass," +
|
||||
" b.modifiedAt = CURRENT_TIMESTAMP" +
|
||||
" WHERE b.passSeq = :passSeq")
|
||||
int updateUsedPass(Integer passSeq, boolean usedPass);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.example.passbatch.repository.booking;
|
||||
|
||||
public enum BookingStatus {
|
||||
READY, PROGRESSED, COMPLETED, CANCELLED
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.passbatch.repository.packages;
|
||||
|
||||
import com.example.passbatch.repository.BaseEntity;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@Entity
|
||||
@Table(name = "package")
|
||||
public class PackageEntity extends BaseEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer packageSeq;
|
||||
|
||||
private String packageName;
|
||||
private Integer count;
|
||||
private Integer period;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.passbatch.repository.packages;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
public interface PackageRepository extends JpaRepository<PackageEntity, Long> {
|
||||
|
||||
List<PackageEntity> findByCreatedAtAfter(LocalDateTime dateTime, Pageable pageable);
|
||||
|
||||
@Transactional
|
||||
@Modifying
|
||||
@Query(value = "UPDATE PackageEntity p" +
|
||||
" SET p.count = :count," +
|
||||
" p.period = :period" +
|
||||
" WHERE p.packageSeq = :packageSeq")
|
||||
int updateCountAndPeriod(Integer packageSeq, Integer count, Integer period);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.example.passbatch.repository.pass;
|
||||
|
||||
import com.example.passbatch.repository.BaseEntity;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@Entity
|
||||
@Table(name = "pass")
|
||||
public class PassEntity extends BaseEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer passSeq;
|
||||
private Integer packageSeq;
|
||||
private String userId;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private PassStatus status;
|
||||
private Integer remainingCount;
|
||||
|
||||
private LocalDateTime startedAt;
|
||||
private LocalDateTime endedAt;
|
||||
private LocalDateTime expiredAt;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.example.passbatch.repository.pass;
|
||||
|
||||
public interface PassRepository extends JpaRepository<PassEntity, Integer> {
|
||||
@Transactional
|
||||
@Modifying
|
||||
@Query(value = "UPDATE PassEntity p" +
|
||||
" SET p.remainingCount = :remainingCount," +
|
||||
" p.modifiedAt = CURRENT_TIMESTAMP" +
|
||||
" WHERE p.passSeq = :passSeq")
|
||||
int updateRemainingCount(Integer passSeq, Integer remainingCount);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.example.passbatch.repository.pass;
|
||||
|
||||
public enum PassStatus {
|
||||
READY, PROGRESSED, EXPIRED
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.example.passbatch.repository.user;
|
||||
|
||||
import com.example.passbatch.repository.BaseEntity;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.hibernate.annotations.Type;
|
||||
import org.hibernate.annotations.TypeDef;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Map;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@Entity
|
||||
@Table(name = "user")
|
||||
// json의 타입을 정의합니다.
|
||||
@TypeDef(name = "json", typeClass = JsonType.class)
|
||||
public class UserEntity extends BaseEntity {
|
||||
@Id
|
||||
private String userId;
|
||||
|
||||
private String userName;
|
||||
@Enumerated(EnumType.STRING)
|
||||
private UserStatus status;
|
||||
private String phone;
|
||||
|
||||
// json 형태로 저장되어 있는 문자열 데이터를 Map으로 매핑합니다.
|
||||
@Type(type = "json")
|
||||
private Map<String, Object> meta;
|
||||
|
||||
public String getUuid() {
|
||||
String uuid = null;
|
||||
if (meta.containsKey("uuid")) {
|
||||
uuid = String.valueOf(meta.get("uuid"));
|
||||
}
|
||||
return uuid;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.example.passbatch.repository.user;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<UserEntity, Integer> {
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.example.passbatch.repository.user;
|
||||
|
||||
public enum UserStatus {
|
||||
ACTIVE, INACTIVE
|
||||
}
|
||||
@@ -1,6 +1,13 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:mydb
|
||||
username: pass_local
|
||||
password: 1234
|
||||
driver-class-name: org.h2.Driver
|
||||
# url: jdbc:h2:mem:mydb
|
||||
url: jdbc:mysql://127.0.0.1:13306/pass_local?zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8&serverTimezone=Asia/Seoul
|
||||
username: pass_local_user
|
||||
password: test1234
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
hikari:
|
||||
maximum-pool-size: 20
|
||||
|
||||
batch:
|
||||
jdbc:
|
||||
initialize-schema: always
|
||||
21
pass-batch/src/test/resources/application.yml
Normal file
21
pass-batch/src/test/resources/application.yml
Normal file
@@ -0,0 +1,21 @@
|
||||
spring:
|
||||
config:
|
||||
activate:
|
||||
on-profile: test
|
||||
datasource:
|
||||
url: jdbc:mysql://127.0.0.1:13306/pass_local?zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8&serverTimezone=Asia/Seoul
|
||||
username: pass_local_user
|
||||
password: test1234
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
jpa:
|
||||
show-sql: true
|
||||
properties:
|
||||
hibernate:
|
||||
format_sql: false
|
||||
batch:
|
||||
jdbc:
|
||||
initialize-schema: always
|
||||
|
||||
logging:
|
||||
level:
|
||||
org.hibernate.type.descriptor.sql: off
|
||||
Reference in New Issue
Block a user