Skip to content

Commit

Permalink
add event
Browse files Browse the repository at this point in the history
  • Loading branch information
Noorwahid717 committed Jul 12, 2024
1 parent b8fb445 commit bed570f
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 5 deletions.
71 changes: 71 additions & 0 deletions src/api/dto/event.go
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,
}
}
96 changes: 96 additions & 0 deletions src/api/handler/event.go
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]
5 changes: 5 additions & 0 deletions src/dependency/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
18 changes: 18 additions & 0 deletions src/usecase/base_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// }
34 changes: 29 additions & 5 deletions src/usecase/dto/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
51 changes: 51 additions & 0 deletions src/usecase/event_usecase.go
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)
// }

0 comments on commit bed570f

Please sign in to comment.