@@ -5,6 +5,7 @@ import com.ffii.core.entity.BaseEntity; | |||||
import com.ffii.tsms.modules.user.entity.User; | import com.ffii.tsms.modules.user.entity.User; | ||||
import jakarta.persistence.*; | import jakarta.persistence.*; | ||||
import jakarta.validation.constraints.NotNull; | import jakarta.validation.constraints.NotNull; | ||||
import com.fasterxml.jackson.annotation.JsonInclude; | |||||
import java.time.LocalDate; | import java.time.LocalDate; | ||||
import java.util.HashSet; | import java.util.HashSet; | ||||
@@ -59,7 +60,8 @@ public class Staff extends BaseEntity<Long> { | |||||
// @JoinColumn(name = "salaryEffId") | // @JoinColumn(name = "salaryEffId") | ||||
// private SalaryEffective salaryEffective; | // private SalaryEffective salaryEffective; | ||||
@OneToMany(mappedBy = "staff", cascade = CascadeType.ALL, orphanRemoval = true) | @OneToMany(mappedBy = "staff", cascade = CascadeType.ALL, orphanRemoval = true) | ||||
@JsonManagedReference("staff") | |||||
@JsonManagedReference("staff-team") | |||||
@JsonInclude(JsonInclude.Include.NON_NULL) | |||||
private Set<StaffSkillset> skillset = new HashSet<>(); | private Set<StaffSkillset> skillset = new HashSet<>(); | ||||
@NotNull | @NotNull | ||||
@@ -1,5 +1,6 @@ | |||||
package com.ffii.tsms.modules.data.entity; | package com.ffii.tsms.modules.data.entity; | ||||
import com.fasterxml.jackson.annotation.JsonBackReference; | |||||
import com.ffii.core.entity.BaseEntity; | import com.ffii.core.entity.BaseEntity; | ||||
import com.ffii.tsms.modules.user.entity.User; | import com.ffii.tsms.modules.user.entity.User; | ||||
import jakarta.persistence.*; | import jakarta.persistence.*; | ||||
@@ -21,6 +22,7 @@ public class Team extends BaseEntity<Long> { | |||||
@NotNull | @NotNull | ||||
@OneToOne | @OneToOne | ||||
@JsonBackReference("staff-team") | |||||
@JoinColumn(name = "teamLead", unique = true) | @JoinColumn(name = "teamLead", unique = true) | ||||
private Staff staff; | private Staff staff; | ||||
@@ -39,6 +39,7 @@ open class TeamService( | |||||
@Transactional(rollbackFor = [Exception::class]) | @Transactional(rollbackFor = [Exception::class]) | ||||
open fun saveTeam(req: NewTeamRequest): Team { | open fun saveTeam(req: NewTeamRequest): Team { | ||||
val ids = req.addStaffIds!! | val ids = req.addStaffIds!! | ||||
println(ids) | |||||
val teamLead = staffRepository.findById(ids[0]).orElseThrow() | val teamLead = staffRepository.findById(ids[0]).orElseThrow() | ||||
val teamName = "Team " + teamLead.name | val teamName = "Team " + teamLead.name | ||||
@@ -46,6 +47,7 @@ open class TeamService( | |||||
val teamCode = initials.joinToString("") | val teamCode = initials.joinToString("") | ||||
val team = Team().apply { | val team = Team().apply { | ||||
this.staff = teamLead | |||||
name = teamName | name = teamName | ||||
code = teamCode | code = teamCode | ||||
description = req.description | description = req.description | ||||
@@ -88,7 +90,15 @@ open class TeamService( | |||||
code = teamCode | code = teamCode | ||||
description = req.description | description = req.description | ||||
} | } | ||||
// println(!req.deleteStaffIds.isNullOrEmpty()) | |||||
for (id in addIds) { | |||||
val staff = staffRepository.findById(id.toLong()).orElseThrow() | |||||
staff.apply { | |||||
this.team = team | |||||
} | |||||
staffRepository.save(staff) | |||||
} | |||||
if (!req.deleteStaffIds.isNullOrEmpty()) { | if (!req.deleteStaffIds.isNullOrEmpty()) { | ||||
for (id in req.deleteStaffIds) { | for (id in req.deleteStaffIds) { | ||||
val staff = staffRepository.findById(id).orElseThrow() | val staff = staffRepository.findById(id).orElseThrow() | ||||
@@ -99,13 +109,6 @@ open class TeamService( | |||||
} | } | ||||
} | } | ||||
for (id in addIds) { | |||||
val staff = staffRepository.findById(id.toLong()).orElseThrow() | |||||
staff.apply { | |||||
this.team = team | |||||
} | |||||
staffRepository.save(staff) | |||||
} | |||||
return team | return team | ||||
} | } | ||||
@@ -5,9 +5,9 @@ import jakarta.validation.constraints.NotNull | |||||
import java.time.LocalDate | import java.time.LocalDate | ||||
data class NewTeamRequest ( | data class NewTeamRequest ( | ||||
@field:NotNull(message = "ids cannot be empty") | |||||
val addStaffIds: List<Long>?, | val addStaffIds: List<Long>?, | ||||
val deleteStaffIds: List<Long>?, | val deleteStaffIds: List<Long>?, | ||||
val description: String, | |||||
val description: String?, | |||||
val id: Long? | val id: Long? | ||||
) | ) |