Compare commits
11 Commits
cb60cb39d5
...
e1a46f5fc7
Author | SHA1 | Date | |
---|---|---|---|
e1a46f5fc7 | |||
4522d4450f | |||
05b5059477 | |||
40ec90687a | |||
79eb50f375 | |||
f4cad689d8 | |||
5f35b57eb4 | |||
33ffa6275b | |||
d929492e3d | |||
579bb031c7 | |||
d41e6301e5 |
|
@ -127,6 +127,10 @@ 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,9 +10,12 @@ 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
|
||||||
}
|
}
|
||||||
|
@ -35,6 +38,7 @@ 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 {
|
||||||
|
@ -50,6 +54,15 @@ 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
|
||||||
}
|
}
|
||||||
|
|
14
db/currency.go
Normal file
14
db/currency.go
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
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
|
||||||
|
}
|
62
db/db.go
62
db/db.go
|
@ -70,6 +70,8 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,40 +79,62 @@ 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 initMetrics(tx *gorm.DB) error {
|
func checkSeededValues[T Identifiable](whatToCheck []*T, errorIfNotFound error, tx *gorm.DB) error {
|
||||||
var metrics []Metric
|
var valuesInDB []T
|
||||||
if err := tx.Find(&metrics).Error; err != nil {
|
if err := tx.Find(&valuesInDB).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},
|
||||||
}
|
}
|
||||||
if len(metrics) == 0 {
|
return checkSeededValues(metricsThatNeeded, CANT_FIND_METRIC, tx)
|
||||||
for _, m := range metricsThatNeeded {
|
|
||||||
if err := tx.Create(m).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initCurrencies(tx *gorm.DB) error {
|
||||||
|
currsThatNeeded := []*Currency{
|
||||||
|
{Name: "Dollar", Symbol: "$", ISOName: "USD"},
|
||||||
|
{Name: "Moldavian Leu", Symbol: "L", ISOName: "MDL"},
|
||||||
|
{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 nil
|
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
|
||||||
}
|
}
|
||||||
|
|
14
db/exchange_rate.go
Normal file
14
db/exchange_rate.go
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
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,7 +2,6 @@ package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
@ -43,7 +42,6 @@ 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
|
||||||
|
@ -63,9 +61,6 @@ 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,7 +2,6 @@ package db
|
||||||
|
|
||||||
type Identifiable interface {
|
type Identifiable interface {
|
||||||
GetID() uint
|
GetID() uint
|
||||||
SetID(id uint)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserIdentifiable interface {
|
type UserIdentifiable interface {
|
||||||
|
|
|
@ -8,3 +8,7 @@ type Metric struct {
|
||||||
Name string
|
Name string
|
||||||
Short string
|
Short string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m Metric) GetID() uint {
|
||||||
|
return m.ID
|
||||||
|
}
|
||||||
|
|
|
@ -13,6 +13,8 @@ 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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,6 +84,8 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,6 +111,8 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
41
handlers/currency.go
Normal file
41
handlers/currency.go
Normal 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)
|
||||||
|
}
|
|
@ -29,6 +29,8 @@ 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 {
|
||||||
|
@ -113,6 +115,13 @@ 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