Skip to content

Commit

Permalink
feat: Implemented delete operation in user repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Knoblauchpilze committed Mar 25, 2024
1 parent 7d9a604 commit 2b267c5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@ curl -X GET http://localhost:60001/v1/users/users/08ce96a3-3430-48a8-a3b2-b1c987

## Create new user
```bash
curl -H "Content-Type: application/json" -X POST http://localhost:60001/v1/users/users -d '{"email":"[email protected]","password":"1234"}'| jq
curl -H "Content-Type: application/json" -X POST http://localhost:60001/v1/users/users -d '{"email":"[email protected]","password":"1234"}' | jq
```

## Delete user
```bash
curl -X DELETE http://localhost:60001/v1/users/users/08ce96a3-3430-48a8-a3b2-b1c987a207cc | jq
```
6 changes: 3 additions & 3 deletions pkg/db/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Connection interface {
Close()

Query(ctx context.Context, sql string, arguments ...interface{}) Rows
Exec(ctx context.Context, sql string, arguments ...interface{}) (string, error)
Exec(ctx context.Context, sql string, arguments ...interface{}) (int, error)
}

type pgxDbConnection interface {
Expand Down Expand Up @@ -72,10 +72,10 @@ func (c *connectionImpl) Query(ctx context.Context, sql string, args ...interfac
return newRows(rows, err)
}

func (c *connectionImpl) Exec(ctx context.Context, sql string, args ...interface{}) (string, error) {
func (c *connectionImpl) Exec(ctx context.Context, sql string, args ...interface{}) (int, error) {
log := middleware.GetLoggerFromContext(ctx)
log.Debugf("Exec: %s (%d)", sql, len(args))

tag, err := c.pool.ExecEx(ctx, sql, nil, args...)
return string(tag), err
return int(tag.RowsAffected()), err
}
17 changes: 12 additions & 5 deletions pkg/repositories/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ func NewUserRepository(conn db.Connection) UserRepository {
}
}

const sqlCreateUserTemplate = "INSERT INTO api_user (id, email, password, created_at) VALUES($1, $2, $3, $4)"
const createUserSqlTemplate = "INSERT INTO api_user (id, email, password, created_at) VALUES($1, $2, $3, $4)"

func (r *userRepositoryImpl) Create(ctx context.Context, user persistence.User) error {
_, err := r.conn.Exec(ctx, sqlCreateUserTemplate, user.Id, user.Email, user.Password, user.CreatedAt)
_, err := r.conn.Exec(ctx, createUserSqlTemplate, user.Id, user.Email, user.Password, user.CreatedAt)
return err
}

const sqlQueryUserTemplate = "SELECT id, email, password, created_at, updated_at FROM api_user WHERE id = $1"
const getUserSqlTemplate = "SELECT id, email, password, created_at, updated_at FROM api_user WHERE id = $1"

func (r *userRepositoryImpl) Get(ctx context.Context, id uuid.UUID) (persistence.User, error) {
res := r.conn.Query(ctx, sqlQueryUserTemplate, id)
res := r.conn.Query(ctx, getUserSqlTemplate, id)
if err := res.Err(); err != nil {
return persistence.User{}, err
}
Expand All @@ -57,6 +57,13 @@ func (r *userRepositoryImpl) Update(ctx context.Context, user persistence.User)
return persistence.User{}, errors.NewCode(errors.NotImplementedCode)
}

const deleteUserSqlTemplate = "DELETE FROM api_user WHERE id = $1"

func (r *userRepositoryImpl) Delete(ctx context.Context, id uuid.UUID) error {
return errors.NewCode(errors.NotImplementedCode)
affected, err := r.conn.Exec(ctx, deleteUserSqlTemplate, id)
if affected != 1 {
return errors.NewCode(db.NoMatchingSqlRows)
}

return err
}
4 changes: 2 additions & 2 deletions pkg/repositories/user_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ func (m *mockConnection) Query(ctx context.Context, sql string, arguments ...int
return &m.rows
}

func (m *mockConnection) Exec(ctx context.Context, sql string, arguments ...interface{}) (string, error) {
func (m *mockConnection) Exec(ctx context.Context, sql string, arguments ...interface{}) (int, error) {
m.execCalled++
m.sqlQuery = sql
m.args = append(m.args, arguments...)
return "", m.execErr
return 0, m.execErr
}

func (m *mockRows) Err() error { return m.err }
Expand Down

0 comments on commit 2b267c5

Please sign in to comment.