Skip to content

Commit

Permalink
implement persistence adapter
Browse files Browse the repository at this point in the history
add swagger ui to document rest api
  • Loading branch information
deepcloudlabs committed Jun 23, 2020
1 parent 683ddca commit 1d2f884
Show file tree
Hide file tree
Showing 11 changed files with 360 additions and 14 deletions.
15 changes: 15 additions & 0 deletions hr-microservice-hexagonal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,53 @@

import java.util.Optional;

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

import com.example.hr.domain.Employee;
import com.example.hr.domain.FullName;
import com.example.hr.domain.TcKimlikNo;
import com.example.hr.orm.EmployeeEntity;
import com.example.hr.repository.EmployeeJpaRepository;
import com.example.hr.repository.EmployeeRepository;

@Repository
public class EmployeeRepositoryAdapter implements EmployeeRepository {
//TODO: Spring Data Jpa
@Autowired
private EmployeeJpaRepository empJpaRepo;

@Override
public Optional<Employee> findByIdentity(TcKimlikNo identity) {
// TODO Auto-generated method stub
return null;
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.empty();
}

@Override
public void save(Employee employee) {
// TODO Auto-generated method stub

EmployeeEntity entity = new EmployeeEntity();
entity.setIdentity(employee.getIdentityNo().getValue());
FullName fullname = employee.getFullname();
entity.setFullname(fullname.getFirst() + " " + fullname.getLast());
entity.setSalary(employee.getSalary().getValue());
entity.setIban(employee.getIban().getValue());
entity.setBirthYear(employee.getBirthYear().getValue());
entity.setDepartment(employee.getDepartment());
entity.setPhoto(employee.getPhoto().getValue());
entity.setFulltime(employee.isFulltime());
empJpaRepo.save(entity);
}

@Override
public void remove(Employee employee) {
// TODO Auto-generated method stub

var identity = employee.getIdentityNo().getValue();
empJpaRepo.deleteById(identity);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class EventPuslisherAdapter implements EventPushlisher {
@Override
public void publishEvent(BusinessEvent event) {
// TODO Auto-generated method stub

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
public class AppConfig {

@Bean
public EmployeeApplication employeeApplication(
EmployeeRepository employeeRepository,
public EmployeeApplication employeeApplication(EmployeeRepository employeeRepository,
EventPushlisher eventPushlisher) {
SimpleEmployeeApplication employeeApplication= new SimpleEmployeeApplication();
SimpleEmployeeApplication employeeApplication = new SimpleEmployeeApplication();
employeeApplication.setEmployeeRepository(employeeRepository);
employeeApplication.setEventPushlisher(eventPushlisher);
return employeeApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.example.hr.config;

import java.util.Date;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.paths.RelativePathProvider;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Value("${major.version}")
private String majorVersion;
@Value("${minor.version}")
private String minorVersion;
@Value("${timestamp}")
private long timestamp;
@Value("${server.servlet.context-path}")
private String contextPath;
@Value("${spring.mvc.servlet.path}")
private String servletPath;

@Value("${server.address}")
private String host;

@Value("${server.port}")
private long port;

@Bean
public Docket api(ServletContext servletContext) {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build().host(host.concat(":").concat(Long.toString(port)))
.pathProvider(new RelativePathProvider(servletContext) {
@Override
public String getApplicationBasePath() {
return contextPath;
}
}).apiInfo(apiInfo());
}

private ApiInfo apiInfo() {

return new ApiInfoBuilder().title("Market Service")
.description("<b>Client FrontEnd API</b><br /><br />Updated: [" + (new Date(timestamp)).toString()
+ " ]" + " <script>document.title = \"Market Service\";"
+ " document.getElementById('header').remove();" + "</script>")
.version(majorVersion + "." + minorVersion).build();
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
public class EmployeeController {
@Autowired
private EmployeeApplication employeeApplication;

@PostMapping
public EmployeeResponse hireEmployee(@RequestBody EmployeeRequest request) {
employeeApplication.hireEmployee(null);
return null;
employeeApplication.hireEmployee(request.toEmployee());
return new EmployeeResponse("success");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,96 @@
package com.example.hr.dto;

import com.example.hr.domain.Department;
import com.example.hr.domain.Employee;
import com.example.hr.domain.MoneyCurrency;

public class EmployeeRequest {
private String identity;
private String fullname;
private double salary;
private String iban;
private boolean fulltime;
private int birthYear;
private byte[] photo;
private Department department;

public EmployeeRequest() {
}

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 isFulltime() {
return fulltime;
}

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

public int getBirthYear() {
return birthYear;
}

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

public byte[] getPhoto() {
return photo;
}

public void setPhoto(byte[] photo) {
this.photo = photo;
}

public Department getDepartment() {
return department;
}

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

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

public Employee toEmployee() {
String[] tokens = fullname.split("\\w+");
return new Employee.Builder(identity).fullname(tokens[0], tokens[1]).iban(iban).salary(salary, MoneyCurrency.TL)
.birthYear(birthYear).fulltime(fulltime).department(department).photo(photo).build();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.example.hr.dto;

public class EmployeeResponse {
private String status;

public EmployeeResponse(String status) {
this.status = status;
}

public String getStatus() {
return status;
}

}
Loading

0 comments on commit 1d2f884

Please sign in to comment.