From bed570fcafd7647baa99f6c71d665c25d06d6c80 Mon Sep 17 00:00:00 2001 From: Noorwahid717 Date: Fri, 12 Jul 2024 15:03:48 +0700 Subject: [PATCH] add event --- src/api/dto/event.go | 71 ++++++++++++++++++++++++++ src/api/handler/event.go | 96 ++++++++++++++++++++++++++++++++++++ src/dependency/dependency.go | 5 ++ src/usecase/base_usecase.go | 18 +++++++ src/usecase/dto/base.go | 34 +++++++++++-- src/usecase/event_usecase.go | 51 +++++++++++++++++++ 6 files changed, 270 insertions(+), 5 deletions(-) create mode 100644 src/api/dto/event.go create mode 100644 src/api/handler/event.go create mode 100644 src/usecase/event_usecase.go diff --git a/src/api/dto/event.go b/src/api/dto/event.go new file mode 100644 index 0000000..360a991 --- /dev/null +++ b/src/api/dto/event.go @@ -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, + } +} diff --git a/src/api/handler/event.go b/src/api/handler/event.go new file mode 100644 index 0000000..182495b --- /dev/null +++ b/src/api/handler/event.go @@ -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] diff --git a/src/dependency/dependency.go b/src/dependency/dependency.go index a6c1f7d..42b95d4 100644 --- a/src/dependency/dependency.go +++ b/src/dependency/dependency.go @@ -111,3 +111,8 @@ func GetRoleRepository(cfg *config.Config) contractRepository.RoleRepository { var preloads []database.PreloadEntity = []database.PreloadEntity{} return infraRepository.NewBaseRepository[model.Role](cfg, preloads) } + +func GetEventRepository(cfg *config.Config) contractRepository.EventRepository { + var preloads []database.PreloadEntity = []database.PreloadEntity{} + return infraRepository.NewBaseRepository[model.Event](cfg, preloads) +} diff --git a/src/usecase/base_usecase.go b/src/usecase/base_usecase.go index 3b68095..43e266d 100644 --- a/src/usecase/base_usecase.go +++ b/src/usecase/base_usecase.go @@ -72,3 +72,21 @@ func (u *BaseUsecase[TEntity, TCreate, TUpdate, TResponse]) GetByFilter(ctx cont return filter.Paginate[TEntity, TResponse](count, entities, req.PageNumber, int64(req.PageSize)) } + +// func (u *BaseUsecase[TEntity, TCreate, TUpdate, TResponse]) GetList(ctx context.Context) ([]TResponse, error) { +// var response []TResponse +// entities, err := u.repository.GetList(ctx) +// if err != nil { +// return response, err +// } + +// for _, entity := range *entities { +// convertedEntity, err := common.TypeConverter[TResponse](entity) +// if err != nil { +// return response, err +// } +// response = append(response, convertedEntity) +// } + +// return response, nil +// } diff --git a/src/usecase/dto/base.go b/src/usecase/dto/base.go index 71d5373..c331db8 100644 --- a/src/usecase/dto/base.go +++ b/src/usecase/dto/base.go @@ -65,18 +65,18 @@ type Company struct { } type CreateColor struct { - Name string - HexCode string + Name string + HexCode string } type UpdateColor struct { - Name string - HexCode string + Name string + HexCode string } type Color struct { IdName - HexCode string + HexCode string } type CreatePersianYear struct { @@ -106,3 +106,27 @@ type PersianYearWithoutDate struct { PersianTitle string Year int } + +type Event struct { + Id int + Name string + Description string + StartDate time.Time + EndDate time.Time + Location string +} + +type CreateEvent struct { + Name string + Description string + StartDate time.Time + EndDate time.Time + Location string +} +type UpdateEvent struct { + Name string + Description string + StartDate time.Time + EndDate time.Time + Location string +} diff --git a/src/usecase/event_usecase.go b/src/usecase/event_usecase.go new file mode 100644 index 0000000..e8a34aa --- /dev/null +++ b/src/usecase/event_usecase.go @@ -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) +// }