Add insurance detail
This commit is contained in:
24
src/main/java/com/hzwnrw/insurance/config/WebConfig.java
Normal file
24
src/main/java/com/hzwnrw/insurance/config/WebConfig.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.hzwnrw.insurance.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig {
|
||||
|
||||
@Bean
|
||||
public WebMvcConfigurer corsConfigurer() {
|
||||
return new WebMvcConfigurer() {
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**") // apply to all endpoints
|
||||
.allowedOrigins("http://localhost:4200") // Angular dev server
|
||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
||||
.allowedHeaders("*")
|
||||
.allowCredentials(true);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,40 @@
|
||||
package com.hzwnrw.insurance.controller;
|
||||
|
||||
import com.hzwnrw.insurance.dto.InsuranceDTO;
|
||||
import com.hzwnrw.insurance.repository.InsuranceRepository;
|
||||
import com.hzwnrw.insurance.dto.InsuranceDetailDTO;
|
||||
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;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/insurance")
|
||||
public class InsuranceController {
|
||||
|
||||
private final InsuranceService insuranceService;
|
||||
private final InsuranceRepository insuranceRepository;
|
||||
public InsuranceController(InsuranceService insuranceService, InsuranceRepository insuranceRepository){
|
||||
public InsuranceController(InsuranceService insuranceService){
|
||||
this.insuranceService = insuranceService;
|
||||
this.insuranceRepository = insuranceRepository;
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public ResponseEntity<InsuranceDTO> createInsurance(@RequestBody InsuranceDTO insurance){
|
||||
return ResponseEntity.ok(insuranceService.createInsurance(insurance));
|
||||
}
|
||||
|
||||
@PostMapping("/all")
|
||||
public List<InsuranceDTO> getAllInsurance() {
|
||||
return insuranceService.getAllInsurance();
|
||||
}
|
||||
|
||||
@GetMapping("/detail/{id}")
|
||||
public ResponseEntity<InsuranceDetailDTO> getInsuranceDetail(@PathVariable Long id) {
|
||||
InsuranceDetailDTO detail = insuranceService.getInsuranceDetailById(id);
|
||||
if (detail != null) {
|
||||
return ResponseEntity.ok(detail);
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
|
||||
public class InsuranceDTO {
|
||||
private long id;
|
||||
private String name;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.hzwnrw.insurance.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class InsuranceDetailDTO {
|
||||
|
||||
private long id;
|
||||
private Long insuranceId;
|
||||
private String coverageDetails;
|
||||
private LocalDate startDate;
|
||||
private LocalDate endDate;
|
||||
private double premiumAmount;
|
||||
private double coverageLimit;
|
||||
private String policyStatus;
|
||||
|
||||
public InsuranceDetailDTO(long id, Long insuranceId, String coverageDetails, LocalDate startDate, LocalDate endDate, double premiumAmount, double coverageLimit, String policyStatus) {
|
||||
this.id = id;
|
||||
this.insuranceId = insuranceId;
|
||||
this.coverageDetails = coverageDetails;
|
||||
this.startDate = startDate;
|
||||
this.endDate = endDate;
|
||||
this.premiumAmount = premiumAmount;
|
||||
this.coverageLimit = coverageLimit;
|
||||
this.policyStatus = policyStatus;
|
||||
}
|
||||
}
|
||||
84
src/main/java/com/hzwnrw/insurance/mapper/GenericMapper.java
Normal file
84
src/main/java/com/hzwnrw/insurance/mapper/GenericMapper.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package com.hzwnrw.insurance.mapper;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class GenericMapper {
|
||||
|
||||
// Convert entity to DTO
|
||||
public static <D, E> D mapToDTO(E entity, Class<D> dtoClass) {
|
||||
if (entity == null) return null;
|
||||
|
||||
try {
|
||||
D dto = dtoClass.getDeclaredConstructor().newInstance();
|
||||
|
||||
// Copy simple properties
|
||||
BeanUtils.copyProperties(entity, dto);
|
||||
|
||||
// Handle nested objects and collections
|
||||
mapNestedProperties(entity, dto);
|
||||
|
||||
return dto;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Mapping failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Convert DTO to entity
|
||||
public static <E, D> E mapToEntity(D dto, Class<E> entityClass) {
|
||||
if (dto == null) return null;
|
||||
|
||||
try {
|
||||
E entity = entityClass.getDeclaredConstructor().newInstance();
|
||||
|
||||
// Copy simple properties
|
||||
BeanUtils.copyProperties(dto, entity);
|
||||
|
||||
// Handle nested objects and collections
|
||||
mapNestedProperties(dto, entity);
|
||||
|
||||
return entity;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Mapping failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Convert list of entities to list of DTOs
|
||||
public static <D, E> List<D> mapList(List<E> entities, Class<D> dtoClass) {
|
||||
if (entities == null) return Collections.emptyList();
|
||||
return entities.stream()
|
||||
.map(e -> mapToDTO(e, dtoClass))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// Convert list of DTOs to list of entities
|
||||
public static <E, D> List<E> mapEntityList(List<D> dtos, Class<E> entityClass) {
|
||||
if (dtos == null) return Collections.emptyList();
|
||||
return dtos.stream()
|
||||
.map(d -> mapToEntity(d, entityClass))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// Optional: handle arrays
|
||||
public static <D, E> D[] mapArray(E[] entities, Class<D> dtoClass) {
|
||||
if (entities == null) return null;
|
||||
@SuppressWarnings("unchecked")
|
||||
D[] dtos = (D[]) Array.newInstance(dtoClass, entities.length);
|
||||
for (int i = 0; i < entities.length; i++) {
|
||||
dtos[i] = mapToDTO(entities[i], dtoClass);
|
||||
}
|
||||
return dtos;
|
||||
}
|
||||
|
||||
// Private helper for nested objects (shallow handling for now)
|
||||
private static void mapNestedProperties(Object source, Object target) {
|
||||
// This is a placeholder. You can implement:
|
||||
// - Detect fields that are collections or arrays
|
||||
// - Recursively map nested objects
|
||||
// - Handle date formatting or enums
|
||||
// For complex mapping, a library like MapStruct is still better.
|
||||
}
|
||||
}
|
||||
@@ -1,29 +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();
|
||||
}
|
||||
}
|
||||
//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();
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -15,7 +15,12 @@ public class Insurance {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "name", nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(name = "category", nullable = false)
|
||||
private String category;
|
||||
|
||||
@Column(name = "price", nullable = false)
|
||||
private double price;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.hzwnrw.insurance.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Entity
|
||||
@Table(name = "insurance_detail")
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class InsuranceDetail {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "insurance_id", nullable = false)
|
||||
private Long insuranceId;
|
||||
|
||||
@Column(name = "coverage_details")
|
||||
private String coverageDetails;
|
||||
|
||||
@Column(name = "start_date", nullable = false)
|
||||
private LocalDate startDate;
|
||||
|
||||
@Column(name = "end_date", nullable = false)
|
||||
private LocalDate endDate;
|
||||
|
||||
@Column(name = "premium_amount", nullable = false)
|
||||
private double premiumAmount;
|
||||
|
||||
@Column(name = "coverage_limit", nullable = false)
|
||||
private double coverageLimit;
|
||||
|
||||
@Column(name = "policy_status", nullable = false)
|
||||
private String policyStatus;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.hzwnrw.insurance.repository;
|
||||
|
||||
import com.hzwnrw.insurance.model.InsuranceDetail;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface InsuranceDetailRepository extends JpaRepository<InsuranceDetail, Long> {
|
||||
}
|
||||
@@ -1,32 +1,50 @@
|
||||
package com.hzwnrw.insurance.service;
|
||||
|
||||
import com.hzwnrw.insurance.dto.InsuranceDTO;
|
||||
import com.hzwnrw.insurance.mapper.InsuranceMapper;
|
||||
import com.hzwnrw.insurance.dto.InsuranceDetailDTO;
|
||||
import com.hzwnrw.insurance.mapper.GenericMapper;
|
||||
import com.hzwnrw.insurance.model.Insurance;
|
||||
import com.hzwnrw.insurance.repository.InsuranceDetailRepository;
|
||||
import com.hzwnrw.insurance.repository.InsuranceRepository;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@Service
|
||||
public class InsuranceService {
|
||||
|
||||
private final InsuranceRepository insuranceRepository;
|
||||
private final InsuranceDetailRepository insuranceDetailRepository;
|
||||
|
||||
public InsuranceService(InsuranceRepository insuranceRepository) {
|
||||
public InsuranceService(InsuranceRepository insuranceRepository, InsuranceDetailRepository insuranceDetailRepository) {
|
||||
this.insuranceRepository = insuranceRepository;
|
||||
this.insuranceDetailRepository = insuranceDetailRepository;
|
||||
}
|
||||
|
||||
public Page<InsuranceDTO> getAllInsurance(Pageable pageable){
|
||||
return insuranceRepository.findAll(pageable)
|
||||
.map(InsuranceMapper::toDTO);
|
||||
public List<InsuranceDTO> getAllInsurance() {
|
||||
return insuranceRepository.findAll()
|
||||
.stream()
|
||||
.map(entity -> GenericMapper.mapToDTO(entity, InsuranceDTO.class))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public InsuranceDTO createInsurance(InsuranceDTO dto) {
|
||||
Insurance saved = insuranceRepository.save(InsuranceMapper.toEntity(dto));
|
||||
return InsuranceMapper.toDTO(saved);
|
||||
Insurance entity = GenericMapper.mapToEntity(dto, Insurance.class);
|
||||
Insurance saved = insuranceRepository.save(entity);
|
||||
return GenericMapper.mapToDTO(saved, InsuranceDTO.class);
|
||||
}
|
||||
|
||||
|
||||
public InsuranceDetailDTO getInsuranceDetailById(Long id) {
|
||||
return insuranceDetailRepository.findById(id)
|
||||
.map(entity -> GenericMapper.mapToDTO(entity, InsuranceDetailDTO.class))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user