78 lines
1.8 KiB
Go
78 lines
1.8 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 (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, convertTransaction(*transaction))
|
|
}
|