All checks were successful
Lint PR / commitlint (pull_request) Successful in 30s
Go / build (1.23) (pull_request) Successful in 1m5s
Go / build (1.24) (pull_request) Successful in 1m3s
Go / build (1.22) (pull_request) Successful in 1m7s
Go / build (1.22) (push) Successful in 20s
Go / build (1.24) (push) Successful in 16s
Go / build (1.23) (push) Successful in 18s
Used to implement pagination on server-side and for the backend to know what is the last available page.
146 lines
3.7 KiB
Go
146 lines
3.7 KiB
Go
package api
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"git.rosemyrtle.work/personal-finance/server/internal/dal"
|
|
"git.rosemyrtle.work/personal-finance/server/internal/entity"
|
|
"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")
|
|
|
|
limit := 100
|
|
if params.Limit != nil {
|
|
limit = int(*params.Limit)
|
|
}
|
|
|
|
offset := 0
|
|
if params.Offset != nil {
|
|
offset = int(*params.Offset)
|
|
}
|
|
|
|
transactions, totalCount, err := server.Dal.Transactions(limit, offset, params.Category)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(transactions) == 0 {
|
|
return ctx.NoContent(http.StatusNoContent)
|
|
}
|
|
|
|
ctx.Response().Header().Set("X-Total-Count", strconv.FormatUint(uint64(totalCount), 10))
|
|
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, transactionId int64) error {
|
|
if exists, err := server.Dal.TransactionExists(entity.TransactionId(transactionId)); err != nil {
|
|
log.Printf("%v", err)
|
|
return ctx.NoContent(http.StatusInternalServerError)
|
|
} else if !exists {
|
|
return ctx.NoContent(http.StatusNotFound)
|
|
}
|
|
|
|
update := new(UpdateTransactionJSONBody)
|
|
if err := ctx.Bind(update); err != nil {
|
|
log.Printf("%v", err)
|
|
return ctx.NoContent(http.StatusBadRequest)
|
|
}
|
|
|
|
success, err := server.Dal.UpdateTransaction(entity.TransactionId(transactionId), update.Category)
|
|
if err != nil {
|
|
log.Printf("%v", err)
|
|
return ctx.NoContent(http.StatusInternalServerError)
|
|
}
|
|
|
|
if !success {
|
|
return ctx.NoContent(http.StatusBadRequest)
|
|
}
|
|
return ctx.NoContent(http.StatusNoContent)
|
|
}
|
|
|
|
func (server *ServerImpl) GetCategories(ctx echo.Context) error {
|
|
categories, err := server.Dal.Categories()
|
|
if err != nil {
|
|
return ctx.NoContent(http.StatusInternalServerError)
|
|
}
|
|
|
|
if len(categories) == 0 {
|
|
return ctx.NoContent(http.StatusNoContent)
|
|
}
|
|
|
|
return ctx.JSON(http.StatusOK, entities2categories(categories))
|
|
}
|