Skip to content

Commit e9fae96

Browse files
committed
feat(event-registraiton): list registration and path registration status
1 parent 7ce9d86 commit e9fae96

8 files changed

Lines changed: 311 additions & 51 deletions

File tree

app/events/delivery/http/event.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hammer-code/lms-be/domain"
1212
"github.com/hammer-code/lms-be/pkg/ngelog"
1313
"github.com/hammer-code/lms-be/utils"
14+
"github.com/sirupsen/logrus"
1415
)
1516

1617
// @Summary Create Event
@@ -345,3 +346,82 @@ func (h Handler) GetEvents(w http.ResponseWriter, r *http.Request) {
345346
Pagination: &pagination,
346347
}, w)
347348
}
349+
350+
func (h Handler) ListRegistrationByEvent(w http.ResponseWriter, r *http.Request) {
351+
vars := mux.Vars(r)
352+
idString := vars["id"]
353+
354+
id, err := strconv.ParseUint(idString, 10, 32)
355+
if err != nil {
356+
ngelog.Error(r.Context(), "failed to convert string to uint", err)
357+
utils.Response(domain.HttpResponse{
358+
Code: http.StatusInternalServerError,
359+
Message: err.Error(),
360+
}, w)
361+
return
362+
}
363+
364+
pagination, err := domain.GetPaginationFromCtx(r)
365+
if err != nil {
366+
logrus.Error("failed to parse pagination parameters: ", err)
367+
utils.Response(domain.HttpResponse{
368+
Code: http.StatusBadRequest,
369+
Message: "Invalid pagination parameters",
370+
}, w)
371+
return
372+
}
373+
374+
data, paginationResp, err := h.usecase.ListRegistrationByEvent(r.Context(), uint(id), pagination)
375+
if err != nil {
376+
ngelog.Error(r.Context(), "failed to get event by id", err)
377+
resp := utils.CustomErrorResponse(err)
378+
utils.Response(resp, w)
379+
return
380+
}
381+
382+
utils.Response(domain.HttpResponse{
383+
Code: http.StatusOK,
384+
Message: "success",
385+
Data: data,
386+
Pagination: &paginationResp,
387+
}, w)
388+
}
389+
390+
func (h Handler) UpdateRegistrationStatus(w http.ResponseWriter, r *http.Request) {
391+
vars := mux.Vars(r)
392+
idString := vars["id"]
393+
394+
id, err := strconv.ParseUint(idString, 10, 32)
395+
if err != nil {
396+
ngelog.Error(r.Context(), "failed to convert string to uint", err)
397+
utils.Response(domain.HttpResponse{
398+
Code: http.StatusBadRequest,
399+
Message: "invalid registration id",
400+
}, w)
401+
return
402+
}
403+
404+
// Parse payload
405+
var req domain.UpdateRegistrationStatusRequest
406+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
407+
utils.Response(domain.HttpResponse{
408+
Code: http.StatusBadRequest,
409+
Message: "invalid request body",
410+
}, w)
411+
return
412+
}
413+
414+
// Call usecase with new status
415+
err = h.usecase.UpdateRegistrationStatus(r.Context(), uint(id), req)
416+
if err != nil {
417+
ngelog.Error(r.Context(), "failed to update registration status", err)
418+
resp := utils.CustomErrorResponse(err)
419+
utils.Response(resp, w)
420+
return
421+
}
422+
423+
utils.Response(domain.HttpResponse{
424+
Code: http.StatusOK,
425+
Message: "status updated successfully",
426+
}, w)
427+
}

