Store when barcode was last scanned
All checks were successful
Docker Image CI / build (push) Successful in 14m3s

This commit is contained in:
James Chapman 2024-10-05 19:15:21 +01:00
parent 68eff11fdc
commit eee2c201fa
Signed by: jamsch0
GPG Key ID: 765FE58130277547
4 changed files with 21 additions and 1 deletions

View File

@ -33,6 +33,9 @@ public class AppDbContext : DbContext
entity.Property(e => e.Format)
.HasDefaultValueSql();
entity.Property(e => e.LastScannedAt)
.HasDefaultValueSql();
});
modelBuilder.Entity<ItemPurchase>(entity =>

View File

@ -12,4 +12,5 @@ public class ItemBarcode
public Guid ItemId { get; init; }
public long BarcodeData { get; init; }
public string Format { get; init; }
public DateTime LastScannedAt { get; set; }
}

View File

@ -0,0 +1,7 @@
ALTER TABLE item_barcodes
ADD COLUMN IF NOT EXISTS last_scanned_at timestamptz NOT NULL DEFAULT current_timestamp;
UPDATE item_barcodes
SET last_scanned_at = created_at
FROM item_purchases
WHERE item_barcodes.item_id = item_purchases.item_id AND is_last_purchase = true;

View File

@ -76,7 +76,16 @@ public class TransactionsController : Controller
.FirstOrDefaultAsync();
item ??= new Item(id: default);
item.Barcodes.Add(new ItemBarcode(item.Id, barcodeData.Value, barcodeFormat));
var barcode = new ItemBarcode(item.Id, barcodeData.Value, barcodeFormat);
item.Barcodes.Add(barcode);
if (item.Id != default)
{
barcode.LastScannedAt = DateTime.UtcNow;
dbContext.Update(barcode);
await dbContext.SaveChangesAsync();
}
// TODO: Fix `MinValue` hack - view models?
transactionItem = new TransactionItem(item.Id, decimal.MinValue, int.MinValue) { Item = item };