Skip to content

Commit

Permalink
feat(event): list registration
Browse files Browse the repository at this point in the history
  • Loading branch information
AfandyW committed Oct 19, 2024
1 parent 46293e0 commit 4b4a697
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/events/delivery/http/list_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import (

func (h Handler) GetEvents(w http.ResponseWriter, r *http.Request) {
flterPagination, err := domain.GetPaginationFromCtx(r)
if err != nil {
logrus.Error("failed to get pagination : ", err)
utils.Response(domain.HttpResponse{
Code: 500,
Message: err.Error(),
}, w)
return
}

startDate, _ := utils.ParseDate(r.URL.Query().Get("start_date"))
endDate, _ := utils.ParseDate(r.URL.Query().Get("end_date"))
Expand Down
47 changes: 47 additions & 0 deletions app/events/delivery/http/list_registration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package http

import (
"net/http"

"github.com/hammer-code/lms-be/domain"
"github.com/hammer-code/lms-be/utils"
"github.com/sirupsen/logrus"
)

func (h Handler) ListRegistration(w http.ResponseWriter, r *http.Request) {
flterPagination, err := domain.GetPaginationFromCtx(r)
if err != nil {
logrus.Error("failed to get pagination : ", err)
utils.Response(domain.HttpResponse{
Code: 500,
Message: err.Error(),
}, w)
return
}

startDate, _ := utils.ParseDate(r.URL.Query().Get("start_date"))
endDate, _ := utils.ParseDate(r.URL.Query().Get("end_date"))

data, pagination, err := h.usecase.ListRegistration(r.Context(), domain.EventFilter{
Status: r.URL.Query().Get("status"),
StartDate: startDate,
EndDate: endDate,
FilterPagination: flterPagination,
})

if err != nil {
logrus.Error("failed to get registration event : ", err)
utils.Response(domain.HttpResponse{
Code: 500,
Message: err.Error(),
}, w)
return
}

utils.Response(domain.HttpResponse{
Code: 200,
Message: "success",
Data: data,
Pagination: pagination,
}, w)
}
29 changes: 29 additions & 0 deletions app/events/repository/get_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,32 @@ func (repo *repository) GetRegistrationEvent(ctx context.Context, orderNo string

return data, err
}

func (repo *repository) ListRegistration(ctx context.Context, filter domain.EventFilter) (tData int, data []domain.RegistrationEvent, err error) {
db := repo.db.DB(ctx).Model(&domain.RegistrationEvent{})

var totalData int64

if filter.Status != "" {
db = db.Where("status = ?", filter.Status)
}

if filter.StartDate.Valid {
db = db.Where("start_date > ?", filter.StartDate)
}

if filter.StartDate.Valid {
db = db.Where("end_date < ?", filter.EndDate)
}

db.Count(&totalData)

err = db.Limit(filter.FilterPagination.GetLimit()).
Offset(filter.FilterPagination.GetOffset()).Find(&data).Error
if err != nil {
logrus.Error("failed to list registration event use generic conditions")
return
}

return int(totalData), data, err
}
18 changes: 18 additions & 0 deletions app/events/usecase/list_registration_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package usecase

import (
"context"

"github.com/hammer-code/lms-be/domain"
"github.com/sirupsen/logrus"
)

func (uc usecase) ListRegistration(ctx context.Context, filter domain.EventFilter) (resp []domain.RegistrationEvent, pagination domain.Pagination, err error) {
tData, datas, err := uc.repository.ListRegistration(ctx, filter)
if err != nil {
logrus.Error("failed to get event")
return
}

return datas, domain.NewPagination(tData, filter.FilterPagination), err
}
1 change: 1 addition & 0 deletions cmd/serve_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func registerHandler(app app.App) *mux.Router {

protectedV1Route.HandleFunc("/events", app.EventHandler.CreateEvent).Methods(http.MethodPost)
protectedV1Route.HandleFunc("/events", app.EventHandler.GetEvents).Methods(http.MethodGet)
protectedV1Route.HandleFunc("/events/registrations", app.EventHandler.ListRegistration).Methods(http.MethodGet)
protectedV1Route.HandleFunc("/events/{id}", app.EventHandler.GetEventByID).Methods(http.MethodGet)
protectedV1Route.HandleFunc("/images", app.ImageHandler.UploadImage).Methods(http.MethodPost)

Expand Down
3 changes: 3 additions & 0 deletions domain/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type EventRepository interface {
CreateRegisterEvent(ctx context.Context, event RegistrationEvent) (uint, error)
GetEvent(ctx context.Context, eventID uint) (data Event, err error)
GetRegistrationEvent(ctx context.Context, orderNo string) (data RegistrationEvent, err error)
ListRegistration(ctx context.Context, filter EventFilter) (tData int, data []RegistrationEvent, err error)
}

type EventUsecase interface {
Expand All @@ -30,6 +31,7 @@ type EventUsecase interface {
CreatePayEvent(ctx context.Context, payload EventPayPayload) error
GetEventByID(ctx context.Context, id uint) (resp Event, err error)
RegistrationStatus(ctx context.Context, orderNo string) (resp RegisterStatusResponse, err error)
ListRegistration(ctx context.Context, filter EventFilter) (resp []RegistrationEvent, pagination Pagination, err error)
}

type EventHandler interface {
Expand All @@ -39,6 +41,7 @@ type EventHandler interface {
PayEvent(w http.ResponseWriter, r *http.Request)
GetEventByID(w http.ResponseWriter, r *http.Request)
RegistrationStatus(w http.ResponseWriter, r *http.Request)
ListRegistration(w http.ResponseWriter, r *http.Request)
}

type Event struct {
Expand Down

0 comments on commit 4b4a697

Please sign in to comment.