Skip to content

Commit

Permalink
refactor: restructure domain handling and update response models
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigBalthazar committed Jan 12, 2025
1 parent 4c91d0c commit 0346004
Show file tree
Hide file tree
Showing 17 changed files with 396 additions and 182 deletions.
4 changes: 2 additions & 2 deletions cmd/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func New(cfg *config.Config) (*Daemon, error) {
return nil, err
}

userRepo := domainRepo.New(db)
domainRepo := domainRepo.New(db.Client, cfg.Database.DBName, time.Duration(cfg.Database.QueryTimeout))

hs := http.New(cfg.HTTPServer, domainService.New(userRepo))
hs := http.New(cfg.HTTPServer, domainService.New(domainRepo))
gs := grpc.New(&cfg.GRPCServer, r, db, time.Now())

return &Daemon{
Expand Down
28 changes: 16 additions & 12 deletions deliveries/http/handlers/domain_handler/domain_create.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package domainhandler

import (
"fmt"
"net/http"

domainhandler "github.com/dezh-tech/panda/deliveries/http/handlers/domain_handler/dto"
"github.com/dezh-tech/panda/pkg"
"github.com/dezh-tech/panda/pkg/validator"
domainService "github.com/dezh-tech/panda/services/domain"
"github.com/labstack/echo/v4"
Expand All @@ -13,37 +13,41 @@ import (
// CreateDomain creates a new domain.
//
// @Summary Create a new domain
// @Description Accepts a JSON payload to create a new domain with the specified attributes.
// @Tags domain
// @Description Create a new domain with the specified attributes.
// @Tags domains
// @Accept json
// @Produce json
// @Param domain body domainhandler.DomainCreateRequest true "Domain creation payload"
// @Success 200 {object} domainhandler.DomainCreateResponse "Domain created successfully"
// @Failure 400 {object} map[string]string "Bad Request - Invalid input"
// @Failure 500 {object} map[string]string "Internal Server Error"
// @Success 200 {object} pkg.ResponseDto{data=domainhandler.DomainCreateResponse} "Domain created successfully"
// @Failure 400 {object} pkg.ResponseDto[validator.Varror] "Bad Request - Validation error"
// @Failure 500 {object} pkg.ResponseDto[string] "Internal Server Error"
// @Router /domains [post]
func (h Handler) domainCreate(c echo.Context) error {
// Parse the request body into the DTO
req := new(domainhandler.DomainCreateRequest)
if err := c.Bind(req); err != nil {
return err
return echo.NewHTTPError(http.StatusBadRequest, map[string]string{"error": "invalid input"})
}

// Validate the request payload
v := validator.NewValidator()
validationErrors := v.Validate(req)
if validationErrors != nil {
return echo.NewHTTPError(http.StatusBadRequest, &validator.Varror{ValidationErrors: validationErrors})
return echo.NewHTTPError(http.StatusBadRequest, pkg.ResponseDto{Success: false, Error: validator.Varror{ValidationErrors: validationErrors}})
}

resp, err := h.domainSvc.Create(domainService.DomainInsertArgs{
// Call the domain service to create the domain
ctx := c.Request().Context() // Extract context from Echo
resp, err := h.domainSvc.Create(ctx, domainService.DomainInsertArgs{
Domain: req.Domain,
BasePricePerIdentifier: req.BasePricePerIdentifier,
DefaultTTL: req.DefaultTTL,
Status: req.Status,
})
if err != nil {
fmt.Println(err)
return echo.NewHTTPError(http.StatusBadRequest, err)
return echo.NewHTTPError(http.StatusInternalServerError, pkg.ResponseDto{Success: false, Error: validator.Varror{Error: echo.ErrInternalServerError.Error()}})
}

return c.JSON(http.StatusOK, &domainhandler.DomainCreateResponse{ID: resp.ID})
// Respond with the created domain's ID
return c.JSON(http.StatusOK, pkg.ResponseDto{Success: true, Data: domainhandler.DomainCreateResponse{ID: resp.ID}})
}
6 changes: 3 additions & 3 deletions deliveries/http/http_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (

// @title Panda Swagger
// @version 1.0
// @description This is a sample server Petstore server.
// @description Panda is a NOSTR NIP-05 management service developed by Dezh.tech (Dezh technologies).
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @contact.url http://www.dezh.tech/
// @contact.email hi@dezh.tech

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
Expand Down
7 changes: 0 additions & 7 deletions deliveries/http/response.go

This file was deleted.

76 changes: 61 additions & 15 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const docTemplate = `{
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "API Support",
"url": "http://www.swagger.io/support",
"email": "support@swagger.io"
"url": "http://www.dezh.tech/",
"email": "hi@dezh.tech"
},
"license": {
"name": "Apache 2.0",
Expand All @@ -26,15 +26,15 @@ const docTemplate = `{
"paths": {
"/domains": {
"post": {
"description": "Accepts a JSON payload to create a new domain with the specified attributes.",
"description": "Create a new domain with the specified attributes.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"domain"
"domains"
],
"summary": "Create a new domain",
"parameters": [
Expand All @@ -52,25 +52,31 @@ const docTemplate = `{
"200": {
"description": "Domain created successfully",
"schema": {
"$ref": "#/definitions/domainhandler.DomainCreateResponse"
"allOf": [
{
"$ref": "#/definitions/pkg.ResponseDto"
},
{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/domainhandler.DomainCreateResponse"
}
}
}
]
}
},
"400": {
"description": "Bad Request - Invalid input",
"description": "Bad Request - Validation error",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
"$ref": "#/definitions/pkg.ResponseDto"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
"$ref": "#/definitions/pkg.ResponseDto"
}
}
}
Expand Down Expand Up @@ -112,6 +118,46 @@ const docTemplate = `{
"properties": {
"id": {}
}
},
"pkg.ResponseDto": {
"type": "object",
"properties": {
"data": {},
"error": {
"$ref": "#/definitions/validator.Varror"
},
"message": {
"type": "string"
},
"success": {
"type": "boolean"
}
}
},
"validator.ValidationError": {
"type": "object",
"properties": {
"field": {
"type": "string"
},
"message": {
"type": "string"
}
}
},
"validator.Varror": {
"type": "object",
"properties": {
"error": {
"type": "string"
},
"validation_errors": {
"type": "array",
"items": {
"$ref": "#/definitions/validator.ValidationError"
}
}
}
}
}
}`
Expand All @@ -123,7 +169,7 @@ var SwaggerInfo = &swag.Spec{
BasePath: "/",
Schemes: []string{},
Title: "Panda Swagger",
Description: "This is a sample server Petstore server.",
Description: "Panda is a NOSTR NIP-05 management service developed by Dezh.tech (Dezh technologies).",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
Expand Down
76 changes: 61 additions & 15 deletions docs/swagger.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"swagger": "2.0",
"info": {
"description": "This is a sample server Petstore server.",
"description": "Panda is a NOSTR NIP-05 management service developed by Dezh.tech (Dezh technologies).",
"title": "Panda Swagger",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "API Support",
"url": "http://www.swagger.io/support",
"email": "support@swagger.io"
"url": "http://www.dezh.tech/",
"email": "hi@dezh.tech"
},
"license": {
"name": "Apache 2.0",
Expand All @@ -20,15 +20,15 @@
"paths": {
"/domains": {
"post": {
"description": "Accepts a JSON payload to create a new domain with the specified attributes.",
"description": "Create a new domain with the specified attributes.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"domain"
"domains"
],
"summary": "Create a new domain",
"parameters": [
Expand All @@ -46,25 +46,31 @@
"200": {
"description": "Domain created successfully",
"schema": {
"$ref": "#/definitions/domainhandler.DomainCreateResponse"
"allOf": [
{
"$ref": "#/definitions/pkg.ResponseDto"
},
{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/domainhandler.DomainCreateResponse"
}
}
}
]
}
},
"400": {
"description": "Bad Request - Invalid input",
"description": "Bad Request - Validation error",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
"$ref": "#/definitions/pkg.ResponseDto"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
"$ref": "#/definitions/pkg.ResponseDto"
}
}
}
Expand Down Expand Up @@ -106,6 +112,46 @@
"properties": {
"id": {}
}
},
"pkg.ResponseDto": {
"type": "object",
"properties": {
"data": {},
"error": {
"$ref": "#/definitions/validator.Varror"
},
"message": {
"type": "string"
},
"success": {
"type": "boolean"
}
}
},
"validator.ValidationError": {
"type": "object",
"properties": {
"field": {
"type": "string"
},
"message": {
"type": "string"
}
}
},
"validator.Varror": {
"type": "object",
"properties": {
"error": {
"type": "string"
},
"validation_errors": {
"type": "array",
"items": {
"$ref": "#/definitions/validator.ValidationError"
}
}
}
}
}
}
Loading

0 comments on commit 0346004

Please sign in to comment.