Skip to content

Commit

Permalink
Changed to using transactions for deleting healings/payments. Fixes #35
Browse files Browse the repository at this point in the history
  • Loading branch information
yashovardhan99 committed Feb 26, 2021
1 parent a773abe commit 1f5d8e5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Added Undo action for Snackbar on deleting Healing and Payment
- A new material design for Progressbar, TimePicker, DatePicker

### Changed
- Switched to using transactions for deleting healings and payments.

### Fixed
- Fixed a bug where deleting multiple healings/payments did not correctly update the due amount.

## [2.0.0-alpha04] - 2020-02-21
### Changed
- Changed splash screen behaviour
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.yashovardhan99.healersdiary.database
import androidx.paging.PagingSource
import androidx.room.*
import com.yashovardhan99.healersdiary.utils.DangerousDatabase
import com.yashovardhan99.healersdiary.utils.InternalDatabase
import kotlinx.coroutines.flow.Flow
import java.util.*

Expand All @@ -12,6 +13,7 @@ import java.util.*
* @see HealersDatabase
* @see DatabaseModule
*/
@OptIn(InternalDatabase::class)
@Dao
abstract class HealersDao {

Expand Down Expand Up @@ -145,6 +147,7 @@ abstract class HealersDao {
* Delete a patient row. Should not be used directly. Use deletePatientData instead
* @param patient The patient to delete
*/
@InternalDatabase
@Delete(entity = Patient::class)
abstract suspend fun deletePatient(patient: Patient)

Expand All @@ -160,18 +163,48 @@ abstract class HealersDao {
}

/**
* Delete a single healing
* Delete a single healing. Internal use only
* @param healing The healing to delete
*/
@InternalDatabase
@Delete(entity = Healing::class)
abstract suspend fun deleteHealing(healing: Healing)
abstract suspend fun deleteHealingInternal(healing: Healing)

/**
* Delete a single healing and update the patient.
* @param healing The healing to delete.
*/
@Transaction
open suspend fun deleteHealing(healing: Healing) {
val patient = getPatient(healing.patientId)
if (patient != null) {
val updatedDue = patient.due - healing.charge
updatePatient(patient.copy(due = updatedDue))
}
deleteHealingInternal(healing)
}

/**
* Delete a single payment
* @param payment The payment to delete
*/
@InternalDatabase
@Delete(entity = Payment::class)
abstract suspend fun deletePayment(payment: Payment)
abstract suspend fun deletePaymentInternal(payment: Payment)

/**
* Delete a single payment and update the patient.
* @param payment The payment to delete.
*/
@Transaction
open suspend fun deletePayment(payment: Payment) {
val patient = getPatient(payment.patientId)
if (patient != null) {
val updatedDue = patient.due + payment.amount
updatePatient(patient.copy(due = updatedDue))
}
deletePaymentInternal(payment)
}

/**
* Get all activities.
Expand All @@ -193,6 +226,7 @@ abstract class HealersDao {
* @param patientId The patient whose healings are to be deleted.
* @see deletePatientData
*/
@InternalDatabase
@Query("DELETE FROM healings WHERE patient_id=:patientId")
abstract suspend fun deleteHealings(patientId: Long)

Expand All @@ -201,6 +235,7 @@ abstract class HealersDao {
* @param patientId The patient whose payments are to be deleted.
* @see deletePatientData
*/
@InternalDatabase
@Query("DELETE FROM payments WHERE patient_id=:patientId")
abstract suspend fun deletePayments(patientId: Long)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,9 @@ class PatientDetailViewModel @Inject constructor(
}

fun deleteHealing(healing: Healing) {
val updatedPatient = patient.value?.let {
it.copy(due = it.due - healing.charge)
}
savedStateHandle["deleted_healing"] = healing.toBundle()
viewModelScope.launch {
repository.deleteHealing(healing)
updatedPatient?.let { repository.updatePatient(it) }
}
}

Expand All @@ -126,13 +122,9 @@ class PatientDetailViewModel @Inject constructor(
}

fun deletePayment(payment: Payment) {
val updatedPatient = patient.value?.let {
it.copy(due = it.due + payment.amount)
}
savedStateHandle["deleted_payment"] = payment.toBundle()
viewModelScope.launch {
repository.deletePayment(payment)
updatedPatient?.let { repository.updatePatient(it) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ const val transitionDurationLarge = 300L
@RequiresOptIn("This API is performing dangerous database operations!", RequiresOptIn.Level.ERROR)
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
public annotation class DangerousDatabase
annotation class DangerousDatabase

@RequiresOptIn("This is an internal database operation. " +
"This should not be used directly. " +
"Use a transaction which calls this method instead.", RequiresOptIn.Level.ERROR)
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.FUNCTION)
annotation class InternalDatabase

0 comments on commit 1f5d8e5

Please sign in to comment.