forked from ebhomengo/niki
feat(admin): add admin my profile route(#217)
This commit is contained in:
parent
5359a0746f
commit
b87f02db05
|
|
@ -0,0 +1,47 @@
|
|||
package adminhandler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
|
||||
"git.gocasts.ir/ebhomengo/niki/pkg/claim"
|
||||
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// Profile godoc
|
||||
// @Summary Admin profile
|
||||
// @Tags Admins
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} adminserviceparam.ProfileResponse
|
||||
// @Failure 400 {string} "Bad request"
|
||||
// @Failure 401 {string} "invalid or expired jwt"
|
||||
// @Failure 403 {string} "user not allowed"
|
||||
// @Failure 404 {string} "record not found"
|
||||
// @Failure 500 {string} "something went wrong"
|
||||
// @Security AuthBearerAdmin
|
||||
// @Router /admins/profile [get].
|
||||
func (h Handler) Profile(c echo.Context) error {
|
||||
var req adminserviceparam.ProfileRequest
|
||||
|
||||
if bErr := c.Bind(&req); bErr != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
req.AdminID = claim.GetClaimsFromEchoContext(c).UserID
|
||||
resp, sErr := h.adminSvc.AdminGetProfile(c.Request().Context(), req)
|
||||
if sErr != nil {
|
||||
msg, code := httpmsg.Error(sErr)
|
||||
if resp.FieldErrors != nil {
|
||||
return c.JSON(code, echo.Map{
|
||||
"message": msg,
|
||||
"errors": resp.FieldErrors,
|
||||
})
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(code, msg)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
|
@ -14,6 +14,7 @@ func (h Handler) SetRoutes(e *echo.Echo) {
|
|||
r.POST("/register", h.Register, middleware.Auth(h.authSvc), middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminAdminRegisterPermission))
|
||||
r.POST("/login-by-phone", h.LoginByPhoneNumber)
|
||||
r.POST("/refresh-access", h.RefreshAccess)
|
||||
r.GET("/profile", h.Profile, middleware.Auth(h.authSvc))
|
||||
//nolint:gocritic
|
||||
//r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq"
|
||||
}
|
||||
|
|
|
|||
71
docs/docs.go
71
docs/docs.go
|
|
@ -1408,6 +1408,63 @@ const docTemplate = `{
|
|||
}
|
||||
}
|
||||
},
|
||||
"/admins/profile": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
"AuthBearerAdmin": []
|
||||
}
|
||||
],
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"Admins"
|
||||
],
|
||||
"summary": "Admin profile",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/adminserviceparam.ProfileResponse"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad request",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"401": {
|
||||
"description": "invalid or expired jwt",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"403": {
|
||||
"description": "user not allowed",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"404": {
|
||||
"description": "record not found",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"500": {
|
||||
"description": "something went wrong",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/admins/refer-times": {
|
||||
"get": {
|
||||
"security": [
|
||||
|
|
@ -4137,6 +4194,20 @@ const docTemplate = `{
|
|||
}
|
||||
}
|
||||
},
|
||||
"adminserviceparam.ProfileResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"$ref": "#/definitions/adminserviceparam.Data"
|
||||
},
|
||||
"field_errors": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"adminserviceparam.RefreshAccessRequest": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
|
|
|||
|
|
@ -1397,6 +1397,63 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"/admins/profile": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
"AuthBearerAdmin": []
|
||||
}
|
||||
],
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"Admins"
|
||||
],
|
||||
"summary": "Admin profile",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/adminserviceparam.ProfileResponse"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad request",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"401": {
|
||||
"description": "invalid or expired jwt",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"403": {
|
||||
"description": "user not allowed",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"404": {
|
||||
"description": "record not found",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"500": {
|
||||
"description": "something went wrong",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/admins/refer-times": {
|
||||
"get": {
|
||||
"security": [
|
||||
|
|
@ -4126,6 +4183,20 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"adminserviceparam.ProfileResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"$ref": "#/definitions/adminserviceparam.Data"
|
||||
},
|
||||
"field_errors": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"adminserviceparam.RefreshAccessRequest": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
|
|
|||
|
|
@ -691,6 +691,15 @@ definitions:
|
|||
tokens:
|
||||
$ref: '#/definitions/adminserviceparam.Tokens'
|
||||
type: object
|
||||
adminserviceparam.ProfileResponse:
|
||||
properties:
|
||||
data:
|
||||
$ref: '#/definitions/adminserviceparam.Data'
|
||||
field_errors:
|
||||
additionalProperties:
|
||||
type: string
|
||||
type: object
|
||||
type: object
|
||||
adminserviceparam.RefreshAccessRequest:
|
||||
properties:
|
||||
refresh_token:
|
||||
|
|
@ -2193,6 +2202,42 @@ paths:
|
|||
summary: "Admin login by\tPhoneNumber"
|
||||
tags:
|
||||
- Admins
|
||||
/admins/profile:
|
||||
get:
|
||||
consumes:
|
||||
- application/json
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/adminserviceparam.ProfileResponse'
|
||||
"400":
|
||||
description: Bad request
|
||||
schema:
|
||||
type: string
|
||||
"401":
|
||||
description: invalid or expired jwt
|
||||
schema:
|
||||
type: string
|
||||
"403":
|
||||
description: user not allowed
|
||||
schema:
|
||||
type: string
|
||||
"404":
|
||||
description: record not found
|
||||
schema:
|
||||
type: string
|
||||
"500":
|
||||
description: something went wrong
|
||||
schema:
|
||||
type: string
|
||||
security:
|
||||
- AuthBearerAdmin: []
|
||||
summary: Admin profile
|
||||
tags:
|
||||
- Admins
|
||||
/admins/refer-times:
|
||||
get:
|
||||
consumes:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
package adminserviceparam
|
||||
|
||||
type ProfileRequest struct {
|
||||
AdminID uint `json:"-"`
|
||||
}
|
||||
|
||||
type ProfileResponse struct {
|
||||
Data Data `json:"data"`
|
||||
FieldErrors map[string]string `json:"field_errors,omitempty"`
|
||||
}
|
||||
|
|
@ -17,3 +17,26 @@ func (s Service) AdminExistByID(ctx context.Context, req param.AdminExistByIDReq
|
|||
|
||||
return param.AdminExistByIDResponse{Admin: admin}, nil
|
||||
}
|
||||
|
||||
func (s Service) AdminGetProfile(ctx context.Context, req param.ProfileRequest) (param.ProfileResponse, error) {
|
||||
const op = "adminservice.AdminGetProfile"
|
||||
|
||||
admin, err := s.repo.GetAdminByID(ctx, req.AdminID)
|
||||
if err != nil {
|
||||
return param.ProfileResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
|
||||
return param.ProfileResponse{
|
||||
Data: param.Data{
|
||||
ID: admin.ID,
|
||||
FirstName: admin.FirstName,
|
||||
LastName: admin.LastName,
|
||||
PhoneNumber: admin.PhoneNumber,
|
||||
Role: admin.Role,
|
||||
Description: admin.Description,
|
||||
Email: admin.Email,
|
||||
Gender: admin.Gender,
|
||||
Status: admin.Status,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue