From d4e5bd0691d7e7244960896ede1ae2283551bf87 Mon Sep 17 00:00:00 2001 From: ZigBalthazar Date: Mon, 20 Jan 2025 21:02:54 +0330 Subject: [PATCH 1/2] feat(identifier): add create and get identifier --- cmd/daemon/daemon.go | 18 +- deliveries/http/handlers/domain/domain_get.go | 1 + .../http/handlers/domain/domain_response.go | 9 +- .../handlers/identifier/identifier_create.go | 65 +++++ .../identifier_get_all_by_pubkey.go | 45 ++++ .../handlers/identifier/identifier_handler.go | 13 + .../handlers/identifier/identifier_request.go | 7 + .../identifier/identifier_response.go | 11 + .../handlers/identifier/identifier_routes.go | 13 + deliveries/http/handlers/user/user_create.go | 63 +++++ deliveries/http/handlers/user/user_request.go | 5 + deliveries/http/handlers/user/user_routes.go | 11 + deliveries/http/http_handlers.go | 10 +- deliveries/http/http_server.go | 14 +- deliveries/http/middlewares/auth.go | 13 +- docs/docs.go | 242 ++++++++++++++++++ docs/swagger.json | 242 ++++++++++++++++++ docs/swagger.yaml | 152 +++++++++++ repositories/base_repository.go | 13 +- repositories/domain_repository.go | 8 +- repositories/identifier_repository.go | 68 +++++ repositories/user_repository.go | 8 +- schemas/domain_schema.go | 12 +- schemas/identifier_Schema.go | 21 ++ schemas/user_schema.go | 12 +- services/domain/domain_get.go | 9 + services/domain/domain_service.go | 17 +- services/identifier/identifier_create.go | 77 ++++++ services/identifier/identifier_error.go | 9 + services/identifier/identifier_get.go | 44 ++++ services/identifier/identifier_service.go | 19 ++ services/user/user_create.go | 2 +- services/user/user_get.go | 9 + services/user/user_service.go | 17 +- 34 files changed, 1214 insertions(+), 65 deletions(-) create mode 100644 deliveries/http/handlers/identifier/identifier_create.go create mode 100644 deliveries/http/handlers/identifier/identifier_get_all_by_pubkey.go create mode 100644 deliveries/http/handlers/identifier/identifier_handler.go create mode 100644 deliveries/http/handlers/identifier/identifier_request.go create mode 100644 deliveries/http/handlers/identifier/identifier_response.go create mode 100644 deliveries/http/handlers/identifier/identifier_routes.go create mode 100644 deliveries/http/handlers/user/user_create.go create mode 100644 deliveries/http/handlers/user/user_request.go create mode 100644 deliveries/http/handlers/user/user_routes.go create mode 100644 repositories/identifier_repository.go create mode 100644 schemas/identifier_Schema.go create mode 100644 services/identifier/identifier_create.go create mode 100644 services/identifier/identifier_error.go create mode 100644 services/identifier/identifier_get.go create mode 100644 services/identifier/identifier_service.go diff --git a/cmd/daemon/daemon.go b/cmd/daemon/daemon.go index fc46e28..5e5b7d4 100644 --- a/cmd/daemon/daemon.go +++ b/cmd/daemon/daemon.go @@ -11,7 +11,9 @@ import ( "github.com/dezh-tech/panda/infrastructures/redis" "github.com/dezh-tech/panda/pkg/logger" "github.com/dezh-tech/panda/repositories" - service "github.com/dezh-tech/panda/services/domain" + domainService "github.com/dezh-tech/panda/services/domain" + identifierService "github.com/dezh-tech/panda/services/identifier" + userService "github.com/dezh-tech/panda/services/user" ) type Daemon struct { @@ -38,10 +40,22 @@ func New(cfg *config.Config) (*Daemon, error) { return nil, err } + // repo domainRepo := repositories.NewDomainRepository(db.Client, cfg.Database.DBName, time.Duration(cfg.Database.QueryTimeout)*time.Millisecond) - hs := http.New(cfg.HTTPServer, service.NewDomainService(domainRepo)) + userRepo := repositories.NewUserRepository(db.Client, cfg.Database.DBName, + time.Duration(cfg.Database.QueryTimeout)*time.Millisecond) + + identifierRepo := repositories.NewIdentifierRepository(db.Client, cfg.Database.DBName, + time.Duration(cfg.Database.QueryTimeout)*time.Millisecond) + + // services + domainSrv := domainService.NewDomainService(domainRepo) + userSrv := userService.NewUserService(userRepo) + identifierSrv := identifierService.NewIdentifierService(identifierRepo, domainSrv, userSrv) + + hs := http.New(cfg.HTTPServer, domainSrv, userSrv, identifierSrv) gs := grpc.New(&cfg.GRPCServer, r, db, time.Now()) return &Daemon{ diff --git a/deliveries/http/handlers/domain/domain_get.go b/deliveries/http/handlers/domain/domain_get.go index 69ba824..e43e13f 100644 --- a/deliveries/http/handlers/domain/domain_get.go +++ b/deliveries/http/handlers/domain/domain_get.go @@ -31,6 +31,7 @@ func (dh Domain) getAll(c echo.Context) error { domainsRes := make([]DomainGetResponse, 0) for _, d := range *domains { domainsRes = append(domainsRes, DomainGetResponse{ + ID: d.ID, Domain: d.Domain, BasePricePerIdentifier: d.BasePricePerIdentifier, DefaultTTL: d.DefaultTTL, diff --git a/deliveries/http/handlers/domain/domain_response.go b/deliveries/http/handlers/domain/domain_response.go index 2721627..fc83092 100644 --- a/deliveries/http/handlers/domain/domain_response.go +++ b/deliveries/http/handlers/domain/domain_response.go @@ -5,8 +5,9 @@ type DomainCreateResponse struct { } type DomainGetResponse struct { - Domain string `json:"domain"` - BasePricePerIdentifier uint `json:"base_price_per_identifier"` - DefaultTTL uint32 `json:"default_ttl"` - Status string `json:"status"` + ID interface{} `json:"id"` + Domain string `json:"domain"` + BasePricePerIdentifier uint `json:"base_price_per_identifier"` + DefaultTTL uint32 `json:"default_ttl"` + Status string `json:"status"` } diff --git a/deliveries/http/handlers/identifier/identifier_create.go b/deliveries/http/handlers/identifier/identifier_create.go new file mode 100644 index 0000000..93afa35 --- /dev/null +++ b/deliveries/http/handlers/identifier/identifier_create.go @@ -0,0 +1,65 @@ +package handlers + +import ( + "errors" + "net/http" + + "github.com/dezh-tech/panda/pkg" + "github.com/dezh-tech/panda/pkg/validator" + domainService "github.com/dezh-tech/panda/services/domain" + identifierService "github.com/dezh-tech/panda/services/identifier" + userService "github.com/dezh-tech/panda/services/user" + "github.com/labstack/echo/v4" +) + +// CreateIdentifier creates a new identifier. +// +// @Summary Create a new identifier +// @Description Creates a new identifier with the specified attributes. Returns success if the identifier is created successfully or relevant error messages if the creation fails. +// @Tags identifiers +// @Accept json +// @Produce json +// @Param identifier body IdentifierCreateRequest true "Identifier creation payload" +// @Success 200 {object} pkg.ResponseDto "Identifier created successfully" +// @Failure 400 {object} pkg.ResponseDto{error=validator.Varror} "Bad Request - Validation error or invalid input" +// @Failure 409 {object} pkg.ResponseDto{error=validator.Varror} "Conflict - Identifier already exists" +// @Failure 500 {object} pkg.ResponseDto{error=validator.Varror} "Internal Server Error - Unexpected server error" +// @Router /identifiers [post] +func (dh Identifier) create(c echo.Context) error { + req := new(IdentifierCreateRequest) + if err := c.Bind(req); err != nil { + return echo.NewHTTPError(http.StatusBadRequest, pkg.ResponseDto{ + Success: false, + Error: validator.Varror{Error: "invalid input"}, + }) + } + + // Validate the request payload + v := validator.NewValidator() + validationErrors := v.Validate(req) + if validationErrors != nil { + return echo.NewHTTPError(http.StatusBadRequest, pkg.ResponseDto{ + Success: false, + Error: validator.Varror{ValidationErrors: validationErrors}, + }) + } + + // Call the domain service to create the domain + ctx := c.Request().Context() + _, err := dh.service.Create(ctx, req.Name, req.DomainID, req.Pubkey) + if err != nil { + if errors.Is(err, domainService.ErrNotFound) || errors.Is(err, userService.ErrNotFound) || errors.Is(err, identifierService.ErrIsExist) || errors.Is(err, identifierService.Err) { + return echo.NewHTTPError(http.StatusConflict, pkg.ResponseDto{ + Success: false, + Error: validator.Varror{Error: err.Error()}, + }) + } + + return echo.NewHTTPError(http.StatusInternalServerError, pkg.ResponseDto{ + Success: false, + Error: validator.Varror{Error: echo.ErrInternalServerError.Error()}, + }) + } + + return c.JSON(http.StatusOK, pkg.ResponseDto{Success: true}) +} diff --git a/deliveries/http/handlers/identifier/identifier_get_all_by_pubkey.go b/deliveries/http/handlers/identifier/identifier_get_all_by_pubkey.go new file mode 100644 index 0000000..a4bfe69 --- /dev/null +++ b/deliveries/http/handlers/identifier/identifier_get_all_by_pubkey.go @@ -0,0 +1,45 @@ +package handlers + +import ( + "net/http" + + "github.com/dezh-tech/panda/pkg" + "github.com/dezh-tech/panda/pkg/validator" + "github.com/labstack/echo/v4" +) + +// IdentifierGetAllByPubkey retrieves all identifiers. +// +// @Summary Retrieve all identifiers +// @Description Get a list of all identifiers associated with the provided public key. +// @Tags identifiers +// @Accept json +// @Produce json +// @Param Authorization header string true "Authorization" +// @Success 200 {object} pkg.ResponseDto{data=[]IdentifierGetResponse} "identifiers retrieved successfully" +// @Failure 500 {object} pkg.ResponseDto[string] "Internal Server Error" +// @Router /identifiers [get] +func (dh Identifier) getAllByPubkey(c echo.Context) error { + ctx := c.Request().Context() + idns, err := dh.service.GetAllByPubKey(ctx, c.Get("pubkey").(string)) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, pkg.ResponseDto{ + Success: false, + Error: validator.Varror{Error: echo.ErrInternalServerError.Error()}, + }) + } + + idnRes := make([]IdentifierGetResponse, 0) + for _, d := range *idns { + idnRes = append(idnRes, IdentifierGetResponse{ + Name: d.Name, + Pubkey: d.Pubkey, + DomainID: d.DomainID, + FullIdentifier: d.FullIdentifier, + ExpiresAt: d.ExpiresAt, + }) + } + + // Respond with the created domain's ID + return c.JSON(http.StatusOK, pkg.ResponseDto{Success: true, Data: idnRes}) +} diff --git a/deliveries/http/handlers/identifier/identifier_handler.go b/deliveries/http/handlers/identifier/identifier_handler.go new file mode 100644 index 0000000..f45f136 --- /dev/null +++ b/deliveries/http/handlers/identifier/identifier_handler.go @@ -0,0 +1,13 @@ +package handlers + +import service "github.com/dezh-tech/panda/services/identifier" + +type Identifier struct { + service service.Identifier +} + +func NewIdentifierService(identifierSvc service.Identifier) Identifier { + return Identifier{ + service: identifierSvc, + } +} diff --git a/deliveries/http/handlers/identifier/identifier_request.go b/deliveries/http/handlers/identifier/identifier_request.go new file mode 100644 index 0000000..cb2301d --- /dev/null +++ b/deliveries/http/handlers/identifier/identifier_request.go @@ -0,0 +1,7 @@ +package handlers + +type IdentifierCreateRequest struct { + DomainID string `json:"domain_id" validate:"required"` + Pubkey string `json:"pubkey" validate:"required"` + Name string `json:"name" validate:"required"` +} diff --git a/deliveries/http/handlers/identifier/identifier_response.go b/deliveries/http/handlers/identifier/identifier_response.go new file mode 100644 index 0000000..bd07304 --- /dev/null +++ b/deliveries/http/handlers/identifier/identifier_response.go @@ -0,0 +1,11 @@ +package handlers + +import "time" + +type IdentifierGetResponse struct { + Name string `json:"name"` + Pubkey string `json:"pubkey"` + DomainID string `json:"domain_id"` + ExpiresAt time.Time `json:"expires_at"` + FullIdentifier string `json:"full_identifier"` +} diff --git a/deliveries/http/handlers/identifier/identifier_routes.go b/deliveries/http/handlers/identifier/identifier_routes.go new file mode 100644 index 0000000..75eca95 --- /dev/null +++ b/deliveries/http/handlers/identifier/identifier_routes.go @@ -0,0 +1,13 @@ +package handlers + +import ( + middleware "github.com/dezh-tech/panda/deliveries/http/middlewares" + "github.com/labstack/echo/v4" +) + +func (dh Identifier) SetIdentifierRoutes(e *echo.Echo) { + userGroup := e.Group("/identifiers") + + userGroup.POST("", dh.create) + userGroup.GET("", dh.getAllByPubkey,middleware.Auth("a")) +} diff --git a/deliveries/http/handlers/user/user_create.go b/deliveries/http/handlers/user/user_create.go new file mode 100644 index 0000000..5e7816d --- /dev/null +++ b/deliveries/http/handlers/user/user_create.go @@ -0,0 +1,63 @@ +package handlers + +import ( + "errors" + "net/http" + + "github.com/dezh-tech/panda/pkg" + "github.com/dezh-tech/panda/pkg/validator" + userService "github.com/dezh-tech/panda/services/user" + "github.com/labstack/echo/v4" +) + +// CreateUser creates a new user. +// +// @Summary Create a new user +// @Description Creates a new user using the provided public key. The request payload must include a valid public key for successful user creation. +// @Tags users +// @Accept json +// @Produce json +// @Param user body UserCreateRequest true "Payload containing the public key for user creation" +// @Success 200 {object} pkg.ResponseDto "User created successfully" +// @Failure 400 {object} pkg.ResponseDto[validator.Varror] "Bad Request - Invalid input or validation errors" +// @Failure 409 {object} pkg.ResponseDto[validator.Varror] "Conflict - User with the specified public key already exists" +// @Failure 500 {object} pkg.ResponseDto[string] "Internal Server Error - An unexpected error occurred" +// @Router /users [post] +func (uh User) create(c echo.Context) error { + req := new(UserCreateRequest) + if err := c.Bind(req); err != nil { + return echo.NewHTTPError(http.StatusBadRequest, pkg.ResponseDto{ + Success: false, + Error: validator.Varror{Error: "invalid input"}, + }) + } + + // Validate the request payload + v := validator.NewValidator() + validationErrors := v.Validate(req) + if validationErrors != nil { + return echo.NewHTTPError(http.StatusBadRequest, pkg.ResponseDto{ + Success: false, + Error: validator.Varror{ValidationErrors: validationErrors}, + }) + } + + // Call the domain service to create the domain + ctx := c.Request().Context() + _, err := uh.service.Create(ctx, req.Pubkey) + if err != nil { + if errors.Is(err, userService.ErrIsExist) { + return echo.NewHTTPError(http.StatusConflict, pkg.ResponseDto{ + Success: false, + Error: validator.Varror{Error: err.Error()}, + }) + } + + return echo.NewHTTPError(http.StatusInternalServerError, pkg.ResponseDto{ + Success: false, + Error: validator.Varror{Error: echo.ErrInternalServerError.Error()}, + }) + } + + return c.JSON(http.StatusOK, pkg.ResponseDto{Success: true, Data: nil}) +} diff --git a/deliveries/http/handlers/user/user_request.go b/deliveries/http/handlers/user/user_request.go new file mode 100644 index 0000000..fb2e7d2 --- /dev/null +++ b/deliveries/http/handlers/user/user_request.go @@ -0,0 +1,5 @@ +package handlers + +type UserCreateRequest struct { + Pubkey string `json:"pubKey" validate:"required"` +} diff --git a/deliveries/http/handlers/user/user_routes.go b/deliveries/http/handlers/user/user_routes.go new file mode 100644 index 0000000..a3344f8 --- /dev/null +++ b/deliveries/http/handlers/user/user_routes.go @@ -0,0 +1,11 @@ +package handlers + +import ( + "github.com/labstack/echo/v4" +) + +func (dh User) SetUserRoutes(e *echo.Echo) { + userGroup := e.Group("/users") + + userGroup.POST("", dh.create) +} diff --git a/deliveries/http/http_handlers.go b/deliveries/http/http_handlers.go index 6bab7c0..4117c28 100644 --- a/deliveries/http/http_handlers.go +++ b/deliveries/http/http_handlers.go @@ -1,7 +1,9 @@ package http import ( - handlers "github.com/dezh-tech/panda/deliveries/http/handlers/domain" + domainHandlers "github.com/dezh-tech/panda/deliveries/http/handlers/domain" + userHandlers "github.com/dezh-tech/panda/deliveries/http/handlers/user" + identifierHandlers "github.com/dezh-tech/panda/deliveries/http/handlers/identifier" _ "github.com/dezh-tech/panda/docs" // revive:disable-line:blank-imports Justification: Required for Swagger documentation "github.com/labstack/echo/v4" echoSwagger "github.com/swaggo/echo-swagger" @@ -23,11 +25,15 @@ import ( // @BasePath / type Handlers struct { - domain handlers.Domain + domain domainHandlers.Domain + user userHandlers.User + identifier identifierHandlers.Identifier } func (h *Handlers) Start(r *echo.Echo) { h.domain.SetDomainRoutes(r) + h.user.SetUserRoutes(r) + h.identifier.SetIdentifierRoutes(r) r.GET("/swagger/*", echoSwagger.WrapHandler) } diff --git a/deliveries/http/http_server.go b/deliveries/http/http_server.go index 96009b6..6d97747 100644 --- a/deliveries/http/http_server.go +++ b/deliveries/http/http_server.go @@ -3,8 +3,12 @@ package http import ( "fmt" - handlers "github.com/dezh-tech/panda/deliveries/http/handlers/domain" - service "github.com/dezh-tech/panda/services/domain" + domain "github.com/dezh-tech/panda/deliveries/http/handlers/domain" + identifier "github.com/dezh-tech/panda/deliveries/http/handlers/identifier" + user "github.com/dezh-tech/panda/deliveries/http/handlers/user" + domainService "github.com/dezh-tech/panda/services/domain" + identifierService "github.com/dezh-tech/panda/services/identifier" + userService "github.com/dezh-tech/panda/services/user" "github.com/labstack/echo/v4" ) @@ -14,13 +18,15 @@ type Server struct { handlers Handlers } -func New(config Config, userSvc service.Domain) Server { +func New(config Config, domainService domainService.Domain, userService userService.User, identifierService identifierService.Identifier) Server { return Server{ Router: echo.New(), config: config, handlers: Handlers{ - domain: handlers.NewDomainService(userSvc), + domain: domain.NewDomainService(domainService), + user: user.NewUserService(userService), + identifier: identifier.NewIdentifierService(identifierService), }, } } diff --git a/deliveries/http/middlewares/auth.go b/deliveries/http/middlewares/auth.go index c23a3cd..c0e4f06 100644 --- a/deliveries/http/middlewares/auth.go +++ b/deliveries/http/middlewares/auth.go @@ -15,7 +15,7 @@ import ( func Auth(url string) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { - token := c.Response().Header().Get("Authorization") + token := c.Request().Header.Get("Authorization") if len(token) <= 6 { return echo.NewHTTPError(http.StatusUnauthorized, pkg.ResponseDto{ Success: false, @@ -23,7 +23,7 @@ func Auth(url string) echo.MiddlewareFunc { }) } - data, err := base64.RawStdEncoding.DecodeString(token[:6]) + data, err := base64.RawStdEncoding.DecodeString(token[6:]) if err != nil { return echo.NewHTTPError(http.StatusUnauthorized, pkg.ResponseDto{ Success: false, @@ -54,16 +54,15 @@ func Auth(url string) echo.MiddlewareFunc { } func CheckAuthEvent(e *nostr.Event, url string) bool { - if len(e.Tags) != 2 { + if e.Kind != nostr.KindHTTPAuth { return false } - if isValid, err := e.CheckSignature(); !isValid || err != nil { + if len(e.Tags) != 2 { return false } - diff := time.Until(e.CreatedAt.Time()) - if !(diff <= time.Minute && diff >= -time.Minute) { + if isValid, err := e.CheckSignature(); !isValid || err != nil { return false } @@ -74,7 +73,7 @@ func CheckAuthEvent(e *nostr.Event, url string) bool { } expiration := time.Unix(expirationInt, 0) - if !(expiration.After(time.Now().UTC()) && time.Until(expiration) >= 10*time.Minute) { + if expiration.Before(time.Now().UTC()) { return false } diff --git a/docs/docs.go b/docs/docs.go index a857dbf..0e6f116 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -123,6 +123,197 @@ const docTemplate = `{ } } } + }, + "/identifiers": { + "get": { + "description": "Get a list of all identifiers associated with the provided public key.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "identifiers" + ], + "summary": "Retrieve all identifiers", + "parameters": [ + { + "type": "string", + "description": "Authorization", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "identifiers retrieved successfully", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/pkg.ResponseDto" + }, + { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/handlers.IdentifierGetResponse" + } + } + } + } + ] + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/pkg.ResponseDto" + } + } + } + }, + "post": { + "description": "Creates a new identifier with the specified attributes. Returns success if the identifier is created successfully or relevant error messages if the creation fails.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "identifiers" + ], + "summary": "Create a new identifier", + "parameters": [ + { + "description": "Identifier creation payload", + "name": "identifier", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handlers.IdentifierCreateRequest" + } + } + ], + "responses": { + "200": { + "description": "Identifier created successfully", + "schema": { + "$ref": "#/definitions/pkg.ResponseDto" + } + }, + "400": { + "description": "Bad Request - Validation error or invalid input", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/pkg.ResponseDto" + }, + { + "type": "object", + "properties": { + "error": { + "$ref": "#/definitions/validator.Varror" + } + } + } + ] + } + }, + "409": { + "description": "Conflict - Identifier already exists", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/pkg.ResponseDto" + }, + { + "type": "object", + "properties": { + "error": { + "$ref": "#/definitions/validator.Varror" + } + } + } + ] + } + }, + "500": { + "description": "Internal Server Error - Unexpected server error", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/pkg.ResponseDto" + }, + { + "type": "object", + "properties": { + "error": { + "$ref": "#/definitions/validator.Varror" + } + } + } + ] + } + } + } + } + }, + "/users": { + "post": { + "description": "Creates a new user using the provided public key. The request payload must include a valid public key for successful user creation.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Create a new user", + "parameters": [ + { + "description": "Payload containing the public key for user creation", + "name": "user", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handlers.UserCreateRequest" + } + } + ], + "responses": { + "200": { + "description": "User created successfully", + "schema": { + "$ref": "#/definitions/pkg.ResponseDto" + } + }, + "400": { + "description": "Bad Request - Invalid input or validation errors", + "schema": { + "$ref": "#/definitions/pkg.ResponseDto" + } + }, + "409": { + "description": "Conflict - User with the specified public key already exists", + "schema": { + "$ref": "#/definitions/pkg.ResponseDto" + } + }, + "500": { + "description": "Internal Server Error - An unexpected error occurred", + "schema": { + "$ref": "#/definitions/pkg.ResponseDto" + } + } + } + } } }, "definitions": { @@ -173,11 +364,62 @@ const docTemplate = `{ "domain": { "type": "string" }, + "id": {}, "status": { "type": "string" } } }, + "handlers.IdentifierCreateRequest": { + "type": "object", + "required": [ + "domain_id", + "name", + "pubkey" + ], + "properties": { + "domain_id": { + "type": "string" + }, + "name": { + "type": "string" + }, + "pubkey": { + "type": "string" + } + } + }, + "handlers.IdentifierGetResponse": { + "type": "object", + "properties": { + "domain_id": { + "type": "string" + }, + "expires_at": { + "type": "string" + }, + "full_identifier": { + "type": "string" + }, + "name": { + "type": "string" + }, + "pubkey": { + "type": "string" + } + } + }, + "handlers.UserCreateRequest": { + "type": "object", + "required": [ + "pubKey" + ], + "properties": { + "pubKey": { + "type": "string" + } + } + }, "pkg.ResponseDto": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 256d890..36cd96e 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -117,6 +117,197 @@ } } } + }, + "/identifiers": { + "get": { + "description": "Get a list of all identifiers associated with the provided public key.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "identifiers" + ], + "summary": "Retrieve all identifiers", + "parameters": [ + { + "type": "string", + "description": "Authorization", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "identifiers retrieved successfully", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/pkg.ResponseDto" + }, + { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/handlers.IdentifierGetResponse" + } + } + } + } + ] + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/pkg.ResponseDto" + } + } + } + }, + "post": { + "description": "Creates a new identifier with the specified attributes. Returns success if the identifier is created successfully or relevant error messages if the creation fails.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "identifiers" + ], + "summary": "Create a new identifier", + "parameters": [ + { + "description": "Identifier creation payload", + "name": "identifier", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handlers.IdentifierCreateRequest" + } + } + ], + "responses": { + "200": { + "description": "Identifier created successfully", + "schema": { + "$ref": "#/definitions/pkg.ResponseDto" + } + }, + "400": { + "description": "Bad Request - Validation error or invalid input", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/pkg.ResponseDto" + }, + { + "type": "object", + "properties": { + "error": { + "$ref": "#/definitions/validator.Varror" + } + } + } + ] + } + }, + "409": { + "description": "Conflict - Identifier already exists", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/pkg.ResponseDto" + }, + { + "type": "object", + "properties": { + "error": { + "$ref": "#/definitions/validator.Varror" + } + } + } + ] + } + }, + "500": { + "description": "Internal Server Error - Unexpected server error", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/pkg.ResponseDto" + }, + { + "type": "object", + "properties": { + "error": { + "$ref": "#/definitions/validator.Varror" + } + } + } + ] + } + } + } + } + }, + "/users": { + "post": { + "description": "Creates a new user using the provided public key. The request payload must include a valid public key for successful user creation.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "users" + ], + "summary": "Create a new user", + "parameters": [ + { + "description": "Payload containing the public key for user creation", + "name": "user", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handlers.UserCreateRequest" + } + } + ], + "responses": { + "200": { + "description": "User created successfully", + "schema": { + "$ref": "#/definitions/pkg.ResponseDto" + } + }, + "400": { + "description": "Bad Request - Invalid input or validation errors", + "schema": { + "$ref": "#/definitions/pkg.ResponseDto" + } + }, + "409": { + "description": "Conflict - User with the specified public key already exists", + "schema": { + "$ref": "#/definitions/pkg.ResponseDto" + } + }, + "500": { + "description": "Internal Server Error - An unexpected error occurred", + "schema": { + "$ref": "#/definitions/pkg.ResponseDto" + } + } + } + } } }, "definitions": { @@ -167,11 +358,62 @@ "domain": { "type": "string" }, + "id": {}, "status": { "type": "string" } } }, + "handlers.IdentifierCreateRequest": { + "type": "object", + "required": [ + "domain_id", + "name", + "pubkey" + ], + "properties": { + "domain_id": { + "type": "string" + }, + "name": { + "type": "string" + }, + "pubkey": { + "type": "string" + } + } + }, + "handlers.IdentifierGetResponse": { + "type": "object", + "properties": { + "domain_id": { + "type": "string" + }, + "expires_at": { + "type": "string" + }, + "full_identifier": { + "type": "string" + }, + "name": { + "type": "string" + }, + "pubkey": { + "type": "string" + } + } + }, + "handlers.UserCreateRequest": { + "type": "object", + "required": [ + "pubKey" + ], + "properties": { + "pubKey": { + "type": "string" + } + } + }, "pkg.ResponseDto": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 770479f..1268ae4 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -33,9 +33,43 @@ definitions: type: integer domain: type: string + id: {} status: type: string type: object + handlers.IdentifierCreateRequest: + properties: + domain_id: + type: string + name: + type: string + pubkey: + type: string + required: + - domain_id + - name + - pubkey + type: object + handlers.IdentifierGetResponse: + properties: + domain_id: + type: string + expires_at: + type: string + full_identifier: + type: string + name: + type: string + pubkey: + type: string + type: object + handlers.UserCreateRequest: + properties: + pubKey: + type: string + required: + - pubKey + type: object pkg.ResponseDto: properties: data: {} @@ -137,4 +171,122 @@ paths: summary: Create a new domain tags: - domains + /identifiers: + get: + consumes: + - application/json + description: Get a list of all identifiers associated with the provided public + key. + parameters: + - description: Authorization + in: header + name: Authorization + required: true + type: string + produces: + - application/json + responses: + "200": + description: identifiers retrieved successfully + schema: + allOf: + - $ref: '#/definitions/pkg.ResponseDto' + - properties: + data: + items: + $ref: '#/definitions/handlers.IdentifierGetResponse' + type: array + type: object + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/pkg.ResponseDto' + summary: Retrieve all identifiers + tags: + - identifiers + post: + consumes: + - application/json + description: Creates a new identifier with the specified attributes. Returns + success if the identifier is created successfully or relevant error messages + if the creation fails. + parameters: + - description: Identifier creation payload + in: body + name: identifier + required: true + schema: + $ref: '#/definitions/handlers.IdentifierCreateRequest' + produces: + - application/json + responses: + "200": + description: Identifier created successfully + schema: + $ref: '#/definitions/pkg.ResponseDto' + "400": + description: Bad Request - Validation error or invalid input + schema: + allOf: + - $ref: '#/definitions/pkg.ResponseDto' + - properties: + error: + $ref: '#/definitions/validator.Varror' + type: object + "409": + description: Conflict - Identifier already exists + schema: + allOf: + - $ref: '#/definitions/pkg.ResponseDto' + - properties: + error: + $ref: '#/definitions/validator.Varror' + type: object + "500": + description: Internal Server Error - Unexpected server error + schema: + allOf: + - $ref: '#/definitions/pkg.ResponseDto' + - properties: + error: + $ref: '#/definitions/validator.Varror' + type: object + summary: Create a new identifier + tags: + - identifiers + /users: + post: + consumes: + - application/json + description: Creates a new user using the provided public key. The request payload + must include a valid public key for successful user creation. + parameters: + - description: Payload containing the public key for user creation + in: body + name: user + required: true + schema: + $ref: '#/definitions/handlers.UserCreateRequest' + produces: + - application/json + responses: + "200": + description: User created successfully + schema: + $ref: '#/definitions/pkg.ResponseDto' + "400": + description: Bad Request - Invalid input or validation errors + schema: + $ref: '#/definitions/pkg.ResponseDto' + "409": + description: Conflict - User with the specified public key already exists + schema: + $ref: '#/definitions/pkg.ResponseDto' + "500": + description: Internal Server Error - An unexpected error occurred + schema: + $ref: '#/definitions/pkg.ResponseDto' + summary: Create a new user + tags: + - users swagger: "2.0" diff --git a/repositories/base_repository.go b/repositories/base_repository.go index f4fff74..a811c72 100644 --- a/repositories/base_repository.go +++ b/repositories/base_repository.go @@ -2,7 +2,6 @@ package repositories import ( "context" - "errors" "time" "go.mongodb.org/mongo-driver/bson" @@ -45,11 +44,11 @@ func (r *Base) FindByField(ctx context.Context, field string, value, result inte filter := bson.M{field: value} err := collection.FindOne(ctx, filter).Decode(result) - if errors.Is(err, mongo.ErrNoDocuments) { - return nil + if err != nil{ + return err } - return err + return nil } // FindOne finds a single document matching the filter. @@ -60,11 +59,11 @@ func (r *Base) FindOne(ctx context.Context, filter, result interface{}) error { defer cancel() err := collection.FindOne(ctx, filter).Decode(result) - if errors.Is(err, mongo.ErrNoDocuments) { - return nil + if err != nil { + return err } - return err + return nil } // FindAll finds all documents matching the filter. diff --git a/repositories/domain_repository.go b/repositories/domain_repository.go index 33ab148..d30f700 100644 --- a/repositories/domain_repository.go +++ b/repositories/domain_repository.go @@ -7,6 +7,7 @@ import ( schema "github.com/dezh-tech/panda/schemas" "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" ) @@ -21,6 +22,7 @@ func NewDomainRepository(client *mongo.Client, dbName string, timeout time.Durat } func (r *Domain) Add(ctx context.Context, d *schema.Domain) (*mongo.InsertOneResult, error) { + d.ID = primitive.NewObjectID() d.CreatedAt = time.Now() d.UpdatedAt = time.Now() @@ -30,9 +32,13 @@ func (r *Domain) Add(ctx context.Context, d *schema.Domain) (*mongo.InsertOneRes func (r *Domain) GetByField(ctx context.Context, fieldName string, value interface{}, ) (*schema.Domain, error) { - var result *schema.Domain + result := new(schema.Domain) err := r.FindOne(ctx, bson.M{fieldName: value}, result) if err != nil { + if errors.Is(err, mongo.ErrNoDocuments) { + return nil, nil + } + return nil, err } diff --git a/repositories/identifier_repository.go b/repositories/identifier_repository.go new file mode 100644 index 0000000..961f710 --- /dev/null +++ b/repositories/identifier_repository.go @@ -0,0 +1,68 @@ +package repositories + +import ( + "context" + "errors" + "time" + + schema "github.com/dezh-tech/panda/schemas" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" +) + +type Identifier struct { + *Base +} + +func NewIdentifierRepository(client *mongo.Client, dbName string, timeout time.Duration) *Identifier { + return &Identifier{ + Base: NewBaseRepository(client, dbName, schema.IdentifierSchemaName, timeout), + } +} + +func (r *Identifier) Add(ctx context.Context, d *schema.Identifier) (*mongo.InsertOneResult, error) { + d.ID = primitive.NewObjectID() + d.CreatedAt = time.Now() + d.UpdatedAt = time.Now() + + return r.InsertOne(ctx, d) +} + +func (r *Identifier) GetByField(ctx context.Context, + fieldName string, value interface{}, +) (*schema.Identifier, error) { + result := new(schema.Identifier) + err := r.FindOne(ctx, bson.M{fieldName: value}, result) + if err != nil { + if errors.Is(err, mongo.ErrNoDocuments) { + return nil, nil + } + + return nil, err + } + + return result, nil +} + +func (r *Identifier) GetAll(ctx context.Context, filter interface{}) (*[]schema.Identifier, error) { + results := new([]schema.Identifier) + err := r.FindAll(ctx, filter, results) + if err != nil { + if errors.Is(err, mongo.ErrNoDocuments) { + return &[]schema.Identifier{}, nil + } + + return nil, err + } + + return results, nil +} + +func (r *Identifier) Update(ctx context.Context, filter, update interface{}) (*mongo.UpdateResult, error) { + return r.UpdateOne(ctx, filter, update) +} + +func (r *Identifier) Delete(ctx context.Context, filter interface{}) (*mongo.DeleteResult, error) { + return r.DeleteOne(ctx, filter) +} diff --git a/repositories/user_repository.go b/repositories/user_repository.go index 546866c..a6f97c8 100644 --- a/repositories/user_repository.go +++ b/repositories/user_repository.go @@ -7,6 +7,7 @@ import ( schema "github.com/dezh-tech/panda/schemas" "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" ) @@ -21,6 +22,7 @@ func NewUserRepository(client *mongo.Client, dbName string, timeout time.Duratio } func (r *User) Add(ctx context.Context, d *schema.User) (*mongo.InsertOneResult, error) { + d.ID = primitive.NewObjectID() d.CreatedAt = time.Now() d.UpdatedAt = time.Now() @@ -30,9 +32,13 @@ func (r *User) Add(ctx context.Context, d *schema.User) (*mongo.InsertOneResult, func (r *User) GetByField(ctx context.Context, fieldName string, value interface{}, ) (*schema.User, error) { - var result *schema.User + result := new(schema.User) err := r.FindOne(ctx, bson.M{fieldName: value}, result) if err != nil { + if errors.Is(err, mongo.ErrNoDocuments) { + return nil, nil + } + return nil, err } diff --git a/schemas/domain_schema.go b/schemas/domain_schema.go index befa1b4..2fcccee 100644 --- a/schemas/domain_schema.go +++ b/schemas/domain_schema.go @@ -1,6 +1,10 @@ package schema -import "time" +import ( + "time" + + "go.mongodb.org/mongo-driver/bson/primitive" +) const DomainSchemaName = "domains" @@ -10,7 +14,7 @@ type Domain struct { DefaultTTL uint32 `bson:"default_ttl"` Status string `bson:"status"` - ID interface{} `bson:"_id"` - CreatedAt time.Time `bson:"created_at"` - UpdatedAt time.Time `bson:"updated_at"` + ID primitive.ObjectID `bson:"_id"` + CreatedAt time.Time `bson:"created_at"` + UpdatedAt time.Time `bson:"updated_at"` } diff --git a/schemas/identifier_Schema.go b/schemas/identifier_Schema.go new file mode 100644 index 0000000..e37c186 --- /dev/null +++ b/schemas/identifier_Schema.go @@ -0,0 +1,21 @@ +package schema + +import ( + "time" + + "go.mongodb.org/mongo-driver/bson/primitive" +) + +const IdentifierSchemaName = "identifier" + +type Identifier struct { + Name string `bson:"name"` + Pubkey string `bson:"pubkey"` + DomainID string `bson:"domain_id"` + ExpiresAt time.Time `bson:"expires_at"` + FullIdentifier string `bson:"full_identifier"` + + ID primitive.ObjectID `bson:"_id"` + CreatedAt time.Time `bson:"created_at"` + UpdatedAt time.Time `bson:"updated_at"` +} diff --git a/schemas/user_schema.go b/schemas/user_schema.go index e34d666..d29122a 100644 --- a/schemas/user_schema.go +++ b/schemas/user_schema.go @@ -1,13 +1,17 @@ package schema -import "time" +import ( + "time" + + "go.mongodb.org/mongo-driver/bson/primitive" +) const UserSchemaName = "users" type User struct { PubKey string `bson:"pubkey"` - ID interface{} `bson:"_id"` - CreatedAt time.Time `bson:"created_at"` - UpdatedAt time.Time `bson:"updated_at"` + ID primitive.ObjectID `bson:"_id"` + CreatedAt time.Time `bson:"created_at"` + UpdatedAt time.Time `bson:"updated_at"` } diff --git a/services/domain/domain_get.go b/services/domain/domain_get.go index 8006c76..189c335 100644 --- a/services/domain/domain_get.go +++ b/services/domain/domain_get.go @@ -24,3 +24,12 @@ func (d Domain) GetAllWithoutFilter(ctx context.Context) (*[]schema.Domain, erro return domains, nil } + +func (s Domain) GetByField(ctx context.Context, fieldName string, value interface{}) (*schema.Domain, error) { + domain, err := s.repo.GetByField(ctx, fieldName, value) + if err != nil { + return nil, err + } + + return domain, nil +} diff --git a/services/domain/domain_service.go b/services/domain/domain_service.go index 904849a..eb33d62 100644 --- a/services/domain/domain_service.go +++ b/services/domain/domain_service.go @@ -1,26 +1,15 @@ package service import ( - "context" - "github.com/dezh-tech/panda/pkg/validator" - schema "github.com/dezh-tech/panda/schemas" - "go.mongodb.org/mongo-driver/mongo" + "github.com/dezh-tech/panda/repositories" ) -type Repository interface { - Add(ctx context.Context, schema *schema.Domain) (*mongo.InsertOneResult, error) - GetByField(ctx context.Context, fieldName string, value interface{}) (*schema.Domain, error) - GetAll(ctx context.Context, filter interface{}) (*[]schema.Domain, error) - Update(ctx context.Context, filter, update interface{}) (*mongo.UpdateResult, error) - Delete(ctx context.Context, filter interface{}) (*mongo.DeleteResult, error) -} - type Domain struct { - repo Repository + repo *repositories.Domain validator *validator.Validator } -func NewDomainService(repo Repository) Domain { +func NewDomainService(repo *repositories.Domain) Domain { return Domain{repo: repo, validator: validator.NewValidator()} } diff --git a/services/identifier/identifier_create.go b/services/identifier/identifier_create.go new file mode 100644 index 0000000..a41917c --- /dev/null +++ b/services/identifier/identifier_create.go @@ -0,0 +1,77 @@ +package service + +import ( + "context" + "time" + + schema "github.com/dezh-tech/panda/schemas" + domainService "github.com/dezh-tech/panda/services/domain" + "go.mongodb.org/mongo-driver/bson/primitive" +) + +func (i Identifier) Create(ctx context.Context, name, domainID, pubkey string) (interface{}, error) { + + d, err := i.checkDomain(ctx, domainID) + if err != nil { + return nil, err + } + + if !i.checkUser(ctx, pubkey) { + return nil, Err + } + + // TODO ::: check method for fullIdentifier + f := name + "@" + d + + fi, err := i.repo.GetByField(ctx, "full_identifier", f) + if err != nil { + return nil, err + } + + if fi != nil { + return nil, ErrIsExist + } + + id, err := i.repo.Add(ctx, &schema.Identifier{ + Name: name, + Pubkey: pubkey, + DomainID: domainID, + ExpiresAt: time.Time{}, + FullIdentifier: f, + }) + if err != nil { + return nil, err + } + + return id.InsertedID, nil +} + +func (i Identifier) checkDomain(ctx context.Context, domainID string) (string, error) { + objectID, err := primitive.ObjectIDFromHex(domainID) + if err != nil { + return "", err + } + d, err := i.domainService.GetByField(ctx, "_id", objectID) + if err != nil { + return "", err + } + + if d == nil { + return "", domainService.ErrNotFound + } + + return d.Domain, nil +} + +func (i Identifier) checkUser(ctx context.Context, pubkey string) bool { + u, err := i.userService.GetByField(ctx, "pubkey", pubkey) + if err != nil { + return false + } + + if u == nil { + return false + } + + return true +} diff --git a/services/identifier/identifier_error.go b/services/identifier/identifier_error.go new file mode 100644 index 0000000..e805e69 --- /dev/null +++ b/services/identifier/identifier_error.go @@ -0,0 +1,9 @@ +package service + +import "errors" + +var ( + Err = errors.New("something went wrong") + ErrNotFound = errors.New("identifier not found") + ErrIsExist = errors.New("identifier exist") +) diff --git a/services/identifier/identifier_get.go b/services/identifier/identifier_get.go new file mode 100644 index 0000000..942dde9 --- /dev/null +++ b/services/identifier/identifier_get.go @@ -0,0 +1,44 @@ +package service + +import ( + "context" + + schema "github.com/dezh-tech/panda/schemas" + "go.mongodb.org/mongo-driver/bson" +) + +func (d Identifier) GetAll(ctx context.Context, filter interface{}) (*[]schema.Identifier, error) { + identifiers, err := d.repo.GetAll(ctx, filter) + if err != nil { + return nil, err + } + + return identifiers, nil +} + +func (d Identifier) GetAllWithoutFilter(ctx context.Context) (*[]schema.Identifier, error) { + identifiers, err := d.GetAll(ctx, bson.M{}) + if err != nil { + return nil, err + } + + return identifiers, nil +} + +func (s Identifier) GetByField(ctx context.Context, fieldName string, value interface{}) (*schema.Identifier, error) { + identifier, err := s.repo.GetByField(ctx, fieldName, value) + if err != nil { + return nil, err + } + + return identifier, nil +} + +func (d Identifier) GetAllByPubKey(ctx context.Context, pubKey string) (*[]schema.Identifier, error) { + identifiers, err := d.GetAll(ctx, bson.M{"pubkey": pubKey}) + if err != nil { + return nil, err + } + + return identifiers, nil +} diff --git a/services/identifier/identifier_service.go b/services/identifier/identifier_service.go new file mode 100644 index 0000000..1a8f46d --- /dev/null +++ b/services/identifier/identifier_service.go @@ -0,0 +1,19 @@ +package service + +import ( + "github.com/dezh-tech/panda/pkg/validator" + "github.com/dezh-tech/panda/repositories" + domainService "github.com/dezh-tech/panda/services/domain" + userService "github.com/dezh-tech/panda/services/user" +) + +type Identifier struct { + repo *repositories.Identifier + domainService domainService.Domain + userService userService.User + validator *validator.Validator +} + +func NewIdentifierService(repo *repositories.Identifier, domainService domainService.Domain, userService userService.User) Identifier { + return Identifier{repo: repo, validator: validator.NewValidator(), domainService: domainService, userService: userService} +} diff --git a/services/user/user_create.go b/services/user/user_create.go index 20080df..f605dce 100644 --- a/services/user/user_create.go +++ b/services/user/user_create.go @@ -14,7 +14,7 @@ func (u User) Create(ctx context.Context, pubKey string) (interface{}, error) { } if d != nil { - return nil, err + return nil, ErrIsExist } id, err := u.repo.Add(ctx, &schema.User{ diff --git a/services/user/user_get.go b/services/user/user_get.go index 7764ee6..2a02771 100644 --- a/services/user/user_get.go +++ b/services/user/user_get.go @@ -24,3 +24,12 @@ func (u User) GetAllWithoutFilter(ctx context.Context) (*[]schema.User, error) { return Users, nil } + +func (s User) GetByField(ctx context.Context, fieldName string, value interface{}) (*schema.User, error) { + domain, err := s.repo.GetByField(ctx, fieldName, value) + if err != nil { + return nil, err + } + + return domain, nil +} diff --git a/services/user/user_service.go b/services/user/user_service.go index 33bbd8c..efebaaf 100644 --- a/services/user/user_service.go +++ b/services/user/user_service.go @@ -1,26 +1,15 @@ package service import ( - "context" - "github.com/dezh-tech/panda/pkg/validator" - schema "github.com/dezh-tech/panda/schemas" - "go.mongodb.org/mongo-driver/mongo" + "github.com/dezh-tech/panda/repositories" ) -type Repository interface { - Add(ctx context.Context, schema *schema.User) (*mongo.InsertOneResult, error) - GetByField(ctx context.Context, fieldName string, value interface{}) (*schema.User, error) - GetAll(ctx context.Context, filter interface{}) (*[]schema.User, error) - Update(ctx context.Context, filter, update interface{}) (*mongo.UpdateResult, error) - Delete(ctx context.Context, filter interface{}) (*mongo.DeleteResult, error) -} - type User struct { - repo Repository + repo *repositories.User validator *validator.Validator } -func NewUserService(repo Repository) User { +func NewUserService(repo *repositories.User) User { return User{repo: repo, validator: validator.NewValidator()} } From 8cff8e8baeccae3e396dc97f06ee4b41207a26ad Mon Sep 17 00:00:00 2001 From: ZigBalthazar Date: Mon, 20 Jan 2025 21:04:02 +0330 Subject: [PATCH 2/2] fix --- deliveries/http/handlers/identifier/identifier_routes.go | 2 +- deliveries/http/http_handlers.go | 6 +++--- repositories/base_repository.go | 2 +- services/identifier/identifier_create.go | 1 - 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/deliveries/http/handlers/identifier/identifier_routes.go b/deliveries/http/handlers/identifier/identifier_routes.go index 75eca95..8a37b35 100644 --- a/deliveries/http/handlers/identifier/identifier_routes.go +++ b/deliveries/http/handlers/identifier/identifier_routes.go @@ -9,5 +9,5 @@ func (dh Identifier) SetIdentifierRoutes(e *echo.Echo) { userGroup := e.Group("/identifiers") userGroup.POST("", dh.create) - userGroup.GET("", dh.getAllByPubkey,middleware.Auth("a")) + userGroup.GET("", dh.getAllByPubkey, middleware.Auth("a")) } diff --git a/deliveries/http/http_handlers.go b/deliveries/http/http_handlers.go index 4117c28..ed2207d 100644 --- a/deliveries/http/http_handlers.go +++ b/deliveries/http/http_handlers.go @@ -2,8 +2,8 @@ package http import ( domainHandlers "github.com/dezh-tech/panda/deliveries/http/handlers/domain" - userHandlers "github.com/dezh-tech/panda/deliveries/http/handlers/user" identifierHandlers "github.com/dezh-tech/panda/deliveries/http/handlers/identifier" + userHandlers "github.com/dezh-tech/panda/deliveries/http/handlers/user" _ "github.com/dezh-tech/panda/docs" // revive:disable-line:blank-imports Justification: Required for Swagger documentation "github.com/labstack/echo/v4" echoSwagger "github.com/swaggo/echo-swagger" @@ -25,8 +25,8 @@ import ( // @BasePath / type Handlers struct { - domain domainHandlers.Domain - user userHandlers.User + domain domainHandlers.Domain + user userHandlers.User identifier identifierHandlers.Identifier } diff --git a/repositories/base_repository.go b/repositories/base_repository.go index a811c72..302fe50 100644 --- a/repositories/base_repository.go +++ b/repositories/base_repository.go @@ -44,7 +44,7 @@ func (r *Base) FindByField(ctx context.Context, field string, value, result inte filter := bson.M{field: value} err := collection.FindOne(ctx, filter).Decode(result) - if err != nil{ + if err != nil { return err } diff --git a/services/identifier/identifier_create.go b/services/identifier/identifier_create.go index a41917c..9b99f8f 100644 --- a/services/identifier/identifier_create.go +++ b/services/identifier/identifier_create.go @@ -10,7 +10,6 @@ import ( ) func (i Identifier) Create(ctx context.Context, name, domainID, pubkey string) (interface{}, error) { - d, err := i.checkDomain(ctx, domainID) if err != nil { return nil, err