mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 14:18:12 +00:00
added exception messages to controllers
This commit is contained in:
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
@@ -21,19 +22,27 @@ Inserts new row to transaction type table.
|
||||
*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 {
|
||||
func (as *TransactionTypeService) New(ctx context.Context, body *models.NewTransactionTypeBody) (*models.TransactionType, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
tm := new(models.TransactionType)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
tm.Init()
|
||||
tm.Name = body.Name
|
||||
tm.Type = body.Type
|
||||
|
||||
db.Model(tm).Insert()
|
||||
_, err := db.Model(tm).Insert()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400125"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error inserting row in \"transactionTypes\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return tm
|
||||
return tm, nil
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -45,14 +54,22 @@ Gets all rows from transaction type table.
|
||||
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 {
|
||||
func (as *TransactionTypeService) GetAll(ctx context.Context, embed string) (*[]models.TransactionType, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.TransactionType)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
query := db.Model(wm)
|
||||
common.GenerateEmbed(query, embed).Select()
|
||||
err := common.GenerateEmbed(query, embed).Select()
|
||||
if err != nil {
|
||||
exceptionReturn.StatusCode = 400
|
||||
exceptionReturn.ErrorCode = "400133"
|
||||
exceptionReturn.Message = fmt.Sprintf("Error selecting row in \"transactionTypes\" table: %s", err)
|
||||
return nil, exceptionReturn
|
||||
}
|
||||
|
||||
return wm
|
||||
return wm, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user