From 548599f40d163b3f6e1c0c219a33a0d0d7633082 Mon Sep 17 00:00:00 2001 From: Andrea Brighi Date: Tue, 11 Jul 2023 12:04:02 +0200 Subject: [PATCH] feat(reminder): create method deleteReminderForLesson to delete a reminder in ReminderRemoteDataSource --- .../datasource/ReminderRemoteDataSource.kt | 13 ++++++ .../ReminderRemoteDataSourceImpl.kt | 42 ++++++++----------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/reminderData/src/main/kotlin/com/intelligentbackpack/reminderdata/datasource/ReminderRemoteDataSource.kt b/reminderData/src/main/kotlin/com/intelligentbackpack/reminderdata/datasource/ReminderRemoteDataSource.kt index 28016dae..96dc46a2 100644 --- a/reminderData/src/main/kotlin/com/intelligentbackpack/reminderdata/datasource/ReminderRemoteDataSource.kt +++ b/reminderData/src/main/kotlin/com/intelligentbackpack/reminderdata/datasource/ReminderRemoteDataSource.kt @@ -57,4 +57,17 @@ interface ReminderRemoteDataSource { * @return the id of the reminder. */ suspend fun createNewReminderForLesson(email: String, lesson: Lesson, isbn: String): String + + /** + * Deletes the reminder for the given lesson. + * + * @param email the email of the student. + * @param lesson the lesson. + * @param isbn the isbn of the book. + */ + suspend fun deleteReminderForLesson( + email: String, + lesson: Lesson, + isbn: String, + ) } diff --git a/reminderData/src/main/kotlin/com/intelligentbackpack/reminderdata/datasource/ReminderRemoteDataSourceImpl.kt b/reminderData/src/main/kotlin/com/intelligentbackpack/reminderdata/datasource/ReminderRemoteDataSourceImpl.kt index c4de4865..eebd94ef 100644 --- a/reminderData/src/main/kotlin/com/intelligentbackpack/reminderdata/datasource/ReminderRemoteDataSourceImpl.kt +++ b/reminderData/src/main/kotlin/com/intelligentbackpack/reminderdata/datasource/ReminderRemoteDataSourceImpl.kt @@ -7,7 +7,6 @@ import com.intelligentbackpack.networkutility.RetrofitHelper import com.intelligentbackpack.schooldata.api.CalendarApi import com.intelligentbackpack.schooldata.datasource.SchoolRemoteDataSourceImpl import okhttp3.RequestBody -import org.json.JSONArray import org.json.JSONObject /** @@ -41,35 +40,30 @@ class ReminderRemoteDataSourceImpl( } } - override suspend fun createNewReminderForLesson(email: String, lesson: Lesson, isbn: String): String { - val jsonParam = mapOf( - ("Email" to email), - ("Nome_lezione" to lesson.nomeLezione), - ("Materia" to lesson.materia), - ("Giorno" to lesson.giorno), - ("Ora_inizio" to lesson.oraInizio), - ("Ora_fine" to lesson.oraFine), - ("Professore" to lesson.professore), - ("Data_Inizio" to lesson.dataInizio), - ("Data_Fine" to lesson.dataFine), - ("ID_Calendario" to lesson.idCalendario), - ) - val lessonJson = JSONObject(jsonParam) - val isbnJson = JSONArray(listOf(isbn)) - val jsonParamReminder = mapOf( - ("lesson" to lessonJson), - ("ISBNs" to isbnJson), - ("email_executor" to email), - ) - val request = RequestBody.create( + private fun createJsonForReminder(email: String, lesson: Lesson, isbn: String): RequestBody { + val json = JSONObject() + json.put("email", email) + json.put("lesson", schoolRemoteDataSource.createJsonForLesson(lesson)) + json.put("isbn", isbn) + return RequestBody.create( okhttp3.MediaType.parse("application/json; charset=utf-8"), - (JSONObject(jsonParamReminder)).toString(), + json.toString(), ) - val response = calendarApi.createReminderForLesson(request).execute() + } + + override suspend fun createNewReminderForLesson(email: String, lesson: Lesson, isbn: String): String { + val response = calendarApi.createReminderForLesson(createJsonForReminder(email, lesson, isbn)).execute() if (response.isSuccessful) { return response.body()?.message ?: "" } else { throw DownloadException(ErrorHandler.getError(response)) } } + + override suspend fun deleteReminderForLesson(email: String, lesson: Lesson, isbn: String) { + val response = calendarApi.deleteReminderForLesson(createJsonForReminder(email, lesson, isbn)).execute() + if (!response.isSuccessful) { + throw DownloadException(ErrorHandler.getError(response)) + } + } }