Luís Murta a52bca5882
Adapt API implementation with DAL interface
Swap direct access to the DB on the API server with an data abstraction
layer.
Implement each API type converter separately and revert changes to the
auto-generated server implementation types.

Add error return to DAL methods. Implement `Transactions`.

Add tools.go with oapi-codegen and mockgen.
https://www.jvt.me/posts/2022/06/15/go-tools-dependency-management/

Update go packages.

Issues #5, #12
2024-05-12 22:16:24 +01:00

47 lines
1.2 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 (*ServerImpl) GetBanks(ctx echo.Context) error {
return echo.NewHTTPError(http.StatusNotImplemented)
}
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 (*ServerImpl) GetTransactionById(ctx echo.Context, transactionId int64) error {
return echo.NewHTTPError(http.StatusNotImplemented)
}