add and delete user methods
This commit is contained in:
parent
13f93cd03e
commit
ea33cdc9f5
5 changed files with 139 additions and 1 deletions
87
internal/user/controller.go
Normal file
87
internal/user/controller.go
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"merch-api/internal/appLog"
|
||||
"merch-api/pkg/responses"
|
||||
"merch-api/pkg/utils"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const controllerLogHeader string = "[Controller]"
|
||||
|
||||
type controller struct {
|
||||
service *service
|
||||
utils utils.Utils
|
||||
}
|
||||
|
||||
func newController(s *service, utils utils.Utils) *controller {
|
||||
return &controller{
|
||||
service: s,
|
||||
utils: utils,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) RegisterRoutes(r *gin.RouterGroup) {
|
||||
userGroup := r.Group("/user")
|
||||
|
||||
userGroup.POST("", h.controller.create)
|
||||
userGroup.DELETE("", h.controller.delete)
|
||||
}
|
||||
|
||||
// create godoc
|
||||
//
|
||||
// @Summary Create new user
|
||||
// @Description Adds local user record based on user uuid from auth service
|
||||
// @Tags Merch
|
||||
// @Accept json
|
||||
// @Success 201
|
||||
// @Failure 400 {object} responses.BadRequest
|
||||
// @Failure 401 {object} responses.Unauthorized
|
||||
// @Failure 500 {object} responses.InternalServerError
|
||||
// @Router /merch/create [POST]
|
||||
func (co *controller) create(c *gin.Context) {
|
||||
u, err := co.utils.GetUserUuidFromContext(c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, responses.BadRequest{Error: err.Error()})
|
||||
appLog.LogErr(pkgLogHeader, controllerLogHeader, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = co.service.createUser(c, u); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, responses.InternalServerError{Error: err.Error()})
|
||||
appLog.LogErr(pkgLogHeader, controllerLogHeader, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusCreated)
|
||||
}
|
||||
|
||||
// deleteMerch godoc
|
||||
//
|
||||
// @Summary Delete user
|
||||
// @Description Marks user as deleted by user uuid
|
||||
// @Tags Merch
|
||||
// @Accept json
|
||||
// @Param uuid path string true "merch uuid"
|
||||
// @Success 204
|
||||
// @Failure 400 {object} responses.BadRequest
|
||||
// @Failure 401 {object} responses.Unauthorized
|
||||
// @Failure 500 {object} responses.InternalServerError
|
||||
// @Router /merch/{uuid} [DELETE]
|
||||
func (co *controller) delete(c *gin.Context) {
|
||||
u, err := co.utils.GetUserUuidFromContext(c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, responses.BadRequest{Error: err.Error()})
|
||||
appLog.LogErr(pkgLogHeader, controllerLogHeader, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = co.service.deleteUser(c, u); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, responses.InternalServerError{Error: err.Error()})
|
||||
appLog.LogErr(pkgLogHeader, controllerLogHeader, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
|
@ -5,8 +5,11 @@ import (
|
|||
"merch-api/pkg/utils"
|
||||
)
|
||||
|
||||
const pkgLogHeader string = "User |"
|
||||
|
||||
type Handler struct {
|
||||
*service
|
||||
controller *controller
|
||||
}
|
||||
|
||||
type Deps struct {
|
||||
|
|
@ -17,8 +20,10 @@ type Deps struct {
|
|||
func New(deps Deps) *Handler {
|
||||
r := newRepository(deps.DB)
|
||||
s := newService(r, deps.Utils)
|
||||
c := newController(s, deps.Utils)
|
||||
|
||||
return &Handler{
|
||||
service: s,
|
||||
controller: c,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
14
internal/user/model.go
Normal file
14
internal/user/model.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
type userModel struct {
|
||||
Id int
|
||||
CreatedAt time.Time
|
||||
UpdatedAt sql.NullTime
|
||||
DeletedAt sql.NullTime
|
||||
Uuid string
|
||||
}
|
||||
|
|
@ -2,11 +2,14 @@ package user
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
getUserId(ctx context.Context, userUuid string) (int64, error)
|
||||
addUser(ctx context.Context, u *userModel) error
|
||||
deleteUser(ctx context.Context, userUuid string, deletedAt sql.NullTime) error
|
||||
}
|
||||
|
||||
type repo struct {
|
||||
|
|
@ -30,3 +33,17 @@ func (r *repo) getUserId(ctx context.Context, userUuid string) (int64, error) {
|
|||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (r *repo) addUser(ctx context.Context, u *userModel) error {
|
||||
q := `INSERT INTO users (created_at, updated_at, deleted_at, uuid) VALUES ($1, $2, $3, $4)`
|
||||
|
||||
_, err := r.db.Exec(ctx, q, u.CreatedAt, u.UpdatedAt, u.DeletedAt, u.Uuid)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *repo) deleteUser(ctx context.Context, userUuid string, deletedAt sql.NullTime) error {
|
||||
q := `UPDATE users SET deleted_at = $1 WHERE uuid = $2`
|
||||
|
||||
_, err := r.db.Exec(ctx, q, deletedAt, userUuid)
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,3 +25,18 @@ func (s *service) GetUserId(ctx context.Context, userUuid string) (int64, error)
|
|||
|
||||
return s.repo.getUserId(ctx, userUuid)
|
||||
}
|
||||
|
||||
func (s *service) createUser(ctx context.Context, userUuid string) error {
|
||||
now := s.utils.TimeNowUTC()
|
||||
|
||||
return s.repo.addUser(ctx, &userModel{
|
||||
CreatedAt: now,
|
||||
UpdatedAt: s.utils.NullTimeFromNow(now),
|
||||
DeletedAt: s.utils.DeletedNullTime(),
|
||||
Uuid: userUuid,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *service) deleteUser(ctx context.Context, userUuid string) error {
|
||||
return s.repo.deleteUser(ctx, userUuid, s.utils.NullTimeNowUTC())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue