2024-05-18 23:18:30 +01:00

70 lines
1.7 KiB
Go

package api
import (
"log"
"net/http"
"git.rosemyrtle.work/personal-finance/server/internal/dal"
"github.com/labstack/echo/v4"
)
//go:generate oapi-codegen --config=api.cfg.yaml ../../docs/openapi.yaml
type ServerImpl struct {
Dal dal.DAL
}
func (pf *ServerImpl) GetBanks(ctx echo.Context) error {
log.Printf("GetBanks")
banks, err := pf.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 (*ServerImpl) GetBankById(ctx echo.Context, bankId int64) error {
return echo.NewHTTPError(http.StatusNotImplemented)
}
func (pf *ServerImpl) GetTransactions(ctx echo.Context, params GetTransactionsParams) error {
if pf.Dal == nil {
log.Panic("database not available")
}
// rows, err := pf.Dal.Query("SELECT tc.name, t.date, t.description, t.id, t.amount FROM pfbudget.transactions t LEFT JOIN pfbudget.transactions_categorized tc ON t.id = tc.id")
transactions, err := pf.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 (pf *ServerImpl) GetTransactionById(ctx echo.Context, transactionId int64) error {
log.Printf("GetTransactionById(%d)", transactionId)
transaction, err := pf.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, convertTransaction(*transaction))
}