Skip to content

Commit 8df07bc

Browse files
committed
use enum for grades
1 parent 4aeeada commit 8df07bc

File tree

8 files changed

+47
-12
lines changed

8 files changed

+47
-12
lines changed

kotlin-ktor/src/main/java/com/baeldung/thymeleaf/data/DataHolder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ object DataHolder {
1212
findStudentById(studentId)
1313
.gradeList.forEach { grade ->
1414
grade.apply {
15-
value = parameters[grade.id] ?: "F"
15+
gradeValue = parameters[grade.id]?.let { GradeValue.valueOf(it) }
1616
}
1717
}
1818
}

kotlin-ktor/src/main/java/com/baeldung/thymeleaf/data/Grade.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package com.baeldung.thymeleaf.data
33
data class Grade (
44
val id: String,
55
val subject: String,
6-
var value: String = String()
6+
var gradeValue: GradeValue? = GradeValue.EMPTY
77
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.thymeleaf.data
2+
3+
enum class GradeValue(val displayValue: String) {
4+
5+
A("A"),
6+
A_PLUS("A+"),
7+
A_MINUS("A-"),
8+
B("B"),
9+
B_PLUS("B+"),
10+
B_MINUS("B-"),
11+
C("C"),
12+
C_PLUS("C+"),
13+
C_MINUS("C-"),
14+
D("D"),
15+
D_PLUS("D+"),
16+
D_MINUS("D-"),
17+
F("F"),
18+
EMPTY("")
19+
20+
}

kotlin-ktor/src/main/java/com/baeldung/thymeleaf/data/Student.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ data class Student(
1010
get() = "$firstName $lastName"
1111

1212
val hasAllGrades: Boolean
13-
get() = gradeList.firstOrNull { grade -> grade.value.isBlank() } == null
13+
get() = gradeList.firstOrNull { grade -> grade.gradeValue == GradeValue.EMPTY} == null
1414
}

kotlin-ktor/src/main/java/com/baeldung/thymeleaf/server/plugins/Routing.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.baeldung.thymeleaf.server.plugins
22

33
import com.baeldung.thymeleaf.data.DataHolder
4+
import com.baeldung.thymeleaf.data.GradeValue
45
import io.ktor.server.application.*
56
import io.ktor.server.request.*
67
import io.ktor.server.response.*
@@ -13,7 +14,14 @@ fun Application.configureRouting() {
1314
call.respond(ThymeleafContent("index", mapOf("studentList" to DataHolder.getStudentList())))
1415
}
1516
get("/report-card/{id}") {
16-
call.respond(ThymeleafContent("report-card", mapOf("student" to DataHolder.findStudentById(call.parameters["id"]))))
17+
call.respond(
18+
ThymeleafContent("report-card",
19+
mapOf(
20+
"student" to DataHolder.findStudentById(call.parameters["id"]),
21+
"gradeOptionList" to GradeValue.entries
22+
)
23+
)
24+
)
1725
}
1826
post("/report-card/{id}") {
1927
val parameters = call.receiveParameters()

kotlin-ktor/src/main/resources/templates/report-card.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ <h3>Name: <span th:text="${student.fullName}"/></h3>
2222
<tbody>
2323
<tr th:each="grade : ${student.gradeList}">
2424
<td th:text="${grade.subject}"></td>
25-
<td><input type="text" th:attr="value = ${grade.value}" th:name="${grade.id}"></td>
25+
<td>
26+
<select th:name="${grade.id}">
27+
<option th:each="gradeOption : ${gradeOptionList}"
28+
th:value="${gradeOption}"
29+
th:text="${gradeOption.displayValue}"
30+
th:selected="${grade.gradeValue == gradeOption}"/>
31+
</select>
32+
</td>
2633
</tr>
2734
</tbody>
2835
</table>

kotlin-ktor/src/test/java/com/baeldung/thymeleaf/data/DataHolderUnitTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class DataHolderUnitTest {
3939

4040
val student = DataHolder.findStudentById("1")
4141
assertNotNull(student.gradeList)
42-
assertEquals("A", student.gradeList.first { it.id == "1" }.value)
43-
assertEquals("B", student.gradeList.first { it.id == "2" }.value)
44-
assertEquals("C", student.gradeList.first { it.id == "3" }.value)
45-
assertEquals("D", student.gradeList.first { it.id == "4" }.value)
42+
assertEquals(GradeValue.A, student.gradeList.first { it.id == "1" }.gradeValue)
43+
assertEquals(GradeValue.B, student.gradeList.first { it.id == "2" }.gradeValue)
44+
assertEquals(GradeValue.C, student.gradeList.first { it.id == "3" }.gradeValue)
45+
assertEquals(GradeValue.D, student.gradeList.first { it.id == "4" }.gradeValue)
4646
}
4747

4848
}

kotlin-ktor/src/test/java/com/baeldung/thymeleaf/data/StudentUnitTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class StudentUnitTest {
2626
firstName = "John",
2727
lastName = "Doe",
2828
gradeList = listOf(
29-
Grade(id = "1", subject = "Reading", value = "A"),
30-
Grade(id = "2", subject = "Writing", value = "A"),
29+
Grade(id = "1", subject = "Reading", gradeValue = GradeValue.A),
30+
Grade(id = "2", subject = "Writing", gradeValue = GradeValue.A),
3131
)
3232
)
3333

@@ -41,7 +41,7 @@ class StudentUnitTest {
4141
firstName = "John",
4242
lastName = "Doe",
4343
gradeList = listOf(
44-
Grade(id = "1", subject = "Reading", value = "A"),
44+
Grade(id = "1", subject = "Reading", gradeValue = GradeValue.A),
4545
Grade(id = "2", subject = "Writing"),
4646
)
4747
)

0 commit comments

Comments
 (0)