Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
feat(reminder): create method deleteReminderForLesson to delete a rem…
Browse files Browse the repository at this point in the history
…inder in ReminderRemoteDataSource
  • Loading branch information
AndreaBrighi committed Jul 11, 2023
1 parent 72c7746 commit 548599f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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))
}
}
}

0 comments on commit 548599f

Please sign in to comment.