Skip to content

Commit

Permalink
initial structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ktm-m committed May 19, 2024
1 parent 827f92c commit af59f5d
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 3 deletions.
8 changes: 7 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

{
Expand Down
30 changes: 30 additions & 0 deletions api/transaction/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
25 changes: 25 additions & 0 deletions api/transaction/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
30 changes: 30 additions & 0 deletions api/transaction/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
41 changes: 39 additions & 2 deletions api/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit af59f5d

Please sign in to comment.