Skip to content

Commit

Permalink
implement update transaction logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitong23 committed May 12, 2024
1 parent 321ba58 commit 4439ebe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
23 changes: 22 additions & 1 deletion api/transaction/transaction_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type handlerTransaction struct {

const (
cStmt = `INSERT INTO transaction (date, amount, category, transaction_type, spender_id, note, image_url) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *;`
uStmt = `UPDATE transaction SET date = $1, amount = $2, category = $3, transaction_type = $4, spender_id = $5, note = $6, image_url = $7 WHERE id = $8 RETURNING *;`
)

func NewHandler(cfg config.FeatureFlag, db *sql.DB) *handlerTransaction {
Expand Down Expand Up @@ -48,5 +49,25 @@ func (h handlerTransaction) Create(c echo.Context) error {
}

func (h handler) Update(c echo.Context) error {
return c.JSON(http.StatusOK, "not implemented yet")

logger := mlog.L(c)
ctx := c.Request().Context()
var trBody TransactionReqBody
err := c.Bind(&trBody)
if err != nil {
logger.Error("bad request body", zap.Error(err))
return c.JSON(http.StatusBadRequest, err.Error())
}

id := c.Param("id")
var updatedTransaction Transaction
err = h.db.QueryRowContext(ctx, uStmt, trBody.Date, trBody.Amount, trBody.Category, trBody.TransactionType, trBody.SpenderID, trBody.Note, trBody.ImageURL, id).Scan(&updatedTransaction.ID, &updatedTransaction.Date, &updatedTransaction.Amount, &updatedTransaction.Category, &updatedTransaction.TransactionType, &updatedTransaction.SpenderID, &updatedTransaction.Note, &updatedTransaction.ImageURL)

if err != nil {
logger.Error("query row error", zap.Error(err))
return c.JSON(http.StatusInternalServerError, err.Error())
}

logger.Info("create successfully", zap.String("id", updatedTransaction.ID))
return c.JSON(http.StatusOK, updatedTransaction)
}
25 changes: 25 additions & 0 deletions api/transaction/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ func TestCreateTransaction(t *testing.T) {

func TestUpdateTransaction(t *testing.T) {
t.Run("update transaction", func(t *testing.T) {

// e := echo.New()
// defer e.Close()

// req := httptest.NewRequest(http.MethodPut, "/", strings.NewReader(`{"date": "2021-08-01", "amount": 1000, "category": "food", "transaction_type": "expense", "spender_id": 1, "note": "lunch", "image_url": "http://image.com"}`))
// req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
// rec := httptest.NewRecorder()
// c := e.NewContext(req, rec)

// db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
// if err != nil {
// t.Fatalf("error creating mock: %v", err)
// }
// defer db.Close()

// column := []string{"id", "date", "amount", "category", "transaction_type", "spender_id", "note", "image_url"}
// // mock.ExpectQuery(uStmt).WithArgs("2021-08-01", 1000.0, "food", "expense", 1, "lunch", "http://image.com").WillReturnRows(sqlmock.NewRows(column).AddRow(1, "2021-08-01", 1000, "food", "expense", 1, "lunch", "http://image.com"))
// mock.ExpectQuery(uStmt).WithArgs("2021-08-01", 555.0, "shopping", "expense", 1, "lunch", "http://image.com", 1).WillReturnRows(sqlmock.NewRows(column).AddRow(1, "2021-08-01", 1000, "food", "expense", 1, "lunch", "http://image.com"))

// h := New(config.FeatureFlag{}, db)
// err = h.Update(c)
// if err != nil {
// t.Fatalf("error creating transaction: %v", err)
// }

t.Skip("not implemented yet")
})
}

0 comments on commit 4439ebe

Please sign in to comment.