Compare commits

...

10 Commits

8 changed files with 264 additions and 200 deletions

View File

@ -1,11 +1,14 @@
package main
import (
"fmt"
"os"
"time"
"github.com/swaggo/files"
"github.com/swaggo/gin-swagger"
"git.qowevisa.me/Qowevisa/fin-check-api/db"
docs "git.qowevisa.me/Qowevisa/fin-check-api/docs"
"git.qowevisa.me/Qowevisa/fin-check-api/handlers"
"git.qowevisa.me/Qowevisa/fin-check-api/middleware"
@ -29,6 +32,10 @@ import (
// @host gonapi.qowevisa.click
// @BasePath /api
func main() {
if err := db.Init(); err != nil {
fmt.Printf("ERROR: db.Init: %v\n", err)
os.Exit(1)
}
r := gin.Default()
docs.SwaggerInfo.BasePath = "/api"
r.Use(cors.New(cors.Config{
@ -77,6 +84,7 @@ func main() {
{
incomeRoutes.POST("/add", handlers.IncomeAdd)
incomeRoutes.GET("/:id", handlers.IncomeGetId)
incomeRoutes.GET("/all", handlers.IncomeGetAll)
incomeRoutes.PUT("/edit/:id", handlers.IncomePutId)
incomeRoutes.DELETE("/delete/:id", handlers.IncomeDeleteId)
}
@ -111,6 +119,10 @@ func main() {
itemRoutes.POST("/filter", handlers.ItemPostFilter)
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))

View File

@ -2,6 +2,7 @@ package db
import (
"errors"
"fmt"
"log"
"os"
"sync"
@ -19,6 +20,15 @@ var (
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 {
conMu.Lock()
defer conMu.Unlock()
@ -59,5 +69,48 @@ func Connect() *gorm.DB {
gormDB.AutoMigrate(&Type{})
gormDB.AutoMigrate(&Session{})
gormDB.AutoMigrate(&Expense{})
gormDB.AutoMigrate(&Metric{})
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
}

View File

@ -85,7 +85,7 @@ func (e *Expense) BeforeUpdate(tx *gorm.DB) (err error) {
if oldCard.UserID != e.UserID {
return ERROR_EXPENSE_INVALID_USERID
}
oldCard.Balance += e.Value
oldCard.Balance += original.Value
if err := tx.Save(oldCard).Error; err != nil {
return err
}

View File

@ -1,6 +1,7 @@
package db
import (
"errors"
"time"
"gorm.io/gorm"
@ -16,3 +17,93 @@ type Income struct {
UserID uint
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
View File

@ -0,0 +1,10 @@
package db
import "gorm.io/gorm"
type Metric struct {
gorm.Model
Value uint8
Name string
Short string
}

View File

@ -1,15 +1,21 @@
package handlers
import (
"fmt"
"strconv"
"git.qowevisa.me/Qowevisa/fin-check-api/db"
"git.qowevisa.me/Qowevisa/fin-check-api/types"
"git.qowevisa.me/Qowevisa/fin-check-api/utils"
"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
// @Description Get income by id
// @Tags income
@ -24,51 +30,35 @@ import (
// @Security ApiKeyAuth
// @Router /income/:id [get]
func IncomeGetId(c *gin.Context) {
userIDAny, exists := c.Get("UserID")
if !exists {
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
return
GetHandler(incomeTransform)(c)
}
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 {
// @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
}
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."})
dbc := db.Connect()
var entities []*db.Income
if err := dbc.Find(&entities, db.Income{UserID: userID}).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,
var ret []types.DbIncome
for _, entity := range entities {
ret = append(ret, incomeTransform(entity))
}
c.JSON(200, ret)
}
@ -87,52 +77,15 @@ func IncomeGetId(c *gin.Context) {
// @Security ApiKeyAuth
// @Router /income/add [post]
func IncomeAdd(c *gin.Context) {
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
}
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)
CreateHandler(
&db.Income{},
func(src types.DbIncome, dst *db.Income) {
dst.CardID = src.CardID
dst.Value = src.Value
dst.Comment = src.Comment
dst.Date = src.Date
},
)(c)
}
// @Summary Edit income by id
@ -150,65 +103,15 @@ func IncomeAdd(c *gin.Context) {
// @Security ApiKeyAuth
// @Router /income/edit/:id [put]
func IncomePutId(c *gin.Context) {
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
}
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)
UpdateHandler(
func(src types.DbIncome, dst *db.Income) {
dst.CardID = src.CardID
dst.Value = src.Value
dst.Comment = src.Comment
dst.Date = src.Date
},
incomeTransform,
)(c)
}
// @Summary Delete income by id
@ -225,55 +128,5 @@ func IncomePutId(c *gin.Context) {
// @Security ApiKeyAuth
// @Router /income/delete/:id [delete]
func IncomeDeleteId(c *gin.Context) {
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)
DeleteHandler[*db.Income]()(c)
}

40
handlers/metric.go Normal file
View 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)
}

View File

@ -56,7 +56,6 @@ type DbIncome struct {
Comment string `json:"comment" example:"pizza"`
Value uint64 `json:"value" example:"20000"`
Date time.Time `json:"date" example:"29/11/2001 12:00"`
UserID uint `json:"user_id" example:"1"`
}
type DbType struct {
@ -117,3 +116,9 @@ type DbItemSearch struct {
CategoryID uint `json:"category_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"`
}