Skip to content

Commit

Permalink
fix: StandardAnswer 업데이트시 기존 StandardAnswer 삭제 안 되던 버그 수정 (#173)
Browse files Browse the repository at this point in the history
* fix: StandardAnswer 업데이트시 기존 StandardAnswer 삭제 안 되던 버그 수정

* refactor: lint
  • Loading branch information
ekzm8523 authored Jan 14, 2024
1 parent c20b56c commit 8724a4a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
17 changes: 16 additions & 1 deletion src/main/kotlin/io/csbroker/apiserver/model/LongProblem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LongProblem(
@OneToMany(mappedBy = "problem", cascade = [CascadeType.ALL])
var userAnswers: MutableList<UserAnswer> = mutableListOf(),

@OneToMany(mappedBy = "longProblem", cascade = [CascadeType.ALL])
@OneToMany(mappedBy = "longProblem", cascade = [CascadeType.ALL], orphanRemoval = true)
var standardAnswers: MutableList<StandardAnswer> = mutableListOf(),
) : Problem(
title = title,
Expand All @@ -45,6 +45,7 @@ class LongProblem(
description = upsertRequestDto.description
isGradable = upsertRequestDto.isGradable
isActive = upsertRequestDto.isActive
updateStandardAnswers(upsertRequestDto.standardAnswers)
}

fun toLongProblemResponseDto(): LongProblemResponseDto {
Expand Down Expand Up @@ -111,4 +112,18 @@ class LongProblem(
problemBookmark.any { it.user.email == email },
)
}

fun updateStandardAnswers(standardAnswers: List<String>) {
if (this.standardAnswers.map { it.content }.toSet() != standardAnswers.toSet()) {
this.standardAnswers.clear()
this.standardAnswers.addAll(
standardAnswers.map {
StandardAnswer(
content = it,
longProblem = this,
)
},
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,6 @@ class AdminLongProblemServiceImpl(
?: throw EntityNotFoundException("${id}번 문제는 존재하지 않는 서술형 문제입니다.")
val gradingStandardList = updateRequestDto.getGradingStandardList(longProblem)

if (longProblem.standardAnswers.map { it.content }.toSet() != updateRequestDto.standardAnswers.toSet()) {
standardAnswerRepository.deleteAllByLongProblem(longProblem)
standardAnswerRepository.saveAll(
updateRequestDto.standardAnswers.map {
StandardAnswer(
content = it,
longProblem = longProblem,
)
},
)
}

if (longProblem.gradingStandards.map { it.content }.toSet() != gradingStandardList.map { it.content }.toSet()) {
gradingStandardRepository.deleteAllById(longProblem.gradingStandards.map { it.id })
longProblem.addGradingStandards(gradingStandardList)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.csbroker.apiserver.controller.v1.admin.problem

import io.csbroker.apiserver.controller.IntegrationTest
import io.csbroker.apiserver.dto.problem.longproblem.LongProblemUpsertRequestDto
import io.csbroker.apiserver.model.LongProblem
import io.csbroker.apiserver.model.StandardAnswer
import io.csbroker.apiserver.model.Tag
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import org.springframework.http.HttpMethod
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

class AdminLongProblemControllerIntegrationTest : IntegrationTest() {

@Test
fun `문제 업데이트`() {
// given
val problem = save(
LongProblem(
title = "문제 제목",
description = "문제 설명",
creator = adminUser,
),
)
save(StandardAnswer(content = "삭제될 모범 답안", longProblem = problem))
save(Tag(name = "tag1"))
val updateRequestDto = LongProblemUpsertRequestDto(
title = "Test problem",
description = "This is a test problem",
tags = mutableListOf("tag1"),
standardAnswers = listOf("업데이트될 모범 답안"),
gradingStandards = mutableListOf(),
)

// when
val response = request(
method = HttpMethod.PUT,
url = "/api/admin/problems/long/${problem.id}",
isAdmin = true,
body = updateRequestDto,
)

// then
response.andExpect(status().isOk)
.andExpect {
val standardAnswers = findAll<StandardAnswer>(
"SELECT s FROM StandardAnswer s where s.longProblem.id = :id",
mapOf("id" to problem.id),
)
standardAnswers.map { it.content }.toSet() shouldBe updateRequestDto.standardAnswers.toSet()
}
}
}

0 comments on commit 8724a4a

Please sign in to comment.