forked from naeemaei/golang-clean-web-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8fb445
commit bed570f
Showing
6 changed files
with
270 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package dto | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/naeemaei/golang-clean-web-api/usecase/dto" | ||
) | ||
|
||
type CreateEvent struct { | ||
Name string `json:"name" binding:"required,alpha,min=3,max=20"` | ||
Description string `json:"description" binding:"required,alpha,min=3,max=20"` | ||
StartDate time.Time `json:"startDate" binding:"required"` | ||
EndDate time.Time `json:"endDate" binding:"required"` | ||
Location string `json:"location" binding:"required,alpha,min=3,max=20"` | ||
} | ||
|
||
type UpdateEvent struct { | ||
Name string `json:"name,omitempty" binding:"alpha,min=3,max=20"` | ||
Description string `json:"description,omitempty" binding:"alpha,min=3,max=20"` | ||
StartDate time.Time `json:"startDate,omitempty"` | ||
EndDate time.Time `json:"endDate,omitempty"` | ||
Location string `json:"location,omitempty" binding:"alpha,min=3,max=20"` | ||
} | ||
|
||
type EventResponse struct { | ||
Id int `json:"id"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
StartDate time.Time `json:"startDate"` | ||
EndDate time.Time `json:"endDate"` | ||
Location string `json:"location"` | ||
} | ||
|
||
func ToEventResponse(from dto.Event) EventResponse { | ||
return EventResponse{ | ||
Id: from.Id, | ||
Name: from.Name, | ||
Description: from.Description, | ||
StartDate: from.StartDate, | ||
EndDate: from.EndDate, | ||
Location: from.Location, | ||
} | ||
} | ||
|
||
// func ToEventListResponse(from []dto.Event) []EventResponse { | ||
// var result []EventResponse | ||
// for _, item := range from { | ||
// result = append(result, ToEventResponse(item)) | ||
// } | ||
// return result | ||
// } | ||
|
||
func ToCreateEvent(from CreateEvent) dto.CreateEvent { | ||
return dto.CreateEvent{ | ||
Name: from.Name, | ||
Description: from.Description, | ||
StartDate: from.StartDate, | ||
EndDate: from.EndDate, | ||
Location: from.Location, | ||
} | ||
} | ||
|
||
func ToUpdateEvent(from UpdateEvent) dto.UpdateEvent { | ||
return dto.UpdateEvent{ | ||
Name: from.Name, | ||
Description: from.Description, | ||
StartDate: from.StartDate, | ||
EndDate: from.EndDate, | ||
Location: from.Location, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package handler | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/naeemaei/golang-clean-web-api/api/dto" | ||
_ "github.com/naeemaei/golang-clean-web-api/api/helper" | ||
"github.com/naeemaei/golang-clean-web-api/config" | ||
"github.com/naeemaei/golang-clean-web-api/dependency" | ||
_ "github.com/naeemaei/golang-clean-web-api/domain/filter" | ||
"github.com/naeemaei/golang-clean-web-api/usecase" | ||
) | ||
|
||
type EventHandler struct { | ||
usecase *usecase.EventUsecase | ||
} | ||
|
||
func NewEventHandler(cfg *config.Config) *EventHandler { | ||
return &EventHandler{ | ||
usecase: usecase.NewEventUsecase(cfg, dependency.GetEventRepository(cfg)), | ||
} | ||
} | ||
|
||
// CreateEvent godoc | ||
// @Summary Create a Event | ||
// @Description Create a Event | ||
// @Tags Events | ||
// @Accept json | ||
// @produces json | ||
// @Param Request body dto.CreateEvent true "Create a Event" | ||
// @Success 201 {object} helper.BaseHttpResponse{result=dto.EventResponse} "Event response" | ||
// @Failure 400 {object} helper.BaseHttpResponse "Bad request" | ||
// @Router /v1/events/ [post] | ||
// @Security AuthBearer | ||
func (h *EventHandler) Create(c *gin.Context) { | ||
Create(c, dto.ToCreateEvent, dto.ToEventResponse, h.usecase.Create) | ||
} | ||
|
||
// UpdateEvent godoc | ||
// @Summary Update a Event | ||
// @Description Update a Event | ||
// @Tags Events | ||
// @Accept json | ||
// @produces json | ||
// @Param id path int true "Id" | ||
// @Param Request body dto.UpdateEvent true "Update a Event" | ||
// @Success 200 {object} helper.BaseHttpResponse{result=dto.EventResponse} "Event response" | ||
// @Failure 400 {object} helper.BaseHttpResponse "Bad request" | ||
// @Failure 404 {object} helper.BaseHttpResponse "Not found" | ||
// @Router /v1/events/{id} [put] | ||
// @Security AuthBearer | ||
func (h *EventHandler) Update(c *gin.Context) { | ||
Update(c, dto.ToUpdateEvent, dto.ToEventResponse, h.usecase.Update) | ||
} | ||
|
||
// DeleteEvent godoc | ||
// @Summary Delete a Event | ||
// @Description Delete a Event | ||
// @Tags Events | ||
// @Accept json | ||
// @produces json | ||
// @Param id path int true "Id" | ||
// @Success 204 {object} helper.BaseHttpResponse "No content" | ||
// @Failure 400 {object} helper.BaseHttpResponse "Bad request" | ||
// @Failure 404 {object} helper.BaseHttpResponse "Not found" | ||
// @Router /v1/events/{id} [delete] | ||
// @Security AuthBearer | ||
func (h *EventHandler) Delete(c *gin.Context) { | ||
Delete(c, h.usecase.Delete) | ||
} | ||
|
||
// GetEvent godoc | ||
// @Summary Get a Event | ||
// @Description Get a Event | ||
// @Tags Events | ||
// @Accept json | ||
// @produces json | ||
// @Param id path int true "Id" | ||
// @Success 200 {object} helper.BaseHttpResponse{result=dto.EventResponse} "Event response" | ||
// @Failure 400 {object} helper.BaseHttpResponse "Bad request" | ||
// @Router /v1/events/{id} [get] | ||
func (h *EventHandler) GetById(c *gin.Context) { | ||
GetById(c, dto.ToEventResponse, h.usecase.GetById) | ||
} | ||
|
||
// GetEvents godoc | ||
// @Summary Get Events | ||
// @Description Get Events | ||
// @Tags Events | ||
// @Accept json | ||
// @produces json | ||
// @Param page query int false "Page" | ||
// @Param limit query int false "Limit" | ||
// @Param filter query string false "Filter" | ||
// @Success 200 {object} helper.BaseHttpResponse{result=filter.PagedList[dto.EventResponse]} "Event response" | ||
// @Failure 400 {object} helper.BaseHttpResponse "Bad request" | ||
// @Router /v1/events/ [get] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package usecase | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/naeemaei/golang-clean-web-api/config" | ||
"github.com/naeemaei/golang-clean-web-api/domain/filter" | ||
model "github.com/naeemaei/golang-clean-web-api/domain/model" | ||
"github.com/naeemaei/golang-clean-web-api/domain/repository" | ||
"github.com/naeemaei/golang-clean-web-api/usecase/dto" | ||
) | ||
|
||
type EventUsecase struct { | ||
base *BaseUsecase[model.Event, dto.CreateEvent, dto.UpdateEvent, dto.Event] | ||
} | ||
|
||
func NewEventUsecase(cfg *config.Config, repository repository.EventRepository) *EventUsecase { | ||
return &EventUsecase{ | ||
base: NewBaseUsecase[model.Event, dto.CreateEvent, dto.UpdateEvent, dto.Event](cfg, repository), | ||
} | ||
} | ||
|
||
// Create | ||
func (u *EventUsecase) Create(ctx context.Context, req dto.CreateEvent) (dto.Event, error) { | ||
return u.base.Create(ctx, req) | ||
} | ||
|
||
// Update | ||
func (s *EventUsecase) Update(ctx context.Context, id int, req dto.UpdateEvent) (dto.Event, error) { | ||
return s.base.Update(ctx, id, req) | ||
} | ||
|
||
// Delete | ||
func (s *EventUsecase) Delete(ctx context.Context, id int) error { | ||
return s.base.Delete(ctx, id) | ||
} | ||
|
||
// Get By Id | ||
func (s *EventUsecase) GetById(ctx context.Context, id int) (dto.Event, error) { | ||
return s.base.GetById(ctx, id) | ||
} | ||
|
||
// Get By Filter | ||
func (s *EventUsecase) GetByFilter(ctx context.Context, req filter.PaginationInputWithFilter) (*filter.PagedList[dto.Event], error) { | ||
return s.base.GetByFilter(ctx, req) | ||
} | ||
|
||
// // Get list | ||
// func (s *EventUsecase) GetList(ctx context.Context) ([]dto.Event, error) { | ||
// return s.base.GetList(ctx) | ||
// } |