Skip to content

Commit

Permalink
[feat]: Added all available roles to employee addition
Browse files Browse the repository at this point in the history
  • Loading branch information
OjasInamdar committed Sep 8, 2022
1 parent 920bfb3 commit db30bf5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ import org.springframework.transaction.annotation.Transactional
import java.util.Optional

interface EmployeeRepository : CrudRepository<Employee, Int> {
@Transactional
@Modifying
@Query(
value = "INSERT INTO manager(branch, employee_id) VALUES (:branch, :employee_id)", nativeQuery = true
)
fun insertIntoManagerTable(@Param("branch") branch: String, @Param("employee_id") id: Int)

@Query(
value = "SELECT name FROM employee JOIN manager ON employee.id=manager.employee_id WHERE employee_id=:employee_id",
nativeQuery = true
)
fun fetchFromManager(@Param("employee_id") employee_id: Int): Optional<String>

@Transactional
@Modifying
@Query(
Expand All @@ -17,8 +30,48 @@ interface EmployeeRepository : CrudRepository<Employee, Int> {
fun insertIntoHrTable(@Param("is_trainer") is_trainer: Boolean, @Param("employee_id") id: Int)

@Query(
value = "SELECT name FROM EMPLOYEE JOIN HR ON EMPLOYEE.ID=HR.EMPLOYEE_ID WHERE EMPLOYEE_ID=:employee_id",
value = "SELECT name FROM employee JOIN hr ON employee.id=hr.employee_id WHERE employee_id=:employee_id",
nativeQuery = true
)
fun fetchFromHr(@Param("employee_id") employee_id: Int): Optional<String>

@Transactional
@Modifying
@Query(
value = "INSERT INTO salesperson(number_of_sales, employee_id) VALUES (:number_of_sales, :employee_id)",
nativeQuery = true
)
fun insertIntoSalespersonTable(@Param("number_of_sales") number_of_sales: Int, @Param("employee_id") id: Int)

@Query(
value = "SELECT name FROM employee JOIN salesperson ON employee.id=salesperson.employee_id WHERE employee_id=:employee_id",
nativeQuery = true
)
fun fetchFromSalesperson(@Param("employee_id") employee_id: Int): Optional<String>

@Transactional
@Modifying
@Query(
value = "INSERT INTO accountant(employee_id) VALUES (:employee_id)", nativeQuery = true
)
fun insertIntoAccountantTable(@Param("employee_id") id: Int)

@Query(
value = "SELECT name FROM employee JOIN accountant ON employee.id=accountant.employee_id WHERE employee_id=:employee_id",
nativeQuery = true
)
fun fetchFromAccountant(@Param("employee_id") employee_id: Int): Optional<String>

@Transactional
@Modifying
@Query(
value = "INSERT INTO darryl(employee_id) VALUES (:employee_id)", nativeQuery = true
)
fun insertIntoDarrylTable(@Param("employee_id") id: Int)

@Query(
value = "SELECT name FROM employee JOIN darryl ON employee.id=darryl.employee_id WHERE employee_id=:employee_id",
nativeQuery = true
)
fun fetchFromDarryl(@Param("employee_id") employee_id: Int): Optional<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,41 @@ class EmployeeService(private val employeeRepository: EmployeeRepository) {
fun getEmployeeById(id: Int): Optional<Employee> = employeeRepository.findById(id)

fun createEmployee(data: JsonNode): Employee {

val employee = Employee(
id = null,
data.get("name").textValue(),
data.get("mobile").textValue(),
)

fun handleManager() {
val branch = data.get("branch").textValue()
employeeRepository.insertIntoManagerTable(branch, employee.id!!)
}

fun handleHr() {
val isTrainer = data.get("is_trainer").booleanValue()
employeeRepository.insertIntoHrTable(isTrainer, employee.id!!)
}

fun handleSalesperson() {
val noOfSales = data.get("no_of_sales").intValue()
employeeRepository.insertIntoSalespersonTable(noOfSales, employee.id!!)
}

val role: String = data.get("role").textValue()

return try {
// @TODO consider a trigger for this instead of 2 operations and their error handling
val createdEmployee = employeeRepository.save(employee)
employeeRepository.insertIntoHrTable(true, employee.id!!)
when (role) {
"manager" -> handleManager()
"salesperson" -> handleSalesperson()
"hr" -> handleHr()
"accountant" -> employeeRepository.insertIntoAccountantTable(employee.id!!)
"darryl" -> employeeRepository.insertIntoDarrylTable(employee.id!!)
}

createdEmployee
} catch (e: Exception) {
throw e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import java.lang.Exception
@AutoConfigureMockMvc
internal class EmployeeServiceTest @Autowired constructor(
val mockMvc: MockMvc,
val objectMapper: ObjectMapper,
val employeeRepository: EmployeeRepository,
) {
val baseURL: String = "/employees/"
Expand All @@ -45,17 +44,18 @@ internal class EmployeeServiceTest @Autowired constructor(
}.andReturn()

// Weird hacks here, REFACTOR
val id = result.response.contentAsString.substring(6, 8).toInt()

// Gets the id from the string version of the response, should ideally be converted to an object and parsed, this will fail if the response structure is changed
val id = result.response.contentAsString.split(',')[0].substring(6).toInt()
try {
val employeeInRoleTable = employeeRepository.fetchFromHr(id)
assertEquals(false, employeeInRoleTable.isEmpty)
} catch (e: Exception) {
println(e)
}
//
// // after
// employeeRepository.deleteById(id);

// after
// Implicitly takes care of dropping value from hr table via cascade constraint
employeeRepository.deleteById(id)
}
}
}

0 comments on commit db30bf5

Please sign in to comment.