chores: refactor struct and structure project
This commit is contained in:
@@ -3,24 +3,27 @@ package authhttp
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
authdomain "legalgo-BE-go/internal/domain/auth"
|
||||
responsedomain "legalgo-BE-go/internal/domain/reponse"
|
||||
staffdomain "legalgo-BE-go/internal/domain/staff"
|
||||
authsvc "legalgo-BE-go/internal/services/auth"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func LoginStaff(
|
||||
router chi.Router,
|
||||
authSvc authsvc.AuthIntf,
|
||||
authSvc authsvc.Auth,
|
||||
validate *validator.Validate,
|
||||
rdb *redis.Client,
|
||||
) {
|
||||
router.Post("/staff/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec authdomain.LoginReq
|
||||
var spec staffdomain.StaffLogin
|
||||
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
@@ -59,50 +62,7 @@ func LoginStaff(
|
||||
return
|
||||
}
|
||||
|
||||
responsePayload := &authdomain.AuthResponse{
|
||||
Token: token,
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, responsePayload)
|
||||
})
|
||||
}
|
||||
|
||||
func LoginUser(
|
||||
router chi.Router,
|
||||
authSvc authsvc.AuthIntf,
|
||||
validate *validator.Validate,
|
||||
) {
|
||||
router.Post("/user/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec authdomain.LoginReq
|
||||
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to unmarshal request",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
token, err := authSvc.LoginAsUser(spec)
|
||||
if err != nil {
|
||||
if err := utils.StoreTokenRedis(ctx, rdb, token, spec.Email); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
@@ -114,7 +74,7 @@ func LoginUser(
|
||||
return
|
||||
}
|
||||
|
||||
responsePayload := &authdomain.AuthResponse{
|
||||
responsePayload := &responsedomain.Auth{
|
||||
Token: token,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package authhttp
|
||||
|
||||
import (
|
||||
responsedomain "legalgo-BE-go/internal/domain/reponse"
|
||||
userdomain "legalgo-BE-go/internal/domain/user"
|
||||
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"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func LoginUser(
|
||||
router chi.Router,
|
||||
authSvc authsvc.Auth,
|
||||
validate *validator.Validate,
|
||||
rdb *redis.Client,
|
||||
) {
|
||||
router.Post("/user/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec userdomain.UserLogin
|
||||
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to unmarshal request",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
token, err := authSvc.LoginAsUser(spec)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := utils.StoreTokenRedis(ctx, rdb, token, spec.Email); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
responsePayload := &responsedomain.Auth{
|
||||
Token: token,
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, responsePayload)
|
||||
})
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
func GetStaffProfile(
|
||||
router chi.Router,
|
||||
authSvc authsvc.AuthIntf,
|
||||
authSvc authsvc.Auth,
|
||||
) {
|
||||
router.Get("/staff/profile", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
@@ -47,7 +47,7 @@ func GetStaffProfile(
|
||||
|
||||
func GetUserProfile(
|
||||
router chi.Router,
|
||||
authSvc authsvc.AuthIntf,
|
||||
authSvc authsvc.Auth,
|
||||
) {
|
||||
router.Get("/user/profile", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
@@ -3,79 +3,27 @@ package authhttp
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
authdomain "legalgo-BE-go/internal/domain/auth"
|
||||
responsedomain "legalgo-BE-go/internal/domain/reponse"
|
||||
staffdomain "legalgo-BE-go/internal/domain/staff"
|
||||
authsvc "legalgo-BE-go/internal/services/auth"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func RegisterUser(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
authSvc authsvc.AuthIntf,
|
||||
) {
|
||||
router.Post("/user/register", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec authdomain.RegisterUserReq
|
||||
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to unmarshal request",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
token, err := authSvc.RegisterUser(spec)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
responsePayload := &authdomain.AuthResponse{
|
||||
Token: token,
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, responsePayload)
|
||||
})
|
||||
}
|
||||
|
||||
func RegisterStaff(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
authSvc authsvc.AuthIntf,
|
||||
authSvc authsvc.Auth,
|
||||
rdb *redis.Client,
|
||||
) {
|
||||
router.Post("/staff/register", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec authdomain.RegisterStaffReq
|
||||
var spec staffdomain.StaffRegister
|
||||
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
@@ -113,7 +61,20 @@ func RegisterStaff(
|
||||
)
|
||||
return
|
||||
}
|
||||
responsePayload := &authdomain.AuthResponse{
|
||||
|
||||
if err := utils.StoreTokenRedis(ctx, rdb, token, spec.Email); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
responsePayload := &responsedomain.Auth{
|
||||
Token: token,
|
||||
}
|
||||
response.RespondJsonSuccess(ctx, w, responsePayload)
|
||||
@@ -0,0 +1,82 @@
|
||||
package authhttp
|
||||
|
||||
import (
|
||||
responsedomain "legalgo-BE-go/internal/domain/reponse"
|
||||
userdomain "legalgo-BE-go/internal/domain/user"
|
||||
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"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func RegisterUser(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
authSvc authsvc.Auth,
|
||||
rdb *redis.Client,
|
||||
) {
|
||||
router.Post("/user/register", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec userdomain.UserRegister
|
||||
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to unmarshal request",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
token, err := authSvc.RegisterUser(spec)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := utils.StoreTokenRedis(ctx, rdb, token, spec.Email); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
responsePayload := &responsedomain.Auth{
|
||||
Token: token,
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, responsePayload)
|
||||
})
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package authhttp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
authdomain "legalgo-BE-go/internal/domain/auth"
|
||||
staffdomain "legalgo-BE-go/internal/domain/staff"
|
||||
authsvc "legalgo-BE-go/internal/services/auth"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
func UpdateStaff(
|
||||
router chi.Router,
|
||||
authSvc authsvc.AuthIntf,
|
||||
authSvc authsvc.Auth,
|
||||
) {
|
||||
router.Patch("/staff/{id}/update", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
@@ -31,7 +31,7 @@ func UpdateStaff(
|
||||
return
|
||||
}
|
||||
|
||||
var spec authdomain.RegisterStaffReq
|
||||
var spec staffdomain.StaffRegister
|
||||
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
@@ -45,7 +45,7 @@ func UpdateStaff(
|
||||
return
|
||||
}
|
||||
|
||||
staff := authdomain.Staff{
|
||||
staff := staffdomain.Staff{
|
||||
ID: id,
|
||||
Email: spec.Email,
|
||||
Password: spec.Password,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package categoryhttp
|
||||
|
||||
import (
|
||||
authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
|
||||
categorydomain "legalgo-BE-go/internal/domain/category"
|
||||
categorysvc "legalgo-BE-go/internal/services/category"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
@@ -16,52 +17,54 @@ func Create(
|
||||
validate *validator.Validate,
|
||||
categorySvc categorysvc.Category,
|
||||
) {
|
||||
router.Post("/category/create", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
router.
|
||||
With(authmiddleware.Authorize()).
|
||||
Post("/category/create", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec categorydomain.CategoryReq
|
||||
var spec categorydomain.CategoryReq
|
||||
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := categorySvc.Create(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrCreateEntity.Code,
|
||||
response.ErrCreateEntity.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
if err := categorySvc.Create(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrCreateEntity.Code,
|
||||
response.ErrCreateEntity.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, struct {
|
||||
Message string
|
||||
}{
|
||||
Message: "category created successfully",
|
||||
response.RespondJsonSuccess(ctx, w, struct {
|
||||
Message string
|
||||
}{
|
||||
Message: "category created successfully",
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ func GetAll(
|
||||
) {
|
||||
router.Get("/category", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
subsPlan, err := categorySvc.GetAllModel()
|
||||
subsPlan, err := categorySvc.GetAll()
|
||||
// subsPlan, err := categorySvc.GetAll()
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
|
||||
@@ -5,4 +5,5 @@ import "go.uber.org/fx"
|
||||
var Module = fx.Module("categories", fx.Invoke(
|
||||
Create,
|
||||
GetAll,
|
||||
Update,
|
||||
))
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package categoryhttp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
|
||||
categorydomain "legalgo-BE-go/internal/domain/category"
|
||||
categorysvc "legalgo-BE-go/internal/services/category"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
func Update(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
categorySvc categorysvc.Category,
|
||||
) {
|
||||
router.
|
||||
With(authmiddleware.Authorize()).
|
||||
Put("/category/{category_id}/update", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
categoryID := chi.URLParam(r, "category_id")
|
||||
|
||||
if categoryID == "" {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
fmt.Errorf("category id is not provided"),
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"category id is not provided",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
var spec categorydomain.CategoryReq
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to unmarshal body",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := categorySvc.Update(categoryID, spec); err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, struct {
|
||||
Message string
|
||||
}{
|
||||
Message: "update category success",
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -1,138 +1,76 @@
|
||||
package authmiddleware
|
||||
|
||||
// import (
|
||||
// "context"
|
||||
// "fmt"
|
||||
// "net/http"
|
||||
// "strings"
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
// redisaccessor "legalgo-BE-go/internal/accessor/redis"
|
||||
// contextkeyenum "legalgo-BE-go/internal/enums/context_key"
|
||||
// jwtclaimenum "legalgo-BE-go/internal/enums/jwt"
|
||||
// resourceenum "legalgo-BE-go/internal/enums/resource"
|
||||
// "legalgo-BE-go/internal/services/auth"
|
||||
// "github.com/golang-jwt/jwt/v5"
|
||||
// )
|
||||
redisaccessor "legalgo-BE-go/internal/accessor/redis"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
|
||||
// const SessionHeader = "Authorization"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
// func Authorization() func(next http.Handler) http.Handler {
|
||||
// return func(next http.Handler) http.Handler {
|
||||
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// ctx := r.Context()
|
||||
const SessionHeader = "Authorization"
|
||||
|
||||
// tokenString, err := GetToken(r)
|
||||
// if err != nil {
|
||||
// RespondWithError(w, r, err, "Invalid auth header")
|
||||
// return
|
||||
// }
|
||||
func Authorize() func(next http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
// token, err := ValidateToken(ctx, tokenString)
|
||||
// if err != nil {
|
||||
// RespondWithError(w, r, err, err.Error())
|
||||
// return
|
||||
// }
|
||||
tokenString, err := utils.GetToken(r)
|
||||
if err != nil {
|
||||
response.RespondWithError(w, r, err, "Invalid auth header")
|
||||
return
|
||||
}
|
||||
|
||||
// if isAuthorized, ctx := VerifyClaims(ctx, token, nil); !isAuthorized {
|
||||
// RespondWithError(w, r, errorcode.ErrCodeUnauthorized, errorcode.ErrCodeUnauthorized.Message)
|
||||
// return
|
||||
// } else {
|
||||
// next.ServeHTTP(w, r.WithContext(ctx))
|
||||
// return
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
spec, err := utils.GetTokenDetail(r)
|
||||
|
||||
// func GetToken(r *http.Request) (string, error) {
|
||||
// tokenString := GetTokenFromHeader(r)
|
||||
// if tokenString == "" {
|
||||
// tokenString = getTokenFromQuery(r)
|
||||
// }
|
||||
token, err := ValidateToken(ctx, spec.Email)
|
||||
if err != nil {
|
||||
response.RespondWithError(w, r, err, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// if tokenString == "" {
|
||||
// return "", fmt.Errorf("token not found")
|
||||
// }
|
||||
isValid := token == tokenString
|
||||
|
||||
// return tokenString, nil
|
||||
// }
|
||||
if !isValid {
|
||||
response.RespondWithError(w, r, err, "invalid token")
|
||||
return
|
||||
}
|
||||
|
||||
// func GetTokenFromHeader(r *http.Request) string {
|
||||
// session := r.Header.Get(SessionHeader)
|
||||
// arr := strings.Split(session, " ")
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// if len(arr) != 2 || strings.ToUpper(arr[0]) != "BEARER" {
|
||||
// return ""
|
||||
// }
|
||||
func ValidateToken(ctx context.Context, id string) (string, error) {
|
||||
redisClient := redisaccessor.Get()
|
||||
redisToken, err := utils.GetTokenRedis(ctx, redisClient, id)
|
||||
|
||||
// return arr[1]
|
||||
// }
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// func getTokenFromQuery(r *http.Request) string {
|
||||
// token := r.URL.Query().Get("token")
|
||||
// return token
|
||||
// }
|
||||
token, err := utils.ParseToken(redisToken)
|
||||
|
||||
// func VerifyClaims(ctx context.Context, token *jwt.Token,
|
||||
// requiredResources []resourceenum.Resource) (bool, context.Context) {
|
||||
// if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
||||
// rawResources := []interface{}{}
|
||||
// if claimValue, exist := claims[string(jwtclaimenum.RESOURCES)]; exist {
|
||||
// rawResources = claimValue.([]interface{})
|
||||
// }
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid token: %w", err)
|
||||
}
|
||||
|
||||
// resources := []resourceenum.Resource{}
|
||||
// resourceMap := map[string]bool{}
|
||||
// Check if the token is valid
|
||||
if !token.Valid {
|
||||
return "", fmt.Errorf("invalid token: token is not valid")
|
||||
}
|
||||
|
||||
// for _, v := range rawResources {
|
||||
// value := v.(string)
|
||||
// resources = append(resources, resourceenum.Resource(value))
|
||||
// resourceMap[value] = true
|
||||
// }
|
||||
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
||||
expirationTime := claims["exp"].(float64) // Expiration time in Unix timestamp format
|
||||
if time.Unix(int64(expirationTime), 0).Before(time.Now()) {
|
||||
return "", fmt.Errorf("token has expired")
|
||||
}
|
||||
}
|
||||
|
||||
// ctx = context.WithValue(ctx, contextkeyenum.Authorization, UserAuthorization{
|
||||
// Type: claims[string(jwtclaimenum.TYPE)].(string),
|
||||
// UserId: claims[string(jwtclaimenum.AUDIENCE)].(string),
|
||||
// Username: claims[string(jwtclaimenum.USERNAME)].(string),
|
||||
// Resources: resources,
|
||||
// })
|
||||
|
||||
// isResourceFulfilled := false
|
||||
|
||||
// for _, v := range requiredResources {
|
||||
// if _, ok := resourceMap[string(v)]; ok {
|
||||
// isResourceFulfilled = true
|
||||
// ctx = context.WithValue(ctx, contextkeyenum.Resource, v)
|
||||
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
|
||||
// if isResourceFulfilled || len(requiredResources) == 0 {
|
||||
// return true, ctx
|
||||
// }
|
||||
// }
|
||||
|
||||
// return false, nil
|
||||
// }
|
||||
|
||||
// func ValidateToken(ctx context.Context, tokenString string) (*jwt.Token, error) {
|
||||
// redisClient := redisaccessor.Get()
|
||||
// redisToken, err := redisClient.Exists(ctx, fmt.Sprintf("%s:%s", auth.BLACKLISTED_TOKEN_KEY, tokenString)).Result()
|
||||
|
||||
// if err != nil || redisToken > 0 {
|
||||
// return nil, fmt.Errorf("session already expired")
|
||||
// }
|
||||
|
||||
// token, err := jwt.Parse(tokenString, authsvc.VerifyToken(conf.JWTAccessToken))
|
||||
// if err != nil {
|
||||
// if ve, ok := err.(*jwt.ValidationError); ok {
|
||||
// if ve.Errors&jwt.ValidationErrorExpired != 0 {
|
||||
// err = errorcode.ErrCodeExpiredToken
|
||||
// }
|
||||
// }
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// return token, nil
|
||||
// }
|
||||
return redisToken, nil
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
func Create(
|
||||
validate *validator.Validate,
|
||||
newsSvc newssvc.News,
|
||||
staffRepo staffrepository.StaffIntf,
|
||||
staffRepo staffrepository.Staff,
|
||||
router chi.Router,
|
||||
) {
|
||||
router.Post("/news/create", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package subscribehttp
|
||||
|
||||
import "go.uber.org/fx"
|
||||
|
||||
var Module = fx.Module("subscribe", fx.Invoke())
|
||||
@@ -0,0 +1,77 @@
|
||||
package subscribehttp
|
||||
|
||||
import (
|
||||
authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
|
||||
userdomain "legalgo-BE-go/internal/domain/user"
|
||||
authsvc "legalgo-BE-go/internal/services/auth"
|
||||
subscribesvc "legalgo-BE-go/internal/services/subscribe"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func Update(
|
||||
router chi.Router,
|
||||
authSvc authsvc.Auth,
|
||||
subSvc subscribesvc.Subscribe,
|
||||
) {
|
||||
router.
|
||||
With(authmiddleware.Authorize()).
|
||||
Patch("/subscribe/update", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
detail, err := utils.GetTokenDetail(r)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if detail.Role != "user" {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrUnauthorized.Code,
|
||||
response.ErrUnauthorized.HttpCode,
|
||||
"unauthorized",
|
||||
)
|
||||
return
|
||||
}
|
||||
var body userdomain.UserSubsUpdate
|
||||
err = utils.UnmarshalBody(r, &body)
|
||||
if err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := authSvc.GetUserProfile(detail.Email)
|
||||
|
||||
if err := subSvc.Update(user.ID, body); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package subscribeplanhttp
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
|
||||
subscribeplandomain "legalgo-BE-go/internal/domain/subscribe_plan"
|
||||
subscribeplansvc "legalgo-BE-go/internal/services/subscribe_plan"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
@@ -15,53 +16,55 @@ import (
|
||||
func CreateSubscribePlan(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
subsSvc subscribeplansvc.SubsPlanIntf,
|
||||
subsSvc subscribeplansvc.SubscribePlan,
|
||||
) {
|
||||
router.Post("/subscribe-plan/create", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
router.
|
||||
With(authmiddleware.Authorize()).
|
||||
Post("/subscribe-plan/create", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec subscribeplandomain.SubscribePlanReq
|
||||
var spec subscribeplandomain.SubscribePlanReq
|
||||
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to unmarshal request",
|
||||
)
|
||||
return
|
||||
}
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to unmarshal request",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := subsSvc.CreatePlan(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrCreateEntity.Code,
|
||||
response.ErrCreateEntity.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
if err := subsSvc.CreatePlan(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrCreateEntity.Code,
|
||||
response.ErrCreateEntity.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, struct {
|
||||
Message string
|
||||
}{
|
||||
Message: "subscription plan created successfully.",
|
||||
response.RespondJsonSuccess(ctx, w, struct {
|
||||
Message string
|
||||
}{
|
||||
Message: "subscription plan created successfully.",
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
func GetAllPlan(
|
||||
router chi.Router,
|
||||
subsPlanSvc subscribeplansvc.SubsPlanIntf,
|
||||
subsPlanSvc subscribeplansvc.SubscribePlan,
|
||||
) {
|
||||
router.Get("/subscribe-plan", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
@@ -3,6 +3,7 @@ package taghttp
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
|
||||
tagdomain "legalgo-BE-go/internal/domain/tag"
|
||||
tagsvc "legalgo-BE-go/internal/services/tag"
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
@@ -15,53 +16,55 @@ import (
|
||||
func Create(
|
||||
router chi.Router,
|
||||
validate *validator.Validate,
|
||||
tagSvc tagsvc.TagIntf,
|
||||
tagSvc tagsvc.Tag,
|
||||
) {
|
||||
router.Post("/tag/create", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
router.
|
||||
With(authmiddleware.Authorize()).
|
||||
Post("/tag/create", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var spec tagdomain.TagReq
|
||||
var spec tagdomain.TagReq
|
||||
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to unmarshal request",
|
||||
)
|
||||
return
|
||||
}
|
||||
if err := utils.UnmarshalBody(r, &spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
"failed to unmarshal request",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if err := tagSvc.Create(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrCreateEntity.Code,
|
||||
response.ErrCreateEntity.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
if err := tagSvc.Create(spec); err != nil {
|
||||
response.ResponseWithErrorCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrCreateEntity.Code,
|
||||
response.ErrCreateEntity.HttpCode,
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
response.RespondJsonSuccess(ctx, w, struct {
|
||||
Message string
|
||||
}{
|
||||
Message: "tag created successfully.",
|
||||
response.RespondJsonSuccess(ctx, w, struct {
|
||||
Message string
|
||||
}{
|
||||
Message: "tag created successfully.",
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
func GetAll(
|
||||
router chi.Router,
|
||||
tagSvc tagsvc.TagIntf,
|
||||
tagSvc tagsvc.Tag,
|
||||
) {
|
||||
router.Get("/tag", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
Reference in New Issue
Block a user