Skip to content

Commit

Permalink
add mongo repository
Browse files Browse the repository at this point in the history
  • Loading branch information
deepcloudlabs committed Jun 25, 2020
1 parent 9a736f7 commit bc89295
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 6 deletions.
4 changes: 4 additions & 0 deletions hr-microservice-hexagonal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,48 @@
package com.example.hr;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.example.hr.adapter.EmployeeRepositoryMongoAdapter;
import com.example.hr.document.EmployeeDocument;
import com.example.hr.domain.Department;
import com.example.hr.domain.Employee;
import com.example.hr.domain.MoneyCurrency;
import com.example.hr.repository.EmployeeMongoRepository;

/**
*
* @author Binnur Kurt <binnur.kurt@gmail.com>
*
*/
@SpringBootApplication
public class HrMicroserviceHexagonalApplication {

public class HrMicroserviceHexagonalApplication implements ApplicationRunner {
@Autowired private EmployeeRepositoryMongoAdapter mongoAdapter;
@Autowired private EmployeeMongoRepository mongoRepo;

public static void main(String[] args) {
SpringApplication.run(HrMicroserviceHexagonalApplication.class, args);
}

@Override
public void run(ApplicationArguments args) throws Exception {
/*
Employee jack = new Employee.Builder("40310900232").fullname("jack", "bauer").birthYear(1956)
.salary(100_000, MoneyCurrency.USD).iban("TR820006235455781668847562").fulltime(true)
.department(Department.IT)
.photo("iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAABC1BMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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D6sfUztppKvMgUMnf/Mv8NIVU+cyYl6AAAAAElFTkSuQmCC"
.getBytes())
.build();
mongoAdapter.save(jack);
*/
List<EmployeeDocument> employees = mongoRepo.findAllByBirthYearBetweenAndDepartment(1950, 1960,Department.IT);
System.err.println(employees);
employees.forEach(System.out::println);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.example.hr.domain.Employee;
import com.example.hr.domain.FullName;
import com.example.hr.domain.TcKimlikNo;
import com.example.hr.meta.Persistence;
import com.example.hr.meta.PersistenceTarget;
import com.example.hr.orm.EmployeeEntity;
import com.example.hr.repository.EmployeeJpaRepository;
import com.example.hr.repository.EmployeeRepository;
Expand All @@ -18,7 +20,8 @@
*
*/
@Repository
public class EmployeeRepositoryAdapter implements EmployeeRepository {
@Persistence(PersistenceTarget.JPA_MYSQL)
public class EmployeeRepositoryJpaAdapter implements EmployeeRepository {
@Autowired
private EmployeeJpaRepository empJpaRepo;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.example.hr.adapter;

import java.util.Optional;

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

import com.example.hr.document.EmployeeDocument;
import com.example.hr.domain.Employee;
import com.example.hr.domain.FullName;
import com.example.hr.domain.TcKimlikNo;
import com.example.hr.meta.Persistence;
import com.example.hr.meta.PersistenceTarget;
import com.example.hr.repository.EmployeeMongoRepository;
import com.example.hr.repository.EmployeeRepository;

@Repository
@Persistence(PersistenceTarget.MONGO)
public class EmployeeRepositoryMongoAdapter implements EmployeeRepository{
@Autowired
private EmployeeMongoRepository empRepo;
@Autowired
private ModelMapper mapper;

@Override
public Optional<Employee> findByIdentity(TcKimlikNo identity) {
Optional<EmployeeDocument> empDoc = empRepo.findById(identity.getValue());
if (!empDoc.isEmpty()) return Optional.empty();
Employee employee = mapper.map(empDoc.get(), Employee.class);
return Optional.of(employee);
}

@Override
public void save(Employee employee) {
//EmployeeDocument empDoc= mapper.map(employee, EmployeeDocument.class);
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());
empRepo.save(employeeDocument);
}

@Override
public void remove(Employee employee) {
empRepo.deleteById(employee.getIdentityNo().getValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.example.hr.application.EmployeeApplication;
import com.example.hr.application.business.SimpleEmployeeApplication;
import com.example.hr.infrastructure.EventPushlisher;
import com.example.hr.meta.Persistence;
import com.example.hr.meta.PersistenceTarget;
import com.example.hr.repository.EmployeeRepository;

/**
Expand All @@ -17,7 +19,8 @@
public class AppConfig {

@Bean
public EmployeeApplication employeeApplication(EmployeeRepository employeeRepository,
public EmployeeApplication employeeApplication(
@Persistence(PersistenceTarget.JPA_MYSQL) EmployeeRepository employeeRepository,
EventPushlisher eventPushlisher) {
SimpleEmployeeApplication employeeApplication = new SimpleEmployeeApplication();
employeeApplication.setEmployeeRepository(employeeRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.example.hr.document.EmployeeDocument;
import com.example.hr.domain.Employee;
import com.example.hr.domain.FullName;
import com.example.hr.domain.MoneyCurrency;
import com.example.hr.dto.EmployeeRequest;

Expand Down Expand Up @@ -33,6 +35,38 @@ public ModelMapper modelMapper() {
.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;
});
return mapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.example.hr.document;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

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

@Document(collection = "employees")
public class EmployeeDocument {
@TcKimlikNo
@Id
private String identity;
@Size(min = 5)
private String fullname;
@Min(3_000)
private Double salary;
@Iban
private String iban;
private Boolean fulltime;
@Max(2002)
private int birthYear;
private String photo;
private Department department;

public EmployeeDocument() {
}

public EmployeeDocument(String identity, @Size(min = 5) String fullname, @Min(3000) Double salary, String iban,
Boolean fulltime, @Max(2002) int birthYear, Department department) {
this.identity = identity;
this.fullname = fullname;
this.salary = salary;
this.iban = iban;
this.fulltime = fulltime;
this.birthYear = birthYear;
this.department = department;
}

public String getIdentity() {
return identity;
}

public void setIdentity(String identity) {
this.identity = identity;
}

public String getFullname() {
return fullname;
}

public void setFullname(String fullname) {
this.fullname = fullname;
}

public Double getSalary() {
return salary;
}

public void setSalary(Double salary) {
this.salary = salary;
}

public String getIban() {
return iban;
}

public void setIban(String iban) {
this.iban = iban;
}

public Boolean getFulltime() {
return fulltime;
}

public void setFulltime(Boolean fulltime) {
this.fulltime = fulltime;
}

public int getBirthYear() {
return birthYear;
}

public void setBirthYear(int birthYear) {
this.birthYear = birthYear;
}

public String getPhoto() {
return photo;
}

public void setPhoto(String photo) {
this.photo = photo;
}

public Department getDepartment() {
return department;
}

public void setDepartment(Department department) {
this.department = department;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((identity == null) ? 0 : identity.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EmployeeDocument other = (EmployeeDocument) obj;
if (identity == null) {
if (other.identity != null)
return false;
} else if (!identity.equals(other.identity))
return false;
return true;
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.hr.meta;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.PARAMETER, ElementType.FIELD})
@Qualifier
public @interface Persistence {
PersistenceTarget value() ;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.hr.meta;

public enum PersistenceTarget {
JPA_MYSQL, MONGO, CASSANDRA, REDIS, ELASTIC_SEARCH
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.hr.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.hr.document.EmployeeDocument;
import com.example.hr.orm.EmployeeEntity;

/**
Expand All @@ -10,5 +13,5 @@
*
*/
public interface EmployeeJpaRepository extends JpaRepository<EmployeeEntity, String> {

List<EmployeeDocument> findByBirthYearBetween(int fromYear,int toYear);
}
Loading

0 comments on commit bc89295

Please sign in to comment.