From af59f5d7da233dca2954bec136d00342463d5d08 Mon Sep 17 00:00:00 2001 From: Montheankul-K Date: Sun, 19 May 2024 11:11:56 +0700 Subject: [PATCH] initial structure --- api/api.go | 8 ++++++- api/transaction/handler.go | 30 +++++++++++++++++++++++++ api/transaction/repository.go | 25 +++++++++++++++++++++ api/transaction/service.go | 30 +++++++++++++++++++++++++ api/transaction/transaction.go | 41 ++++++++++++++++++++++++++++++++-- 5 files changed, 131 insertions(+), 3 deletions(-) diff --git a/api/api.go b/api/api.go index 5624abf..a0ac4c6 100644 --- a/api/api.go +++ b/api/api.go @@ -38,7 +38,13 @@ func New(db *sql.DB, cfg config.Config, logger *zap.Logger) *Server { repository := transaction.NewRepository(db) service := transaction.NewService(repository) handler := transaction.NewHandler(service) - v1.GET("/expenses", handler.GetAll, middlewareHandler.SetFilterExpense, middlewareHandler.SetPagination) + v1.GET("/transactions", handler.GetAll, middlewareHandler.SetFilterExpense, middlewareHandler.SetPagination) + v1.POST("/transactions", handler.Create) + v1.GET("/transactions/expense/detail", handler.GetExpenses) + v1.GET("/transactions/summary", handler.GetSummary) + v1.GET("/transactions/balance", handler.GetBalance) + v1.PUT("/transactions/:id", handler.UpdateExpense) + v1.DELETE("/transactions/:id", handler.DeleteExpense) } { diff --git a/api/transaction/handler.go b/api/transaction/handler.go index 03ede99..b78575c 100644 --- a/api/transaction/handler.go +++ b/api/transaction/handler.go @@ -11,6 +11,12 @@ type handler struct { type Handler interface { GetAll(c echo.Context) error + Create(c echo.Context) error + GetExpenses(c echo.Context) error + GetSummary(c echo.Context) error + GetBalance(c echo.Context) error + UpdateExpense(c echo.Context) error + DeleteExpense(c echo.Context) error } func NewHandler(service Service) Handler { @@ -37,3 +43,27 @@ func (h handler) GetAll(c echo.Context) error { return c.JSON(http.StatusOK, result) } + +func (h handler) Create(c echo.Context) error { + return nil +} + +func (h handler) GetExpenses(c echo.Context) error { + return nil +} + +func (h handler) GetSummary(c echo.Context) error { + return nil +} + +func (h handler) GetBalance(c echo.Context) error { + return nil +} + +func (h handler) UpdateExpense(c echo.Context) error { + return nil +} + +func (h handler) DeleteExpense(c echo.Context) error { + return nil +} diff --git a/api/transaction/repository.go b/api/transaction/repository.go index 81d1726..dcae102 100644 --- a/api/transaction/repository.go +++ b/api/transaction/repository.go @@ -8,6 +8,11 @@ import ( type Repository interface { GetAll(filter Filter, paginate Pagination) ([]Transaction, error) + Create(request CreateTransactionRequest) (CreateTransactionResponse, error) + GetExpenses(spenderId int) ([]Transaction, error) + GetSummary(spenderId int, txnTypes []string) ([]GetTransactionResponse, error) + UpdateExpense(transaction Transaction) error + DeleteExpense(id int) error } type repository struct { @@ -76,3 +81,23 @@ func (r repository) GetAll(filter Filter, paginate Pagination) ([]Transaction, e return expenses, nil } + +func (r repository) Create(request CreateTransactionRequest) (CreateTransactionResponse, error) { + return CreateTransactionResponse{}, nil +} + +func (r repository) GetExpenses(spenderId int) ([]Transaction, error) { + return nil, nil +} + +func (r repository) GetSummary(spenderId int, txnTypes []string) ([]GetTransactionResponse, error) { + return nil, nil +} + +func (r repository) UpdateExpense(transaction Transaction) error { + return nil +} + +func (r repository) DeleteExpense(id int) error { + return nil +} diff --git a/api/transaction/service.go b/api/transaction/service.go index a9e20fa..c219320 100644 --- a/api/transaction/service.go +++ b/api/transaction/service.go @@ -6,6 +6,12 @@ type service struct { type Service interface { GetAll(filter Filter, pagination Pagination) ([]Transaction, error) + Create(request CreateTransactionRequest) (CreateTransactionResponse, error) + GetExpenses(spenderId int) ([]Transaction, error) + GetSummary(spenderId int, txnType string) (SummaryResponse, error) + GetBalance(spenderId int) (BalanceResponse, error) + UpdateExpense(transaction Transaction) error + DeleteExpense(id int) error } func NewService(repository Repository) Service { @@ -20,3 +26,27 @@ func (s service) GetAll(filter Filter, paginate Pagination) ([]Transaction, erro return result, nil } + +func (s service) Create(request CreateTransactionRequest) (CreateTransactionResponse, error) { + return CreateTransactionResponse{}, nil +} + +func (s service) GetBalance(spenderId int) (BalanceResponse, error) { + return BalanceResponse{}, nil +} + +func (s service) UpdateExpense(transaction Transaction) error { + return nil +} + +func (s service) DeleteExpense(id int) error { + return nil +} + +func (s service) GetSummary(spenderId int, txnType string) (SummaryResponse, error) { + return SummaryResponse{}, nil +} + +func (s service) GetExpenses(spenderId int) ([]Transaction, error) { + return make([]Transaction, 0), nil +} diff --git a/api/transaction/transaction.go b/api/transaction/transaction.go index 8e30891..3d0d3ef 100644 --- a/api/transaction/transaction.go +++ b/api/transaction/transaction.go @@ -18,7 +18,44 @@ type Transaction struct { Date *time.Time `json:"date"` Amount float32 `json:"amount"` Category string `json:"category"` - ImageUrl string `json:"imageUrl"` + ImageUrl string `json:"image_url"` Note string `json:"note"` - SpenderId string `json:"spenderId"` + SpenderId string `json:"spender_id"` +} + +type CreateTransactionRequest struct { + Date *time.Time `json:"date"` + Amount float32 `json:"amount"` + Category string `json:"category"` + ImageUrl string `json:"image_url"` + Note string `json:"note"` + SpenderId string `json:"spender_id"` + TxnType string `json:"transaction_type"` +} + +type CreateTransactionResponse struct { + ID int `json:"id"` +} + +type SummaryResponse struct { + TotalAmountSpend float32 `json:"total_amount_spend"` + AvgAmountSpendPerDay float32 `json:"average_amount_spend_per_day"` + Total int `json:"total"` +} + +type BalanceResponse struct { + TotalAmountEarned float32 `json:"total_amount_earned"` + TotalAmountSpend float32 `json:"total_amount_spend"` + TotalAmountSaved float32 `json:"total_amount_saved"` +} + +type GetTransactionResponse struct { + ID int `json:"id"` + Date *time.Time `json:"date"` + Amount float32 `json:"amount"` + Category string `json:"category"` + ImageUrl string `json:"image_url"` + Note string `json:"note"` + SpenderId string `json:"spender_id"` + TxnType string `json:"transaction_type"` }