Setup insurance backend
This commit is contained in:
79
pom.xml
79
pom.xml
@@ -38,26 +38,67 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.persistence</groupId>
|
||||
<artifactId>jakarta.persistence-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
<version>3.4.3</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
||||
31
src/main/java/com/hzwnrw/insurance/config/MySQLConfig.java
Normal file
31
src/main/java/com/hzwnrw/insurance/config/MySQLConfig.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.hzwnrw.insurance.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class MySQLConfig {
|
||||
|
||||
@Value("${spring.datasource.url}")
|
||||
private String dbUrl;
|
||||
|
||||
@Value("${spring.datasource.username}")
|
||||
private String dbUsername;
|
||||
|
||||
@Value("${spring.datasource.password}")
|
||||
private String dbPassword;
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
return DataSourceBuilder.create()
|
||||
.url(dbUrl)
|
||||
.username(dbUsername)
|
||||
.password(dbPassword)
|
||||
.driverClassName("com.mysql.cj.jdbc.Driver")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.hzwnrw.insurance.controller;
|
||||
|
||||
import com.hzwnrw.insurance.dto.InsuranceDTO;
|
||||
import com.hzwnrw.insurance.repository.InsuranceRepository;
|
||||
import com.hzwnrw.insurance.service.InsuranceService;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/insurance")
|
||||
public class InsuranceController {
|
||||
|
||||
private final InsuranceService insuranceService;
|
||||
private final InsuranceRepository insuranceRepository;
|
||||
public InsuranceController(InsuranceService insuranceService, InsuranceRepository insuranceRepository){
|
||||
this.insuranceService = insuranceService;
|
||||
this.insuranceRepository = insuranceRepository;
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public ResponseEntity<InsuranceDTO> createInsurance(@RequestBody InsuranceDTO insurance){
|
||||
return ResponseEntity.ok(insuranceService.createInsurance(insurance));
|
||||
}
|
||||
}
|
||||
23
src/main/java/com/hzwnrw/insurance/dto/InsuranceDTO.java
Normal file
23
src/main/java/com/hzwnrw/insurance/dto/InsuranceDTO.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.hzwnrw.insurance.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
|
||||
public class InsuranceDTO {
|
||||
private long id;
|
||||
private String name;
|
||||
private String category;
|
||||
private double price;
|
||||
|
||||
public InsuranceDTO(long id, String name, String category, double price) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.category = category;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.hzwnrw.insurance.mapper;
|
||||
|
||||
import com.hzwnrw.insurance.dto.InsuranceDTO;
|
||||
import com.hzwnrw.insurance.model.Insurance;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class InsuranceMapper {
|
||||
|
||||
private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
public static InsuranceDTO toDTO(Insurance insurance){
|
||||
return new InsuranceDTO(
|
||||
insurance.getId(),
|
||||
insurance.getName(),
|
||||
insurance.getCategory(),
|
||||
insurance.getPrice()
|
||||
);
|
||||
}
|
||||
|
||||
public static Insurance toEntity(InsuranceDTO dto){
|
||||
return Insurance.builder()
|
||||
// .id(dto.getId())
|
||||
.name(dto.getName())
|
||||
.category(dto.getCategory())
|
||||
.price(dto.getPrice())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
21
src/main/java/com/hzwnrw/insurance/model/Insurance.java
Normal file
21
src/main/java/com/hzwnrw/insurance/model/Insurance.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.hzwnrw.insurance.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "insurance")
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class Insurance {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
private String category;
|
||||
private double price;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.hzwnrw.insurance.repository;
|
||||
|
||||
import com.hzwnrw.insurance.model.Insurance;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface InsuranceRepository extends JpaRepository<Insurance, Long> {
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.hzwnrw.insurance.service;
|
||||
|
||||
import com.hzwnrw.insurance.dto.InsuranceDTO;
|
||||
import com.hzwnrw.insurance.mapper.InsuranceMapper;
|
||||
import com.hzwnrw.insurance.model.Insurance;
|
||||
import com.hzwnrw.insurance.repository.InsuranceRepository;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
|
||||
@Service
|
||||
public class InsuranceService {
|
||||
|
||||
private final InsuranceRepository insuranceRepository;
|
||||
|
||||
public InsuranceService(InsuranceRepository insuranceRepository) {
|
||||
this.insuranceRepository = insuranceRepository;
|
||||
}
|
||||
|
||||
public Page<InsuranceDTO> getAllInsurance(Pageable pageable){
|
||||
return insuranceRepository.findAll(pageable)
|
||||
.map(InsuranceMapper::toDTO);
|
||||
}
|
||||
|
||||
public InsuranceDTO createInsurance(InsuranceDTO dto) {
|
||||
Insurance saved = insuranceRepository.save(InsuranceMapper.toEntity(dto));
|
||||
return InsuranceMapper.toDTO(saved);
|
||||
}
|
||||
}
|
||||
@@ -1 +1,16 @@
|
||||
spring.application.name=insurance
|
||||
server.port=1204
|
||||
app.env=DEV
|
||||
|
||||
# MySQL Configuration
|
||||
spring.datasource.url=jdbc:mysql://192.168.100.104:3306/insurance_db?useSSL=false&serverTimezone=Asia/Kuala_Lumpur&allowPublicKeyRetrieval=true
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=232vQLxF3UdsR
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
|
||||
# JPA Configuration
|
||||
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.jpa.show-sql=true
|
||||
|
||||
logging.level.org.springframework=debug
|
||||
|
||||
Reference in New Issue
Block a user