feat: get user/staff profile
This commit is contained in:
parent
290c0edff7
commit
5fb0764630
@ -12,7 +12,7 @@ func (sr *StaffRepository) GetStaffByEmail(email string) (*authdomain.Staff, err
|
|||||||
var staff authdomain.Staff
|
var staff authdomain.Staff
|
||||||
|
|
||||||
if email == "" {
|
if email == "" {
|
||||||
return nil, errors.New("email is empty")
|
return nil, errors.New("email is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := sr.DB.Where("email = ?", email).First(&staff).Error; err != nil {
|
if err := sr.DB.Where("email = ?", email).First(&staff).Error; err != nil {
|
||||||
25
internal/accessor/staff/get_by_id.go
Normal file
25
internal/accessor/staff/get_by_id.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package staffrepository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
authdomain "legalgo-BE-go/internal/domain/auth"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (sr *StaffRepository) GetStaffByID(ID string) (*authdomain.Staff, error) {
|
||||||
|
var staff authdomain.Staff
|
||||||
|
|
||||||
|
if ID == "" {
|
||||||
|
return nil, errors.New("id is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := sr.DB.Where("id = ? ", ID).First(&staff).Error; err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, errors.New("staff not found")
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &staff, nil
|
||||||
|
}
|
||||||
@ -11,7 +11,9 @@ type StaffRepository struct {
|
|||||||
|
|
||||||
type StaffIntf interface {
|
type StaffIntf interface {
|
||||||
GetStaffByEmail(string) (*authdomain.Staff, error)
|
GetStaffByEmail(string) (*authdomain.Staff, error)
|
||||||
|
GetStaffByID(string) (*authdomain.Staff, error)
|
||||||
Create(*authdomain.Staff) (*authdomain.Staff, error)
|
Create(*authdomain.Staff) (*authdomain.Staff, error)
|
||||||
|
Update(authdomain.Staff) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(db *database.DB) StaffIntf {
|
func New(db *database.DB) StaffIntf {
|
||||||
|
|||||||
@ -1,7 +1,19 @@
|
|||||||
package staffrepository
|
package staffrepository
|
||||||
|
|
||||||
func (ur *StaffRepository) Update() error {
|
import (
|
||||||
// if err := ur.DB.Update(column string, value interface{})
|
authdomain "legalgo-BE-go/internal/domain/auth"
|
||||||
|
"legalgo-BE-go/internal/utilities/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (ur *StaffRepository) Update(spec authdomain.Staff) error {
|
||||||
|
val, err := utils.StructToMap(spec)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ur.DB.Model(&authdomain.Staff{}).Where("id = ?", spec.ID).Updates(val).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
30
internal/accessor/user_repository/get_user_by_id.go
Normal file
30
internal/accessor/user_repository/get_user_by_id.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package userrepository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
authdomain "legalgo-BE-go/internal/domain/auth"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (ur *UserRepository) GetUserByID(email string) (*authdomain.UserProfile, error) {
|
||||||
|
var users []authdomain.UserProfile
|
||||||
|
|
||||||
|
if email == "" {
|
||||||
|
return nil, errors.New("email is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ur.DB.Table("users u").
|
||||||
|
Select("u.email, u.id, s.status as subscribe_status, sp.code as subscribe_plan_code, sp.name as subscribe_plan_name").
|
||||||
|
Joins("join subscribes s on s.id = u.subscribe_id").
|
||||||
|
Joins("join subscribe_plans sp on s.subscribe_plan_id = sp.id").
|
||||||
|
Scan(&users).Error; err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, errors.New("user not found")
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &users[0], nil
|
||||||
|
}
|
||||||
@ -11,6 +11,7 @@ type UserRepository struct {
|
|||||||
|
|
||||||
type UserIntf interface {
|
type UserIntf interface {
|
||||||
GetUserByEmail(string) (*authdomain.User, error)
|
GetUserByEmail(string) (*authdomain.User, error)
|
||||||
|
GetUserByID(string) (*authdomain.UserProfile, error)
|
||||||
CreateUser(*authdomain.User) (*authdomain.User, error)
|
CreateUser(*authdomain.User) (*authdomain.User, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -8,5 +8,8 @@ var Module = fx.Module("auth-api",
|
|||||||
LoginUser,
|
LoginUser,
|
||||||
RegisterUser,
|
RegisterUser,
|
||||||
RegisterStaff,
|
RegisterStaff,
|
||||||
|
UpdateStaff,
|
||||||
|
GetStaffProfile,
|
||||||
|
GetUserProfile,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
82
internal/api/http/auth/profile.go
Normal file
82
internal/api/http/auth/profile.go
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
package authhttp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
authsvc "legalgo-BE-go/internal/services/auth"
|
||||||
|
"legalgo-BE-go/internal/utilities/response"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetStaffProfile(
|
||||||
|
router chi.Router,
|
||||||
|
authSvc authsvc.AuthIntf,
|
||||||
|
) {
|
||||||
|
router.Get("/staff/{id}/profile", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := r.Context()
|
||||||
|
id := chi.URLParam(r, "id")
|
||||||
|
if id == "" {
|
||||||
|
response.ResponseWithErrorCode(
|
||||||
|
ctx,
|
||||||
|
w,
|
||||||
|
errors.New("provided id is empty"),
|
||||||
|
response.ErrBadRequest.Code,
|
||||||
|
response.ErrBadRequest.HttpCode,
|
||||||
|
"required params is not provided",
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
staffProfile, err := authSvc.GetStaffProfile(id)
|
||||||
|
if err != nil {
|
||||||
|
response.ResponseWithErrorCode(
|
||||||
|
ctx,
|
||||||
|
w,
|
||||||
|
err,
|
||||||
|
response.ErrBadRequest.Code,
|
||||||
|
response.ErrBadRequest.HttpCode,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response.RespondJsonSuccess(ctx, w, staffProfile)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetUserProfile(
|
||||||
|
router chi.Router,
|
||||||
|
authSvc authsvc.AuthIntf,
|
||||||
|
) {
|
||||||
|
router.Get("/user/{id}/profile", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := r.Context()
|
||||||
|
id := chi.URLParam(r, "id")
|
||||||
|
if id == "" {
|
||||||
|
response.ResponseWithErrorCode(
|
||||||
|
ctx,
|
||||||
|
w,
|
||||||
|
errors.New("provided id is empty"),
|
||||||
|
response.ErrBadRequest.Code,
|
||||||
|
response.ErrBadRequest.HttpCode,
|
||||||
|
"required params is not provided",
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userProfile, err := authSvc.GetUserProfile(id)
|
||||||
|
if err != nil {
|
||||||
|
response.ResponseWithErrorCode(
|
||||||
|
ctx,
|
||||||
|
w,
|
||||||
|
err,
|
||||||
|
response.ErrBadRequest.Code,
|
||||||
|
response.ErrBadRequest.HttpCode,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response.RespondJsonSuccess(ctx, w, userProfile)
|
||||||
|
})
|
||||||
|
}
|
||||||
75
internal/api/http/auth/update.go
Normal file
75
internal/api/http/auth/update.go
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package authhttp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
authdomain "legalgo-BE-go/internal/domain/auth"
|
||||||
|
authsvc "legalgo-BE-go/internal/services/auth"
|
||||||
|
"legalgo-BE-go/internal/utilities/response"
|
||||||
|
"legalgo-BE-go/internal/utilities/utils"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
func UpdateStaff(
|
||||||
|
router chi.Router,
|
||||||
|
authSvc authsvc.AuthIntf,
|
||||||
|
) {
|
||||||
|
router.Patch("/staff/{id}/update", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := r.Context()
|
||||||
|
|
||||||
|
id := chi.URLParam(r, "id")
|
||||||
|
if id == "" {
|
||||||
|
response.ResponseWithErrorCode(
|
||||||
|
ctx,
|
||||||
|
w,
|
||||||
|
errors.New("provided id is empty"),
|
||||||
|
response.ErrBadRequest.Code,
|
||||||
|
response.ErrBadRequest.HttpCode,
|
||||||
|
"required params is not provided",
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var spec authdomain.RegisterStaffReq
|
||||||
|
|
||||||
|
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||||
|
response.ResponseWithErrorCode(
|
||||||
|
ctx,
|
||||||
|
w,
|
||||||
|
err,
|
||||||
|
response.ErrBadRequest.Code,
|
||||||
|
response.ErrBadRequest.HttpCode,
|
||||||
|
"failed to unmarshal request",
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
staff := authdomain.Staff{
|
||||||
|
ID: id,
|
||||||
|
Email: spec.Email,
|
||||||
|
Password: spec.Password,
|
||||||
|
Username: spec.Username,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := authSvc.UpdateStaff(staff); err != nil {
|
||||||
|
response.ResponseWithErrorCode(
|
||||||
|
ctx,
|
||||||
|
w,
|
||||||
|
err,
|
||||||
|
response.ErrBadRequest.Code,
|
||||||
|
response.ErrBadRequest.HttpCode,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
responsePayload := struct{
|
||||||
|
Message string
|
||||||
|
} {
|
||||||
|
Message: "update staff success",
|
||||||
|
}
|
||||||
|
|
||||||
|
response.RespondJsonSuccess(ctx, w, responsePayload)
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -13,4 +13,19 @@ type LoginRepoResponse struct {
|
|||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type StaffProfile struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserProfile struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
SubscribePlanCode string `json:"subscribe_plan_code"`
|
||||||
|
SubscribePlanName string `json:"subscribe_plan_name"`
|
||||||
|
SubscribeStatus string `json:"subscribe_status"`
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,6 +21,12 @@ type RegisterStaffReq struct {
|
|||||||
Username string `json:"username" validate:"required"`
|
Username string `json:"username" validate:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdateStaffReq struct {
|
||||||
|
Email string `json:"email"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
}
|
||||||
|
|
||||||
type Staff struct {
|
type Staff struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
|
|||||||
18
internal/services/auth/get_staff.go
Normal file
18
internal/services/auth/get_staff.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package authsvc
|
||||||
|
|
||||||
|
import authdomain "legalgo-BE-go/internal/domain/auth"
|
||||||
|
|
||||||
|
func (as *AuthSvc) GetStaffProfile(id string) (*authdomain.StaffProfile, error) {
|
||||||
|
staff, err := as.staffRepo.GetStaffByID(id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
profile := &authdomain.StaffProfile{
|
||||||
|
ID: staff.ID,
|
||||||
|
Username: staff.Username,
|
||||||
|
Email: staff.Email,
|
||||||
|
}
|
||||||
|
|
||||||
|
return profile, nil
|
||||||
|
}
|
||||||
12
internal/services/auth/get_user.go
Normal file
12
internal/services/auth/get_user.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package authsvc
|
||||||
|
|
||||||
|
import authdomain "legalgo-BE-go/internal/domain/auth"
|
||||||
|
|
||||||
|
func (as *AuthSvc) GetUserProfile(id string) (*authdomain.UserProfile, error) {
|
||||||
|
user, err := as.userRepo.GetUserByID(id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
@ -17,9 +17,13 @@ type AuthSvc struct {
|
|||||||
|
|
||||||
type AuthIntf interface {
|
type AuthIntf interface {
|
||||||
LoginAsStaff(authdomain.LoginReq) (string, error)
|
LoginAsStaff(authdomain.LoginReq) (string, error)
|
||||||
LoginAsUser(authdomain.LoginReq) (string, error)
|
|
||||||
RegisterUser(authdomain.RegisterUserReq) (string, error)
|
RegisterUser(authdomain.RegisterUserReq) (string, error)
|
||||||
|
GetUserProfile(string) (*authdomain.UserProfile, error)
|
||||||
|
|
||||||
|
LoginAsUser(authdomain.LoginReq) (string, error)
|
||||||
RegisterStaff(authdomain.RegisterStaffReq) (string, error)
|
RegisterStaff(authdomain.RegisterStaffReq) (string, error)
|
||||||
|
UpdateStaff(authdomain.Staff) error
|
||||||
|
GetStaffProfile(string) (*authdomain.StaffProfile, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(
|
func New(
|
||||||
|
|||||||
@ -23,6 +23,7 @@ func (a *AuthSvc) RegisterStaff(spec authdomain.RegisterStaffReq) (string, error
|
|||||||
ID: uuid.NewString(),
|
ID: uuid.NewString(),
|
||||||
Email: spec.Email,
|
Email: spec.Email,
|
||||||
Password: hashedPwd,
|
Password: hashedPwd,
|
||||||
|
Username: spec.Username,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = a.staffRepo.Create(&user)
|
_, err = a.staffRepo.Create(&user)
|
||||||
|
|||||||
15
internal/services/auth/update_staff.go
Normal file
15
internal/services/auth/update_staff.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package authsvc
|
||||||
|
|
||||||
|
import authdomain "legalgo-BE-go/internal/domain/auth"
|
||||||
|
|
||||||
|
func (as *AuthSvc) UpdateStaff(spec authdomain.Staff) error {
|
||||||
|
if _, err := as.staffRepo.GetStaffByID(spec.ID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := as.staffRepo.Update(spec); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
29
internal/utilities/utils/struct_to_map.go
Normal file
29
internal/utilities/utils/struct_to_map.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
func StructToMap(s any) (map[string]any, error) {
|
||||||
|
result := make(map[string]any)
|
||||||
|
|
||||||
|
val := reflect.ValueOf(s)
|
||||||
|
|
||||||
|
if val.Kind() != reflect.Struct {
|
||||||
|
return nil, errors.New("provided value is not struct")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range val.NumField() {
|
||||||
|
field := val.Type().Field(i)
|
||||||
|
value := val.Field(i)
|
||||||
|
|
||||||
|
if value.IsZero() || value.IsNil() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
result[field.Name] = value.Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
92
openapi.yml
92
openapi.yml
@ -120,6 +120,98 @@ paths:
|
|||||||
message:
|
message:
|
||||||
type: string
|
type: string
|
||||||
|
|
||||||
|
/staff/{id}/profile:
|
||||||
|
get:
|
||||||
|
summary: "get staff profile"
|
||||||
|
tags:
|
||||||
|
- staff
|
||||||
|
parameters:
|
||||||
|
- name: "id"
|
||||||
|
in: query
|
||||||
|
description: "staff id"
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: Success get profile
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
data:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
id:
|
||||||
|
type: string
|
||||||
|
email:
|
||||||
|
type: string
|
||||||
|
username:
|
||||||
|
type: string
|
||||||
|
"400":
|
||||||
|
description: Bad request
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
error:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
code:
|
||||||
|
type: string
|
||||||
|
message:
|
||||||
|
type: string
|
||||||
|
|
||||||
|
/user/{id}/profile:
|
||||||
|
get:
|
||||||
|
summary: "get staff profile"
|
||||||
|
tags:
|
||||||
|
- user
|
||||||
|
parameters:
|
||||||
|
- name: "id"
|
||||||
|
in: query
|
||||||
|
description: "user id"
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: Success get profile
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
data:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
id:
|
||||||
|
type: string
|
||||||
|
email:
|
||||||
|
type: string
|
||||||
|
subscribe_plan_code:
|
||||||
|
type: string
|
||||||
|
subscribe_plan_name:
|
||||||
|
type: string
|
||||||
|
subscribe_status:
|
||||||
|
type: string
|
||||||
|
"400":
|
||||||
|
description: Bad request
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
error:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
code:
|
||||||
|
type: string
|
||||||
|
message:
|
||||||
|
type: string
|
||||||
|
|
||||||
/user/login:
|
/user/login:
|
||||||
post:
|
post:
|
||||||
summary: Login for user
|
summary: Login for user
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user