| @@ -17,4 +17,5 @@ public interface StaffRepository extends AbstractRepository<Staff, Long> { | |||||
| Optional<Staff> findByStaffId(@Param("staffId") String staffId); | Optional<Staff> findByStaffId(@Param("staffId") String staffId); | ||||
| Optional<Staff> findByUserId(@Param("userId") Long userId); | Optional<Staff> findByUserId(@Param("userId") Long userId); | ||||
| Optional<List<Staff>> findAllByTeamIdAndDeletedFalse(Long id); | |||||
| } | } | ||||
| @@ -74,6 +74,10 @@ open class StaffsService( | |||||
| return staffRepository.findByUserId(userId); | return staffRepository.findByUserId(userId); | ||||
| } | } | ||||
| open fun findAllByTeamId(id: Long): Optional<List<Staff>> { | |||||
| return staffRepository.findAllByTeamIdAndDeletedFalse(id); | |||||
| } | |||||
| @Transactional(rollbackFor = [Exception::class]) | @Transactional(rollbackFor = [Exception::class]) | ||||
| open fun saveStaff(req: NewStaffRequest): Staff { | open fun saveStaff(req: NewStaffRequest): Staff { | ||||
| val user = userRepository.saveAndFlush( | val user = userRepository.saveAndFlush( | ||||
| @@ -10,11 +10,13 @@ import com.ffii.tsms.modules.data.entity.projections.StaffSearchInfo | |||||
| import com.ffii.tsms.modules.data.web.models.NewTeamRequest | import com.ffii.tsms.modules.data.web.models.NewTeamRequest | ||||
| import org.springframework.stereotype.Service | import org.springframework.stereotype.Service | ||||
| import org.springframework.transaction.annotation.Transactional | import org.springframework.transaction.annotation.Transactional | ||||
| import java.util.Optional | |||||
| @Service | @Service | ||||
| open class TeamService( | open class TeamService( | ||||
| private val teamRepository: TeamRepository, | private val teamRepository: TeamRepository, | ||||
| private val staffRepository: StaffRepository, | private val staffRepository: StaffRepository, | ||||
| private val staffsService: StaffsService, | |||||
| private val jdbcDao: JdbcDao, | private val jdbcDao: JdbcDao, | ||||
| ) : AbstractBaseEntityService<Team, Long, TeamRepository>(jdbcDao, teamRepository) { | ) : AbstractBaseEntityService<Team, Long, TeamRepository>(jdbcDao, teamRepository) { | ||||
| @@ -82,6 +84,17 @@ open class TeamService( | |||||
| return team | return team | ||||
| } | } | ||||
| @Transactional(rollbackFor = [Exception::class]) | |||||
| open fun setStaffTeamIdToNull(id: Long) { | |||||
| val staffList: List<Staff>? = staffsService.findAllByTeamId(id).orElseThrow() | |||||
| if (!staffList.isNullOrEmpty()) { | |||||
| for (staff in staffList) { | |||||
| staff.apply { | |||||
| team = null; | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| open fun saveOrUpdate(req: NewTeamRequest): Team { | open fun saveOrUpdate(req: NewTeamRequest): Team { | ||||
| val team = if(req.id != null) find(req.id).get() else Team() | val team = if(req.id != null) find(req.id).get() else Team() | ||||
| if (req.id != null) { | if (req.id != null) { | ||||
| @@ -7,6 +7,8 @@ import com.ffii.tsms.modules.data.service.TeamService | |||||
| import com.ffii.tsms.modules.data.web.models.NewTeamRequest | import com.ffii.tsms.modules.data.web.models.NewTeamRequest | ||||
| import jakarta.servlet.http.HttpServletRequest | import jakarta.servlet.http.HttpServletRequest | ||||
| import jakarta.validation.Valid | import jakarta.validation.Valid | ||||
| import org.springframework.http.HttpStatus | |||||
| import org.springframework.transaction.annotation.Transactional | |||||
| import org.springframework.web.bind.ServletRequestBindingException | import org.springframework.web.bind.ServletRequestBindingException | ||||
| import org.springframework.web.bind.annotation.* | import org.springframework.web.bind.annotation.* | ||||
| @@ -32,6 +34,14 @@ class TeamController(private val teamService: TeamService) { | |||||
| return teamService.getTeamDetail(args); | return teamService.getTeamDetail(args); | ||||
| } | } | ||||
| // @Transactional(rollbackFor = [Exception::class]) | |||||
| @DeleteMapping("/delete/{id}") | |||||
| @ResponseStatus(HttpStatus.NO_CONTENT) | |||||
| fun delete(@PathVariable id: Long) { | |||||
| teamService.markDelete(id) | |||||
| teamService.setStaffTeamIdToNull(id) | |||||
| } | |||||
| @GetMapping("/combo") | @GetMapping("/combo") | ||||
| @Throws(ServletRequestBindingException::class) | @Throws(ServletRequestBindingException::class) | ||||
| fun combo(request: HttpServletRequest?): RecordsRes<Map<String, Any>> { | fun combo(request: HttpServletRequest?): RecordsRes<Map<String, Any>> { | ||||