Skip to content

Commit

Permalink
feat(domain): add get all route (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigBalthazar authored Jan 13, 2025
1 parent ea5e956 commit 4144c36
Show file tree
Hide file tree
Showing 34 changed files with 566 additions and 314 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
build
**/.env
**/config.yml
.vscode
**/*.log
9 changes: 5 additions & 4 deletions cmd/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
grpcClient "github.com/dezh-tech/panda/infrastructures/grpc_client"
"github.com/dezh-tech/panda/infrastructures/redis"
"github.com/dezh-tech/panda/pkg/logger"
domainRepo "github.com/dezh-tech/panda/repositories/domain"
domainService "github.com/dezh-tech/panda/services/domain"
"github.com/dezh-tech/panda/repositories"
service "github.com/dezh-tech/panda/services/domain"
)

type Daemon struct {
Expand All @@ -38,9 +38,10 @@ func New(cfg *config.Config) (*Daemon, error) {
return nil, err
}

domainRepo := domainRepo.New(db.Client, cfg.Database.DBName, time.Duration(cfg.Database.QueryTimeout))
domainRepo := repositories.NewDomainRepository(db.Client, cfg.Database.DBName,
time.Duration(cfg.Database.QueryTimeout)*time.Millisecond)

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

return &Daemon{
Expand Down
5 changes: 3 additions & 2 deletions deliveries/grpc/gen/health.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions deliveries/grpc/gen/health_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions deliveries/http/handlers/domain/domain_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package handlers

import (
"net/http"

"github.com/dezh-tech/panda/pkg"
"github.com/dezh-tech/panda/pkg/validator"
"github.com/labstack/echo/v4"
)

// CreateDomain creates a new domain.
//
// @Summary Create a new domain
// @Description Create a new domain with the specified attributes.
// @Tags domains
// @Accept json
// @Produce json
// @Param domain body DomainCreateRequest true "Domain creation payload"
// @Success 200 {object} pkg.ResponseDto{data=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 (dh Domain) create(c echo.Context) error {
req := new(DomainCreateRequest)
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()
resp, err := dh.service.Create(ctx, req.Domain, req.Status, req.BasePricePerIdentifier, req.DefaultTTL)
if err != nil {
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: DomainCreateResponse{ID: resp}})
}
43 changes: 43 additions & 0 deletions deliveries/http/handlers/domain/domain_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package handlers

import (
"net/http"

"github.com/dezh-tech/panda/pkg"
"github.com/dezh-tech/panda/pkg/validator"
"github.com/labstack/echo/v4"
)

// DomainGetAll retrieves all domains.
//
// @Summary Retrieve all domains
// @Description Get a list of all domains with their attributes.
// @Tags domains
// @Accept json
// @Produce json
// @Success 200 {object} pkg.ResponseDto{data=[]DomainGetResponse} "Domains retrieved successfully"
// @Failure 500 {object} pkg.ResponseDto[string] "Internal Server Error"
// @Router /domains [get]
func (dh Domain) getAll(c echo.Context) error {
ctx := c.Request().Context()
domains, err := dh.service.GetAllWithoutFilter(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, pkg.ResponseDto{
Success: false,
Error: validator.Varror{Error: echo.ErrInternalServerError.Error()},
})
}

domainsRes := make([]DomainGetResponse, 0)
for _, d := range *domains {
domainsRes = append(domainsRes, DomainGetResponse{
Domain: d.Domain,
BasePricePerIdentifier: d.BasePricePerIdentifier,
DefaultTTL: d.DefaultTTL,
Status: d.Status,
})
}

// Respond with the created domain's ID
return c.JSON(http.StatusOK, pkg.ResponseDto{Success: true, Data: domainsRes})
}
13 changes: 13 additions & 0 deletions deliveries/http/handlers/domain/domain_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package handlers

import service "github.com/dezh-tech/panda/services/domain"

type Domain struct {
service service.Domain
}

func NewDomainService(domainSvc service.Domain) Domain {
return Domain{
service: domainSvc,
}
}
8 changes: 8 additions & 0 deletions deliveries/http/handlers/domain/domain_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package handlers

type DomainCreateRequest struct {
Domain string `json:"domain" validate:"required,hostname"`
BasePricePerIdentifier uint `json:"base_price_per_identifier" validate:"required,min=1"`
DefaultTTL uint32 `json:"default_ttl" validate:"required,min=1"`
Status string `json:"status" validate:"required,oneof=active inactive"`
}
12 changes: 12 additions & 0 deletions deliveries/http/handlers/domain/domain_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package handlers

type DomainCreateResponse struct {
ID interface{} `json:"id"`
}

type DomainGetResponse struct {
Domain string `json:"domain"`
BasePricePerIdentifier uint `json:"base_price_per_identifier"`
DefaultTTL uint32 `json:"default_ttl"`
Status string `json:"status"`
}
12 changes: 12 additions & 0 deletions deliveries/http/handlers/domain/domain_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package handlers

import (
"github.com/labstack/echo/v4"
)

func (dh Domain) SetDomainRoutes(e *echo.Echo) {
userGroup := e.Group("/domains")

userGroup.POST("", dh.create)
userGroup.GET("", dh.getAll)
}
53 changes: 0 additions & 53 deletions deliveries/http/handlers/domain_handler/domain_create.go

This file was deleted.

8 changes: 0 additions & 8 deletions deliveries/http/handlers/domain_handler/dto/domain_request.go

This file was deleted.

This file was deleted.

13 changes: 0 additions & 13 deletions deliveries/http/handlers/domain_handler/handler.go

This file was deleted.

11 changes: 0 additions & 11 deletions deliveries/http/handlers/domain_handler/router.go

This file was deleted.

12 changes: 6 additions & 6 deletions deliveries/http/http_handlers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package http

import (
domainhandler "github.com/dezh-tech/panda/deliveries/http/handlers/domain_handler"
_ "github.com/dezh-tech/panda/docs"
handlers "github.com/dezh-tech/panda/deliveries/http/handlers/domain"
_ "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"
)
Expand All @@ -22,12 +22,12 @@ import (
// @host localhost:8080
// @BasePath /

type HttpHandlers struct {
user domainhandler.Handler
type Handlers struct {
domain handlers.Domain
}

func (h *HttpHandlers) Start(r *echo.Echo) {
h.user.SetRoutes(r)
func (h *Handlers) Start(r *echo.Echo) {
h.domain.SetDomainRoutes(r)

r.GET("/swagger/*", echoSwagger.WrapHandler)
}
12 changes: 6 additions & 6 deletions deliveries/http/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ package http
import (
"fmt"

domainhandler "github.com/dezh-tech/panda/deliveries/http/handlers/domain_handler"
domainService "github.com/dezh-tech/panda/services/domain"
handlers "github.com/dezh-tech/panda/deliveries/http/handlers/domain"
service "github.com/dezh-tech/panda/services/domain"
"github.com/labstack/echo/v4"
)

type Server struct {
Router *echo.Echo
config Config
handlers HttpHandlers
handlers Handlers
}

func New(config Config, userSvc domainService.DomainService) Server {
func New(config Config, userSvc service.Domain) Server {
return Server{
Router: echo.New(),
config: config,

handlers: HttpHandlers{
user: domainhandler.New(userSvc),
handlers: Handlers{
domain: handlers.NewDomainService(userSvc),
},
}
}
Expand Down
Loading

0 comments on commit 4144c36

Please sign in to comment.