This commit is contained in:
Fran Jurmanović
2022-09-26 21:17:30 +02:00
parent 73a41f2b54
commit 13ce0735d0
19 changed files with 466 additions and 341 deletions

View File

@@ -9,21 +9,27 @@ import (
)
type ApiService struct {
Db *pg.DB
db *pg.DB
}
func NewApiService(db *pg.DB) *ApiService {
return &ApiService{
db: db,
}
}
/*
GetFirst
Gets first row from API table.
Args:
context.Context: Application context
Returns:
models.ApiModel: Api object from database.
*/
func (as *ApiService) GetFirst(ctx context.Context) models.ApiModel {
db := as.Db.WithContext(ctx)
Args:
context.Context: Application context
Returns:
models.ApiModel: Api object from database.
*/
func (as ApiService) GetFirst(ctx context.Context) models.ApiModel {
db := as.db.WithContext(ctx)
apiModel := models.ApiModel{Api: "Works"}
db.Model(&apiModel).First()
return apiModel
@@ -33,15 +39,16 @@ func (as *ApiService) GetFirst(ctx context.Context) models.ApiModel {
PostMigrate
Starts database migration.
Args:
context.Context: Application context
string: Migration version
Returns:
*models.MessageResponse: Message response object.
*models.Exception: Exception response object.
Args:
context.Context: Application context
string: Migration version
Returns:
*models.MessageResponse: Message response object.
*models.Exception: Exception response object.
*/
func (as *ApiService) PostMigrate(ctx context.Context, version string) (*models.MessageResponse, *models.Exception) {
db := as.Db.WithContext(ctx)
func (as ApiService) PostMigrate(ctx context.Context, version string) (*models.MessageResponse, *models.Exception) {
db := as.db.WithContext(ctx)
mr := new(models.MessageResponse)
er := new(models.Exception)

View File

@@ -10,22 +10,29 @@ import (
)
type SubscriptionTypeService struct {
Db *pg.DB
db *pg.DB
}
func NewSubscriptionTypeService(db *pg.DB) *SubscriptionTypeService {
return &SubscriptionTypeService{
db: db,
}
}
/*
New
Inserts new row to subscription type table.
Args:
context.Context: Application context
*models.NewSubscriptionTypeBody: Values to create new row
Returns:
*models.SubscriptionType: Created row from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.NewSubscriptionTypeBody: Values to create new row
Returns:
*models.SubscriptionType: Created row from database.
*models.Exception: Exception payload.
*/
func (as *SubscriptionTypeService) New(ctx context.Context, body *models.NewSubscriptionTypeBody) (*models.SubscriptionType, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
tm := new(models.SubscriptionType)
exceptionReturn := new(models.Exception)
@@ -49,15 +56,16 @@ func (as *SubscriptionTypeService) New(ctx context.Context, body *models.NewSubs
GetAll
Gets all rows from subscription type table.
Args:
context.Context: Application context
string: Relations to embed
Returns:
*[]models.SubscriptionType: List of subscription type objects.
*models.Exception: Exception payload.
Args:
context.Context: Application context
string: Relations to embed
Returns:
*[]models.SubscriptionType: List of subscription type objects.
*models.Exception: Exception payload.
*/
func (as *SubscriptionTypeService) GetAll(ctx context.Context, embed string) (*[]models.SubscriptionType, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
wm := new([]models.SubscriptionType)
exceptionReturn := new(models.Exception)

View File

@@ -12,22 +12,29 @@ import (
)
type SubscriptionService struct {
Db *pg.DB
db *pg.DB
}
func NewSubscriptionService(db *pg.DB) *SubscriptionService {
return &SubscriptionService{
db: db,
}
}
/*
New
Inserts new row to subscription table.
Args:
context.Context: Application context
*models.NewSubscriptionBody: Request body
Returns:
*models.Subscription: Created Subscription row object from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.NewSubscriptionBody: Request body
Returns:
*models.Subscription: Created Subscription row object from database.
*models.Exception: Exception payload.
*/
func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscriptionBody) (*models.Subscription, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
tm := new(models.Subscription)
exceptionReturn := new(models.Exception)
@@ -69,17 +76,18 @@ func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscrip
Get
Gets row from subscription table by id.
Args:
context.Context: Application context
*models.Auth: Authentication model
string: subscription id to search
params: *models.Params
Returns:
*models.Subscription: Subscription row object from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.Auth: Authentication model
string: subscription id to search
params: *models.Params
Returns:
*models.Subscription: Subscription row object from database.
*models.Exception: Exception payload.
*/
func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) (*models.Subscription, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
exceptionReturn := new(models.Exception)
wm := new(models.Subscription)
@@ -106,16 +114,17 @@ func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id stri
GetAll
Gets filtered rows from subscription table.
Args:
context.Context: Application context
*models.Auth: Authentication object
string: Wallet id to search
*models.FilteredResponse: filter options
Returns:
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.Auth: Authentication object
string: Wallet id to search
*models.FilteredResponse: filter options
Returns:
*models.Exception: Exception payload.
*/
func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) *models.Exception {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
wm := new([]models.Subscription)
exceptionReturn := new(models.Exception)
@@ -144,16 +153,17 @@ func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, wall
Edit
Updates row from subscription table by id.
Args:
context.Context: Application context
*models.SubscriptionEdit: Values to edit
string: id to search
Returns:
*models.Subscription: Edited Subscription row object from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.SubscriptionEdit: Values to edit
string: id to search
Returns:
*models.Subscription: Edited Subscription row object from database.
*models.Exception: Exception payload.
*/
func (as *SubscriptionService) Edit(ctx context.Context, body *models.SubscriptionEdit, id string) (*models.Subscription, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
amount, _ := body.Amount.Float64()
exceptionReturn := new(models.Exception)
@@ -188,15 +198,16 @@ End
Updates row in subscription table by id.
Ends subscription with current date.
Args:
context.Context: Application context
string: id to search
Returns:
*models.Subscription: Created Subscription row object from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
string: id to search
Returns:
*models.Subscription: Created Subscription row object from database.
*models.Exception: Exception payload.
*/
func (as *SubscriptionService) End(ctx context.Context, id string) (*models.Subscription, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
exceptionReturn := new(models.Exception)
tm := new(models.Subscription)
@@ -224,11 +235,12 @@ func (as *SubscriptionService) End(ctx context.Context, id string) (*models.Subs
SubToTrans
Generates and Inserts new Transaction rows from the subscription model.
Args:
*models.Subscription: Subscription model to generate new transactions from
*pg.Tx: Postgres query context
Returns:
*models.Exception: Exception payload.
Args:
*models.Subscription: Subscription model to generate new transactions from
*pg.Tx: Postgres query context
Returns:
*models.Exception: Exception payload.
*/
func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.Tx) *models.Exception {
exceptionReturn := new(models.Exception)

View File

@@ -10,22 +10,29 @@ import (
)
type TransactionStatusService struct {
Db *pg.DB
db *pg.DB
}
func NewTransactionStatusService(db *pg.DB) *TransactionStatusService {
return &TransactionStatusService{
db: db,
}
}
/*
New
Inserts new row to transaction status table.
Args:
context.Context: Application context
*models.NewTransactionStatusBody: object to create
Returns:
*models.TransactionType: Transaction Type object from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.NewTransactionStatusBody: object to create
Returns:
*models.TransactionType: Transaction Type object from database.
*models.Exception: Exception payload.
*/
func (as *TransactionStatusService) New(ctx context.Context, body *models.NewTransactionStatusBody) (*models.TransactionStatus, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
tm := new(models.TransactionStatus)
exceptionReturn := new(models.Exception)
@@ -49,15 +56,16 @@ func (as *TransactionStatusService) New(ctx context.Context, body *models.NewTra
GetAll
Gets all rows from transaction status table.
Args:
context.Context: Application context
string: Relations to embed
Returns:
*[]models.TransactionStatus: List of Transaction status objects from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
string: Relations to embed
Returns:
*[]models.TransactionStatus: List of Transaction status objects from database.
*models.Exception: Exception payload.
*/
func (as *TransactionStatusService) GetAll(ctx context.Context, embed string) (*[]models.TransactionStatus, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
wm := new([]models.TransactionStatus)
exceptionReturn := new(models.Exception)

View File

@@ -10,22 +10,29 @@ import (
)
type TransactionTypeService struct {
Db *pg.DB
db *pg.DB
}
func NewTransactionTypeService(db *pg.DB) *TransactionTypeService {
return &TransactionTypeService{
db: db,
}
}
/*
New
Inserts new row to transaction type table.
Args:
context.Context: Application context
*models.NewTransactionTypeBody: object to create
Returns:
*models.TransactionType: Transaction Type object from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.NewTransactionTypeBody: object to create
Returns:
*models.TransactionType: Transaction Type object from database.
*models.Exception: Exception payload.
*/
func (as *TransactionTypeService) New(ctx context.Context, body *models.NewTransactionTypeBody) (*models.TransactionType, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
tm := new(models.TransactionType)
exceptionReturn := new(models.Exception)
@@ -49,15 +56,16 @@ func (as *TransactionTypeService) New(ctx context.Context, body *models.NewTrans
GetAll
Gets all rows from transaction type table.
Args:
context.Context: Application context
string: Relations to embed
Returns:
*[]models.TransactionType: List of Transaction type objects from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
string: Relations to embed
Returns:
*[]models.TransactionType: List of Transaction type objects from database.
*models.Exception: Exception payload.
*/
func (as *TransactionTypeService) GetAll(ctx context.Context, embed string) (*[]models.TransactionType, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
wm := new([]models.TransactionType)
exceptionReturn := new(models.Exception)

View File

@@ -12,23 +12,31 @@ import (
)
type TransactionService struct {
Db *pg.DB
Ss *SubscriptionService
db *pg.DB
subscriptionService *SubscriptionService
}
func NewTransactionService(db *pg.DB, ss *SubscriptionService) *TransactionService {
return &TransactionService{
db: db,
subscriptionService: ss,
}
}
/*
New new row into transaction table
New row into transaction table
Inserts
Args:
context.Context: Application context
*models.NewTransactionBody: Transaction body object
Returns:
*models.Transaction: Transaction object
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.NewTransactionBody: Transaction body object
Returns:
*models.Transaction: Transaction object
*models.Exception: Exception payload.
*/
func (as *TransactionService) New(ctx context.Context, body *models.NewTransactionBody) (*models.Transaction, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
exceptionReturn := new(models.Exception)
tm := new(models.Transaction)
@@ -83,7 +91,7 @@ Gets all rows from subscription type table.
*/
// Gets filtered rows from transaction table.
func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse, noPending bool) *models.Exception {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
exceptionReturn := new(models.Exception)
wm := new([]models.Transaction)
@@ -132,7 +140,7 @@ Checks subscriptions and create transacitons.
*/
// Gets filtered rows from transaction table.
func (as *TransactionService) Check(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) *models.Exception {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
wm := new([]models.Transaction)
sm := new([]models.Subscription)
@@ -157,7 +165,7 @@ func (as *TransactionService) Check(ctx context.Context, am *models.Auth, wallet
for _, sub := range *sm {
if sub.HasNew() {
as.Ss.SubToTrans(&sub, tx)
as.subscriptionService.SubToTrans(&sub, tx)
}
}
@@ -183,16 +191,17 @@ func (as *TransactionService) Check(ctx context.Context, am *models.Auth, wallet
Edit
Updates row in transaction table by id.
Args:
context.Context: Application context
*models.TransactionEdit: Object to edit
string: id to search
Returns:
*models.Transaction: Transaction object from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.TransactionEdit: Object to edit
string: id to search
Returns:
*models.Transaction: Transaction object from database.
*models.Exception: Exception payload.
*/
func (as *TransactionService) Edit(ctx context.Context, body *models.TransactionEdit, id string) (*models.Transaction, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
amount, _ := body.Amount.Float64()
@@ -236,16 +245,17 @@ func (as *TransactionService) Edit(ctx context.Context, body *models.Transaction
Bulk Edit
Updates row in transaction table by id.
Args:
context.Context: Application context
?[]models.Transaction Bulk Edit: Object to edit
string: id to search
Returns:
*models.Transaction: Transaction object from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
?[]models.Transaction Bulk Edit: Object to edit
string: id to search
Returns:
*models.Transaction: Transaction object from database.
*models.Exception: Exception payload.
*/
func (as *TransactionService) BulkEdit(ctx context.Context, body *[]models.TransactionEdit) (*[]models.Transaction, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
tx, _ := db.Begin()
defer tx.Rollback()
@@ -284,17 +294,18 @@ func (as *TransactionService) BulkEdit(ctx context.Context, body *[]models.Trans
Get
Gets row from transaction table by id.
Args:
context.Context: Application context
*models.Auth: Authentication object
string: id to search
*model.Params: url query parameters
Returns:
*models.Transaction: Transaction object from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.Auth: Authentication object
string: id to search
*model.Params: url query parameters
Returns:
*models.Transaction: Transaction object from database.
*models.Exception: Exception payload.
*/
func (as *TransactionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) (*models.Transaction, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
exceptionReturn := new(models.Exception)
wm := new(models.Transaction)

View File

@@ -15,22 +15,29 @@ import (
)
type UsersService struct {
Db *pg.DB
db *pg.DB
}
func NewUsersService(db *pg.DB) *UsersService {
return &UsersService{
db: db,
}
}
/*
Create
Inserts new row to users table.
Args:
context.Context: Application context
*models.User: User object to create
Returns:
*models.User: User object from database
*models.Exception
Args:
context.Context: Application context
*models.User: User object to create
Returns:
*models.User: User object from database
*models.Exception
*/
func (us *UsersService) Create(ctx context.Context, registerBody *models.User) (*models.User, *models.Exception) {
db := us.Db.WithContext(ctx)
db := us.db.WithContext(ctx)
check := new(models.User)
exceptionReturn := new(models.Exception)
@@ -67,15 +74,16 @@ func (us *UsersService) Create(ctx context.Context, registerBody *models.User) (
Login
Gets row from users table by email and valid password.
Args:
context.Context: Application context
*models.Login: object to search
Returns:
*models.Token: new session token
*models.Exception
Args:
context.Context: Application context
*models.Login: object to search
Returns:
*models.Token: new session token
*models.Exception
*/
func (us *UsersService) Login(ctx context.Context, loginBody *models.Login) (*models.Token, *models.Exception) {
db := us.Db.WithContext(ctx)
db := us.db.WithContext(ctx)
check := new(models.User)
exceptionReturn := new(models.Exception)
@@ -123,15 +131,16 @@ Deactivate
Updates row in users table.
IsActive column is set to false
Args:
context.Context: Application context
*models.Auth: Authentication object
Returns:
*models.MessageResponse
*models.Exception
Args:
context.Context: Application context
*models.Auth: Authentication object
Returns:
*models.MessageResponse
*models.Exception
*/
func (us *UsersService) Deactivate(ctx context.Context, auth *models.Auth) (*models.MessageResponse, *models.Exception) {
db := us.Db.WithContext(ctx)
db := us.db.WithContext(ctx)
mm := new(models.MessageResponse)
me := new(models.Exception)
@@ -171,12 +180,13 @@ CreateToken
Generates new jwt token.
It encodes the user id. Based on rememberMe it is valid through 48hours or 2hours.
Args:
*models.User: User object to encode
bool: Should function generate longer lasting token (48hrs)
Returns:
string: Generated token
error: Error that occured in the process
Args:
*models.User: User object to encode
bool: Should function generate longer lasting token (48hrs)
Returns:
string: Generated token
error: Error that occured in the process
*/
func CreateToken(user *models.User, rememberMe bool) (string, error) {
atClaims := jwt.MapClaims{}

View File

@@ -11,23 +11,31 @@ import (
)
type WalletService struct {
Db *pg.DB
Ss *SubscriptionService
db *pg.DB
subscriptionService *SubscriptionService
}
func NewWalletService(db *pg.DB, ss *SubscriptionService) *WalletService {
return &WalletService{
db: db,
subscriptionService: ss,
}
}
/*
New
Inserts row to wallets table.
Args:
context.Context: Application context
*models.NewWalletBody: Object to be inserted
Returns:
*models.Wallet: Wallet object from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.NewWalletBody: Object to be inserted
Returns:
*models.Wallet: Wallet object from database.
*models.Exception: Exception payload.
*/
func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) (*models.Wallet, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
exceptionReturn := new(models.Exception)
walletModel := new(models.Wallet)
@@ -48,16 +56,17 @@ func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) (*mo
Edit
Updates row in wallets table by id.
Args:
context.Context: Application context
*models.WalletEdit: Object to be edited
string: id to search
Returns:
*models.Wallet: Wallet object from database.
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.WalletEdit: Object to be edited
string: id to search
Returns:
*models.Wallet: Wallet object from database.
*models.Exception: Exception payload.
*/
func (as *WalletService) Edit(ctx context.Context, body *models.WalletEdit, id string) (*models.Wallet, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
exceptionReturn := new(models.Exception)
tm := new(models.Wallet)
@@ -84,16 +93,17 @@ func (as *WalletService) Edit(ctx context.Context, body *models.WalletEdit, id s
Get
Gets row in wallets table by id.
Args:
context.Context: Application context
string: id to search
*models.Params: url query parameters
Returns:
*models.Wallet: Wallet object from database
*models.Exception: Exception payload.
Args:
context.Context: Application context
string: id to search
*models.Params: url query parameters
Returns:
*models.Wallet: Wallet object from database
*models.Exception: Exception payload.
*/
func (as *WalletService) Get(ctx context.Context, id string, params *models.Params) (*models.Wallet, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
exceptionReturn := new(models.Exception)
wm := new(models.Wallet)
@@ -120,16 +130,17 @@ func (as *WalletService) Get(ctx context.Context, id string, params *models.Para
GetAll
Gets filtered rows from wallets table.
Args:
context.Context: Application context
*models.Auth: Authentication object
*models.FilteredResponse: filter options
Returns:
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.Auth: Authentication object
*models.FilteredResponse: filter options
Returns:
*models.Exception: Exception payload.
*/
func (as *WalletService) GetAll(ctx context.Context, am *models.Auth, filtered *models.FilteredResponse) *models.Exception {
exceptionReturn := new(models.Exception)
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
wm := new([]models.Wallet)
query := db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
@@ -149,16 +160,17 @@ GetHeader
Gets row from wallets table.
Calculates previous month, current and next month totals.
Args:
context.Context: Application context
*models.Auth: Authentication object
string: wallet id to search
Returns:
*models.WalletHeader: generated wallet header object
*models.Exception: Exception payload.
Args:
context.Context: Application context
*models.Auth: Authentication object
string: wallet id to search
Returns:
*models.WalletHeader: generated wallet header object
*models.Exception: Exception payload.
*/
func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletId string) (*models.WalletHeader, *models.Exception) {
db := as.Db.WithContext(ctx)
db := as.db.WithContext(ctx)
wm := new(models.WalletHeader)
wallets := new([]models.WalletTransactions)
@@ -286,12 +298,13 @@ addWhere
Appends Transaction to the belonging walletId.
If missing, it creates the item list.
Args:
*[]models.WalletTransactions: list to append to
string: wallet id to check
models.Transaction: Transaction to append
Returns:
*models.Exception: Exception payload.
Args:
*[]models.WalletTransactions: list to append to
string: wallet id to check
models.Transaction: Transaction to append
Returns:
*models.Exception: Exception payload.
*/
func addWhere(s *[]models.WalletTransactions, walletId string, e models.Transaction) {
var exists bool