user package
This commit is contained in:
parent
f962cd8cd9
commit
f159014374
4 changed files with 84 additions and 0 deletions
24
internal/user/handler.go
Normal file
24
internal/user/handler.go
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"merch-api/pkg/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Handler struct {
|
||||||
|
*service
|
||||||
|
}
|
||||||
|
|
||||||
|
type Deps struct {
|
||||||
|
DB *sql.DB
|
||||||
|
Utils utils.Utils
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(deps Deps) *Handler {
|
||||||
|
r := newRepository(deps.DB)
|
||||||
|
s := newService(r, deps.Utils)
|
||||||
|
|
||||||
|
return &Handler{
|
||||||
|
service: s,
|
||||||
|
}
|
||||||
|
}
|
||||||
5
internal/user/interface.go
Normal file
5
internal/user/interface.go
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
package user
|
||||||
|
|
||||||
|
type Provider interface {
|
||||||
|
GetUserId(userUuid string) (string, error)
|
||||||
|
}
|
||||||
29
internal/user/repository.go
Normal file
29
internal/user/repository.go
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
package user
|
||||||
|
|
||||||
|
import "database/sql"
|
||||||
|
|
||||||
|
type Repository interface {
|
||||||
|
getUserId(userUuid string) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type repo struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRepository(db *sql.DB) Repository {
|
||||||
|
return &repo{
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *repo) getUserId(userUuid string) (string, error) {
|
||||||
|
q := `SELECT id FROM users WHERE uuid = $1 AND deleted_at IS NULL LIMIT 1`
|
||||||
|
row := r.db.QueryRow(q, userUuid)
|
||||||
|
|
||||||
|
var id string
|
||||||
|
if err := row.Scan(&id); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
26
internal/user/service.go
Normal file
26
internal/user/service.go
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"merch-api/pkg/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
type service struct {
|
||||||
|
repo Repository
|
||||||
|
utils utils.Utils
|
||||||
|
}
|
||||||
|
|
||||||
|
func newService(repo Repository, utils utils.Utils) *service {
|
||||||
|
return &service{
|
||||||
|
repo: repo,
|
||||||
|
utils: utils,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *service) GetUserId(userUuid string) (string, error) {
|
||||||
|
if userUuid == "" {
|
||||||
|
return "", errors.New("user uuid is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.repo.getUserId(userUuid)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue