Skip to content

Commit

Permalink
✅ Update router
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono committed Nov 8, 2024
1 parent a5467fe commit 4cc12ba
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
13 changes: 11 additions & 2 deletions router/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

type Transaction struct {
ID uuid.UUID `json:"id"`
Title string `json:"title"`
Amount int `json:"amount"`
Target string `json:"target"`
Request *uuid.UUID `json:"request"`
Expand All @@ -26,6 +27,7 @@ type Transaction struct {
}

type TransactionOverview struct {
Title string `json:"title"`
Amount int `json:"amount"`
Targets []*string `json:"targets"`
Tags []*uuid.UUID `json:"tags"`
Expand All @@ -34,6 +36,7 @@ type TransactionOverview struct {
}

type TransactionOverviewWithOneTarget struct {
Title string `json:"title"`
Amount int `json:"amount"`
Target string `json:"target"`
Tags []*uuid.UUID `json:"tags"`
Expand Down Expand Up @@ -160,6 +163,7 @@ func (h Handlers) GetTransactions(c echo.Context) error {
}
return &Transaction{
ID: tx.ID,
Title: tx.Title,
Amount: tx.Amount,
Target: tx.Target,
Request: tx.Request,
Expand All @@ -175,6 +179,7 @@ func (h Handlers) GetTransactions(c echo.Context) error {

func (h Handlers) PostTransaction(c echo.Context) error {
var tx *TransactionOverview
// TODO: validate
if err := c.Bind(&tx); err != nil {
h.Logger.Info("could not get transaction overview from request", zap.Error(err))
return echo.NewHTTPError(http.StatusBadRequest, err)
Expand All @@ -189,7 +194,7 @@ func (h Handlers) PostTransaction(c echo.Context) error {
}
created, err := h.Repository.CreateTransaction(
ctx,
tx.Amount, *target, tx.Tags, tx.Group, tx.Request)
tx.Title, tx.Amount, *target, tx.Tags, tx.Group, tx.Request)
if err != nil {
h.Logger.Error("failed to create transaction in repository", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
Expand Down Expand Up @@ -217,6 +222,7 @@ func (h Handlers) PostTransaction(c echo.Context) error {
}
res := Transaction{
ID: created.ID,
Title: created.Title,
Amount: created.Amount,
Target: created.Target,
Request: created.Request,
Expand Down Expand Up @@ -267,6 +273,7 @@ func (h Handlers) GetTransaction(c echo.Context) error {
}
res := Transaction{
ID: tx.ID,
Title: tx.Title,
Amount: tx.Amount,
Target: tx.Target,
Request: tx.Request,
Expand All @@ -287,6 +294,7 @@ func (h Handlers) PutTransaction(c echo.Context) error {
}

var tx *TransactionOverviewWithOneTarget
// TODO: validate
if err := c.Bind(&tx); err != nil {
h.Logger.Info(
"could not get transaction overview with one target from request",
Expand All @@ -297,7 +305,7 @@ func (h Handlers) PutTransaction(c echo.Context) error {
ctx := c.Request().Context()
updated, err := h.Repository.UpdateTransaction(
ctx,
txID, tx.Amount, tx.Target, tx.Tags, tx.Group, tx.Request)
txID, tx.Title, tx.Amount, tx.Target, tx.Tags, tx.Group, tx.Request)
if err != nil {
h.Logger.Error("failed to update transaction in repository", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
Expand Down Expand Up @@ -325,6 +333,7 @@ func (h Handlers) PutTransaction(c echo.Context) error {
}
res := Transaction{
ID: updated.ID,
Title: updated.Title,
Amount: updated.Amount,
Target: updated.Target,
Request: updated.Request,
Expand Down
34 changes: 27 additions & 7 deletions router/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestHandlers_GetTransactions(t *testing.T) {
}
tx1 := &model.TransactionResponse{
ID: uuid.New(),
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: random.AlphaNumeric(t, 20),
Tags: []*model.Tag{
Expand All @@ -58,6 +59,7 @@ func TestHandlers_GetTransactions(t *testing.T) {

tx2 := &model.TransactionResponse{
ID: uuid.New(),
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: random.AlphaNumeric(t, 20),
Tags: []*model.Tag{
Expand Down Expand Up @@ -120,6 +122,7 @@ func TestHandlers_GetTransactions(t *testing.T) {
}
return &Transaction{
ID: tx.ID,
Title: tx.Title,
Amount: tx.Amount,
Target: tx.Target,
Tags: tag,
Expand Down Expand Up @@ -154,6 +157,7 @@ func TestHandlers_GetTransactions(t *testing.T) {
}
tx1 := &model.TransactionResponse{
ID: uuid.New(),
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: random.AlphaNumeric(t, 20),
Tags: []*model.Tag{
Expand All @@ -173,6 +177,7 @@ func TestHandlers_GetTransactions(t *testing.T) {

tx2 := &model.TransactionResponse{
ID: uuid.New(),
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: random.AlphaNumeric(t, 20),
Tags: []*model.Tag{
Expand Down Expand Up @@ -233,6 +238,7 @@ func TestHandlers_GetTransactions(t *testing.T) {
}
return &Transaction{
ID: tx.ID,
Title: tx.Title,
Amount: tx.Amount,
Target: tx.Target,
Tags: tag,
Expand Down Expand Up @@ -267,6 +273,7 @@ func TestHandlers_GetTransactions(t *testing.T) {
}
tx1 := &model.TransactionResponse{
ID: uuid.New(),
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: random.AlphaNumeric(t, 20),
Tags: []*model.Tag{
Expand All @@ -286,6 +293,7 @@ func TestHandlers_GetTransactions(t *testing.T) {

tx2 := &model.TransactionResponse{
ID: uuid.New(),
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: random.AlphaNumeric(t, 20),
Tags: []*model.Tag{
Expand Down Expand Up @@ -347,6 +355,7 @@ func TestHandlers_GetTransactions(t *testing.T) {
}
return &Transaction{
ID: tx.ID,
Title: tx.Title,
Amount: tx.Amount,
Target: tx.Target,
Tags: tag,
Expand Down Expand Up @@ -384,6 +393,7 @@ func TestHandlers_GetTransactions(t *testing.T) {

tx1 := &model.TransactionResponse{
ID: uuid.New(),
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: target1,
Tags: []*model.Tag{
Expand All @@ -403,6 +413,7 @@ func TestHandlers_GetTransactions(t *testing.T) {

tx2 := &model.TransactionResponse{
ID: uuid.New(),
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: random.AlphaNumeric(t, 20),
Tags: []*model.Tag{
Expand Down Expand Up @@ -466,6 +477,7 @@ func TestHandlers_GetTransactions(t *testing.T) {
}
return &Transaction{
ID: tx.ID,
Title: tx.Title,
Amount: tx.Amount,
Target: tx.Target,
Tags: tag,
Expand Down Expand Up @@ -503,6 +515,7 @@ func TestHandlers_GetTransactions(t *testing.T) {

tx1 := &model.TransactionResponse{
ID: uuid.New(),
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: target1,
Tags: []*model.Tag{
Expand Down Expand Up @@ -569,6 +582,7 @@ func TestHandlers_GetTransactions(t *testing.T) {
}
return &Transaction{
ID: tx.ID,
Title: tx.Title,
Amount: tx.Amount,
Target: tx.Target,
Tags: tag,
Expand Down Expand Up @@ -612,6 +626,7 @@ func TestHandlers_PostTransaction(t *testing.T) {

tx1 := &model.TransactionResponse{
ID: uuid.New(),
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: target1,
Tags: []*model.Tag{
Expand Down Expand Up @@ -652,7 +667,7 @@ func TestHandlers_PostTransaction(t *testing.T) {

h.Repository.MockTransactionRepository.
EXPECT().
CreateTransaction(c.Request().Context(), tx1.Amount, tx1.Target, tags, &group, nil).
CreateTransaction(c.Request().Context(), tx1.Title, tx1.Amount, tx1.Target, tags, &group, nil).
Return(tx1, nil)

res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction {
Expand All @@ -675,6 +690,7 @@ func TestHandlers_PostTransaction(t *testing.T) {
}
return &Transaction{
ID: tx.ID,
Title: tx.Title,
Amount: tx.Amount,
Target: tx.Target,
Tags: tag,
Expand Down Expand Up @@ -712,6 +728,7 @@ func TestHandlers_PostTransaction(t *testing.T) {

tx := &model.TransactionResponse{
ID: uuid.New(),
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: target1,
Tags: []*model.Tag{
Expand Down Expand Up @@ -767,7 +784,7 @@ func TestHandlers_PostTransaction(t *testing.T) {
EXPECT().
CreateTransaction(
c.Request().Context(),
tx.Amount, tx.Target,
tx.Title, tx.Amount, tx.Target,
tags, &group, &request.ID).
Return(tx, nil)

Expand All @@ -791,6 +808,7 @@ func TestHandlers_PostTransaction(t *testing.T) {
res := []*Transaction{
{
ID: tx.ID,
Title: tx.Title,
Amount: tx.Amount,
Target: tx.Target,
Tags: to,
Expand Down Expand Up @@ -827,6 +845,7 @@ func TestHandlers_GetTransaction(t *testing.T) {

tx := &model.TransactionResponse{
ID: uuid.New(),
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: random.AlphaNumeric(t, 20),
Tags: []*model.Tag{
Expand Down Expand Up @@ -861,7 +880,6 @@ func TestHandlers_GetTransaction(t *testing.T) {
GetTransaction(c.Request().Context(), tx.ID).
Return(tx, nil)

var resOverview Transaction
to := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview {
return &TagOverview{
ID: modelTag.ID,
Expand All @@ -879,8 +897,9 @@ func TestHandlers_GetTransaction(t *testing.T) {
CreatedAt: tx.Group.CreatedAt,
UpdatedAt: tx.Group.UpdatedAt,
}
resOverview = Transaction{
resOverview := Transaction{
ID: tx.ID,
Title: tx.Title,
Amount: tx.Amount,
Target: tx.Target,
Tags: to,
Expand Down Expand Up @@ -924,6 +943,7 @@ func TestHandlers_PutTransaction(t *testing.T) {

tx := &model.TransactionResponse{
ID: uuid.New(),
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: random.AlphaNumeric(t, 20),
Tags: []*model.Tag{
Expand Down Expand Up @@ -986,11 +1006,10 @@ func TestHandlers_PutTransaction(t *testing.T) {
EXPECT().
UpdateTransaction(
c.Request().Context(),
tx.ID, updated.Amount, updated.Target,
tx.ID, updated.Title, updated.Amount, updated.Target,
updatedTags, nil, nil).
Return(updated, nil)

var resOverview Transaction
to := lo.Map(updated.Tags, func(modelTag *model.Tag, _ int) *TagOverview {
return &TagOverview{
ID: modelTag.ID,
Expand All @@ -1007,8 +1026,9 @@ func TestHandlers_PutTransaction(t *testing.T) {
CreatedAt: updated.Group.CreatedAt,
UpdatedAt: updated.Group.UpdatedAt,
}
resOverview = Transaction{
resOverview := Transaction{
ID: tx.ID,
Title: updated.Title,
Amount: updated.Amount,
Target: updated.Target,
Tags: to,
Expand Down

0 comments on commit 4cc12ba

Please sign in to comment.