fixed code documentation

This commit is contained in:
Fran Jurmanović
2021-08-29 10:51:29 +02:00
parent 0682252859
commit f441f46494
35 changed files with 674 additions and 70 deletions

View File

@@ -12,7 +12,15 @@ type ApiService struct {
Db *pg.DB
}
// Gets first row from API table.
/*
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)
@@ -21,9 +29,17 @@ func (as *ApiService) GetFirst(ctx context.Context) models.ApiModel {
return apiModel
}
// Starts database migration.
//
// Takes migration version.
/*
PostMigrate
Starts database migration.
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)

View File

@@ -7,7 +7,15 @@ import (
"github.com/go-pg/pg/v10"
)
// Adds filters to query and executes it.
/*
FilteredResponse
Adds filters to query and executes it.
Args:
*pg.Query: postgres query
interface{}: model to be mapped from query execution.
*models.FilteredResponse: filter options.
*/
func FilteredResponse(qry *pg.Query, mdl interface{}, filtered *models.FilteredResponse) {
if filtered.Page == 0 {
filtered.Page = 1

View File

@@ -12,7 +12,16 @@ type SubscriptionTypeService struct {
Db *pg.DB
}
// Inserts new row to subscription type table.
/*
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.
*/
func (as *SubscriptionTypeService) New(ctx context.Context, body *models.NewSubscriptionTypeBody) *models.SubscriptionType {
db := as.Db.WithContext(ctx)
@@ -27,7 +36,16 @@ func (as *SubscriptionTypeService) New(ctx context.Context, body *models.NewSubs
return tm
}
// Gets all rows from subscription type table.
/*
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.
*/
func (as *SubscriptionTypeService) GetAll(ctx context.Context, embed string) *[]models.SubscriptionType {
db := as.Db.WithContext(ctx)

View File

@@ -14,7 +14,16 @@ type SubscriptionService struct {
Db *pg.DB
}
// Inserts new row to subscription table.
/*
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.
*/
func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscriptionBody) *models.Subscription {
db := as.Db.WithContext(ctx)
@@ -49,7 +58,18 @@ func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscrip
return tm
}
// Gets row from subscription table by id.
/*
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.
*/
func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) *models.Subscription {
db := as.Db.WithContext(ctx)
@@ -71,7 +91,16 @@ func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id stri
return wm
}
// Gets filtered rows from subscription table.
/*
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
*/
func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) {
db := as.Db.WithContext(ctx)
@@ -94,7 +123,17 @@ func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, wall
tx.Commit()
}
// Updates row from subscription table by id.
/*
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.
*/
func (as *SubscriptionService) Edit(ctx context.Context, body *models.SubscriptionEdit, id string) *models.Subscription {
db := as.Db.WithContext(ctx)
@@ -118,9 +157,18 @@ func (as *SubscriptionService) Edit(ctx context.Context, body *models.Subscripti
return tm
}
// Updates row in subscription table by id.
//
// Ends subscription with current date.
/*
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.
*/
func (as *SubscriptionService) End(ctx context.Context, id string) *models.Subscription {
db := as.Db.WithContext(ctx)
@@ -139,7 +187,14 @@ func (as *SubscriptionService) End(ctx context.Context, id string) *models.Subsc
return tm
}
// Generates and Inserts new Transaction rows from the subscription model.
/*
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
*/
func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.Tx) {
now := time.Now()

View File

@@ -12,7 +12,16 @@ type TransactionTypeService struct {
Db *pg.DB
}
// Inserts new row to transaction type table.
/*
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.
*/
func (as *TransactionTypeService) New(ctx context.Context, body *models.NewTransactionTypeBody) *models.TransactionType {
db := as.Db.WithContext(ctx)
@@ -27,7 +36,16 @@ func (as *TransactionTypeService) New(ctx context.Context, body *models.NewTrans
return tm
}
// Gets all rows from transaction type table.
/*
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.
*/
func (as *TransactionTypeService) GetAll(ctx context.Context, embed string) *[]models.TransactionType {
db := as.Db.WithContext(ctx)

View File

@@ -15,6 +15,16 @@ type TransactionService struct {
Ss *SubscriptionService
}
/*
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.
*/
// Inserts new row to transaction table.
func (as *TransactionService) New(ctx context.Context, body *models.NewTransactionBody) *models.Transaction {
db := as.Db.WithContext(ctx)
@@ -43,6 +53,16 @@ func (as *TransactionService) New(ctx context.Context, body *models.NewTransacti
return tm
}
/*
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.
*/
// Gets filtered rows from transaction table.
func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) {
db := as.Db.WithContext(ctx)
@@ -75,7 +95,17 @@ func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walle
tx.Commit()
}
// Updates row in transaction table by id.
/*
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.
*/
func (as *TransactionService) Edit(ctx context.Context, body *models.TransactionEdit, id string) *models.Transaction {
db := as.Db.WithContext(ctx)
@@ -99,7 +129,18 @@ func (as *TransactionService) Edit(ctx context.Context, body *models.Transaction
return tm
}
// Gets row from transaction table by id.
/*
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.
*/
func (as *TransactionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) *models.Transaction {
db := as.Db.WithContext(ctx)

View File

@@ -18,7 +18,18 @@ type UsersService struct {
Db *pg.DB
}
// Inserts new row to users table.
/*
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
*/
func (us *UsersService) Create(ctx context.Context, registerBody *models.User) (*models.User, *models.Exception) {
db := us.Db.WithContext(ctx)
@@ -53,7 +64,17 @@ func (us *UsersService) Create(ctx context.Context, registerBody *models.User) (
return registerBody, exceptionReturn
}
// Gets row from users table by email and valid password.
/*
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
*/
func (us *UsersService) Login(ctx context.Context, loginBody *models.Login) (*models.Token, *models.Exception) {
db := us.Db.WithContext(ctx)
@@ -91,9 +112,19 @@ func (us *UsersService) Login(ctx context.Context, loginBody *models.Login) (*mo
return tokenPayload, exceptionReturn
}
// Updates row in users table.
//
// IsActive column is set to false
/*
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
*/
func (us *UsersService) Deactivate(ctx context.Context, auth *models.Auth) (*models.MessageResponse, *models.Exception) {
db := us.Db.WithContext(ctx)
@@ -129,9 +160,19 @@ func (us *UsersService) Deactivate(ctx context.Context, auth *models.Auth) (*mod
return mm, me
}
// Generates new jwt token.
//
// It encodes the user id. Based on rememberMe it is valid through 48hours or 2hours.
/*
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
*/
func CreateToken(user *models.User, rememberMe bool) (string, error) {
atClaims := jwt.MapClaims{}
atClaims["authorized"] = true

View File

@@ -14,7 +14,16 @@ type WalletService struct {
Ss *SubscriptionService
}
// Inserts row to wallets table.
/*
New
Inserts row to wallets table.
Args:
context.Context: Application context
*models.NewWalletBody: Object to be inserted
Returns:
*models.Wallet: Wallet object from database.
*/
func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) *models.Wallet {
db := as.Db.WithContext(ctx)
@@ -26,7 +35,17 @@ func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) *mod
return walletModel
}
// Updates row in wallets table by id.
/*
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.
*/
func (as *WalletService) Edit(ctx context.Context, body *models.WalletEdit, id string) *models.Wallet {
db := as.Db.WithContext(ctx)
@@ -44,7 +63,17 @@ func (as *WalletService) Edit(ctx context.Context, body *models.WalletEdit, id s
return tm
}
// Gets row in wallets table by id.
/*
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
*/
func (as *WalletService) Get(ctx context.Context, id string, params *models.Params) *models.Wallet {
db := as.Db.WithContext(ctx)
@@ -62,7 +91,15 @@ func (as *WalletService) Get(ctx context.Context, id string, params *models.Para
return wm
}
// Gets filtered rows from wallets table.
/*
GetAll
Gets filtered rows from wallets table.
Args:
context.Context: Application context
*models.Auth: Authentication object
*models.FilteredResponse: filter options
*/
func (as *WalletService) GetAll(ctx context.Context, am *models.Auth, filtered *models.FilteredResponse) {
db := as.Db.WithContext(ctx)
wm := new([]models.Wallet)
@@ -71,9 +108,19 @@ func (as *WalletService) GetAll(ctx context.Context, am *models.Auth, filtered *
FilteredResponse(query, wm, filtered)
}
// Gets row from wallets table.
//
// Calculates previous month, current and next month totals.
/*
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
*/
func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletId string) *models.WalletHeader {
db := as.Db.WithContext(ctx)
@@ -181,9 +228,17 @@ func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletI
return wm
}
// Appends Transaction to the belonging walletId
//
// If missing, it creates the item list.
/*
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
*/
func addWhere(s *[]models.WalletTransactions, walletId string, e models.Transaction) {
var exists bool
for a := range *s {