Skip to content

Commit

Permalink
Merge branch 'main' into #156
Browse files Browse the repository at this point in the history
  • Loading branch information
ShanePark committed May 17, 2024
2 parents 7d87cd2 + 8a0e035 commit 76e385f
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.tistory.shanepark.dutypark.department.domain.enums.DepartmentNameChec
import com.tistory.shanepark.dutypark.department.domain.enums.DepartmentNameCheckResult.*
import com.tistory.shanepark.dutypark.department.repository.DepartmentRepository
import com.tistory.shanepark.dutypark.department.service.DepartmentService
import com.tistory.shanepark.dutypark.duty.enums.Color
import com.tistory.shanepark.dutypark.member.repository.MemberRepository
import jakarta.validation.Valid
import org.springframework.data.domain.Page
Expand Down Expand Up @@ -65,14 +64,13 @@ class DepartmentAdminController(
return ResponseEntity.ok().build()
}

@PatchMapping("/{id}/off-color")
fun changeOffColor(
@PatchMapping("/{id}/default-duty")
fun updateDefaultDuty(
@PathVariable id: Long,
@RequestParam color: String
@RequestParam color: String,
@RequestParam name: String,
): ResponseEntity<Any> {
val department = departmentRepository.findById(id).orElseThrow()
department.offColor = Color.valueOf(color)
departmentRepository.save(department)
departmentService.updateDefaultDuty(id, name, color)
return ResponseEntity.ok().build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ data class DepartmentDto(
it.color.toString()
)
}.toMutableList()
sortedTypes.add(0, DutyTypeDto(name = "OFF", position = -1, color = department.offColor.toString()))
sortedTypes.add(
0,
DutyTypeDto(
name = department.defaultDutyName,
position = -1,
color = department.defaultDutyColor.toString()
)
)

return DepartmentDto(
id = department.id!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class Department(
var manager: Member? = null

@Enumerated(EnumType.STRING)
var offColor: Color = Color.GREY
@Column(nullable = false, name = "default_duty_color")
var defaultDutyColor: Color = Color.GREY

@Column(nullable = false, name = "default_duty_name")
var defaultDutyName: String = "OFF"

@OneToMany(mappedBy = "department", fetch = FetchType.LAZY, cascade = [CascadeType.ALL], orphanRemoval = true)
val dutyTypes: MutableList<DutyType> = mutableListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.tistory.shanepark.dutypark.department.domain.dto.DepartmentDto
import com.tistory.shanepark.dutypark.department.domain.dto.SimpleDepartmentDto
import com.tistory.shanepark.dutypark.department.domain.entity.Department
import com.tistory.shanepark.dutypark.department.repository.DepartmentRepository
import com.tistory.shanepark.dutypark.duty.enums.Color
import com.tistory.shanepark.dutypark.duty.repository.DutyRepository
import com.tistory.shanepark.dutypark.member.repository.MemberRepository
import org.springframework.data.domain.Page
Expand Down Expand Up @@ -100,4 +101,14 @@ class DepartmentService(
departmentRepository.save(department)
}

fun updateDefaultDuty(departmentId: Long, newDutyName: String?, newDutyColor: String?) {
val department = departmentRepository.findById(departmentId).orElseThrow()
if (newDutyColor != null) {
department.defaultDutyColor = Color.valueOf(newDutyColor)
}
if (newDutyName != null) {
department.defaultDutyName = newDutyName
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class DutyService(
val member = memberRepository.findMemberWithDepartment(memberId).orElseThrow()
friendService.checkVisibility(loginMember, member)
val department = member.department ?: return emptyList()
val offColor = department.offColor
val defaultDutyColor = department.defaultDutyColor

val answer = mutableListOf<DutyDto>()
val calendarView = CalendarView(yearMonth)
Expand All @@ -103,7 +103,7 @@ class DutyService(
for (i in 1..calendarView.paddingBefore) {
val day = calendarView.prevMonth.atEndOfMonth().dayOfMonth - (calendarView.paddingBefore - i)
val duty = dutiesLastMonth[day]
addDutyDto(calendarView.prevMonth, day, duty, answer, offColor)
addDutyDto(calendarView.prevMonth, day, duty, answer, defaultDutyColor)
}

val dutiesOfMonth =
Expand All @@ -112,7 +112,7 @@ class DutyService(
val lengthOfMonth = yearMonth.lengthOfMonth()
for (i in 1..lengthOfMonth) {
val duty = dutiesOfMonth[i]
addDutyDto(yearMonth, i, duty, answer, offColor)
addDutyDto(yearMonth, i, duty, answer, defaultDutyColor)
}

val dutiesNextMonth = dutyRepository.findAllByMemberAndDutyYearAndDutyMonth(
Expand All @@ -122,19 +122,19 @@ class DutyService(
).associateBy { it.dutyDay }
for (i in 1..calendarView.paddingAfter) {
val duty = dutiesNextMonth[i]
addDutyDto(calendarView.nextMonth, i, duty, answer, offColor)
addDutyDto(calendarView.nextMonth, i, duty, answer, defaultDutyColor)
}

return answer
}

private fun addDutyDto(yearMonth: YearMonth, day: Int, duty: Duty?, list: MutableList<DutyDto>, offColor: Color) {
private fun addDutyDto(yearMonth: YearMonth, day: Int, duty: Duty?, list: MutableList<DutyDto>, defaultDutyColor: Color) {
val dutyDto = DutyDto(
year = yearMonth.year,
month = yearMonth.month.value,
day = day,
dutyType = duty?.dutyType?.name,
dutyColor = duty?.dutyType?.color?.name ?: offColor.name
dutyColor = duty?.dutyType?.color?.name ?: defaultDutyColor.name
)
list.add(dutyDto)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE department
RENAME COLUMN off_color TO default_duty_color;

ALTER TABLE department
ADD COLUMN default_duty_name VARCHAR(255) NOT NULL DEFAULT 'OFF';
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ <h3>Duty Types
<th scope="col"></th>
<th scope="col">Name</th>
<th scope="col">Color</th>
<th scope="col"></th>
<th scope="col">Note</th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -107,6 +107,7 @@ <h3>Duty Types
v-on:click.stop="removeDutyType(dutyType)">Delete
</button>
</td>
<td>{{dutyType.id ? '' : 'Default'}}</td>
</tr>
</tbody>
</table>
Expand Down Expand Up @@ -415,9 +416,7 @@ <h5 class="modal-title" v-text="mode=='memberSearch' ? 'Add Member' : 'Change Ma
const modal = document.querySelector('#duty-type-modal').cloneNode(true);
let nameInput = modal.querySelector('input[name="name"]');
nameInput.setAttribute('value', dutyTypeName);
if (!dutyTypeId) {
nameInput.disabled = true;
}

modal.querySelectorAll('option').forEach(option => {
if (option.value === dutyTypeColor) {
option.setAttribute('selected', 'selected');
Expand Down Expand Up @@ -446,7 +445,7 @@ <h5 class="modal-title" v-text="mode=='memberSearch' ? 'Add Member' : 'Change Ma
}).then((result) => {
if (result.isConfirmed) {
if (!dutyTypeId) {
app.changeOffColor(result.value[1]);
app.updateDefaultDuty(result.value[0], result.value[1]);
return;
}
$.ajax({
Expand Down Expand Up @@ -607,13 +606,14 @@ <h5 class="modal-title" v-text="mode=='memberSearch' ? 'Add Member' : 'Change Ma
})
}
})
}, changeOffColor(color) {
}, updateDefaultDuty(name, color) {
const departmentId = app.department.id;
$.ajax({
url: '/admin/api/departments/' + departmentId + '/off-color',
url: '/admin/api/departments/' + departmentId + '/default-duty',
type: 'PATCH',
data: {
color: color
name: name,
color: color,
},
success: function () {
app.load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.tistory.shanepark.dutypark.department.service
import com.tistory.shanepark.dutypark.DutyparkIntegrationTest
import com.tistory.shanepark.dutypark.department.domain.dto.DepartmentCreateDto
import com.tistory.shanepark.dutypark.duty.domain.dto.DutyUpdateDto
import com.tistory.shanepark.dutypark.duty.enums.Color
import com.tistory.shanepark.dutypark.duty.repository.DutyRepository
import com.tistory.shanepark.dutypark.duty.service.DutyService
import org.assertj.core.api.Assertions.assertThat
Expand Down Expand Up @@ -293,4 +294,20 @@ class DepartmentServiceTest : DutyparkIntegrationTest() {

}

@Test
fun `update default duty success`() {
// Given
val department = service.create(DepartmentCreateDto("deptName", "deptDesc"))

// When
val updatedDutyName = "newDutyName"
val updatedDutyColor = Color.RED
service.updateDefaultDuty(department.id, updatedDutyName, updatedDutyColor.name)

// Then
val updated = departmentRepository.findById(department.id).orElseThrow()
assertThat(updated.defaultDutyName).isEqualTo(updatedDutyName)
assertThat(updated.defaultDutyColor).isEqualTo(updatedDutyColor)
}

}

0 comments on commit 76e385f

Please sign in to comment.