| @@ -7,5 +7,5 @@ import com.ffii.tsms.modules.data.entity.projections.StaffSearchInfo; | |||
| import java.util.List; | |||
| public interface StaffRepository extends AbstractRepository<Staff, Long> { | |||
| List<StaffSearchInfo> findStaffSearchInfoBy(); | |||
| List<StaffSearchInfo> findStaffSearchInfoByAndDeletedFalse(); | |||
| } | |||
| @@ -2,6 +2,7 @@ package com.ffii.tsms.modules.data.service | |||
| import com.ffii.tsms.modules.data.entity.Staff | |||
| import com.ffii.tsms.modules.data.entity.StaffRepository | |||
| import com.ffii.tsms.modules.user.entity.User | |||
| import com.ffii.tsms.modules.user.entity.UserRepository | |||
| import com.ffii.tsms.modules.data.entity.PositionRepository | |||
| import com.ffii.tsms.modules.data.entity.CompanyRepository | |||
| @@ -11,14 +12,15 @@ import com.ffii.tsms.modules.data.entity.SkillRepository | |||
| import com.ffii.tsms.modules.data.entity.SalaryEffectiveRepository | |||
| import com.ffii.tsms.modules.data.entity.DepartmentRepository | |||
| import com.ffii.tsms.modules.data.entity.projections.StaffSearchInfo | |||
| import com.ffii.tsms.modules.project.web.models.NewStaffRequest | |||
| // import com.ffii.tsms.modules.data.web.models.NewStaffRequest | |||
| import com.ffii.tsms.modules.data.web.models.NewStaffRequest | |||
| import org.springframework.stereotype.Service | |||
| import com.ffii.core.support.JdbcDao; | |||
| import com.ffii.core.support.AbstractBaseEntityService; | |||
| import org.springframework.security.crypto.password.PasswordEncoder | |||
| import org.springframework.transaction.annotation.Transactional | |||
| @Service | |||
| public final class StaffsService( | |||
| open class StaffsService( | |||
| private val staffRepository: StaffRepository, | |||
| private val userRepository: UserRepository, | |||
| private val positionRepository: PositionRepository, | |||
| @@ -29,17 +31,19 @@ public final class StaffsService( | |||
| private val salaryEffectiveRepository: SalaryEffectiveRepository, | |||
| private val departmentRepository: DepartmentRepository, | |||
| private val jdbcDao: JdbcDao, | |||
| ) { | |||
| fun getTeamLeads(): List<StaffSearchInfo> { | |||
| private val passwordEncoder: PasswordEncoder | |||
| ) | |||
| : AbstractBaseEntityService<Staff, Long, StaffRepository>(jdbcDao, staffRepository) { | |||
| open fun getTeamLeads(): List<Staff> { | |||
| // TODO: Replace with actual logic and make a projection instead of returning all fields | |||
| return staffRepository.findStaffSearchInfoBy() | |||
| return staffRepository.findAll() | |||
| } | |||
| fun allStaff(): List<StaffSearchInfo> { | |||
| return staffRepository.findStaffSearchInfoBy(); | |||
| open fun allStaff(): List<StaffSearchInfo> { | |||
| return staffRepository.findStaffSearchInfoByAndDeletedFalse(); | |||
| } | |||
| // fun fetchStaffList(args: Map<String, Any>): List<Map<String, Any>> { | |||
| // fun allStaff(args: Map<String, Any>): List<Map<String, Any>> { | |||
| // val sql = StringBuilder("select" | |||
| // + " s.*," | |||
| // + " date_format(s.created, '%Y-%m-%d') as created," | |||
| @@ -55,8 +59,15 @@ public final class StaffsService( | |||
| // return jdbcDao.queryForList(sql.toString(), args) | |||
| // } | |||
| fun saveStaff(req: NewStaffRequest): Staff { | |||
| val user = userRepository.findById(req.userId).orElseThrow() | |||
| @Transactional(rollbackFor = [Exception::class]) | |||
| open fun saveStaff(req: NewStaffRequest): Staff { | |||
| val user = userRepository.saveAndFlush( | |||
| User().apply { | |||
| username = req.name | |||
| password = passwordEncoder.encode("mms1234") | |||
| name = req.name | |||
| } | |||
| ) | |||
| val currentPosition = positionRepository.findById(req.currentPositionId).orElseThrow() | |||
| val joinPosition = positionRepository.findById(req.joinPositionId).orElseThrow() | |||
| val company = companyRepository.findById(req.companyId).orElseThrow() | |||
| @@ -90,7 +101,6 @@ public final class StaffsService( | |||
| this.salaryEffective = salaryEffective | |||
| this.department = department | |||
| } | |||
| return staffRepository.save(staff) | |||
| } | |||
| } | |||
| @@ -1,32 +1,34 @@ | |||
| package com.ffii.tsms.modules.data.web | |||
| import com.ffii.tsms.modules.data.entity.Staff | |||
| import com.ffii.tsms.modules.data.entity.projections.StaffSearchInfo | |||
| import com.ffii.tsms.modules.data.service.StaffsService | |||
| import org.springframework.web.bind.annotation.GetMapping | |||
| import org.springframework.web.bind.annotation.RequestMapping | |||
| import org.springframework.web.bind.annotation.RestController | |||
| import org.springframework.web.bind.annotation.PostMapping | |||
| import org.springframework.web.bind.annotation.RequestBody | |||
| import com.ffii.tsms.modules.project.web.models.NewStaffRequest | |||
| import com.ffii.tsms.modules.data.web.models.NewStaffRequest | |||
| import jakarta.validation.Valid | |||
| import com.ffii.tsms.modules.data.entity.projections.StaffSearchInfo | |||
| import org.springframework.http.HttpStatus | |||
| import org.springframework.web.bind.annotation.* | |||
| @RestController | |||
| @RequestMapping("/staffs") | |||
| class StaffsController(private val staffsService: StaffsService) { | |||
| @GetMapping("/teamLeads") | |||
| fun teamLeads(): List<StaffSearchInfo> { | |||
| fun teamLeads(): List<Staff> { | |||
| return staffsService.getTeamLeads() | |||
| } | |||
| @GetMapping | |||
| fun allStaff(): List<StaffSearchInfo> { | |||
| return staffsService.allStaff() | |||
| } | |||
| // @GetMapping("/list") | |||
| // fun list(): List<Staff> { | |||
| // return staffsService.getTeamLeads() | |||
| // } | |||
| @GetMapping("/list") | |||
| fun list(): List<Staff> { | |||
| return staffsService.getTeamLeads() | |||
| } | |||
| @DeleteMapping("/delete/{id}") | |||
| @ResponseStatus(HttpStatus.NO_CONTENT) | |||
| fun delete(@PathVariable id: Long?) { | |||
| staffsService.markDelete(id) | |||
| } | |||
| @PostMapping("/new") | |||
| fun saveStaff(@Valid @RequestBody newStaff: NewStaffRequest): Staff { | |||
| return staffsService.saveStaff(newStaff) | |||
| @@ -1,53 +1,36 @@ | |||
| package com.ffii.tsms.modules.project.web.models | |||
| package com.ffii.tsms.modules.data.web.models | |||
| import jakarta.validation.constraints.NotBlank | |||
| import jakarta.validation.constraints.NotNull | |||
| import java.time.LocalDate | |||
| data class NewStaffRequest( | |||
| @field:NotBlank(message = "Staff userId cannot be empty") | |||
| @field:NotNull(message = "Staff userId cannot be empty") | |||
| val userId: Long, | |||
| val joinDate: LocalDate, | |||
| @field:NotBlank(message = "Staff name cannot be empty") | |||
| val name: String, | |||
| @field:NotBlank(message = "Staff staffId cannot be empty") | |||
| val staffId: String, | |||
| @field:NotNull(message = "Staff companyId cannot be empty") | |||
| val companyId: Long, | |||
| @field:NotNull(message = "Staff salaryEffId cannot be empty") | |||
| val salaryEffId: Long, | |||
| @field:NotNull(message = "Staff salaryEffId cannot be empty") | |||
| val skillSetId: Long, | |||
| val joinDate: LocalDate, | |||
| val currentPositionId: Long, | |||
| val joinPositionId: Long, | |||
| @field:NotBlank(message = "Staff companyId cannot be empty") | |||
| val companyId: Long, | |||
| val gradeId: Long, | |||
| val teamId: Long, | |||
| val skillSetId: Long, | |||
| @field:NotBlank(message = "Staff salaryEffId cannot be empty") | |||
| val salaryEffId: Long, | |||
| val departmentId: Long, | |||
| val phone1: String, | |||
| val phone2: String, | |||
| val email: String, | |||
| val emergContactName: String, | |||
| val emergContactPhone: String, | |||
| val employType: String, | |||
| val departDate: LocalDate, | |||
| val departReason: String, | |||
| val remark: String, | |||
| ) | |||