261012_01_FST: first commit
This commit is contained in:
14
src/main/java/com/kri/kart/server/KartServerApplication.java
Normal file
14
src/main/java/com/kri/kart/server/KartServerApplication.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.kri.kart.server;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ConfigurationPropertiesScan
|
||||
public class KartServerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(KartServerApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.kri.kart.server.common.consts;
|
||||
|
||||
public final class ExampleConstant {
|
||||
|
||||
public static final int DEFAULT_BATCH_SIZE = 1_000;
|
||||
|
||||
private ExampleConstant() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.kri.kart.server.common.enums;
|
||||
|
||||
public enum ExampleStatus {
|
||||
ACTIVE,
|
||||
INACTIVE
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.kri.kart.server.common.util;
|
||||
|
||||
public final class StringNormalizer {
|
||||
|
||||
private StringNormalizer() {
|
||||
}
|
||||
|
||||
public static String trimToNull(String value) {
|
||||
if (value == null) return null;
|
||||
String normalized = value.trim();
|
||||
return normalized.isEmpty() ? null : normalized;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* 프로젝트 전용 Bean Validation annotation과 validator를 배치합니다.
|
||||
*/
|
||||
package com.kri.kart.server.common.validation;
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.kri.kart.server.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ConfigurationProperties(prefix = "kri.file")
|
||||
public class FileStorageProperties {
|
||||
|
||||
private String baseDir = "./upload-file";
|
||||
private String resourcePattern = "/files/**";
|
||||
}
|
||||
15
src/main/java/com/kri/kart/server/config/JdbcConfig.java
Normal file
15
src/main/java/com/kri/kart/server/config/JdbcConfig.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.kri.kart.server.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
@Configuration
|
||||
public class JdbcConfig {
|
||||
|
||||
@Bean
|
||||
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
|
||||
return new JdbcTemplate(dataSource);
|
||||
}
|
||||
}
|
||||
19
src/main/java/com/kri/kart/server/config/QueryDSLConfig.java
Normal file
19
src/main/java/com/kri/kart/server/config/QueryDSLConfig.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.kri.kart.server.config;
|
||||
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class QueryDSLConfig {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
@Bean
|
||||
public JPAQueryFactory jpaQueryFactory() {
|
||||
return new JPAQueryFactory(entityManager);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.kri.kart.server.config;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class WebResourceConfig implements WebMvcConfigurer {
|
||||
|
||||
private final FileStorageProperties fileStorageProperties;
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
String location = Path.of(fileStorageProperties.getBaseDir())
|
||||
.toAbsolutePath()
|
||||
.normalize()
|
||||
.toUri()
|
||||
.toString();
|
||||
|
||||
registry.addResourceHandler(fileStorageProperties.getResourcePattern())
|
||||
.addResourceLocations(location.endsWith("/") ? location : location + "/");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.kri.kart.server.controller;
|
||||
|
||||
import com.kri.kart.server.dto.request.CreateExampleRequest;
|
||||
import com.kri.kart.server.dto.response.ExampleResponse;
|
||||
import com.kri.kart.server.service.ExampleService;
|
||||
import jakarta.validation.Valid;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/v1/examples")
|
||||
public class ExampleController {
|
||||
|
||||
private final ExampleService exampleService;
|
||||
|
||||
@GetMapping
|
||||
public List<ExampleResponse> search(
|
||||
@RequestParam(defaultValue = "") String keyword,
|
||||
@RequestParam(defaultValue = "20") int limit
|
||||
) {
|
||||
return exampleService.search(keyword, limit);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public ExampleResponse create(@Valid @RequestBody CreateExampleRequest request) {
|
||||
return exampleService.create(request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.kri.kart.server.controller;
|
||||
|
||||
import java.util.Map;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class PingController {
|
||||
|
||||
@GetMapping("/ping")
|
||||
public Map<String, String> ping() {
|
||||
return Map.of("status", "ok");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.kri.kart.server.dto.request;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Positive;
|
||||
|
||||
public record CreateExampleRequest(
|
||||
@NotNull @Positive Long groupId,
|
||||
@NotNull @Positive Integer sequence,
|
||||
@NotBlank String name
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.kri.kart.server.dto.response;
|
||||
|
||||
import com.kri.kart.server.persistence.entity.ExampleEntity;
|
||||
|
||||
public record ExampleResponse(
|
||||
Long groupId,
|
||||
Integer sequence,
|
||||
String name,
|
||||
String status
|
||||
) {
|
||||
public static ExampleResponse from(ExampleEntity entity) {
|
||||
return new ExampleResponse(
|
||||
entity.getId().getGroupId(),
|
||||
entity.getId().getSequence(),
|
||||
entity.getName(),
|
||||
entity.getStatus().name()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.kri.kart.server.persistence.entity;
|
||||
|
||||
import com.kri.kart.server.common.enums.ExampleStatus;
|
||||
import com.kri.kart.server.persistence.entity.id.ExampleEntityId;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.EmbeddedId;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@Entity
|
||||
@Table(name = "t_example")
|
||||
@NoArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class ExampleEntity {
|
||||
|
||||
@EmbeddedId
|
||||
private ExampleEntityId id;
|
||||
|
||||
@Column(name = "name", nullable = false, length = 100)
|
||||
private String name;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(name = "status", nullable = false, length = 20)
|
||||
private ExampleStatus status;
|
||||
|
||||
@Builder
|
||||
public ExampleEntity(ExampleEntityId id, String name, ExampleStatus status) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.status = status == null ? ExampleStatus.ACTIVE : status;
|
||||
}
|
||||
|
||||
public void changeName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.kri.kart.server.persistence.entity.id;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Embeddable;
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Getter
|
||||
@Embeddable
|
||||
@EqualsAndHashCode
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class ExampleEntityId implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Column(name = "group_id", nullable = false)
|
||||
private Long groupId;
|
||||
|
||||
@Column(name = "sequence", nullable = false)
|
||||
private Integer sequence;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.kri.kart.server.persistence.jdbc;
|
||||
|
||||
public record ExampleBulkItem(
|
||||
Long groupId,
|
||||
Integer sequence,
|
||||
String name,
|
||||
String status
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.kri.kart.server.persistence.jdbc;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@RequiredArgsConstructor
|
||||
public class ExampleBulkJdbcRepository {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public void insertAll(List<ExampleBulkItem> items, int batchSize) {
|
||||
if (items == null || items.isEmpty()) return;
|
||||
|
||||
String sql = """
|
||||
insert into t_example (group_id, sequence, name, status)
|
||||
values (?, ?, ?, ?)
|
||||
""";
|
||||
|
||||
int safeBatchSize = Math.max(batchSize, 1);
|
||||
for (int from = 0; from < items.size(); from += safeBatchSize) {
|
||||
int to = Math.min(from + safeBatchSize, items.size());
|
||||
List<ExampleBulkItem> batch = items.subList(from, to);
|
||||
|
||||
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
|
||||
@Override
|
||||
public void setValues(PreparedStatement statement, int index) throws SQLException {
|
||||
ExampleBulkItem item = batch.get(index);
|
||||
statement.setLong(1, item.groupId());
|
||||
statement.setInt(2, item.sequence());
|
||||
statement.setString(3, item.name());
|
||||
statement.setString(4, item.status());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBatchSize() {
|
||||
return batch.size();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.kri.kart.server.persistence.mapper;
|
||||
|
||||
import com.kri.kart.server.dto.response.ExampleResponse;
|
||||
import java.util.List;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
@Mapper
|
||||
public interface ExampleMapper {
|
||||
List<ExampleResponse> findByName(@Param("keyword") String keyword);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.kri.kart.server.persistence.query;
|
||||
|
||||
import com.kri.kart.server.persistence.entity.QExampleEntity;
|
||||
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||
|
||||
public final class ExamplePredicates {
|
||||
|
||||
private ExamplePredicates() {
|
||||
}
|
||||
|
||||
public static BooleanExpression nameContains(QExampleEntity example, String keyword) {
|
||||
if (keyword == null || keyword.isBlank()) return null;
|
||||
return example.name.containsIgnoreCase(keyword.trim());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.kri.kart.server.persistence.repository;
|
||||
|
||||
import com.kri.kart.server.persistence.entity.ExampleEntity;
|
||||
import java.util.List;
|
||||
|
||||
public interface ExampleQueryRepository {
|
||||
List<ExampleEntity> findByNameContains(String keyword, int limit);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.kri.kart.server.persistence.repository;
|
||||
|
||||
import static com.kri.kart.server.persistence.entity.QExampleEntity.exampleEntity;
|
||||
|
||||
import com.kri.kart.server.persistence.entity.ExampleEntity;
|
||||
import com.kri.kart.server.persistence.query.ExamplePredicates;
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class ExampleQueryRepositoryImpl implements ExampleQueryRepository {
|
||||
|
||||
private final JPAQueryFactory jpaQueryFactory;
|
||||
|
||||
@Override
|
||||
public List<ExampleEntity> findByNameContains(String keyword, int limit) {
|
||||
return jpaQueryFactory
|
||||
.selectFrom(exampleEntity)
|
||||
.where(ExamplePredicates.nameContains(exampleEntity, keyword))
|
||||
.orderBy(exampleEntity.id.groupId.asc(), exampleEntity.id.sequence.asc())
|
||||
.limit(Math.max(limit, 1))
|
||||
.fetch();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.kri.kart.server.persistence.repository;
|
||||
|
||||
import com.kri.kart.server.persistence.entity.ExampleEntity;
|
||||
import com.kri.kart.server.persistence.entity.id.ExampleEntityId;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ExampleRepository
|
||||
extends JpaRepository<ExampleEntity, ExampleEntityId>, ExampleQueryRepository {
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.kri.kart.server.scheduler;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@ConditionalOnProperty(
|
||||
prefix = "app.scheduler.example",
|
||||
name = "enabled",
|
||||
havingValue = "true"
|
||||
)
|
||||
public class ExampleScheduler {
|
||||
|
||||
@Scheduled(cron = "${app.scheduler.example.cron:0 0 1 * * *}")
|
||||
public void execute() {
|
||||
log.info("Example scheduler executed");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.kri.kart.server.service;
|
||||
|
||||
import com.kri.kart.server.common.enums.ExampleStatus;
|
||||
import com.kri.kart.server.dto.request.CreateExampleRequest;
|
||||
import com.kri.kart.server.dto.response.ExampleResponse;
|
||||
import com.kri.kart.server.persistence.entity.ExampleEntity;
|
||||
import com.kri.kart.server.persistence.entity.id.ExampleEntityId;
|
||||
import com.kri.kart.server.persistence.repository.ExampleRepository;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Transactional(readOnly = true)
|
||||
public class ExampleService {
|
||||
|
||||
private final ExampleRepository exampleRepository;
|
||||
|
||||
@Transactional
|
||||
public ExampleResponse create(CreateExampleRequest request) {
|
||||
ExampleEntity entity = ExampleEntity.builder()
|
||||
.id(new ExampleEntityId(request.groupId(), request.sequence()))
|
||||
.name(request.name())
|
||||
.status(ExampleStatus.ACTIVE)
|
||||
.build();
|
||||
return ExampleResponse.from(exampleRepository.save(entity));
|
||||
}
|
||||
|
||||
public List<ExampleResponse> search(String keyword, int limit) {
|
||||
return exampleRepository.findByNameContains(keyword == null ? "" : keyword, limit)
|
||||
.stream()
|
||||
.map(ExampleResponse::from)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user