Compare commits
No commits in common. "a52b108bf88241f59eae14eb8e5b1d3c3e9d4686" and "ead0a4bd60e9a6a7f992126ce40fc1a9261efad5" have entirely different histories.
a52b108bf8
...
ead0a4bd60
|
@ -1,14 +1,11 @@
|
||||||
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"
|
||||||
|
@ -32,10 +29,6 @@ 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{
|
||||||
|
@ -84,7 +77,6 @@ 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)
|
||||||
}
|
}
|
||||||
|
@ -119,10 +111,6 @@ 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,7 +2,6 @@ package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -20,15 +19,6 @@ 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()
|
||||||
|
@ -69,48 +59,5 @@ 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 += original.Value
|
oldCard.Balance += e.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,7 +1,6 @@
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
@ -17,93 +16,3 @@ 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
10
db/metric.go
|
@ -1,10 +0,0 @@
|
||||||
package db
|
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
|
||||||
|
|
||||||
type Metric struct {
|
|
||||||
gorm.Model
|
|
||||||
Value uint8
|
|
||||||
Name string
|
|
||||||
Short string
|
|
||||||
}
|
|
|
@ -1,21 +1,15 @@
|
||||||
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
|
||||||
|
@ -30,35 +24,51 @@ var incomeTransform func(inp *db.Income) types.DbIncome = func(inp *db.Income) t
|
||||||
// @Security ApiKeyAuth
|
// @Security ApiKeyAuth
|
||||||
// @Router /income/:id [get]
|
// @Router /income/:id [get]
|
||||||
func IncomeGetId(c *gin.Context) {
|
func IncomeGetId(c *gin.Context) {
|
||||||
GetHandler(incomeTransform)(c)
|
userIDAny, exists := c.Get("UserID")
|
||||||
}
|
if !exists {
|
||||||
|
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
|
||||||
// @Summary Get all incomes for user
|
|
||||||
// @Description Get all incomes for user
|
|
||||||
// @Tags type
|
|
||||||
// @Produce json
|
|
||||||
// @Param Authorization header string true "Bearer token"
|
|
||||||
// @Success 200 {object} []types.DbIncome
|
|
||||||
// @Failure 401 {object} types.ErrorResponse
|
|
||||||
// @Failure 500 {object} types.ErrorResponse
|
|
||||||
// @Security ApiKeyAuth
|
|
||||||
// @Router /income/all [get]
|
|
||||||
func IncomeGetAll(c *gin.Context) {
|
|
||||||
userID, err := GetUserID(c)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
||||||
return
|
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()
|
dbc := db.Connect()
|
||||||
var entities []*db.Income
|
if err := dbc.Find(&dbIncome, id).Error; err != nil {
|
||||||
if err := dbc.Find(&entities, db.Income{UserID: userID}).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 {
|
||||||
|
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 ret []types.DbIncome
|
ret := types.DbIncome{
|
||||||
for _, entity := range entities {
|
ID: dbIncome.ID,
|
||||||
ret = append(ret, incomeTransform(entity))
|
CardID: dbIncome.CardID,
|
||||||
|
Comment: dbIncome.Comment,
|
||||||
|
Value: dbIncome.Value,
|
||||||
|
Date: dbIncome.Date,
|
||||||
|
UserID: dbIncome.UserID,
|
||||||
}
|
}
|
||||||
c.JSON(200, ret)
|
c.JSON(200, ret)
|
||||||
}
|
}
|
||||||
|
@ -77,15 +87,52 @@ func IncomeGetAll(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) {
|
||||||
CreateHandler(
|
userIDAny, exists := c.Get("UserID")
|
||||||
&db.Income{},
|
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 {
|
||||||
)(c)
|
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
|
||||||
|
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
|
||||||
|
@ -103,15 +150,65 @@ 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) {
|
||||||
UpdateHandler(
|
userIDAny, exists := c.Get("UserID")
|
||||||
func(src types.DbIncome, dst *db.Income) {
|
if !exists {
|
||||||
dst.CardID = src.CardID
|
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
|
||||||
dst.Value = src.Value
|
return
|
||||||
dst.Comment = src.Comment
|
}
|
||||||
dst.Date = src.Date
|
|
||||||
},
|
var userID uint
|
||||||
incomeTransform,
|
if userIDVal, ok := userIDAny.(uint); !ok {
|
||||||
)(c)
|
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
|
||||||
|
}
|
||||||
|
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
|
||||||
|
@ -128,5 +225,55 @@ 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) {
|
||||||
DeleteHandler[*db.Income]()(c)
|
userIDAny, exists := c.Get("UserID")
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,40 +0,0 @@
|
||||||
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,6 +56,7 @@ 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 {
|
||||||
|
@ -116,9 +117,3 @@ 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