Skip to content

Commit

Permalink
fix converter errors
Browse files Browse the repository at this point in the history
fix transaction error
  • Loading branch information
deepcloudlabs committed Jun 25, 2020
1 parent ef3c430 commit 832f5eb
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.hr.application;

import java.util.Optional;

import com.example.hr.domain.Employee;
import com.example.hr.domain.TcKimlikNo;

Expand All @@ -12,6 +14,6 @@ public interface EmployeeApplication {

boolean hireEmployee(Employee employee);

boolean fireEmployee(TcKimlikNo identity);
Optional<Employee> fireEmployee(TcKimlikNo identity);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.hr.application.business;

import java.util.Optional;
import java.util.UUID;

import com.example.hr.application.EmployeeApplication;
import com.example.hr.domain.Employee;
import com.example.hr.domain.TcKimlikNo;
Expand Down Expand Up @@ -28,18 +31,19 @@ public void setEventPushlisher(EventPushlisher eventPushlisher) {
@Override
public boolean hireEmployee(Employee employee) {
employeeRepository.save(employee);
eventPushlisher.publishEvent(new EmployeeHiredEvent("", "employees", employee));
eventPushlisher.publishEvent(new EmployeeHiredEvent(UUID.randomUUID().toString(), "employees", employee));
return true;
}

@Override
public boolean fireEmployee(TcKimlikNo identity) {
public Optional<Employee> fireEmployee(TcKimlikNo identity) {
var employee = employeeRepository.findByIdentity(identity);
if (employee.isEmpty()) return false;
Employee emp = employee.get();
if (employee.isEmpty())
return Optional.empty();
var emp = employee.get();
employeeRepository.remove(emp);
eventPushlisher.publishEvent(new EmployeeFiredEvent("1", "employees", emp));
return true;
eventPushlisher.publishEvent(new EmployeeFiredEvent(UUID.randomUUID().toString(), "employees", emp));
return Optional.of(emp);
}

}
13 changes: 11 additions & 2 deletions hr-domain/src/com/example/hr/domain/Employee.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
// Ctrl + Shift + F (Format Source Code) (Ctrl + Alt + L)
public class Employee {
private static final Money MIN_SALARY = Money.of(3_000, MoneyCurrency.TL);
private final TcKimlikNo identityNo;
private TcKimlikNo identityNo;
private FullName fullname;
private Money salary;
private Iban iban;
private final BirthYear birthYear;
private BirthYear birthYear;
private Photo photo;
private boolean fulltime;
private Department department;

public Employee() {
}

public Employee(TcKimlikNo identityNo, FullName fullname, Money salary, Iban iban, BirthYear birthYear, Photo photo,
boolean fulltime, Department department) {
this.identityNo = identityNo;
Expand Down Expand Up @@ -128,6 +131,12 @@ public void hireToParttime() {
this.decreaseSalary(50.);
}

@Override
public String toString() {
return "Employee [identityNo=" + identityNo + ", fullname=" + fullname + ", salary=" + salary + ", iban=" + iban
+ ", birthYear=" + birthYear + ", fulltime=" + fulltime + ", department=" + department + "]";
}

public static class Builder {
private final TcKimlikNo identityNo;
private FullName fullname;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Tue Jun 23 17:09:21 EET 2020
#Thu Jun 25 22:30:37 EET 2020
m2e.projectLocation=C\:\\DEVEL\\stage\\tmp\\dcl350\\hr-domain
m2e.projectName=hr-domain
groupId=com.example
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.hr.application.business;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Optional;
Expand Down Expand Up @@ -73,9 +74,9 @@ void fireEmployee_success() {
Mockito.when(empRepo.findByIdentity(TcKimlikNo.of("40310900232"))).thenReturn(Optional.of(jack));
Mockito.doNothing().when(eventPublisher).publishEvent(new EmployeeFiredEvent("2", "employees", jack));
// 2. call exercise method
var success = app.fireEmployee(TcKimlikNo.of("40310900232"));
var emp = app.fireEmployee(TcKimlikNo.of("40310900232"));
// 3. Verification
assertTrue(success);
assertEquals(jack, emp.get());
// 4. destroy setup
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.util.Optional;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.hr.domain.Employee;
import com.example.hr.domain.FullName;
Expand All @@ -24,21 +26,20 @@
public class EmployeeRepositoryJpaAdapter implements EmployeeRepository {
@Autowired
private EmployeeJpaRepository empJpaRepo;
@Autowired
private ModelMapper mapper;

@Override
public Optional<Employee> findByIdentity(TcKimlikNo identity) {
var emp = empJpaRepo.findById(identity.getValue());
if (emp.isPresent()) {
// EmployeeEntity -> Employee
Employee employee = new Employee.Builder(emp.get().getIdentity())
// TODO: set other fields of domain entity
.build();
return Optional.of(employee);
return Optional.of(mapper.map(emp.get(), Employee.class));
}
return Optional.empty();
}

@Override
@Transactional
public void save(Employee employee) {
EmployeeEntity entity = new EmployeeEntity();
entity.setIdentity(employee.getIdentityNo().getValue());
Expand All @@ -54,6 +55,7 @@ public void save(Employee employee) {
}

@Override
@Transactional
public void remove(Employee employee) {
var identity = employee.getIdentityNo().getValue();
empJpaRepo.deleteById(identity);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.hr.config;

import org.modelmapper.Converter;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeMap;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -10,6 +10,7 @@
import com.example.hr.domain.FullName;
import com.example.hr.domain.MoneyCurrency;
import com.example.hr.dto.EmployeeRequest;
import com.example.hr.orm.EmployeeEntity;

/**
*
Expand All @@ -18,55 +19,52 @@
*/
@Configuration
public class ModelMapperConfig {
private static final Converter<EmployeeDocument, Employee> employeeDocumentToEmployeeConverter = context -> {
var employeeDocument = context.getSource();
String[] tokens = employeeDocument.getFullname().split("\\w+");
return new Employee.Builder(employeeDocument.getIdentity()).fullname(tokens[0], tokens[1])
.iban(employeeDocument.getIban()).salary(employeeDocument.getSalary(), MoneyCurrency.TL)
.birthYear(employeeDocument.getBirthYear()).fulltime(employeeDocument.getFulltime())
.department(employeeDocument.getDepartment()).photo(employeeDocument.getPhoto().getBytes()).build();
};
private static final Converter<Employee, EmployeeDocument> employeeToEmployeeDocumentConverter = (context -> {
var employee = context.getSource();
EmployeeDocument employeeDocument = new EmployeeDocument();
employeeDocument.setIdentity(employee.getIdentityNo().getValue());
FullName fullname = employee.getFullname();
employeeDocument.setFullname(fullname.getFirst() + " " + fullname.getLast());
employeeDocument.setSalary(employee.getSalary().getValue());
employeeDocument.setIban(employee.getIban().getValue());
employeeDocument.setPhoto(new String(employee.getPhoto().getValue()));
employeeDocument.setBirthYear(employee.getBirthYear().getValue());
employeeDocument.setDepartment(employee.getDepartment());
employeeDocument.setFulltime(employee.isFulltime());
return employeeDocument;
});
private static final Converter<EmployeeRequest, Employee> employeeRequestToEmployeeConverter = (context -> {
var employeeRequest = context.getSource();
String[] tokens = employeeRequest.getFullname().split("\\s+");
return new Employee.Builder(employeeRequest.getIdentity()).fullname(tokens[0], tokens[1])
.iban(employeeRequest.getIban()).salary(employeeRequest.getSalary(), MoneyCurrency.TL)
.birthYear(employeeRequest.getBirthYear()).fulltime(employeeRequest.isFulltime())
.department(employeeRequest.getDepartment()).photo(employeeRequest.getPhoto()).build();
});
private static final Converter<EmployeeEntity, Employee> employeeEntityToEmployeeConverter = (context -> {
var employeeEntity = context.getSource();
String[] tokens = employeeEntity.getFullname().split("\\s+");
return new Employee.Builder(employeeEntity.getIdentity()).fullname(tokens[0], tokens[1])
.iban(employeeEntity.getIban()).salary(employeeEntity.getSalary(), MoneyCurrency.TL)
.birthYear(employeeEntity.getBirthYear()).fulltime(employeeEntity.isFulltime())
.department(employeeEntity.getDepartment()).photo(employeeEntity.getPhoto()).build();
});

@Bean
public ModelMapper modelMapper() {
ModelMapper mapper = new ModelMapper();
TypeMap<EmployeeRequest, Employee> employeeRequestToEmployeeTypeMap = mapper.getTypeMap(EmployeeRequest.class,
Employee.class);
if (employeeRequestToEmployeeTypeMap == null) {
employeeRequestToEmployeeTypeMap = mapper.createTypeMap(EmployeeRequest.class, Employee.class);
}
employeeRequestToEmployeeTypeMap.setProvider(provisionRequest -> {
var employeeRequest = EmployeeRequest.class.cast(provisionRequest.getSource());
String[] tokens = employeeRequest.getFullname().split("\\w+");
return new Employee.Builder(employeeRequest.getIdentity()).fullname(tokens[0], tokens[1])
.iban(employeeRequest.getIban()).salary(employeeRequest.getSalary(), MoneyCurrency.TL)
.birthYear(employeeRequest.getBirthYear()).fulltime(employeeRequest.isFulltime())
.department(employeeRequest.getDepartment()).photo(employeeRequest.getPhoto()).build();
});
TypeMap<EmployeeDocument, Employee> employeeDocumentToEmployeeTypeMap = mapper
.getTypeMap(EmployeeDocument.class, Employee.class);
if (employeeDocumentToEmployeeTypeMap == null) {
employeeDocumentToEmployeeTypeMap = mapper.createTypeMap(EmployeeDocument.class, Employee.class);
}
employeeRequestToEmployeeTypeMap.setProvider(provisionRequest -> {
var employeeDocument = EmployeeDocument.class.cast(provisionRequest.getSource());
String[] tokens = employeeDocument.getFullname().split("\\w+");
return new Employee.Builder(employeeDocument.getIdentity()).fullname(tokens[0], tokens[1])
.iban(employeeDocument.getIban()).salary(employeeDocument.getSalary(), MoneyCurrency.TL)
.birthYear(employeeDocument.getBirthYear()).fulltime(employeeDocument.getFulltime())
.department(employeeDocument.getDepartment()).photo(employeeDocument.getPhoto().getBytes()).build();
});
TypeMap<Employee, EmployeeDocument> employeeToEmployeeDocumentTypeMap = mapper.getTypeMap(Employee.class,
EmployeeDocument.class);
if (employeeToEmployeeDocumentTypeMap == null) {
employeeToEmployeeDocumentTypeMap = mapper.createTypeMap(Employee.class, EmployeeDocument.class);
}
employeeToEmployeeDocumentTypeMap.setProvider(provisionRequest -> {
var employee = Employee.class.cast(provisionRequest.getSource());
EmployeeDocument employeeDocument = new EmployeeDocument();
employeeDocument.setIdentity(employee.getIdentityNo().getValue());
FullName fullname = employee.getFullname();
employeeDocument.setFullname(fullname.getFirst() + " " + fullname.getLast());
employeeDocument.setSalary(employee.getSalary().getValue());
employeeDocument.setIban(employee.getIban().getValue());
employeeDocument.setPhoto(new String(employee.getPhoto().getValue()));
employeeDocument.setBirthYear(employee.getBirthYear().getValue());
employeeDocument.setDepartment(employee.getDepartment());
employeeDocument.setFulltime(employee.isFulltime());
return employeeDocument;
});
mapper.addConverter(employeeRequestToEmployeeConverter, EmployeeRequest.class, Employee.class);
mapper.addConverter(employeeEntityToEmployeeConverter, EmployeeEntity.class, Employee.class);
mapper.addConverter(employeeToEmployeeDocumentConverter, Employee.class, EmployeeDocument.class);
mapper.addConverter(employeeDocumentToEmployeeConverter, EmployeeDocument.class, Employee.class);
return mapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

import com.example.hr.application.EmployeeApplication;
import com.example.hr.domain.Employee;
import com.example.hr.domain.TcKimlikNo;
import com.example.hr.dto.EmployeeRequest;
import com.example.hr.dto.EmployeeResponse;
import com.example.hr.orm.EmployeeEntity;
import com.example.validation.TcKimlikNo;
import com.example.validation.TcKimlik;

/**
*
Expand All @@ -41,17 +42,18 @@ public class EmployeeController {

@PostMapping
public EmployeeResponse hireEmployee(@RequestBody @Validated EmployeeRequest request) {
employeeApplication.hireEmployee(mapper.map(request, Employee.class));
Employee emp = mapper.map(request, Employee.class);
employeeApplication.hireEmployee(emp);
return new EmployeeResponse("success");
}

@PutMapping("{identity}")
public void updateEmployee(@PathVariable @TcKimlikNo String identity, @Validated @RequestBody EmployeeEntity e) {
public void updateEmployee(@PathVariable @TcKimlik String identity, @Validated @RequestBody EmployeeEntity e) {

}

@PatchMapping("{identity}")
public void patchEmployee(@PathVariable @TcKimlikNo String identity, Map<String, Object> employee) {
public void patchEmployee(@PathVariable @TcKimlik String identity, Map<String, Object> employee) {
EmployeeEntity employeeEntity = null;
var clazz = EmployeeEntity.class;
employee.forEach((field, value) -> {
Expand All @@ -62,11 +64,11 @@ public void patchEmployee(@PathVariable @TcKimlikNo String identity, Map<String,
});
}

// @GetMapping

@DeleteMapping("{identity}")
public EmployeeResponse fireEmployee(@PathVariable @TcKimlikNo String identity) {
employeeApplication.fireEmployee(null);
return null;
public EmployeeResponse fireEmployee(@PathVariable @TcKimlik String identity) {
var removedEmp = employeeApplication.fireEmployee(TcKimlikNo.of(identity));
if (removedEmp.isPresent())
return new EmployeeResponse("success");
return new EmployeeResponse("fail");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import com.example.hr.domain.Department;
import com.example.validation.Iban;
import com.example.validation.TcKimlikNo;
import com.example.validation.TcKimlik;

/**
*
Expand All @@ -20,7 +20,7 @@
@Document(collection = "employees")
@Sharded(shardKey = "identity", immutableKey = true)
public class EmployeeDocument {
@TcKimlikNo
@TcKimlik
@Id
private String identity;
@Size(min = 5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

import com.example.hr.domain.Department;
import com.example.validation.Iban;
import com.example.validation.TcKimlikNo;
import com.example.validation.TcKimlik;

/**
*
* @author Binnur Kurt <binnur.kurt@gmail.com>
*
*/
public class EmployeeRequest {
@TcKimlikNo
@TcKimlik
private String identity;
@Size(min = 6)
private String fullname;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class RestErrorHandler {
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
public RestErrorMessage handleException(Exception e) {
return new RestErrorMessage(100, e.getMessage(), "6b8038e5-3b8f-4230-bfe8-4de2c422469f");
return new RestErrorMessage(100, e.getClass().getName(), "6b8038e5-3b8f-4230-bfe8-4de2c422469f");
}

@ExceptionHandler(ConstraintViolationException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import com.example.hr.domain.Department;
import com.example.validation.Iban;
import com.example.validation.TcKimlikNo;
import com.example.validation.TcKimlik;

/**
*
Expand All @@ -28,7 +28,7 @@
public class EmployeeEntity {
@Id
@Column(name = "identity")
@TcKimlikNo
@TcKimlik
private String identity;
@Size(min = 5)
private String fullname;
Expand All @@ -44,7 +44,7 @@ public class EmployeeEntity {
@Lob
@Column(columnDefinition = "longblob")
private byte[] photo;
@Enumerated(EnumType.STRING)
@Enumerated(EnumType.ORDINAL)
private Department department;

public EmployeeEntity() {
Expand Down
Loading

0 comments on commit 832f5eb

Please sign in to comment.