Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ready implementation of microservices
- Loading branch information
Showing
17 changed files
with
203 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 43 additions & 3 deletions
46
...rc/main/kotlin/pl/piomin/samples/kubernetes/department/controller/DepartmentController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,49 @@ | ||
package pl.piomin.samples.kubernetes.department.controller | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import org.springframework.web.bind.annotation.* | ||
import org.springframework.web.client.RestTemplate | ||
import pl.piomin.samples.kubernetes.department.domain.Department | ||
import pl.piomin.samples.kubernetes.department.domain.Employee | ||
import pl.piomin.samples.kubernetes.department.repository.DepartmentRepository | ||
import java.util.* | ||
|
||
@RestController | ||
@RequestMapping("/departments") | ||
class DepartmentController { | ||
class DepartmentController(val repository: DepartmentRepository, | ||
val restTemplate: RestTemplate) { | ||
|
||
@PostMapping | ||
fun add(@RequestBody department: Department): Department = repository.save(department) | ||
|
||
@PostMapping("/with-employees") | ||
fun addWithEmployees(@RequestBody department: Department): Department { | ||
val departmentSaved: Department = repository.save(department) | ||
department.employees.forEach { | ||
restTemplate.postForObject("http://employee-service:8080/employees", | ||
it, Employee::class.java) | ||
} | ||
return departmentSaved | ||
} | ||
|
||
@GetMapping("/{id}") | ||
fun findById(@PathVariable id: Int): Optional<Department> = repository.findById(id) | ||
|
||
@GetMapping("/{id}/with-employees") | ||
fun findByIdWithEmployees(@PathVariable id: Int): Department? { | ||
val optDepartment: Optional<Department> = repository.findById(id) | ||
return if (optDepartment.isPresent) { | ||
val department: Department = optDepartment.get() | ||
val employees = restTemplate.getForObject("http://employee-service:8080/employees/department/{departmentId}", | ||
Array<Employee>::class.java, department.id) | ||
department.employees.addAll(employees!!) | ||
department | ||
} else null | ||
} | ||
|
||
@GetMapping() | ||
fun findAll(): Iterable<Department> = repository.findAll() | ||
|
||
@GetMapping("/organization/{organizationId}") | ||
fun findByOrganizationId(@PathVariable organizationId: Int) = | ||
repository.findByOrganizationId(organizationId) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...rtment-service/src/main/kotlin/pl/piomin/samples/kubernetes/department/domain/Employee.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package pl.piomin.samples.kubernetes.department.domain | ||
|
||
data class Employee(var id: Int = 0, | ||
val firstName: String = "", | ||
val lastName: String = "", | ||
val position: String = "") |
10 changes: 10 additions & 0 deletions
10
...rc/main/kotlin/pl/piomin/samples/kubernetes/department/repository/DepartmentRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package pl.piomin.samples.kubernetes.department.repository | ||
|
||
import org.springframework.data.repository.CrudRepository | ||
import pl.piomin.samples.kubernetes.department.domain.Department | ||
|
||
interface DepartmentRepository: CrudRepository<Department, Int> { | ||
|
||
fun findByOrganizationId(organizationId: Int) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 22 additions & 3 deletions
25
...ce/src/main/kotlin/pl/piomin/samples/kubernetes/employee/controller/EmployeeController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
package pl.piomin.samples.kubernetes.employee.controller | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import org.springframework.web.bind.annotation.* | ||
import pl.piomin.samples.kubernetes.employee.domain.Employee | ||
import pl.piomin.samples.kubernetes.employee.repository.EmployeeRepository | ||
import java.util.* | ||
|
||
@RestController | ||
@RequestMapping("/employees") | ||
class EmployeeController { | ||
class EmployeeController(val repository: EmployeeRepository) { | ||
|
||
@PostMapping | ||
fun add(@RequestBody employee: Employee): Employee = repository.save(employee) | ||
|
||
@GetMapping("/{id}") | ||
fun findById(@PathVariable id: Int): Optional<Employee> = repository.findById(id) | ||
|
||
@GetMapping | ||
fun findAll(): Iterable<Employee> = repository.findAll() | ||
|
||
@GetMapping("/organization/{organizationId}") | ||
fun findByOrganizationId(@PathVariable organizationId: Int): Set<Employee> = | ||
repository.findByOrganizationId(organizationId) | ||
|
||
@GetMapping("/department/{departmentId}") | ||
fun findByDepartmentId(@PathVariable departmentId: Int): Set<Employee> = | ||
repository.findByDepartmentId(departmentId) | ||
} |
11 changes: 10 additions & 1 deletion
11
...employee-service/src/main/kotlin/pl/piomin/samples/kubernetes/employee/domain/Employee.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
package pl.piomin.samples.kubernetes.employee.domain | ||
|
||
class Employee | ||
import javax.persistence.Entity | ||
import javax.persistence.Id | ||
|
||
@Entity | ||
data class Employee(@Id var id: Int = 0, | ||
val firstName: String = "", | ||
val lastName: String = "", | ||
val position: String = "", | ||
val organizationId: Int = 0, | ||
val departmentId: Int = 0) |
10 changes: 10 additions & 0 deletions
10
...ce/src/main/kotlin/pl/piomin/samples/kubernetes/employee/repository/EmployeeRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package pl.piomin.samples.kubernetes.employee.repository | ||
|
||
import org.springframework.data.repository.CrudRepository | ||
import pl.piomin.samples.kubernetes.employee.domain.Employee | ||
|
||
interface EmployeeRepository: CrudRepository<Employee, Int> { | ||
|
||
fun findByOrganizationId(organizationId: Int): Set<Employee> | ||
fun findByDepartmentId(departmentId: Int): Set<Employee> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 43 additions & 3 deletions
46
...ain/kotlin/pl/piomin/samples/kubernetes/organization/controller/OrganizationController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,49 @@ | ||
package pl.piomin.samples.kubernetes.organization.controller | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import org.springframework.web.bind.annotation.* | ||
import org.springframework.web.client.RestTemplate | ||
import pl.piomin.samples.kubernetes.organization.domain.Department | ||
import pl.piomin.samples.kubernetes.organization.domain.Employee | ||
import pl.piomin.samples.kubernetes.organization.domain.Organization | ||
import pl.piomin.samples.kubernetes.organization.repository.OrganizationRepository | ||
import java.util.* | ||
|
||
@RestController | ||
@RequestMapping("/organizations") | ||
class OrganizationController { | ||
class OrganizationController(val repository: OrganizationRepository, | ||
val restTemplate: RestTemplate) { | ||
|
||
@PostMapping | ||
fun add(@RequestBody organization: Organization): Organization = repository.save(organization) | ||
|
||
@GetMapping("/{id}") | ||
fun findById(@PathVariable id: Int): Optional<Organization> = repository.findById(id) | ||
|
||
@GetMapping("/{id}/with-employees") | ||
fun findByIdWithEmployees(@PathVariable id: Int): Organization? { | ||
val optOrganization: Optional<Organization> = repository.findById(id) | ||
return if (optOrganization.isPresent) { | ||
val organization: Organization = optOrganization.get() | ||
val employees = restTemplate.getForObject("http://employee-service:8080/employees/organization/{organizationId}", | ||
Array<Employee>::class.java, organization.id) | ||
organization.employees.addAll(employees!!) | ||
organization | ||
} else null | ||
} | ||
|
||
@GetMapping("/{id}/with-departments") | ||
fun findByIdWithDepartments(@PathVariable id: Int): Organization? { | ||
val optOrganization: Optional<Organization> = repository.findById(id) | ||
return if (optOrganization.isPresent) { | ||
val organization: Organization = optOrganization.get() | ||
val departments = restTemplate.getForObject("http://department-service:8080/departments/organization/{organizationId}", | ||
Array<Department>::class.java, organization.id) | ||
organization.departments.addAll(departments!!) | ||
organization | ||
} else null | ||
} | ||
|
||
@GetMapping() | ||
fun findAll(): Iterable<Organization> = repository.findAll() | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
...on-service/src/main/kotlin/pl/piomin/samples/kubernetes/organization/domain/Department.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package pl.piomin.samples.kubernetes.organization.domain | ||
|
||
data class Department(var id: Int = 0, | ||
val name: String = "", | ||
val organizationId: Int = 0, | ||
val employees: MutableSet<Employee> = mutableSetOf()) |
6 changes: 6 additions & 0 deletions
6
...tion-service/src/main/kotlin/pl/piomin/samples/kubernetes/organization/domain/Employee.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package pl.piomin.samples.kubernetes.organization.domain | ||
|
||
data class Employee(var id: Int = 0, | ||
val firstName: String = "", | ||
val lastName: String = "", | ||
val position: String = "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...ain/kotlin/pl/piomin/samples/kubernetes/organization/repository/OrganizationRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package pl.piomin.samples.kubernetes.organization.repository | ||
|
||
import org.springframework.data.repository.CrudRepository | ||
import pl.piomin.samples.kubernetes.organization.domain.Organization | ||
|
||
interface OrganizationRepository: CrudRepository<Organization, Int> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters