forked from ebhomengo/niki
delivery & handler
This commit is contained in:
parent
58c1f57de8
commit
a986f03e44
|
|
@ -1,8 +1,9 @@
|
|||
package entity
|
||||
|
||||
import "time"
|
||||
|
||||
type ID uint64
|
||||
import (
|
||||
"git.gocasts.ir/ebhomengo/niki/types"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CampaignStatus string
|
||||
|
||||
|
|
@ -15,7 +16,7 @@ const (
|
|||
)
|
||||
|
||||
type Campaign struct {
|
||||
ID ID `json:"id"`
|
||||
ID types.ID `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
GoalAmount float64 `json:"goal_amount"`
|
||||
|
|
@ -23,7 +24,7 @@ type Campaign struct {
|
|||
Status CampaignStatus `json:"status"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
DeadlineAt *time.Time `json:"deadline_at,omitempty"`
|
||||
AdminID ID `json:"creator_id"`
|
||||
AdminID types.ID `json:"creator_id"`
|
||||
}
|
||||
|
||||
// Behavior
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"git.gocasts.ir/ebhomengo/niki/types"
|
||||
"time"
|
||||
)
|
||||
|
||||
type GetCampaignResponse struct {
|
||||
ID types.ID `json:"user_id"`
|
||||
}
|
||||
|
||||
type AddCampaignRequest struct {
|
||||
ID uint64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
GoalAmount float64 `json:"goal_amount"`
|
||||
DeadlineAt *time.Time `json:"deadline_at,omitempty"`
|
||||
AdminID types.ID `json:"admin_id"`
|
||||
}
|
||||
|
||||
type UpdateCampaignRequest struct {
|
||||
Title *string `json:"title,omitempty"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
GoalAmount *float64 `json:"goal_amount,omitempty"`
|
||||
DeadlineAt *time.Time `json:"deadline_at,omitempty"`
|
||||
Status *string `json:"status,omitempty"` // draft/active/completed/paused/cancelled
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
package donate_server
|
||||
|
||||
type Handler struct{}
|
||||
|
||||
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
package donate_server
|
||||
|
||||
import "github.com/labstack/echo/v4"
|
||||
|
||||
func (h Handler) RegisterRoutes(e *echo.Echo) {
|
||||
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
package donate_server
|
||||
|
||||
import (
|
||||
httpserver "git.gocasts.ir/ebhomengo/niki/delivery/http_server"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Server httpserver.Server
|
||||
Handler Handler
|
||||
Router *echo.Echo
|
||||
}
|
||||
|
||||
func (s Server) Start() {
|
||||
s.Handler.RegisterRoutes(s.Router)
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"git.gocasts.ir/ebhomengo/niki/domain/campaign/entity"
|
||||
"git.gocasts.ir/ebhomengo/niki/domain/campaign/service"
|
||||
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
svc service.CampaignService
|
||||
}
|
||||
|
||||
func NewHandler(svc service.CampaignService) Handler {
|
||||
return Handler{svc: svc}
|
||||
}
|
||||
|
||||
func (h Handler) createCampaign(c echo.Context) error {
|
||||
|
||||
var req entity.Campaign
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"error": "invalid request body",
|
||||
})
|
||||
}
|
||||
req.CreatedAt = time.Now()
|
||||
req.RaisedAmount = 0
|
||||
|
||||
createdID, err := h.svc.CreateCampaign(c.Request().Context(), req)
|
||||
if err != nil {
|
||||
msg, code := httpmsg.Error(err)
|
||||
c.Logger().Errorf("Service error creating campaign: %v (Code: %d)", err, code)
|
||||
return c.JSON(code, msg)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusCreated, map[string]interface{}{
|
||||
"message": "campaign created successfully",
|
||||
"id": createdID,
|
||||
})
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (s Server) healthCheck(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, echo.Map{
|
||||
"message": "everything is good!",
|
||||
})
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.gocasts.ir/ebhomengo/niki/pkg/httpserver"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
handler Handler
|
||||
HTTPServer *httpserver.Server
|
||||
}
|
||||
|
||||
func NewServer(handler Handler, hS *httpserver.Server) Server {
|
||||
return Server{handler: handler, HTTPServer: hS}
|
||||
}
|
||||
|
||||
func (s Server) Serve() error {
|
||||
s.registerRoutes()
|
||||
if err := s.HTTPServer.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Server) Stop(ctx context.Context) error {
|
||||
return s.HTTPServer.Stop(ctx)
|
||||
}
|
||||
|
||||
func (s Server) registerRoutes() {
|
||||
router := s.HTTPServer.GetRouter()
|
||||
|
||||
router.GET("campaign/health-check", s.healthCheck)
|
||||
|
||||
r := router.Group("campaign")
|
||||
|
||||
r.POST("/", s.handler.createCampaign)
|
||||
|
||||
}
|
||||
|
|
@ -1 +1,26 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"git.gocasts.ir/ebhomengo/niki/types"
|
||||
"time"
|
||||
)
|
||||
|
||||
type GetCampaignResponse struct {
|
||||
ID types.ID `json:"user_id"`
|
||||
}
|
||||
|
||||
type AddCampaignRequest struct {
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
GoalAmount float64 `json:"goal_amount"`
|
||||
DeadlineAt *time.Time `json:"deadline_at,omitempty"`
|
||||
AdminID types.ID `json:"admin_id"`
|
||||
}
|
||||
|
||||
type UpdateCampaignRequest struct {
|
||||
Title *string `json:"title,omitempty"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
GoalAmount *float64 `json:"goal_amount,omitempty"`
|
||||
DeadlineAt *time.Time `json:"deadline_at,omitempty"`
|
||||
Status *string `json:"status,omitempty"` // draft/active/completed/paused/cancelled
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,38 +1 @@
|
|||
package service
|
||||
|
||||
|
||||
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"git.gocasts.ir/ebhomengo/niki/campaign/entity"
|
||||
"git.gocasts.ir/ebhomengo/niki/repository"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
"git.gocasts.ir/ebhomengo/niki/types"
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
type CampaignService struct {
|
||||
repo repository.repo
|
||||
}
|
||||
|
||||
|
||||
// type CampaignServiceInterface interface {
|
||||
// CreateCampaign(ctx context.Context, req CampaignRepository) (types.ID, error)
|
||||
// }
|
||||
|
||||
func NewCampaignService(
|
||||
repo repository.CampaignRepository,
|
||||
participantRepo repository.CampaignParticipantRepository,
|
||||
) *CampaignService {
|
||||
return &CampaignService{
|
||||
repo: repo,
|
||||
participantRepo: participantRepo,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue