Compare commits
10 Commits
ead0a4bd60
...
a52b108bf8
Author | SHA1 | Date | |
---|---|---|---|
a52b108bf8 | |||
f4342ba019 | |||
8855cc3e54 | |||
06b317511d | |||
83f69a2135 | |||
9f7d93ed19 | |||
21afd95d98 | |||
78d420e1d2 | |||
602b3da13b | |||
b9cfa2004a |
|
@ -1,11 +1,14 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/swaggo/files"
|
"github.com/swaggo/files"
|
||||||
"github.com/swaggo/gin-swagger"
|
"github.com/swaggo/gin-swagger"
|
||||||
|
|
||||||
|
"git.qowevisa.me/Qowevisa/fin-check-api/db"
|
||||||
docs "git.qowevisa.me/Qowevisa/fin-check-api/docs"
|
docs "git.qowevisa.me/Qowevisa/fin-check-api/docs"
|
||||||
"git.qowevisa.me/Qowevisa/fin-check-api/handlers"
|
"git.qowevisa.me/Qowevisa/fin-check-api/handlers"
|
||||||
"git.qowevisa.me/Qowevisa/fin-check-api/middleware"
|
"git.qowevisa.me/Qowevisa/fin-check-api/middleware"
|
||||||
|
@ -29,6 +32,10 @@ import (
|
||||||
// @host gonapi.qowevisa.click
|
// @host gonapi.qowevisa.click
|
||||||
// @BasePath /api
|
// @BasePath /api
|
||||||
func main() {
|
func main() {
|
||||||
|
if err := db.Init(); err != nil {
|
||||||
|
fmt.Printf("ERROR: db.Init: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
docs.SwaggerInfo.BasePath = "/api"
|
docs.SwaggerInfo.BasePath = "/api"
|
||||||
r.Use(cors.New(cors.Config{
|
r.Use(cors.New(cors.Config{
|
||||||
|
@ -77,6 +84,7 @@ func main() {
|
||||||
{
|
{
|
||||||
incomeRoutes.POST("/add", handlers.IncomeAdd)
|
incomeRoutes.POST("/add", handlers.IncomeAdd)
|
||||||
incomeRoutes.GET("/:id", handlers.IncomeGetId)
|
incomeRoutes.GET("/:id", handlers.IncomeGetId)
|
||||||
|
incomeRoutes.GET("/all", handlers.IncomeGetAll)
|
||||||
incomeRoutes.PUT("/edit/:id", handlers.IncomePutId)
|
incomeRoutes.PUT("/edit/:id", handlers.IncomePutId)
|
||||||
incomeRoutes.DELETE("/delete/:id", handlers.IncomeDeleteId)
|
incomeRoutes.DELETE("/delete/:id", handlers.IncomeDeleteId)
|
||||||
}
|
}
|
||||||
|
@ -111,6 +119,10 @@ func main() {
|
||||||
itemRoutes.POST("/filter", handlers.ItemPostFilter)
|
itemRoutes.POST("/filter", handlers.ItemPostFilter)
|
||||||
itemRoutes.DELETE("/delete/:id", handlers.ItemDeleteId)
|
itemRoutes.DELETE("/delete/:id", handlers.ItemDeleteId)
|
||||||
}
|
}
|
||||||
|
metricRoutes := api.Group("/metric", middleware.AuthMiddleware())
|
||||||
|
{
|
||||||
|
metricRoutes.GET("/all", handlers.MetricGetAll)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||||
|
|
53
db/db.go
53
db/db.go
|
@ -2,6 +2,7 @@ package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -19,6 +20,15 @@ var (
|
||||||
ERROR_DB_NOT_INIT = errors.New("Database connection is not initialized")
|
ERROR_DB_NOT_INIT = errors.New("Database connection is not initialized")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Init() error {
|
||||||
|
dbc := Connect()
|
||||||
|
// Seeds
|
||||||
|
if err := initStateOfDb(dbc); err != nil {
|
||||||
|
return fmt.Errorf("initStateOfDb: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func Connect() *gorm.DB {
|
func Connect() *gorm.DB {
|
||||||
conMu.Lock()
|
conMu.Lock()
|
||||||
defer conMu.Unlock()
|
defer conMu.Unlock()
|
||||||
|
@ -59,5 +69,48 @@ func Connect() *gorm.DB {
|
||||||
gormDB.AutoMigrate(&Type{})
|
gormDB.AutoMigrate(&Type{})
|
||||||
gormDB.AutoMigrate(&Session{})
|
gormDB.AutoMigrate(&Session{})
|
||||||
gormDB.AutoMigrate(&Expense{})
|
gormDB.AutoMigrate(&Expense{})
|
||||||
|
gormDB.AutoMigrate(&Metric{})
|
||||||
return newUDB
|
return newUDB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
CANT_FIND_METRIC = errors.New("Can't find proper metrics in database")
|
||||||
|
)
|
||||||
|
|
||||||
|
func initMetrics(tx *gorm.DB) error {
|
||||||
|
var metrics []Metric
|
||||||
|
if err := tx.Find(&metrics).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
metricsThatNeeded := []*Metric{
|
||||||
|
&Metric{Name: "None", Short: "pcs", Value: 0},
|
||||||
|
&Metric{Name: "Gram", Short: "g", Value: 1},
|
||||||
|
&Metric{Name: "Kilogram", Short: "kg", Value: 2},
|
||||||
|
&Metric{Name: "Liter", Short: "l", Value: 3},
|
||||||
|
}
|
||||||
|
if len(metrics) == 0 {
|
||||||
|
for _, m := range metricsThatNeeded {
|
||||||
|
if err := tx.Create(m).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, m := range metricsThatNeeded {
|
||||||
|
tmp := &Metric{}
|
||||||
|
if err := tx.Find(tmp, m).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if tmp.ID == 0 {
|
||||||
|
return CANT_FIND_METRIC
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func initStateOfDb(tx *gorm.DB) error {
|
||||||
|
if err := initMetrics(tx); err != nil {
|
||||||
|
return fmt.Errorf("initMetrics: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -85,7 +85,7 @@ func (e *Expense) BeforeUpdate(tx *gorm.DB) (err error) {
|
||||||
if oldCard.UserID != e.UserID {
|
if oldCard.UserID != e.UserID {
|
||||||
return ERROR_EXPENSE_INVALID_USERID
|
return ERROR_EXPENSE_INVALID_USERID
|
||||||
}
|
}
|
||||||
oldCard.Balance += e.Value
|
oldCard.Balance += original.Value
|
||||||
if err := tx.Save(oldCard).Error; err != nil {
|
if err := tx.Save(oldCard).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
91
db/income.go
91
db/income.go
|
@ -1,6 +1,7 @@
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
@ -16,3 +17,93 @@ type Income struct {
|
||||||
UserID uint
|
UserID uint
|
||||||
User *User
|
User *User
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Implements db.UserIdentifiable:1
|
||||||
|
func (e Income) GetID() uint {
|
||||||
|
return e.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implements db.UserIdentifiable:2
|
||||||
|
func (e Income) GetUserID() uint {
|
||||||
|
return e.UserID
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implements db.UserIdentifiable:3
|
||||||
|
func (e *Income) SetUserID(id uint) {
|
||||||
|
e.UserID = id
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ERROR_INCOME_INVALID_USERID = errors.New("Income's `UserID` and Card's `UserID` are not equal")
|
||||||
|
ERROR_INCOME_CARD_INSUFFICIENT_BALANCE = errors.New("Card's `Balance` is lower than Income's Value")
|
||||||
|
ERROR_INCOME_INVALID_TYPE_USERID = errors.New("Income's `UserID` and Type's `UserID` are not equal")
|
||||||
|
)
|
||||||
|
|
||||||
|
func (i *Income) BeforeCreate(tx *gorm.DB) error {
|
||||||
|
card := &Card{}
|
||||||
|
if err := tx.Find(card, i.CardID).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if card.UserID != i.UserID {
|
||||||
|
return ERROR_INCOME_INVALID_USERID
|
||||||
|
}
|
||||||
|
card.Balance += i.Value
|
||||||
|
if err := tx.Save(card).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Income) BeforeUpdate(tx *gorm.DB) (err error) {
|
||||||
|
var original Income
|
||||||
|
if err := tx.Model(&Income{}).Select("card_id", "value").Where("id = ?", i.ID).First(&original).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if original.CardID != 0 {
|
||||||
|
oldCard := &Card{}
|
||||||
|
if err := tx.Find(oldCard, original.CardID).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if oldCard.UserID != i.UserID {
|
||||||
|
return ERROR_INCOME_INVALID_USERID
|
||||||
|
}
|
||||||
|
if oldCard.Balance < original.Value {
|
||||||
|
return ERROR_INCOME_CARD_INSUFFICIENT_BALANCE
|
||||||
|
}
|
||||||
|
oldCard.Balance -= original.Value
|
||||||
|
if err := tx.Save(oldCard).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if i.CardID != 0 {
|
||||||
|
newCard := &Card{}
|
||||||
|
if err := tx.Find(newCard, i.CardID).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if newCard.UserID != i.UserID {
|
||||||
|
return ERROR_INCOME_INVALID_USERID
|
||||||
|
}
|
||||||
|
newCard.Balance += i.Value
|
||||||
|
if err := tx.Save(newCard).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Income) AfterDelete(tx *gorm.DB) (err error) {
|
||||||
|
card := &Card{}
|
||||||
|
if err := tx.Find(card, e.CardID).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if card.UserID != e.UserID {
|
||||||
|
return ERROR_INCOME_INVALID_USERID
|
||||||
|
}
|
||||||
|
card.Balance -= e.Value
|
||||||
|
if err := tx.Save(card).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
10
db/metric.go
Normal file
10
db/metric.go
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
package db
|
||||||
|
|
||||||
|
import "gorm.io/gorm"
|
||||||
|
|
||||||
|
type Metric struct {
|
||||||
|
gorm.Model
|
||||||
|
Value uint8
|
||||||
|
Name string
|
||||||
|
Short string
|
||||||
|
}
|
|
@ -1,15 +1,21 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"git.qowevisa.me/Qowevisa/fin-check-api/db"
|
"git.qowevisa.me/Qowevisa/fin-check-api/db"
|
||||||
"git.qowevisa.me/Qowevisa/fin-check-api/types"
|
"git.qowevisa.me/Qowevisa/fin-check-api/types"
|
||||||
"git.qowevisa.me/Qowevisa/fin-check-api/utils"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var incomeTransform func(inp *db.Income) types.DbIncome = func(inp *db.Income) types.DbIncome {
|
||||||
|
return types.DbIncome{
|
||||||
|
ID: inp.ID,
|
||||||
|
CardID: inp.CardID,
|
||||||
|
Comment: inp.Comment,
|
||||||
|
Value: inp.Value,
|
||||||
|
Date: inp.Date,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// @Summary Get income by id
|
// @Summary Get income by id
|
||||||
// @Description Get income by id
|
// @Description Get income by id
|
||||||
// @Tags income
|
// @Tags income
|
||||||
|
@ -24,51 +30,35 @@ import (
|
||||||
// @Security ApiKeyAuth
|
// @Security ApiKeyAuth
|
||||||
// @Router /income/:id [get]
|
// @Router /income/:id [get]
|
||||||
func IncomeGetId(c *gin.Context) {
|
func IncomeGetId(c *gin.Context) {
|
||||||
userIDAny, exists := c.Get("UserID")
|
GetHandler(incomeTransform)(c)
|
||||||
if !exists {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var userID uint
|
// @Summary Get all incomes for user
|
||||||
if userIDVal, ok := userIDAny.(uint); !ok {
|
// @Description Get all incomes for user
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
|
// @Tags type
|
||||||
return
|
// @Produce json
|
||||||
} else {
|
// @Param Authorization header string true "Bearer token"
|
||||||
userID = userIDVal
|
// @Success 200 {object} []types.DbIncome
|
||||||
}
|
// @Failure 401 {object} types.ErrorResponse
|
||||||
|
// @Failure 500 {object} types.ErrorResponse
|
||||||
idStr := c.Param("id")
|
// @Security ApiKeyAuth
|
||||||
var id uint
|
// @Router /income/all [get]
|
||||||
if idVal, err := strconv.ParseUint(idStr, 10, 32); err != nil {
|
func IncomeGetAll(c *gin.Context) {
|
||||||
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
|
userID, err := GetUserID(c)
|
||||||
return
|
if err != nil {
|
||||||
} else {
|
|
||||||
id = uint(idVal)
|
|
||||||
}
|
|
||||||
|
|
||||||
var dbIncome db.Income
|
|
||||||
dbc := db.Connect()
|
|
||||||
if err := dbc.Find(&dbIncome, id).Error; err != nil {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if dbIncome.ID == 0 {
|
dbc := db.Connect()
|
||||||
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
|
var entities []*db.Income
|
||||||
return
|
if err := dbc.Find(&entities, db.Income{UserID: userID}).Error; err != nil {
|
||||||
}
|
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
||||||
if dbIncome.UserID != userID {
|
|
||||||
c.JSON(401, types.ErrorResponse{Message: "This income.id is not yours, you sneaky."})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ret := types.DbIncome{
|
var ret []types.DbIncome
|
||||||
ID: dbIncome.ID,
|
for _, entity := range entities {
|
||||||
CardID: dbIncome.CardID,
|
ret = append(ret, incomeTransform(entity))
|
||||||
Comment: dbIncome.Comment,
|
|
||||||
Value: dbIncome.Value,
|
|
||||||
Date: dbIncome.Date,
|
|
||||||
UserID: dbIncome.UserID,
|
|
||||||
}
|
}
|
||||||
c.JSON(200, ret)
|
c.JSON(200, ret)
|
||||||
}
|
}
|
||||||
|
@ -87,52 +77,15 @@ func IncomeGetId(c *gin.Context) {
|
||||||
// @Security ApiKeyAuth
|
// @Security ApiKeyAuth
|
||||||
// @Router /income/add [post]
|
// @Router /income/add [post]
|
||||||
func IncomeAdd(c *gin.Context) {
|
func IncomeAdd(c *gin.Context) {
|
||||||
userIDAny, exists := c.Get("UserID")
|
CreateHandler(
|
||||||
if !exists {
|
&db.Income{},
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
|
func(src types.DbIncome, dst *db.Income) {
|
||||||
return
|
dst.CardID = src.CardID
|
||||||
}
|
dst.Value = src.Value
|
||||||
|
dst.Comment = src.Comment
|
||||||
var userID uint
|
dst.Date = src.Date
|
||||||
if userIDVal, ok := userIDAny.(uint); !ok {
|
},
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
|
)(c)
|
||||||
return
|
|
||||||
} else {
|
|
||||||
userID = userIDVal
|
|
||||||
}
|
|
||||||
|
|
||||||
var income types.DbIncome
|
|
||||||
if err := c.ShouldBindJSON(&income); err != nil {
|
|
||||||
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if income.UserID != 0 && userID != income.UserID {
|
|
||||||
c.JSON(403, types.ErrorResponse{Message: "UserID in body is different than yours!"})
|
|
||||||
}
|
|
||||||
if income.UserID == 0 {
|
|
||||||
income.UserID = userID
|
|
||||||
}
|
|
||||||
|
|
||||||
dbIncome := &db.Income{
|
|
||||||
CardID: income.CardID,
|
|
||||||
Comment: income.Comment,
|
|
||||||
Value: income.Value,
|
|
||||||
Date: income.Date,
|
|
||||||
UserID: income.UserID,
|
|
||||||
}
|
|
||||||
dbc := db.Connect()
|
|
||||||
if err := dbc.Create(&dbIncome).Error; err != nil {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if dbIncome.ID == 0 {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "DAFUQ004"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
msg := types.Message{
|
|
||||||
Info: fmt.Sprintf("Income with id %d was successfully created!", dbIncome.ID),
|
|
||||||
}
|
|
||||||
c.JSON(200, msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Edit income by id
|
// @Summary Edit income by id
|
||||||
|
@ -150,65 +103,15 @@ func IncomeAdd(c *gin.Context) {
|
||||||
// @Security ApiKeyAuth
|
// @Security ApiKeyAuth
|
||||||
// @Router /income/edit/:id [put]
|
// @Router /income/edit/:id [put]
|
||||||
func IncomePutId(c *gin.Context) {
|
func IncomePutId(c *gin.Context) {
|
||||||
userIDAny, exists := c.Get("UserID")
|
UpdateHandler(
|
||||||
if !exists {
|
func(src types.DbIncome, dst *db.Income) {
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
|
dst.CardID = src.CardID
|
||||||
return
|
dst.Value = src.Value
|
||||||
}
|
dst.Comment = src.Comment
|
||||||
|
dst.Date = src.Date
|
||||||
var userID uint
|
},
|
||||||
if userIDVal, ok := userIDAny.(uint); !ok {
|
incomeTransform,
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
|
)(c)
|
||||||
return
|
|
||||||
} else {
|
|
||||||
userID = userIDVal
|
|
||||||
}
|
|
||||||
|
|
||||||
idStr := c.Param("id")
|
|
||||||
var id uint
|
|
||||||
if idVal, err := strconv.ParseUint(idStr, 10, 32); err != nil {
|
|
||||||
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
id = uint(idVal)
|
|
||||||
}
|
|
||||||
|
|
||||||
var dbIncome db.Income
|
|
||||||
dbc := db.Connect()
|
|
||||||
if err := dbc.Find(&dbIncome, id).Error; err != nil {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if dbIncome.ID == 0 {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if dbIncome.UserID != userID {
|
|
||||||
c.JSON(401, types.ErrorResponse{Message: "This income.id is not yours, you sneaky."})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var income types.DbIncome
|
|
||||||
if err := c.ShouldBindJSON(&income); err != nil {
|
|
||||||
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
utils.MergeNonZeroFields(income, dbIncome)
|
|
||||||
|
|
||||||
if err := dbc.Save(dbIncome).Error; err != nil {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := types.DbIncome{
|
|
||||||
ID: dbIncome.ID,
|
|
||||||
CardID: dbIncome.CardID,
|
|
||||||
Comment: dbIncome.Comment,
|
|
||||||
Value: dbIncome.Value,
|
|
||||||
Date: dbIncome.Date,
|
|
||||||
UserID: dbIncome.UserID,
|
|
||||||
}
|
|
||||||
c.JSON(200, ret)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Delete income by id
|
// @Summary Delete income by id
|
||||||
|
@ -225,55 +128,5 @@ func IncomePutId(c *gin.Context) {
|
||||||
// @Security ApiKeyAuth
|
// @Security ApiKeyAuth
|
||||||
// @Router /income/delete/:id [delete]
|
// @Router /income/delete/:id [delete]
|
||||||
func IncomeDeleteId(c *gin.Context) {
|
func IncomeDeleteId(c *gin.Context) {
|
||||||
userIDAny, exists := c.Get("UserID")
|
DeleteHandler[*db.Income]()(c)
|
||||||
if !exists {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var userID uint
|
|
||||||
if userIDVal, ok := userIDAny.(uint); !ok {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
userID = userIDVal
|
|
||||||
}
|
|
||||||
|
|
||||||
idStr := c.Param("id")
|
|
||||||
var id uint
|
|
||||||
if idVal, err := strconv.ParseUint(idStr, 10, 32); err != nil {
|
|
||||||
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
id = uint(idVal)
|
|
||||||
}
|
|
||||||
|
|
||||||
var dbIncome db.Income
|
|
||||||
dbc := db.Connect()
|
|
||||||
if err := dbc.Find(&dbIncome, id).Error; err != nil {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if dbIncome.ID == 0 {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if dbIncome.UserID != userID {
|
|
||||||
c.JSON(401, types.ErrorResponse{Message: "This income.id is not yours, you sneaky."})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := dbc.Delete(dbIncome).Error; err != nil {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := types.DbIncome{
|
|
||||||
ID: dbIncome.ID,
|
|
||||||
CardID: dbIncome.CardID,
|
|
||||||
Comment: dbIncome.Comment,
|
|
||||||
Value: dbIncome.Value,
|
|
||||||
Date: dbIncome.Date,
|
|
||||||
UserID: dbIncome.UserID,
|
|
||||||
}
|
|
||||||
c.JSON(200, ret)
|
|
||||||
}
|
}
|
||||||
|
|
40
handlers/metric.go
Normal file
40
handlers/metric.go
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
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 metricTransform func(inp *db.Metric) types.DbMetric = func(inp *db.Metric) types.DbMetric {
|
||||||
|
return types.DbMetric{
|
||||||
|
Value: inp.Value,
|
||||||
|
Name: inp.Name,
|
||||||
|
Short: inp.Short,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Summary Get all metrics for user
|
||||||
|
// @Description Get all metrics for user
|
||||||
|
// @Tags type
|
||||||
|
// @Produce json
|
||||||
|
// @Param Authorization header string true "Bearer token"
|
||||||
|
// @Success 200 {object} []types.DbMetric
|
||||||
|
// @Failure 401 {object} types.ErrorResponse
|
||||||
|
// @Failure 500 {object} types.ErrorResponse
|
||||||
|
// @Security ApiKeyAuth
|
||||||
|
// @Router /metric/all [get]
|
||||||
|
func MetricGetAll(c *gin.Context) {
|
||||||
|
dbc := db.Connect()
|
||||||
|
var entities []*db.Metric
|
||||||
|
if err := dbc.Find(&entities).Error; err != nil {
|
||||||
|
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var ret []types.DbMetric
|
||||||
|
for _, entity := range entities {
|
||||||
|
ret = append(ret, metricTransform(entity))
|
||||||
|
}
|
||||||
|
c.JSON(200, ret)
|
||||||
|
}
|
|
@ -56,7 +56,6 @@ type DbIncome struct {
|
||||||
Comment string `json:"comment" example:"pizza"`
|
Comment string `json:"comment" example:"pizza"`
|
||||||
Value uint64 `json:"value" example:"20000"`
|
Value uint64 `json:"value" example:"20000"`
|
||||||
Date time.Time `json:"date" example:"29/11/2001 12:00"`
|
Date time.Time `json:"date" example:"29/11/2001 12:00"`
|
||||||
UserID uint `json:"user_id" example:"1"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type DbType struct {
|
type DbType struct {
|
||||||
|
@ -117,3 +116,9 @@ type DbItemSearch struct {
|
||||||
CategoryID uint `json:"category_id" example:"1"`
|
CategoryID uint `json:"category_id" example:"1"`
|
||||||
TypeID uint `json:"type_id" example:"1"`
|
TypeID uint `json:"type_id" example:"1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DbMetric struct {
|
||||||
|
Value uint8 `json:"value" example:"1"`
|
||||||
|
Name string `json:"name" example:"Kilogram"`
|
||||||
|
Short string `json:"short" example:"kg"`
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user