app/events/repository/get_registration_event.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,14 @@ func (repo *repository) GetRegistrationEvent(ctx context.Context, orderNo string
1616

1717
return data, err
1818
}
19+
20+
func (repo *repository) GetRegistrationEventByID(ctx context.Context, id uint) (data domain.RegistrationEvent, err error) {
21+
db := repo.db.DB(ctx).Model(&domain.RegistrationEvent{})
22+
23+
err = db.Where("id = ?", id).Find(&data).Error
24+
if err != nil {
25+
return
26+
}
27+
28+
return data, err
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
6+
"github.com/hammer-code/lms-be/domain"
7+
"github.com/sirupsen/logrus"
8+
)
9+
10+
func (repo *repository) ListRegistrationByEvent(ctx context.Context, eventID uint, filterPagination domain.FilterPagination) (data []domain.RegistrationEvent, totalCount int64, err error) {
11+
if err := repo.db.DB(ctx).Model(&domain.RegistrationEvent{}).Count(&totalCount).Error; err != nil {
12+
logrus.Error("failed to count blog posts: ", err)
13+
return nil, 0, err
14+
}
15+
16+
offset := filterPagination.GetOffset()
17+
limit := filterPagination.GetLimit()
18+
orderBy := filterPagination.GetOrderBy()
19+
20+
db := repo.db.DB(ctx).
21+
Model(&domain.RegistrationEvent{}).
22+
Where("event_id = ?", eventID).
23+
Preload("User")
24+
25+
if orderBy == "" {
26+
orderBy = "created_at DESC"
27+
}
28+
29+
db = db.Order(orderBy)
30+
31+
if limit > 0 {
32+
db = db.Limit(limit)
33+
}
34+
if offset > 0 {
35+
db = db.Offset(offset)
36+
}
37+
38+
if err := db.Find(&data).Error; err != nil {
39+
return nil, 0, err
40+
}
41+
42+
return data, totalCount, nil
43+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package usecase
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/hammer-code/lms-be/domain"
8+
"github.com/hammer-code/lms-be/utils"
9+
)
10+
11+
func (uc usecase) ListRegistrationByEvent(ctx context.Context, id uint, filterPagination domain.FilterPagination) (resp []domain.EventRegistrationDTO, pagination domain.Pagination, err error) {
12+
13+
registrations, totalCount, err := uc.repository.ListRegistrationByEvent(ctx, id, filterPagination)
14+
if err != nil {
15+
err = utils.NewInternalServerError(ctx, err)
16+
return resp, pagination, err
17+
}
18+
19+
if len(registrations) == 0 {
20+
err = utils.NewNotFoundError(ctx, "registration event not found", errors.New("registration event not found"))
21+
return resp, pagination, err
22+
}
23+
24+
for _, registration := range registrations {
25+
resp = append(resp, registration.ToRegistrationDTO())
26+
}
27+
28+
pagination = domain.NewPagination(int(totalCount), filterPagination)
29+
30+
return
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package usecase
2+
3+
import (
4+
"context"
5+
"errors"
6+
"time"
7+
8+
"github.com/hammer-code/lms-be/domain"
9+
contextkey "github.com/hammer-code/lms-be/pkg/context_key"
10+
"github.com/hammer-code/lms-be/utils"
11+
"gopkg.in/guregu/null.v4"
12+
)
13+
14+
func (uc usecase) UpdateRegistrationStatus(ctx context.Context, id uint, payload domain.UpdateRegistrationStatusRequest) error {
15+
registrations, err := uc.repository.GetRegistrationEventByID(ctx, id)
16+
if err != nil {
17+
err = utils.NewInternalServerError(ctx, err)
18+
return err
19+
}
20+
21+
if registrations.ID == 0 {
22+
err = utils.NewNotFoundError(ctx, "registration event not found", errors.New("registration event not found"))
23+
return err
24+
}
25+
26+
userData := ctx.Value(contextkey.UserKey).(domain.User)
27+
28+
registrations.Status = payload.Status
29+
registrations.UpdatedAt = null.TimeFrom(time.Now())
30+
registrations.UpdatedByUserID = userData.ID
31+
32+
err = uc.repository.UpdateRegistrationEvent(ctx, registrations)
33+
if err != nil {
34+
err = utils.NewInternalServerError(ctx, err)
35+
return err
36+
}
37+
38+
return nil
39+
}

cmd/serve_http.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,15 @@ func registerHandler(app app.App) *mux.Router {
190190
protectedV1Route.HandleFunc("/blogs/{id}", app.BlogPostHandler.DeleteBlogPost).Methods(http.MethodDelete)
191191

192192
// Admin Route
193+
protectedV1AdminRoute.HandleFunc("/events", app.EventHandler.CreateEvent).Methods(http.MethodPost)
193194
protectedV1AdminRoute.HandleFunc("/events", app.EventHandler.GetEvents).Methods(http.MethodGet)
195+
protectedV1AdminRoute.HandleFunc("/events/{id}", app.EventHandler.GetDetail).Methods(http.MethodGet)
194196
protectedV1AdminRoute.HandleFunc("/events/{id}", app.EventHandler.UpdateEvent).Methods(http.MethodPut)
195197
protectedV1AdminRoute.HandleFunc("/events/{id}", app.EventHandler.DeleteEvent).Methods(http.MethodDelete)
196-
protectedV1AdminRoute.HandleFunc("/events", app.EventHandler.CreateEvent).Methods(http.MethodPost)
197-
protectedV1AdminRoute.HandleFunc("/events/{id}", app.EventHandler.GetDetail).Methods(http.MethodGet)
198+
protectedV1AdminRoute.HandleFunc("/events/{id}/registration", app.EventHandler.ListRegistrationByEvent).Methods(http.MethodGet)
199+
protectedV1AdminRoute.HandleFunc("/events/registration/{id}/status", app.EventHandler.UpdateRegistrationStatus).Methods(http.MethodPatch)
200+
201+
// users
198202
protectedV1AdminRoute.HandleFunc("/users", app.UserHandler.GetUsers).Methods(http.MethodGet)
199203

200204
protectedV1AdminRoute.HandleFunc("/images", app.ImageHandler.UploadImage).Methods(http.MethodPost)

domain/event.go

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type EventRepository interface {
2929
GetEventPay(ctx context.Context, orderNo string) (data EventPay, err error)
3030
UpdateRegistrationEvent(ctx context.Context, event RegistrationEvent) error
3131
UpdateEvent(ctx context.Context, event Event) error
32+
ListRegistrationByEvent(ctx context.Context, eventID uint, pagination FilterPagination) (data []RegistrationEvent, totalCount int64, err error)
33+
GetRegistrationEventByID(ctx context.Context, id uint) (data RegistrationEvent, err error)
3234
}
3335

3436
type EventUsecase interface {
@@ -44,6 +46,8 @@ type EventUsecase interface {
4446
ListRegistration(ctx context.Context, filter EventFilter) (resp []RegistrationEvent, pagination Pagination, err error)
4547
ListEventPay(ctx context.Context, filter EventFilter) (data []EventPay, pagination Pagination, err error)
4648
PayProcess(ctx context.Context, payload PayProcessPayload) error
49+
ListRegistrationByEvent(ctx context.Context, eventID uint, filterPagination FilterPagination) (data []EventRegistrationDTO, pagination Pagination, err error)
50+
UpdateRegistrationStatus(ctx context.Context, registrationID uint, payload UpdateRegistrationStatusRequest) error
4751
}
4852

4953
type EventHandler interface {
@@ -60,6 +64,8 @@ type EventHandler interface {
6064
ListEventPay(w http.ResponseWriter, r *http.Request)
6165
PayProcess(w http.ResponseWriter, r *http.Request)
6266
UpdateEvent(w http.ResponseWriter, r *http.Request)
67+
ListRegistrationByEvent(w http.ResponseWriter, r *http.Request)
68+
UpdateRegistrationStatus(w http.ResponseWriter, r *http.Request)
6369
}
6470

6571
type Event struct {
@@ -197,7 +203,7 @@ type EventAdminDTO struct {
197203
ReservationStartDate null.Time `json:"reservation_start_date"`
198204
ReservationEndDate null.Time `json:"reservation_end_date"`
199205
// AdditionalLink string `json:"additional_link"`
200-
SessionType string `json:"session_type"`
206+
SessionType string `json:"session_type"`
201207
}
202208

203209
type UpdateEvenPayload struct {
@@ -238,57 +244,12 @@ func (EventPay) TableName() string {
238244
return "event_pays"
239245
}
240246

241-
type RegisterEventPayload struct {
242-
EventID uint `json:"event_id"`
243-
UserID string `json:"user_id"`
244-
Name string `json:"name"`
245-
Email string `json:"email"`
246-
PhoneNumber string `json:"phone_number"`
247-
ImageProofPayment string `json:"image_proof_payment"`
248-
NetAmount float64 `json:"net_amount"`
249-
}
250-
251-
type RegistrationEvent struct {
252-
ID uint `json:"id" gorm:"primarykey"`
253-
OrderNo string `json:"order_no"`
254-
EventID uint `json:"event_id"` // lock event
255-
UserID string `json:"user_id"` // lock user
256-
ImageProofPayment string `json:"image_proof_payment"`
257-
PaymentDate null.Time `json:"payment_date"`
258-
Status string `json:"status"` // register, pay, approve/cancel/decline
259-
UpToYou string `json:"-"`
260-
CreatedByUserID int `json:"-"`
261-
UpdatedByUserID int `json:"-"`
262-
DeletedByUserID int `json:"-"`
263-
CreatedAt time.Time `json:"created_at"`
264-
UpdatedAt null.Time `json:"-"`
265-
DeletedAt null.Time `json:"-"`
266-
Event Event `json:"event_detail" gorm:"foreignKey:EventID"`
267-
}
268-
269-
func (RegistrationEvent) TableName() string {
270-
return "registration_events"
271-
}
272-
273-
type RegistrationEventDTO struct {
274-
OrderNo string `json:"order_no"`
275-
}
276-
277-
type RegisterEventResponse struct {
278-
OrderNo string `json:"order_no"`
279-
}
280-
281247
type EventPayPayload struct {
282248
OrderNo string `json:"order_no"`
283249
ImageProofPayment string `json:"image_proof_payment"`
284250
NetAmount float64 `json:"net_amount"`
285251
}
286252

287-
type RegisterStatusResponse struct {
288-
OrderNo string `json:"order_no"`
289-
Status string `json:"string"`
290-
}
291-
292253
type PayProcessPayload struct {
293254
OrderNo string `json:"order_no"`
294255
Status string `json:"status"`
@@ -319,12 +280,12 @@ func (e Event) ToDTO() EventDTO {
319280

320281
func (e Event) ToAdminDTO() EventAdminDTO {
321282
id := int(e.ID)
322-
283+
323284
tags := make([]string, len(e.Tags))
324285
for i, tag := range e.Tags {
325286
tags[i] = tag.Tag
326287
}
327-
288+
328289
speakers := make([]string, len(e.Speakers))
329290
for i, speaker := range e.Speakers {
330291
speakers[i] = speaker.Name
@@ -350,6 +311,6 @@ func (e Event) ToAdminDTO() EventAdminDTO {
350311
ReservationStartDate: e.ReservationStartDate,
351312
ReservationEndDate: e.ReservationEndDate,
352313
// AdditionalLink: e.AdditionalLink,
353-
SessionType: e.SessionType,
314+
SessionType: e.SessionType,
354315
}
355316
}

0 commit comments

Comments
 (0)