feat(expense): starting expense handle
This commit is contained in:
@@ -6,6 +6,30 @@ type Database struct {
|
|||||||
*postgresql.DatabasePostgreSQL
|
*postgresql.DatabasePostgreSQL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d Database) GetAllExpensesOfAnAccount(id string) (interface{}, interface{}) {
|
||||||
|
q := `
|
||||||
|
SELECT a.id, a.user_id, a.name, a.provider, a.created_at, a.updated_at
|
||||||
|
FROM public.account a
|
||||||
|
WHERE a.user_id = $1
|
||||||
|
`
|
||||||
|
rows, err := db.Session.Query(q, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
as := make([]*Account, 0)
|
||||||
|
for rows.Next() {
|
||||||
|
a := Account{}
|
||||||
|
err := rows.Scan(&a.ID, &a.UserId, &a.Name, &a.Provider, &a.CreatedAt, &a.UpdatedAt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
as = append(as, &a)
|
||||||
|
}
|
||||||
|
return as, nil
|
||||||
|
}
|
||||||
|
|
||||||
func NewDatabase(db *postgresql.DatabasePostgreSQL) *Database {
|
func NewDatabase(db *postgresql.DatabasePostgreSQL) *Database {
|
||||||
return &Database{db}
|
return &Database{db}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package expense
|
package expense
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"nos-comptes/handler"
|
"nos-comptes/handler"
|
||||||
"nos-comptes/internal/storage/dao/postgresql"
|
"nos-comptes/internal/storage/dao/postgresql"
|
||||||
|
"nos-comptes/internal/storage/model"
|
||||||
|
"nos-comptes/internal/utils"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@@ -21,8 +24,23 @@ func (c *Context) DeleteExpense(context *gin.Context) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) GetAllExpenses(context *gin.Context) {
|
func (c *Context) GetAllExpenses(gc *gin.Context) {
|
||||||
|
accountId := gc.Param("accountId")
|
||||||
|
accounts, err := c.service.GetAllExpensesOfAnAccount(accountId)
|
||||||
|
if e, ok := err.(*model.APIError); ok {
|
||||||
|
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetAllExpenses: get expenses")
|
||||||
|
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
|
||||||
|
} else if err != nil {
|
||||||
|
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get expenses")
|
||||||
|
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(accounts) == 0 {
|
||||||
|
utils.JSON(gc.Writer, http.StatusNoContent, nil)
|
||||||
|
} else {
|
||||||
|
utils.JSON(gc.Writer, http.StatusOK, accounts)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) GetAnExpenses(context *gin.Context) {
|
func (c *Context) GetAnExpenses(context *gin.Context) {
|
||||||
|
|||||||
@@ -1,4 +1,17 @@
|
|||||||
package expense
|
package expense
|
||||||
|
|
||||||
type Account struct {
|
import "time"
|
||||||
|
|
||||||
|
type Expense struct {
|
||||||
|
ExpenseEditable
|
||||||
|
AccountId string `json:"accountId,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExpenseEditable struct {
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Value float32 `json:"value"`
|
||||||
|
TypeExpense string `json:"typeExpense"`
|
||||||
|
ExpenseDate time.Time `json:"expenseDate,omitempty"`
|
||||||
|
CreatedAt *time.Time `json:"createdAt,omitempty"`
|
||||||
|
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,33 @@
|
|||||||
package expense
|
package expense
|
||||||
|
|
||||||
|
import (
|
||||||
|
"nos-comptes/internal/storage/dao"
|
||||||
|
"nos-comptes/internal/storage/model"
|
||||||
|
)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
Db *Database
|
db *Database
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Service) GetAllExpensesOfAnAccount(accountId string) (interface{}, interface{}) {
|
||||||
|
expenses, err := s.db.GetAllExpensesOfAnAccount(accountId)
|
||||||
|
if e, ok := err.(*dao.Error); ok {
|
||||||
|
switch {
|
||||||
|
case e.Type == dao.ErrTypeNotFound:
|
||||||
|
return nil, &model.ErrNotFound
|
||||||
|
default:
|
||||||
|
return nil, &model.ErrInternalServer
|
||||||
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
return nil, &model.ErrInternalServer
|
||||||
|
}
|
||||||
|
|
||||||
|
if expenses == nil {
|
||||||
|
return nil, &model.ErrNotFound
|
||||||
|
}
|
||||||
|
return expenses, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService(database *Database) *Service {
|
func NewService(database *Database) *Service {
|
||||||
return &Service{Db: database}
|
return &Service{db: database}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user