Compare commits
No commits in common. "e1a46f5fc7895cd6403e49d0cb88f2190a19f782" and "cb60cb39d5e1865d99482509f0c29b7562defc3f" have entirely different histories.
e1a46f5fc7
...
cb60cb39d5
|
@ -127,10 +127,6 @@ func main() {
|
||||||
{
|
{
|
||||||
paymentRoutes.POST("/add", handlers.PaymentAdd)
|
paymentRoutes.POST("/add", handlers.PaymentAdd)
|
||||||
}
|
}
|
||||||
currencyRoutes := api.Group("/currency", middleware.AuthMiddleware())
|
|
||||||
{
|
|
||||||
currencyRoutes.GET("/all", handlers.CurrencyGetAll)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||||
|
|
13
db/card.go
13
db/card.go
|
@ -10,12 +10,9 @@ import (
|
||||||
type Card struct {
|
type Card struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
Name string
|
Name string
|
||||||
LastDigits string
|
|
||||||
Balance uint64
|
Balance uint64
|
||||||
HaveCreditLine bool
|
HaveCreditLine bool
|
||||||
CreditLine uint64
|
CreditLine uint64
|
||||||
CurrencyID uint
|
|
||||||
Currency *Currency
|
|
||||||
UserID uint
|
UserID uint
|
||||||
User *User
|
User *User
|
||||||
}
|
}
|
||||||
|
@ -38,7 +35,6 @@ func (c *Card) SetUserID(id uint) {
|
||||||
var (
|
var (
|
||||||
ERROR_CARD_NAME_EMPTY = errors.New("The 'Name' field for 'Card' cannot be empty")
|
ERROR_CARD_NAME_EMPTY = errors.New("The 'Name' field for 'Card' cannot be empty")
|
||||||
ERROR_CARD_NAME_NOT_UNIQUE = errors.New("The 'Name' field for 'Card' have to be unique for user")
|
ERROR_CARD_NAME_NOT_UNIQUE = errors.New("The 'Name' field for 'Card' have to be unique for user")
|
||||||
ERROR_CARD_CANT_FIND_CURR = errors.New("The 'CurrencyID' field for 'Card' is invalid")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Card) BeforeSave(tx *gorm.DB) error {
|
func (c *Card) BeforeSave(tx *gorm.DB) error {
|
||||||
|
@ -54,15 +50,6 @@ func (c *Card) BeforeSave(tx *gorm.DB) error {
|
||||||
if c.ID != dup.ID && dup.ID != 0 {
|
if c.ID != dup.ID && dup.ID != 0 {
|
||||||
return ERROR_CARD_NAME_NOT_UNIQUE
|
return ERROR_CARD_NAME_NOT_UNIQUE
|
||||||
}
|
}
|
||||||
if c.CurrencyID != 0 {
|
|
||||||
var currency Currency
|
|
||||||
if err := tx.Find(¤cy, c.CurrencyID).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if currency.ID == 0 {
|
|
||||||
return ERROR_CARD_CANT_FIND_CURR
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
package db
|
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
|
||||||
|
|
||||||
type Currency struct {
|
|
||||||
gorm.Model
|
|
||||||
Name string
|
|
||||||
ISOName string
|
|
||||||
Symbol string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c Currency) GetID() uint {
|
|
||||||
return c.ID
|
|
||||||
}
|
|
64
db/db.go
64
db/db.go
|
@ -70,8 +70,6 @@ func Connect() *gorm.DB {
|
||||||
gormDB.AutoMigrate(&Session{})
|
gormDB.AutoMigrate(&Session{})
|
||||||
gormDB.AutoMigrate(&Expense{})
|
gormDB.AutoMigrate(&Expense{})
|
||||||
gormDB.AutoMigrate(&Metric{})
|
gormDB.AutoMigrate(&Metric{})
|
||||||
gormDB.AutoMigrate(&Currency{})
|
|
||||||
gormDB.AutoMigrate(&ExchangeRate{})
|
|
||||||
return newUDB
|
return newUDB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,62 +77,40 @@ var (
|
||||||
CANT_FIND_METRIC = errors.New("Can't find proper metrics in database")
|
CANT_FIND_METRIC = errors.New("Can't find proper metrics in database")
|
||||||
)
|
)
|
||||||
|
|
||||||
func checkSeededValues[T Identifiable](whatToCheck []*T, errorIfNotFound error, tx *gorm.DB) error {
|
func initMetrics(tx *gorm.DB) error {
|
||||||
var valuesInDB []T
|
var metrics []Metric
|
||||||
if err := tx.Find(&valuesInDB).Error; err != nil {
|
if err := tx.Find(&metrics).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(valuesInDB) == 0 {
|
|
||||||
for _, v := range whatToCheck {
|
|
||||||
if err := tx.Create(v).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
for _, v := range whatToCheck {
|
|
||||||
var tmp T
|
|
||||||
if err := tx.Find(&tmp, v).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if tmp.GetID() == 0 {
|
|
||||||
return errorIfNotFound
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func initMetrics(tx *gorm.DB) error {
|
|
||||||
metricsThatNeeded := []*Metric{
|
metricsThatNeeded := []*Metric{
|
||||||
&Metric{Name: "None", Short: "pcs", Value: 0},
|
&Metric{Name: "None", Short: "pcs", Value: 0},
|
||||||
&Metric{Name: "Gram", Short: "g", Value: 1},
|
&Metric{Name: "Gram", Short: "g", Value: 1},
|
||||||
&Metric{Name: "Kilogram", Short: "kg", Value: 2},
|
&Metric{Name: "Kilogram", Short: "kg", Value: 2},
|
||||||
&Metric{Name: "Liter", Short: "l", Value: 3},
|
&Metric{Name: "Liter", Short: "l", Value: 3},
|
||||||
}
|
}
|
||||||
return checkSeededValues(metricsThatNeeded, CANT_FIND_METRIC, tx)
|
if len(metrics) == 0 {
|
||||||
}
|
for _, m := range metricsThatNeeded {
|
||||||
|
if err := tx.Create(m).Error; err != nil {
|
||||||
func initCurrencies(tx *gorm.DB) error {
|
return err
|
||||||
currsThatNeeded := []*Currency{
|
}
|
||||||
{Name: "Dollar", Symbol: "$", ISOName: "USD"},
|
}
|
||||||
{Name: "Moldavian Leu", Symbol: "L", ISOName: "MDL"},
|
return nil
|
||||||
{Name: "Romanian Leu", Symbol: "RL", ISOName: "RON"},
|
|
||||||
{Name: "Polish Zloty", Symbol: "zł", ISOName: "PLN"},
|
|
||||||
{Name: "Ukrainian Hryvnia", Symbol: "₴", ISOName: "UAH"},
|
|
||||||
{Name: "Euro", Symbol: "€", ISOName: "EUR"},
|
|
||||||
{Name: "Russian Ruble", Symbol: "₽", ISOName: "RUB"},
|
|
||||||
{Name: "Kazakhstani Tenge", Symbol: "₸", ISOName: "KZT"},
|
|
||||||
{Name: "Chinese Yuan", Symbol: "¥", ISOName: "CNY"},
|
|
||||||
}
|
}
|
||||||
return checkSeededValues(currsThatNeeded, CANT_FIND_METRIC, tx)
|
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 {
|
func initStateOfDb(tx *gorm.DB) error {
|
||||||
if err := initMetrics(tx); err != nil {
|
if err := initMetrics(tx); err != nil {
|
||||||
return fmt.Errorf("initMetrics: %w", err)
|
return fmt.Errorf("initMetrics: %w", err)
|
||||||
}
|
}
|
||||||
if err := initCurrencies(tx); err != nil {
|
|
||||||
return fmt.Errorf("initCurrencies: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
package db
|
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
|
||||||
|
|
||||||
type ExchangeRate struct {
|
|
||||||
gorm.Model
|
|
||||||
FromCurrID uint
|
|
||||||
FromCurr *Currency
|
|
||||||
From uint64
|
|
||||||
ToCurrID uint
|
|
||||||
ToCurr *Currency
|
|
||||||
To uint64
|
|
||||||
Rate uint64
|
|
||||||
}
|
|
|
@ -2,6 +2,7 @@ package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
@ -42,6 +43,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e *Expense) BeforeCreate(tx *gorm.DB) error {
|
func (e *Expense) BeforeCreate(tx *gorm.DB) error {
|
||||||
|
log.Printf("BeforeCreate")
|
||||||
card := &Card{}
|
card := &Card{}
|
||||||
if err := tx.Find(card, e.CardID).Error; err != nil {
|
if err := tx.Find(card, e.CardID).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -61,6 +63,9 @@ func (e *Expense) BeforeCreate(tx *gorm.DB) error {
|
||||||
if err := tx.Find(typ, e.TypeID).Error; err != nil {
|
if err := tx.Find(typ, e.TypeID).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
log.Printf("e.UserID = %d\n", e.UserID)
|
||||||
|
log.Printf("e.TypeID= %d\n", e.TypeID)
|
||||||
|
log.Printf("typ.UserID= %d\n", typ.UserID)
|
||||||
if typ.UserID != e.UserID {
|
if typ.UserID != e.UserID {
|
||||||
return ERROR_EXPENSE_INVALID_TYPE_USERID
|
return ERROR_EXPENSE_INVALID_TYPE_USERID
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package db
|
||||||
|
|
||||||
type Identifiable interface {
|
type Identifiable interface {
|
||||||
GetID() uint
|
GetID() uint
|
||||||
|
SetID(id uint)
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserIdentifiable interface {
|
type UserIdentifiable interface {
|
||||||
|
|
|
@ -8,7 +8,3 @@ type Metric struct {
|
||||||
Name string
|
Name string
|
||||||
Short string
|
Short string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Metric) GetID() uint {
|
|
||||||
return m.ID
|
|
||||||
}
|
|
||||||
|
|
|
@ -13,8 +13,6 @@ var cardTransform func(inp *db.Card) types.DbCard = func(inp *db.Card) types.DbC
|
||||||
Balance: inp.Balance,
|
Balance: inp.Balance,
|
||||||
HaveCreditLine: inp.HaveCreditLine,
|
HaveCreditLine: inp.HaveCreditLine,
|
||||||
CreditLine: inp.CreditLine,
|
CreditLine: inp.CreditLine,
|
||||||
LastDigits: inp.LastDigits,
|
|
||||||
CurrencyID: inp.CurrencyID,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,8 +82,6 @@ func CardAdd(c *gin.Context) {
|
||||||
dst.Balance = src.Balance
|
dst.Balance = src.Balance
|
||||||
dst.HaveCreditLine = src.HaveCreditLine
|
dst.HaveCreditLine = src.HaveCreditLine
|
||||||
dst.CreditLine = src.CreditLine
|
dst.CreditLine = src.CreditLine
|
||||||
dst.LastDigits = src.LastDigits
|
|
||||||
dst.CurrencyID = src.CurrencyID
|
|
||||||
})(c)
|
})(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,8 +107,6 @@ func CardPutId(c *gin.Context) {
|
||||||
dst.Balance = src.Balance
|
dst.Balance = src.Balance
|
||||||
dst.CreditLine = src.CreditLine
|
dst.CreditLine = src.CreditLine
|
||||||
dst.HaveCreditLine = src.HaveCreditLine
|
dst.HaveCreditLine = src.HaveCreditLine
|
||||||
dst.LastDigits = src.LastDigits
|
|
||||||
dst.CurrencyID = src.CurrencyID
|
|
||||||
},
|
},
|
||||||
cardTransform)(c)
|
cardTransform)(c)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,41 +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 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)
|
|
||||||
}
|
|
|
@ -29,8 +29,6 @@ type DbCard struct {
|
||||||
Balance uint64 `json:"balance" example:"1000"`
|
Balance uint64 `json:"balance" example:"1000"`
|
||||||
HaveCreditLine bool `json:"have_credit_line" example:"true"`
|
HaveCreditLine bool `json:"have_credit_line" example:"true"`
|
||||||
CreditLine uint64 `json:"credit_line" example:"500000"`
|
CreditLine uint64 `json:"credit_line" example:"500000"`
|
||||||
LastDigits string `json:"last_digits" example:"1111"`
|
|
||||||
CurrencyID uint `json:"currency_id" example:"1"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type DbCategory struct {
|
type DbCategory struct {
|
||||||
|
@ -115,13 +113,6 @@ type DbMetric struct {
|
||||||
Short string `json:"short" example:"kg"`
|
Short string `json:"short" example:"kg"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DbCurrency struct {
|
|
||||||
ID uint `json:"id" example:"1"`
|
|
||||||
Name string `json:"name" example:"Dollar"`
|
|
||||||
ISOName string `json:"iso_name" example:"USD"`
|
|
||||||
Symbol string `json:"symbol" example:"$"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Payment struct {
|
type Payment struct {
|
||||||
ID uint `json:"id" example:"1"`
|
ID uint `json:"id" example:"1"`
|
||||||
CardID uint `json:"card_id" example:"1"`
|
CardID uint `json:"card_id" example:"1"`
|
||||||
|
|
Loading…
Reference in New Issue
Block a user