Skip to content

Commit

Permalink
시간표 강의 추가,삭제,변경 api 버그픽스 (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hank-Choi authored Jan 6, 2024
1 parent 84dd46d commit e05c649
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 37 deletions.
5 changes: 3 additions & 2 deletions api/src/main/kotlin/handler/TimetableLectureHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.ServerResponse
import org.springframework.web.reactive.function.server.awaitBody
import org.springframework.web.reactive.function.server.awaitBodyOrNull

@Component
class TimetableLectureHandler(
Expand All @@ -36,7 +37,7 @@ class TimetableLectureHandler(
val userId = req.userId
val timetableId = req.pathVariable("timetableId")
val lectureId = req.pathVariable("lectureId")
val isForced = req.awaitBody<ForcedReq>().isForced
val isForced = req.awaitBodyOrNull<ForcedReq>()?.isForced ?: false

timetableLectureService.addLecture(
userId = userId,
Expand Down Expand Up @@ -88,6 +89,6 @@ class TimetableLectureHandler(

data class ForcedReq(
@JsonProperty("is_forced")
val isForced: Boolean
val isForced: Boolean?
)
}
4 changes: 2 additions & 2 deletions core/src/main/kotlin/timetables/data/TimetableLecture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.PropertyNamingStrategies
import com.fasterxml.jackson.databind.annotation.JsonNaming
import com.wafflestudio.snu4t.lectures.data.ClassPlaceAndTime
import com.wafflestudio.snu4t.lectures.data.Lecture
import org.bson.types.ObjectId
import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.index.Indexed
import org.springframework.data.mongodb.core.mapping.Field
Expand All @@ -14,7 +15,7 @@ import org.springframework.data.mongodb.core.mapping.FieldType
data class TimetableLecture(
@Id
@JsonProperty("_id")
var id: String? = null,
var id: String = ObjectId.get().toHexString(),
@Field("academic_year")
@JsonProperty("academic_year")
var academicYear: String?,
Expand Down Expand Up @@ -45,7 +46,6 @@ data class TimetableLecture(
)

fun TimetableLecture(lecture: Lecture, colorIndex: Int) = TimetableLecture(
id = null,
lectureId = lecture.id,
academicYear = lecture.academicYear,
category = lecture.category,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import com.fasterxml.jackson.annotation.JsonProperty
import com.wafflestudio.snu4t.timetables.data.ColorSet

data class TimetableLectureModifyLegacyRequestDto(
@JsonProperty("_id")
val id: String,
@JsonProperty("course_title")
val courseTitle: String,
val courseTitle: String?,
@JsonProperty("academic_year")
val academicYear: String?,
val category: String?,
val classification: String?,
val instructor: String?,
val credit: Long?,
@JsonProperty("class_time_json")
val classPlaceAndTimes: List<ClassPlaceAndTimeLegacyRequestDto>?,
val remark: String?,
val color: ColorSet?,
val colorIndex: Int?,
@JsonProperty("is_forced")
val isForced: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import com.wafflestudio.snu4t.timetables.data.TimetableLecture
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.reactive.asFlow
import kotlinx.coroutines.reactor.awaitSingleOrNull
import org.bson.types.ObjectId
import org.springframework.data.mapping.toDotPath
import org.springframework.data.mongodb.core.FindAndModifyOptions
import org.springframework.data.mongodb.core.ReactiveMongoTemplate
import org.springframework.data.mongodb.core.find
import org.springframework.data.mongodb.core.findModifyAndAwait
Expand All @@ -29,16 +31,11 @@ interface TimetableCustomRepository {
lectureNumber: String
): Flow<Timetable>

suspend fun pushLecture(timeTableId: String, lecture: TimetableLecture): Timetable
suspend fun pullLecture(timeTableId: String, lectureId: String): Timetable
suspend fun pullLectures(timeTableId: String, lectureIds: List<String>): Timetable
suspend fun pushLecture(timeTableId: String, timetableLecture: TimetableLecture): Timetable
suspend fun pullLecture(timeTableId: String, timetableLectureId: String): Timetable
suspend fun pullLectures(timeTableId: String, timetableLectureIds: List<String>): Timetable
suspend fun updateLecture(timeTableId: String, timetableLecture: TimetableLecture): Timetable
suspend fun findLatestChildTimetable(
userId: String,
year: Int,
semester: Semester,
title: String
): Timetable?
suspend fun findLatestChildTimetable(userId: String, year: Int, semester: Semester, title: String): Timetable?
}

class TimetableCustomRepositoryImpl(
Expand Down Expand Up @@ -72,35 +69,32 @@ class TimetableCustomRepositoryImpl(
).asFlow()
}

override suspend fun pushLecture(timeTableId: String, lecture: TimetableLecture): Timetable =
override suspend fun pushLecture(timeTableId: String, timetableLecture: TimetableLecture): Timetable =
reactiveMongoTemplate.update<Timetable>().matching(Timetable::id isEqualTo timeTableId).apply(
Update().push(Timetable::lectures.toDotPath(), lecture).currentDate(Timetable::updatedAt.toDotPath()),
).findModifyAndAwait()

override suspend fun pullLecture(timeTableId: String, lectureId: String): Timetable =
Update().push(Timetable::lectures.toDotPath(), timetableLecture)
.currentDate(Timetable::updatedAt.toDotPath()),
).withOptions(FindAndModifyOptions.options().returnNew(true)).findModifyAndAwait()
override suspend fun pullLecture(timeTableId: String, timetableLectureId: String): Timetable =
reactiveMongoTemplate.update<Timetable>().matching(Timetable::id isEqualTo timeTableId).apply(
Update().pull(
Timetable::lectures.toDotPath(),
Query.query(TimetableLecture::lectureId isEqualTo lectureId)
Query.query(TimetableLecture::id isEqualTo timetableLectureId)
).currentDate(Timetable::updatedAt.toDotPath()),
).findModifyAndAwait()
).withOptions(FindAndModifyOptions.options().returnNew(true)).findModifyAndAwait()

override suspend fun pullLectures(timeTableId: String, lectureIds: List<String>): Timetable =
override suspend fun pullLectures(timeTableId: String, timetableLectureIds: List<String>): Timetable =
reactiveMongoTemplate.update<Timetable>().matching(Timetable::id isEqualTo timeTableId).apply(
Update().pull(
Timetable::lectures.toDotPath(),
Query.query(TimetableLecture::lectureId.inValues(lectureIds))
Query.query(TimetableLecture::id.inValues(timetableLectureIds.map { ObjectId(it) }))
).currentDate(Timetable::updatedAt.toDotPath()),
).findModifyAndAwait()
).withOptions(FindAndModifyOptions.options().returnNew(true)).findModifyAndAwait()

override suspend fun updateLecture(
timeTableId: String,
timetableLecture: TimetableLecture
): Timetable =
reactiveMongoTemplate.update<Timetable>()
.matching(Timetable::id.isEqualTo(timeTableId).and("lecture_list._id").isEqualTo(timetableLecture.id)).apply(
Update().apply { set("""lecture_list.$""", timetableLecture) }
).findModifyAndAwait()
override suspend fun updateLecture(timeTableId: String, timetableLecture: TimetableLecture): Timetable =
reactiveMongoTemplate.update<Timetable>().matching(
Timetable::id.isEqualTo(timeTableId).and("lecture_list._id").isEqualTo(ObjectId(timetableLecture.id))
).apply(Update().apply { set("""lecture_list.$""", timetableLecture) })
.withOptions(FindAndModifyOptions.options().returnNew(true)).findModifyAndAwait()

override suspend fun findLatestChildTimetable(
userId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ class TimetableLectureServiceImpl(
val timetable = timetableRepository.findByUserIdAndId(userId, timetableId) ?: throw TimetableNotFoundException
val timetableLecture = timetable.lectures.find { it.id == timetableLectureId } ?: throw LectureNotFoundException
timetableLecture.apply {
courseTitle = modifyTimetableLectureRequestDto.courseTitle
courseTitle = modifyTimetableLectureRequestDto.courseTitle ?: courseTitle
academicYear = modifyTimetableLectureRequestDto.academicYear ?: academicYear
category = modifyTimetableLectureRequestDto.category ?: category
classification = modifyTimetableLectureRequestDto.classification ?: classification
instructor = modifyTimetableLectureRequestDto.instructor ?: instructor
credit = modifyTimetableLectureRequestDto.credit ?: credit
remark = modifyTimetableLectureRequestDto.remark ?: remark
Expand Down Expand Up @@ -125,7 +128,7 @@ class TimetableLectureServiceImpl(
}

overlappingLectures.isNotEmpty() && isForced -> {
timetableRepository.pullLectures(timetable.id!!, overlappingLectures.map { it.id!! })
timetableRepository.pullLectures(timetable.id!!, overlappingLectures.map { it.id })
}
}
return timetableRepository.pushLecture(timetable.id!!, timetableLecture)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/application-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spring:
database:
logging:
level:
org.springframework.data.mongodb.core.MongoTemplate: DEBUG
org.springframework.data.mongodb.core.ReactiveMongoTemplate: DEBUG

google:
firebase:
Expand Down

0 comments on commit e05c649

Please sign in to comment.