Skip to content

Commit

Permalink
🗃️ Modify Use Data Structure to support description field
Browse files Browse the repository at this point in the history
Part of #753

Signed-off-by: Leonardo Colman Lopes <[email protected]>
  • Loading branch information
LeoColman committed Jan 11, 2025
1 parent 6b2cae3 commit 50444df
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ data class Use(

val costPerGram: BigDecimal = BigDecimal.ZERO,

val id: String = UUID.randomUUID().toString()
val id: String = UUID.randomUUID().toString(),

val description: String = ""
) {

@Transient
Expand All @@ -35,6 +37,7 @@ data class Use(
if (date != other.date) return false
if (amountGrams != other.amountGrams) return false
if (costPerGram != other.costPerGram) return false
if (description != other.description) return false

return true
}
Expand All @@ -43,6 +46,7 @@ data class Use(
var result = date.hashCode()
result = 31 * result + amountGrams.hashCode()
result = 31 * result + costPerGram.hashCode()
result = 31 * result + description.hashCode()
return result
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ fun Use.toEntity(): UseEntity = UseEntity(
date.format(ISO_LOCAL_DATE_TIME),
amountGrams.toPlainString(),
costPerGram.toPlainString(),
id
id,
description
)

fun UseEntity.toUse() = Use(
parse(date),
amount_grams.toBigDecimal(),
cost_per_gram.toBigDecimal(),
id
id,
description
)
6 changes: 4 additions & 2 deletions app/src/main/sqldelight/br/com/colman/petals/Use.sq
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ CREATE TABLE Use(
date TEXT NOT NULL,
amount_grams TEXT NOT NULL,
cost_per_gram TEXT NOT NULL,
id TEXT PRIMARY KEY
id TEXT PRIMARY KEY,
description TEXT NOT NULL DEFAULT ""
);

upsert:
INSERT INTO Use(date, amount_grams, cost_per_gram, id) VALUES ?
INSERT INTO Use(date, amount_grams, cost_per_gram, id, description) VALUES ?
ON CONFLICT (id) DO UPDATE SET date = excluded.date,
amount_grams = excluded.amount_grams,
description = excluded.description,
cost_per_gram = excluded.cost_per_gram;

selectLast:
Expand Down
1 change: 1 addition & 0 deletions app/src/main/sqldelight/migrations/3.sqm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE Use ADD COLUMN description TEXT NOT NULL DEFAULT "";

0 comments on commit 50444df

Please sign in to comment.