feat(transaction): #66 refine inputs
This commit is contained in:
@@ -83,9 +83,9 @@ func (s TransactionImpl) Update(user *types.User, input types.TransactionInput)
|
||||
return nil, fmt.Errorf("could not parse Id: %w", ErrBadRequest)
|
||||
}
|
||||
|
||||
var transaction *types.Transaction
|
||||
err = s.db.Select(transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError("transaction Get", nil, err)
|
||||
transaction := &types.Transaction{}
|
||||
err = s.db.Get(transaction, `SELECT * FROM "transaction" WHERE user_id = ? AND id = ?`, user.Id, uuid)
|
||||
err = db.TransformAndLogDbError("transaction Update", nil, err)
|
||||
if err != nil {
|
||||
if err == db.ErrNotFound {
|
||||
return nil, fmt.Errorf("transaction %v not found: %w", input.Id, ErrBadRequest)
|
||||
@@ -99,7 +99,7 @@ func (s TransactionImpl) Update(user *types.User, input types.TransactionInput)
|
||||
}
|
||||
|
||||
r, err := s.db.NamedExec(`
|
||||
UPDATE transaction
|
||||
UPDATE "transaction"
|
||||
SET
|
||||
account_id = :account_id,
|
||||
treasure_chest_id = :treasure_chest_id,
|
||||
@@ -182,8 +182,8 @@ func (s TransactionImpl) validateAndEnrichTransaction(transaction *types.Transac
|
||||
|
||||
var (
|
||||
id uuid.UUID
|
||||
accountUuid uuid.UUID
|
||||
treasureChestUuid uuid.UUID
|
||||
accountUuid *uuid.UUID
|
||||
treasureChestUuid *uuid.UUID
|
||||
createdAt time.Time
|
||||
createdBy uuid.UUID
|
||||
updatedAt *time.Time
|
||||
@@ -210,12 +210,13 @@ func (s TransactionImpl) validateAndEnrichTransaction(transaction *types.Transac
|
||||
}
|
||||
|
||||
if input.AccountId != "" {
|
||||
accountUuid, err = uuid.Parse(input.AccountId)
|
||||
temp, err := uuid.Parse(input.AccountId)
|
||||
if err != nil {
|
||||
log.Error("transaction validate: %v", err)
|
||||
return nil, fmt.Errorf("could not parse accountId: %w", ErrBadRequest)
|
||||
}
|
||||
err = s.db.Select(&rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, accountUuid, userId)
|
||||
accountUuid = &temp
|
||||
err = s.db.Get(&rowCount, `SELECT COUNT(*) FROM account WHERE id = ? AND user_id = ?`, accountUuid, userId)
|
||||
err = db.TransformAndLogDbError("transaction validate", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -228,12 +229,13 @@ func (s TransactionImpl) validateAndEnrichTransaction(transaction *types.Transac
|
||||
}
|
||||
|
||||
if input.TreasureChestId != "" {
|
||||
treasureChestUuid, err = uuid.Parse(input.TreasureChestId)
|
||||
temp, err := uuid.Parse(input.TreasureChestId)
|
||||
if err != nil {
|
||||
log.Error("transaction validate: %v", err)
|
||||
return nil, fmt.Errorf("could not parse treasureChestId: %w", ErrBadRequest)
|
||||
}
|
||||
err = s.db.Select(&rowCount, `SELECT COUNT(*) FROM treasure_chest WHERE id = ? AND user_id = ?`, accountUuid, userId)
|
||||
treasureChestUuid = &temp
|
||||
err = s.db.Get(&rowCount, `SELECT COUNT(*) FROM treasure_chest WHERE id = ? AND user_id = ?`, treasureChestUuid, userId)
|
||||
err = db.TransformAndLogDbError("transaction validate", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -276,8 +278,8 @@ func (s TransactionImpl) validateAndEnrichTransaction(transaction *types.Transac
|
||||
Id: id,
|
||||
UserId: userId,
|
||||
|
||||
AccountId: &accountUuid,
|
||||
TreasureChestId: &treasureChestUuid,
|
||||
AccountId: accountUuid,
|
||||
TreasureChestId: treasureChestUuid,
|
||||
Value: valueInt,
|
||||
Timestamp: timestampTime,
|
||||
Note: input.Note,
|
||||
|
||||
@@ -29,28 +29,28 @@ templ EditTransaction(transaction *types.Transaction, accounts []*types.Account,
|
||||
{{
|
||||
var (
|
||||
timestamp time.Time
|
||||
note string
|
||||
// accountId string
|
||||
// treasureChestId string
|
||||
value string
|
||||
value string
|
||||
|
||||
id string
|
||||
cancelUrl string
|
||||
)
|
||||
note := ""
|
||||
accountId := ""
|
||||
treasureChestId := ""
|
||||
if transaction == nil {
|
||||
timestamp = time.Now().UTC().Truncate(time.Minute)
|
||||
note = ""
|
||||
// accountId = ""
|
||||
// treasureChestId = ""
|
||||
value = "0.00"
|
||||
|
||||
id = "new"
|
||||
cancelUrl = "/empty"
|
||||
} else {
|
||||
timestamp = transaction.Timestamp.UTC().Truncate(time.Minute)
|
||||
note = transaction.Note
|
||||
// accountId = transaction.AccountId.String()
|
||||
// treasureChestId = transaction.TreasureChestId.String()
|
||||
if transaction.AccountId != nil {
|
||||
accountId = transaction.AccountId.String()
|
||||
}
|
||||
if transaction.TreasureChestId != nil {
|
||||
treasureChestId = transaction.TreasureChestId.String()
|
||||
}
|
||||
value = displayBalance(transaction.Value)
|
||||
|
||||
id = transaction.Id.String()
|
||||
@@ -96,7 +96,7 @@ templ EditTransaction(transaction *types.Transaction, accounts []*types.Account,
|
||||
>
|
||||
<option value="">-</option>
|
||||
for _, account := range accounts {
|
||||
<option value={ account.Id.String() }>{ account.Name }</option>
|
||||
<option selected?={ account.Id.String() == accountId } value={ account.Id.String() }>{ account.Name }</option>
|
||||
}
|
||||
</select>
|
||||
<label for="treasure-chest-id" class="text-sm text-gray-500">Treasure Chest</label>
|
||||
@@ -106,7 +106,7 @@ templ EditTransaction(transaction *types.Transaction, accounts []*types.Account,
|
||||
>
|
||||
<option value="">-</option>
|
||||
for _, treasureChest := range treasureChests {
|
||||
<option value={ treasureChest.Id.String() }>
|
||||
<option selected?={ treasureChest.Id.String() == treasureChestId } value={ treasureChest.Id.String() }>
|
||||
if treasureChest.ParentId != uuid.Nil {
|
||||
{ treasureChest.Name }
|
||||
} else {
|
||||
@@ -141,7 +141,7 @@ templ TransactionItem(transaction *types.Transaction) {
|
||||
<div id="transaction" class="border-1 border-gray-300 w-full my-4 p-4 bg-gray-50 rounded-lg">
|
||||
<div class="text-xl flex justify-end gap-4">
|
||||
<p class="mr-auto datetime">{ transaction.Timestamp.String() }</p>
|
||||
<p class="mr-20 text-green-700">{ displayBalance(transaction.Value) }</p>
|
||||
<p class="mr-20 text-green-700">{ displayBalance(transaction.Value)+" €" }</p>
|
||||
<button
|
||||
hx-get={ "/transaction/" + transaction.Id.String() + "?edit=true" }
|
||||
hx-target="closest #transaction"
|
||||
|
||||
Reference in New Issue
Block a user