Add currency/all endpoint and handler

This commit is contained in:
qowevisa 2024-11-20 20:29:00 +02:00
parent 79eb50f375
commit 40ec90687a
2 changed files with 45 additions and 0 deletions

View File

@ -127,6 +127,10 @@ func main() {
{
paymentRoutes.POST("/add", handlers.PaymentAdd)
}
currencyRoutes := api.Group("/currency", middleware.AuthMiddleware())
{
currencyRoutes.GET("/all", handlers.CurrencyGetAll)
}
}
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

41
handlers/currency.go Normal file
View File

@ -0,0 +1,41 @@
package handlers
import (
"git.qowevisa.me/Qowevisa/fin-check-api/db"
"git.qowevisa.me/Qowevisa/fin-check-api/types"
"github.com/gin-gonic/gin"
)
var currencyTransform func(inp *db.Currency) types.DbCurrency = func(inp *db.Currency) types.DbCurrency {
return types.DbCurrency{
ID: inp.ID,
Name: inp.Name,
ISOName: inp.ISOName,
Symbol: inp.Symbol,
}
}
// @Summary Get all currencies for user
// @Description Get all currencies for user
// @Tags type
// @Produce json
// @Param Authorization header string true "Bearer token"
// @Success 200 {object} []types.DbCurrency
// @Failure 401 {object} types.ErrorResponse
// @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth
// @Router /currency/all [get]
func CurrencyGetAll(c *gin.Context) {
dbc := db.Connect()
var entities []*db.Currency
if err := dbc.Find(&entities).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
var ret []types.DbCurrency
for _, entity := range entities {
ret = append(ret, currencyTransform(entity))
}
c.JSON(200, ret)
}