Skip to content

Commit

Permalink
finished today work
Browse files Browse the repository at this point in the history
  • Loading branch information
upangka committed Jun 2, 2023
1 parent c47399d commit 83f24eb
Show file tree
Hide file tree
Showing 20 changed files with 316 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.hzz.spring.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@AllArgsConstructor
@NoArgsConstructor
public class AnotherCarDTO {
private int id;
private String name;
private FuelType fuelType;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.hzz.spring.entity;
package org.hzz.spring.dto;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hzz.spring.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class DivisionDTO {
private int id;
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.hzz.spring.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class EmployeeDTO {
private int employeeId;
private String employeeName;
private DivisionDTO division;
private Date employeeStartDt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.hzz.spring.dto;

public enum FuelType {
ELECTRIC, BIO_DIESEL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hzz.spring.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class TransactionDTO {
private String uuid;
private Long totalInCents;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hzz.spring.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class AnotherCar {
protected int id;
protected String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.hzz.spring.entity;

public class BioDieselCar extends AnotherCar{
public BioDieselCar(int id, String name) {
super(id, name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hzz.spring.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Division {
private int id;
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.hzz.spring.entity;

public class ElectricCar extends AnotherCar{
public ElectricCar(int id, String name) {
super(id, name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.hzz.spring.entity;

import lombok.Data;

import java.util.Date;

@Data
public class Employee {
private int id;
private String name;
private Division division;
private String startDt;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.hzz.spring.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.UUID;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Transaction {
private Long id;
private String uuid = UUID.randomUUID().toString();
private BigDecimal total;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.hzz.spring.mapper;

import org.hzz.spring.dto.AnotherCarDTO;
import org.hzz.spring.dto.FuelType;
import org.hzz.spring.entity.AnotherCar;
import org.hzz.spring.entity.BioDieselCar;
import org.hzz.spring.entity.ElectricCar;
import org.mapstruct.AfterMapping;
import org.mapstruct.BeforeMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;

@Mapper(componentModel = "spring")
public abstract class AnotherCarMapper {

@BeforeMapping
protected void enrichDTOWithFuelType(AnotherCar car,
@MappingTarget AnotherCarDTO carDTO){
if (car instanceof ElectricCar) {
carDTO.setFuelType(FuelType.ELECTRIC);
}

if (car instanceof BioDieselCar){
carDTO.setFuelType(FuelType.BIO_DIESEL);
}
}

public abstract AnotherCarDTO carToCarDto(AnotherCar car);

@AfterMapping
protected void convertNameToUpperCase(@MappingTarget AnotherCarDTO carDTO){
carDTO.setName(carDTO.getName().toUpperCase());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.hzz.spring.mapper;

import org.hzz.spring.entity.Car;
import org.hzz.spring.entity.CarDto;
import org.hzz.spring.dto.CarDto;
import org.hzz.spring.service.CarService;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.hzz.spring.mapper;

import org.hzz.spring.dto.DivisionDTO;
import org.hzz.spring.dto.EmployeeDTO;
import org.hzz.spring.entity.Division;
import org.hzz.spring.entity.Employee;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
public abstract class EmployeeAndDTOMapper {
@Mapping(target = "id", source = "dto.employeeId")
@Mapping(target = "name", source = "dto.employeeName")
@Mapping(target = "startDt", source = "dto.employeeStartDt", dateFormat = "dd-MM-yyyy HH:mm:ss")
public abstract Employee employeeDTOToEmployee(EmployeeDTO dto);

public abstract Division divisionDTOtoDivision(DivisionDTO dto);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.hzz.spring.mapper;

import org.hzz.spring.dto.TransactionDTO;
import org.hzz.spring.entity.Transaction;
import org.mapstruct.Mapper;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

@Mapper(componentModel = "spring")
public abstract class TransactionMapper {

public TransactionDTO transactionToTransactionDTO(Transaction transaction) {
if ( transaction == null ) {
return null;
}
TransactionDTO transactionDTO = new TransactionDTO();

transactionDTO.setUuid( transaction.getUuid() );
transactionDTO.setTotalInCents(transaction.getTotal().multiply(
new BigDecimal("100")
).longValue());

return transactionDTO;
}

public abstract List<TransactionDTO> toTransactionDTO(Collection<Transaction> transactions);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.hzz.mapper;


import lombok.extern.slf4j.Slf4j;
import org.hzz.spring.dto.AnotherCarDTO;
import org.hzz.spring.entity.AnotherCar;
import org.hzz.spring.entity.BioDieselCar;
import org.hzz.spring.mapper.AnotherCarMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class AnotherCarMapperTest {

@Autowired
private AnotherCarMapper anotherCarMapper;

@Test
public void test(){
AnotherCar car = new BioDieselCar(1001, "q10_car");
AnotherCarDTO anotherCarDTO = anotherCarMapper.carToCarDto(car);
System.out.println(anotherCarDTO);
// AnotherCarDTO(id=1001, name=Q10_CAR, fuelType=BIO_DIESEL)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

import lombok.extern.slf4j.Slf4j;
import org.hzz.spring.entity.Car;
import org.hzz.spring.entity.CarDto;
import org.hzz.spring.dto.CarDto;
import org.hzz.spring.mapper.CarandCarDTOMapper;
import org.hzz.spring.service.CarService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.hzz.mapper;

import lombok.extern.slf4j.Slf4j;
import org.hzz.spring.dto.DivisionDTO;
import org.hzz.spring.dto.EmployeeDTO;
import org.hzz.spring.entity.Employee;
import org.hzz.spring.mapper.EmployeeAndDTOMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class EmployAndDTOTest {
@Autowired
private EmployeeAndDTOMapper employeeAndDTOMapper;

@Test
public void testChildren(){
EmployeeDTO employeeDTO = new EmployeeDTO();
employeeDTO.setEmployeeId(1);
employeeDTO.setEmployeeName("Q10Viking");
employeeDTO.setEmployeeStartDt(new Date());
employeeDTO.setDivision(new DivisionDTO(1,"IT"));

Employee employee = employeeAndDTOMapper.employeeDTOToEmployee(employeeDTO);
log.info(employee.toString());
// org.hzz.mapper.EmployAndDTOTest : Employee(id=1, name=Q10Viking, division=Division(id=1, name=IT), startDt=02-06-2023 22:02:46)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.hzz.mapper;

import org.hzz.spring.dto.TransactionDTO;
import org.hzz.spring.entity.Transaction;
import org.hzz.spring.mapper.TransactionMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TransactionMapperTest {

@Autowired
private TransactionMapper transactionMapper;

@Test
public void test(){
Transaction transaction = new Transaction();
transaction.setId(1l);
transaction.setTotal(new BigDecimal(2));

TransactionDTO transactionDTO = transactionMapper.transactionToTransactionDTO(transaction);
System.out.println(transactionDTO);
// TransactionDTO(uuid=fde2ef85-8269-4808-9eb6-e1d27de2376f, totalInCents=200)
}
}

0 comments on commit 83f24eb

Please sign in to comment.