Luís Murta ac396ac259
All checks were successful
Go / build (1.21) (push) Successful in 1m33s
Go / build (1.22) (push) Successful in 1m31s
Implements /transactions PUT method
Adds PUT method to OpenAPI spec.

Given that the transaction IDs are generated on server-side, for the PUT
method to remain idempotent, it can only update existing transactions.

It also adds a TransactionExists method on the DAL.

Issue: #20
2024-08-03 22:21:19 +00:00

122 lines
2.9 KiB
Go

package api
import (
"log"
"net/http"
"git.rosemyrtle.work/personal-finance/server/internal/dal"
"github.com/labstack/echo/v4"
)
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@v2.1.0 --config=api.cfg.yaml ../../docs/openapi.yaml
type ServerImpl struct {
Dal dal.DAL
}
func (server *ServerImpl) GetBanks(ctx echo.Context) error {
log.Print("GetBanks")
banks, err := server.Dal.Banks()
if err != nil {
return ctx.NoContent(http.StatusInternalServerError)
}
if len(banks) == 0 {
return ctx.NoContent(http.StatusNoContent)
}
return ctx.JSON(http.StatusOK, convertBanks(banks))
}
func (server *ServerImpl) GetBankById(ctx echo.Context, bankId string) error {
log.Printf("GetBankById(%v)", bankId)
bank, err := server.Dal.Bank(bankId)
if err != nil {
log.Printf("%v", err)
return ctx.NoContent(http.StatusInternalServerError)
}
if bank == nil {
return ctx.NoContent(http.StatusNotFound)
}
return ctx.JSON(http.StatusOK, convertBank(*bank))
}
func (server *ServerImpl) GetTransactions(ctx echo.Context, params GetTransactionsParams) error {
log.Print("GetTransactions")
transactions, err := server.Dal.Transactions()
if err != nil {
return ctx.NoContent(http.StatusInternalServerError)
}
if len(transactions) == 0 {
return ctx.NoContent(http.StatusNoContent)
}
return ctx.JSON(http.StatusOK, convertTransactions(transactions))
}
func (server *ServerImpl) GetTransactionById(ctx echo.Context, transactionId int64) error {
log.Printf("GetTransactionById(%v)", transactionId)
transaction, err := server.Dal.Transaction(transactionId)
if err != nil {
log.Printf("%v", err)
return ctx.NoContent(http.StatusInternalServerError)
}
if transaction == nil {
return ctx.NoContent(http.StatusNotFound)
}
return ctx.JSON(http.StatusOK, entity2transaction(*transaction))
}
func (server *ServerImpl) CreateTransaction(ctx echo.Context) error {
t := new(Transaction)
if err := ctx.Bind(t); err != nil {
log.Printf("%v", err)
return ctx.NoContent(http.StatusBadRequest)
}
transaction, err := server.Dal.InsertTransaction(transaction2entity(*t))
if err != nil {
log.Printf("%v", err)
return ctx.NoContent(http.StatusInternalServerError)
}
return ctx.JSON(http.StatusCreated, entity2transaction(transaction))
}
func (server *ServerImpl) UpdateTransaction(ctx echo.Context) error {
t := new(Transaction)
if err := ctx.Bind(t); err != nil {
log.Printf("%v", err)
return ctx.NoContent(http.StatusBadRequest)
}
transaction := transaction2entity(*t)
exists, err := server.Dal.TransactionExists(transaction.Id)
if err != nil {
log.Printf("%v", err)
return ctx.NoContent(http.StatusInternalServerError)
}
if exists {
transaction, err := server.Dal.UpdateTransaction(transaction)
if err != nil {
log.Printf("%v", err)
return ctx.NoContent(http.StatusInternalServerError)
}
return ctx.JSON(http.StatusOK, entity2transaction(transaction))
} else {
return ctx.NoContent(http.StatusNotFound)
}
}