mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-11-11 07:41:10 +00:00
partial repository layer added
This commit is contained in:
6
pkg/model/api.go
Normal file
6
pkg/model/api.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package model
|
||||
|
||||
type ApiModel struct {
|
||||
tableName struct{} `pg:"api,alias:api"`
|
||||
Api string `json:"api"`
|
||||
}
|
||||
19
pkg/model/auth.go
Normal file
19
pkg/model/auth.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package model
|
||||
|
||||
type Token struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
type Login struct {
|
||||
Email string `json:"email" form:"email"`
|
||||
Password string `json:"password" form:"password"`
|
||||
RememberMe bool `json:"rememberMe" form:"rememberMe"`
|
||||
}
|
||||
|
||||
type Auth struct {
|
||||
Id string
|
||||
}
|
||||
|
||||
type CheckToken struct {
|
||||
Valid bool `json:"valid"`
|
||||
}
|
||||
25
pkg/model/db.go
Normal file
25
pkg/model/db.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type BaseModel struct {
|
||||
Id string `json:"id" pg:"id,pk,notnull"`
|
||||
DateCreated time.Time `json:"dateCreated" pg:"date_created"`
|
||||
DateUpdated time.Time `json:"dateUpdated" pg:"date_updated"`
|
||||
}
|
||||
|
||||
/*
|
||||
Init
|
||||
|
||||
Initializes base model with DateCreated, DateUpdated, and Id values.
|
||||
*/
|
||||
func (cm *BaseModel) Init() {
|
||||
date := time.Now()
|
||||
cm.Id = uuid.NewString()
|
||||
cm.DateCreated = date
|
||||
cm.DateUpdated = date
|
||||
}
|
||||
7
pkg/model/exception.go
Normal file
7
pkg/model/exception.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package model
|
||||
|
||||
type Exception struct {
|
||||
ErrorCode string `json:"errorCode"`
|
||||
Message string `json:"message"`
|
||||
StatusCode int `json:"statusCode"`
|
||||
}
|
||||
22
pkg/model/model.go
Normal file
22
pkg/model/model.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package model
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
type FilteredResponse struct {
|
||||
Items interface{} `json:"items"`
|
||||
Params
|
||||
}
|
||||
|
||||
type ResponseFunc func(*gin.Context) *[]interface{}
|
||||
|
||||
type MessageResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Params struct {
|
||||
SortBy string `json:"sortBy"`
|
||||
Embed string `json:"embed"`
|
||||
Page int `json:"page"`
|
||||
Rpp int `json:"rpp"`
|
||||
TotalRecords int `json:"totalRecords"`
|
||||
}
|
||||
34
pkg/model/register.go
Normal file
34
pkg/model/register.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package model
|
||||
|
||||
type User struct {
|
||||
tableName struct{} `pg:"users,alias:users"`
|
||||
BaseModel
|
||||
IsActive bool `json:"isActive" pg:"is_active" form:"isActive"`
|
||||
Username string `json:"username" pg:"username,notnull" form:"username"`
|
||||
Password string `json:"password" pg:"password,notnull" form:"password"`
|
||||
Email string `json:"email" pg:"email,notnull" form:"email"`
|
||||
}
|
||||
|
||||
type UserReturnInfo struct {
|
||||
tableName struct{} `pg:"users,alias:users"`
|
||||
BaseModel
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
/*
|
||||
Payload
|
||||
|
||||
Maps User object to UserReturnInfo object.
|
||||
|
||||
Returns:
|
||||
*UserReturnInfo: mapped UserReturnInfo object
|
||||
*/
|
||||
func (um *User) Payload() *UserReturnInfo {
|
||||
payload := new(UserReturnInfo)
|
||||
payload.BaseModel = um.BaseModel
|
||||
payload.Username = um.Username
|
||||
payload.Email = um.Email
|
||||
|
||||
return payload
|
||||
}
|
||||
113
pkg/model/subscription.go
Normal file
113
pkg/model/subscription.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Subscription struct {
|
||||
tableName struct{} `pg:"subscriptions,alias:subscriptions"`
|
||||
BaseModel
|
||||
Description string `json:"description" pg:"description"`
|
||||
StartDate time.Time `json:"startDate" pg:"start_date", type:timestamptz`
|
||||
EndDate time.Time `json:"endDate" pg:"end_date", type:timestamptz`
|
||||
HasEnd bool `json:"hasEnd" pg:"hasEnd"`
|
||||
SubscriptionTypeID string `json:"subscriptionTypeId" pg:"subscription_type_id"`
|
||||
SubscriptionType *SubscriptionType `json:"subscriptionType", pg:"rel:has-one, fk:subscription_type_id"`
|
||||
CustomRange int `json:"customRange", pg:"custom_range,default:1"`
|
||||
WalletID string `json:"walletId", pg:"wallet_id"`
|
||||
Wallet *Wallet `json:"wallet" pg:"rel:has-one, fk:wallet_id"`
|
||||
TransactionTypeID string `json:"transactionTypeId", pg:"transaction_type_id"`
|
||||
TransactionType *TransactionType `json:"transactionType", pg:"rel:has-one, fk:transaction_type_id"`
|
||||
LastTransactionDate time.Time `json:"lastTransactionDate", pg:"last_transaction_date", type:timestamptz`
|
||||
Amount float32 `json:"amount", pg:"amount,default:0"`
|
||||
}
|
||||
|
||||
type SubscriptionEdit struct {
|
||||
tableName struct{} `pg:"subscriptions,alias:subscriptions"`
|
||||
Id string `json:"id" form:"id"`
|
||||
Description string `json:"description" form:"description"`
|
||||
EndDate time.Time `json:"endDate" form:"endDate" `
|
||||
HasEnd bool `json:"hasEnd" form:"hasEnd"`
|
||||
WalletID string `json:"walletId" form:"walletId"`
|
||||
Amount json.Number `json:"amount" form:"amount"`
|
||||
}
|
||||
|
||||
type NewSubscriptionBody struct {
|
||||
WalletID string `json:"walletId" form:"walletId"`
|
||||
TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"`
|
||||
SubscriptionTypeID string `json:"subscriptionTypeId" pg:"subscription_type_id"`
|
||||
CustomRange json.Number `json:"customRange", pg:"custom_range"`
|
||||
StartDate time.Time `json:"startDate" pg:"start_date"`
|
||||
EndDate time.Time `json:"endDate" pg:"end_date"`
|
||||
HasEnd bool `json:"hasEnd" pg:"hasEnd"`
|
||||
Description string `json:"description" form:"description"`
|
||||
Amount json.Number `json:"amount" form:"amount"`
|
||||
}
|
||||
|
||||
type SubscriptionEnd struct {
|
||||
Id string `json:"id" form:"id"`
|
||||
}
|
||||
|
||||
/*
|
||||
ToTrans
|
||||
|
||||
Maps Subscription object to Transaction object.
|
||||
|
||||
Returns:
|
||||
*Transaction: mapped Transaction object
|
||||
*/
|
||||
func (cm *Subscription) ToTrans() *Transaction {
|
||||
trans := new(Transaction)
|
||||
trans.Init()
|
||||
trans.Amount = cm.Amount
|
||||
trans.Description = cm.Description
|
||||
trans.WalletID = cm.WalletID
|
||||
trans.Wallet = cm.Wallet
|
||||
trans.TransactionTypeID = cm.TransactionTypeID
|
||||
trans.TransactionType = cm.TransactionType
|
||||
trans.DateCreated = cm.DateCreated
|
||||
trans.SubscriptionID = cm.Id
|
||||
return trans
|
||||
}
|
||||
|
||||
/*
|
||||
HasNew
|
||||
|
||||
Checks if Subscription reached new transaction interval.
|
||||
|
||||
Returns:
|
||||
bool: Is new transaction interval reached
|
||||
*/
|
||||
func (cm *Subscription) HasNew() bool {
|
||||
trans := cm.TransactionType
|
||||
if trans != nil {
|
||||
switch trans.Type {
|
||||
case "monthly":
|
||||
lastDate := time.Now().AddDate(0, -cm.CustomRange, 0)
|
||||
if cm.LastTransactionDate.Before(lastDate) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
case "weekly":
|
||||
lastDate := time.Now().AddDate(0, 0, -(7 * cm.CustomRange))
|
||||
if cm.LastTransactionDate.Before(lastDate) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
case "daily":
|
||||
lastDate := time.Now().AddDate(0, 0, -cm.CustomRange)
|
||||
if cm.LastTransactionDate.Before(lastDate) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
default:
|
||||
lastDate := time.Now().AddDate(-cm.CustomRange, 0, 0)
|
||||
if cm.LastTransactionDate.Before(lastDate) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
13
pkg/model/subscriptionType.go
Normal file
13
pkg/model/subscriptionType.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package model
|
||||
|
||||
type SubscriptionType struct {
|
||||
tableName struct{} `pg:"subscriptionTypes,alias:subscriptionTypes"`
|
||||
BaseModel
|
||||
Name string `json:"name" pg:"name"`
|
||||
Type string `json:"type" pg:"type,notnull"`
|
||||
}
|
||||
|
||||
type NewSubscriptionTypeBody struct {
|
||||
Name string `json:"name" form:"name"`
|
||||
Type string `json:"type" form:"type"`
|
||||
}
|
||||
40
pkg/model/transaction.go
Normal file
40
pkg/model/transaction.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Transaction struct {
|
||||
tableName struct{} `pg:"transactions,alias:transactions"`
|
||||
BaseModel
|
||||
Description string `json:"description" pg:"description"`
|
||||
TransactionTypeID string `json:"transactionTypeId", pg:"transaction_type_id"`
|
||||
TransactionType *TransactionType `json:"transactionType", pg:"rel:has-one, fk:transaction_type_id"`
|
||||
TransactionStatusID string `json:"transactionStatusId", pg:"transaction_status_id"`
|
||||
TransactionStatus *TransactionStatus `json:"transactionStatus", pg:"rel:has-one, fk:transaction_status_id"`
|
||||
WalletID string `json:"walletId", pg:"wallet_id"`
|
||||
Amount float32 `json:"amount", pg:"amount,default:0"`
|
||||
Wallet *Wallet `json:"wallet" pg:"rel:has-one, fk:wallet_id"`
|
||||
TransactionDate time.Time `json:"transactionDate" pg:"transaction_date, type:timestamptz"`
|
||||
SubscriptionID string `json:"subscriptionId", pg:"subscription_id"`
|
||||
Subscription *Subscription `json:"subscription", pg:"rel:has-one, fk:subscription_id"`
|
||||
}
|
||||
|
||||
type NewTransactionBody struct {
|
||||
WalletID string `json:"walletId" form:"walletId"`
|
||||
TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"`
|
||||
TransactionDate time.Time `json:"transactionDate" form:"transactionDate"`
|
||||
Description string `json:"description" form:"description"`
|
||||
Amount json.Number `json:"amount" form:"amount"`
|
||||
}
|
||||
|
||||
type TransactionEdit struct {
|
||||
Id string `json:"id" form:"id"`
|
||||
WalletID string `json:"walletId" form:"walletId"`
|
||||
TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"`
|
||||
TransactionDate time.Time `json:"transactionDate" form:"transactionDate"`
|
||||
TransactionStatusID string `json:"transactionStatusId" form:"transactionStatusId"`
|
||||
Description string `json:"description" form:"description"`
|
||||
Amount json.Number `json:"amount" form:"amount"`
|
||||
}
|
||||
13
pkg/model/transactionStatus.go
Normal file
13
pkg/model/transactionStatus.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package model
|
||||
|
||||
type TransactionStatus struct {
|
||||
tableName struct{} `pg:"transactionStatus,alias:transactionStatus"`
|
||||
BaseModel
|
||||
Name string `json:"name" pg:"name"`
|
||||
Status string `json:"status" pg:"status,notnull"`
|
||||
}
|
||||
|
||||
type NewTransactionStatusBody struct {
|
||||
Name string `json:"name" form:"name"`
|
||||
Status string `json:"status" form:"status"`
|
||||
}
|
||||
13
pkg/model/transactionType.go
Normal file
13
pkg/model/transactionType.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package model
|
||||
|
||||
type TransactionType struct {
|
||||
tableName struct{} `pg:"transactionTypes,alias:transactionTypes"`
|
||||
BaseModel
|
||||
Name string `json:"name" pg:"name"`
|
||||
Type string `json:"type" pg:"type,notnull"`
|
||||
}
|
||||
|
||||
type NewTransactionTypeBody struct {
|
||||
Name string `json:"name" form:"name"`
|
||||
Type string `json:"type" form:"type"`
|
||||
}
|
||||
35
pkg/model/wallet.go
Normal file
35
pkg/model/wallet.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package model
|
||||
|
||||
type Wallet struct {
|
||||
tableName struct{} `pg:"wallets,alias:wallets"`
|
||||
BaseModel
|
||||
Name string `json:"name" pg:"name"`
|
||||
UserID string `json:"userId" pg:"user_id"`
|
||||
User *UserReturnInfo `json:"user" pg:"rel:has-one,fk:user_id"`
|
||||
}
|
||||
|
||||
type NewWalletBody struct {
|
||||
Name string `json:"name" form:"name"`
|
||||
UserID string `json:"userId" form:"userId"`
|
||||
}
|
||||
|
||||
type WalletHeader struct {
|
||||
WalletId string `json:"walletId"`
|
||||
CurrentBalance float32 `json:"currentBalance"`
|
||||
LastMonth float32 `json:"lastMonth"`
|
||||
NextMonth float32 `json:"nextMonth"`
|
||||
Currency string `json:"currency"`
|
||||
}
|
||||
|
||||
type WalletTransactions struct {
|
||||
WalletId string
|
||||
Transactions []Transaction
|
||||
CurrentBalance float32
|
||||
LastMonth float32
|
||||
NextMonth float32
|
||||
}
|
||||
|
||||
type WalletEdit struct {
|
||||
Id string `json:"id" form:"id"`
|
||||
Name string `json:"name" form:"name"`
|
||||
}
|
||||
Reference in New Issue
Block a user