Skip to content

Commit ef26486

Browse files
authored
Merge pull request #25 from hammercode-dev/enhance/registration-event
[ENHANCE] - return full event details in registration list API
2 parents 1782d03 + 6c8d41c commit ef26486

File tree

8 files changed

+83
-27
lines changed

8 files changed

+83
-27
lines changed

app/events/delivery/http/event.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strconv"
88

99
"github.com/gorilla/mux"
10+
"github.com/hammer-code/lms-be/constants"
1011
"github.com/hammer-code/lms-be/domain"
1112
"github.com/hammer-code/lms-be/pkg/ngelog"
1213
"github.com/hammer-code/lms-be/utils"
@@ -268,7 +269,7 @@ func (h Handler) List(w http.ResponseWriter, r *http.Request) {
268269

269270
data, pagination, err := h.usecase.GetEvents(r.Context(), domain.EventFilter{
270271
Title: r.URL.Query().Get("title"),
271-
Type: r.URL.Query().Get("type"),
272+
Type: constants.EventType(r.URL.Query().Get("type")),
272273
Status: r.URL.Query().Get("status"),
273274
StartDate: startDate,
274275
EndDate: endDate,
@@ -323,7 +324,7 @@ func (h Handler) GetEvents(w http.ResponseWriter, r *http.Request) {
323324
endDate, _ := utils.ParseDate(r.URL.Query().Get("end_date"))
324325
data, pagination, err := h.usecase.GetEvents(r.Context(), domain.EventFilter{
325326
Title: r.URL.Query().Get("title"),
326-
Type: r.URL.Query().Get("type"),
327+
Type: constants.EventType(r.URL.Query().Get("type")),
327328
Status: r.URL.Query().Get("status"),
328329
StartDate: startDate,
329330
EndDate: endDate,

app/events/delivery/http/list_registration.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package http
33
import (
44
"net/http"
55

6+
"github.com/hammer-code/lms-be/constants"
67
"github.com/hammer-code/lms-be/domain"
78
"github.com/hammer-code/lms-be/pkg/ngelog"
89
"github.com/hammer-code/lms-be/utils"
@@ -20,6 +21,7 @@ import (
2021
// @Param start_date query string false "string"
2122
// @Param end_date query string false "string"
2223
// @Param status query string false "string"
24+
// @Param type query string false "string"
2325
// @Failure 400 {object} domain.HttpResponse
2426
// @Failure 500 {object} domain.HttpResponse
2527
// @Success 200 {object} []domain.RegistrationEvent
@@ -39,6 +41,7 @@ func (h Handler) ListRegistration(w http.ResponseWriter, r *http.Request) {
3941

4042
data, pagination, err := h.usecase.ListRegistration(r.Context(), domain.EventFilter{
4143
Status: r.URL.Query().Get("status"),
44+
Type: constants.EventType(r.URL.Query().Get("type")),
4245
StartDate: startDate,
4346
EndDate: endDate,
4447
FilterPagination: flterPagination,

app/events/repository/list_registration.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,30 @@ import (
77
)
88

99
func (repo *repository) ListRegistration(ctx context.Context, filter domain.EventFilter, email string) (tData int, data []domain.RegistrationEvent, err error) {
10-
db := repo.db.DB(ctx).Model(&domain.RegistrationEvent{})
10+
db := repo.db.DB(ctx).Model(&domain.RegistrationEvent{}).
11+
Preload("Event").
12+
Preload("Event.Tags").
13+
Preload("Event.Speakers")
1114

1215
if filter.Status != "" {
13-
db = db.Where("status = ?", filter.Status)
16+
db = db.Where("registration_events.status = ?", filter.Status)
17+
}
18+
19+
if filter.Type != "" {
20+
db = db.Joins("JOIN events ON events.id = registration_events.event_id").
21+
Where("events.type = ?", filter.Type)
1422
}
1523

1624
if filter.StartDate.Valid {
17-
db = db.Where("start_date > ?", filter.StartDate)
25+
db = db.Where("registration_events.created_at >= ?", filter.StartDate)
1826
}
1927

2028
if filter.EndDate.Valid {
21-
db = db.Where("end_date < ?", filter.EndDate)
29+
db = db.Where("registration_events.created_at <= ?", filter.EndDate)
2230
}
2331

2432
if email != "" {
25-
db = db.Where("email = ?", email)
33+
db = db.Where("registration_events.email = ?", email)
2634
}
2735

2836
var totalData int64

app/events/usecase/create_event.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import (
44
"context"
55
"errors"
66

7+
"github.com/hammer-code/lms-be/constants"
78
"github.com/hammer-code/lms-be/domain"
89
"github.com/hammer-code/lms-be/utils"
910
)
1011

1112
func (uc usecase) CreateEvent(ctx context.Context, payload domain.CreateEventPayload) error {
13+
if !constants.IsValidEventType(payload.Type) {
14+
return utils.NewBadRequestError(ctx, "Sorry, invalid event type", errors.New("event type is not valid"))
15+
}
16+
1217
dataImage, err := uc.imageRepository.GetImage(ctx, payload.FileName)
1318
if err != nil {
1419
err = utils.NewInternalServerError(ctx, err)

app/events/usecase/list_registration_event.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ func (uc usecase) ListRegistration(ctx context.Context, filter domain.EventFilte
2323

2424
for i, data := range datas {
2525
datas[i].ImageProofPayment = fmt.Sprintf("%s/api/v1/public/storage/images/%s", baseURL, data.ImageProofPayment)
26+
27+
// Update event image URL
28+
if datas[i].Event.Image != "" {
29+
datas[i].Event.Image = fmt.Sprintf("%s/api/v1/public/storage/images/%s", baseURL, datas[i].Event.Image)
30+
}
2631
}
2732

2833
return datas, domain.NewPagination(tData, filter.FilterPagination), err

app/events/usecase/update_event.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ package usecase
22

33
import (
44
"context"
5+
"errors"
56
"time"
67

8+
"github.com/hammer-code/lms-be/constants"
79
"github.com/hammer-code/lms-be/domain"
810
"github.com/hammer-code/lms-be/utils"
911
"gopkg.in/guregu/null.v4"
1012
)
1113

1214
func (uc usecase) UpdateEvent(ctx context.Context, id uint, payload domain.UpdateEventPayload) error {
15+
if !constants.IsValidEventType(payload.Type) {
16+
return utils.NewBadRequestError(ctx, "Sorry, invalid event type", errors.New("event type is not valid"))
17+
}
18+
1319
err := uc.repository.UpdateEvent(ctx, domain.Event{
1420
ID: id,
1521
Title: payload.Title,

constants/event.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,29 @@ const (
55
Soon = "soon"
66
Closed = "closed"
77
)
8+
9+
type EventType string
10+
11+
const (
12+
EventTypeConference EventType = "Conference"
13+
EventTypeTechTalk EventType = "Tech Talk"
14+
EventTypeNgobar EventType = "Ngobar"
15+
)
16+
17+
func GetValidEventTypes() []EventType {
18+
return []EventType{
19+
EventTypeConference,
20+
EventTypeTechTalk,
21+
EventTypeNgobar,
22+
}
23+
}
24+
25+
func IsValidEventType(eventType EventType) bool {
26+
validTypes := GetValidEventTypes()
27+
for _, validType := range validTypes {
28+
if validType == eventType {
29+
return true
30+
}
31+
}
32+
return false
33+
}

domain/event.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"time"
77

8+
"github.com/hammer-code/lms-be/constants"
89
"gopkg.in/guregu/null.v4"
910
)
1011

@@ -67,23 +68,23 @@ type Event struct {
6768
Author string `json:"author"`
6869
Image string `json:"image"`
6970
Date null.Time `json:"date"`
70-
Type string `json:"type"`
71+
Type constants.EventType `json:"type"`
7172
Location string `json:"location"`
7273
Duration string `json:"duration"`
7374
Capacity int `json:"capacity"`
74-
Status string `json:"status"` // comming soon
75-
Tags []EventTag `gorm:"foreignKey:EventID;constraint:OnDelete:CASCADE;"` // Ensure foreign key is correctly referenced
76-
Speakers []EventSpeaker `gorm:"foreignKey:EventID;constraint:OnDelete:CASCADE;"` // Ensure foreign key is correctly referenced
75+
Status string `json:"status"`
76+
Tags []EventTag `json:"tags" gorm:"foreignKey:EventID;constraint:OnDelete:CASCADE;"`
77+
Speakers []EventSpeaker `json:"speakers" gorm:"foreignKey:EventID;constraint:OnDelete:CASCADE;"`
7778
RegistrationLink string `json:"registration_link"`
7879
Price float64 `json:"price"` // 0 == free
79-
CreatedBy int `json:"created_by"`
80-
UpdatedBy int `json:"updated_by"`
81-
DeletedBy int `json:"deleted_by"`
80+
CreatedBy int `json:"-"`
81+
UpdatedBy int `json:"-"`
82+
DeletedBy int `json:"-"`
8283
ReservationStartDate null.Time `json:"reservation_start_date"`
8384
ReservationEndDate null.Time `json:"reservation_end_date"`
8485
CreatedAt time.Time `json:"created_at"`
85-
UpdatedAt null.Time `json:"updated_at"`
86-
DeletedAt null.Time `json:"deleted_at"`
86+
UpdatedAt null.Time `json:"-"`
87+
DeletedAt null.Time `json:"-"`
8788
AdditionalLink string `json:"additional_link"`
8889
}
8990

@@ -119,7 +120,7 @@ type CreateEventPayload struct {
119120
Slug string `json:"slug" validate:"required"`
120121
IsOnline string `json:"is_online" validate:"required"`
121122
Date null.Time `json:"date" validate:"required"`
122-
Type string `json:"type" validate:"required"`
123+
Type constants.EventType `json:"type" validate:"required"`
123124
Location string `json:"location" validate:"required"`
124125
Duration string `json:"duration" validate:"required"`
125126
Status string `json:"status" validate:"required"`
@@ -141,7 +142,7 @@ type UpdateEventPayload struct {
141142
Slug string `json:"slug" validate:"required"`
142143
IsOnline string `json:"is_online" validate:"required"`
143144
Date null.Time `json:"date" validate:"required"`
144-
Type string `json:"type" validate:"required"`
145+
Type constants.EventType `json:"type" validate:"required"`
145146
Location string `json:"location" validate:"required"`
146147
Duration string `json:"duration" validate:"required"`
147148
Status string `json:"status" validate:"required"`
@@ -162,7 +163,7 @@ type EventDTO struct {
162163
Author string `json:"author"`
163164
ImageEvent string `json:"image_event"`
164165
DateEvent null.Time `json:"date_event"`
165-
Type string `json:"type"`
166+
Type constants.EventType `json:"type"`
166167
Location string `json:"location"`
167168
Duration string `json:"duration"`
168169
Capacity int `json:"capacity"`
@@ -175,7 +176,7 @@ type UpdateEvenPayload struct {
175176
Author string `json:"author"`
176177
ImageEvent string `json:"image_event"`
177178
DateEvent null.Time `json:"date_event"`
178-
Type string `json:"type"`
179+
Type constants.EventType `json:"type"`
179180
Location string `json:"location"`
180181
Duration string `json:"duration"`
181182
Capacity int `json:"capacity"`
@@ -185,7 +186,7 @@ type UpdateEvenPayload struct {
185186
type EventFilter struct {
186187
ID uint
187188
Title string
188-
Type string
189+
Type constants.EventType
189190
Status string
190191
StartDate null.Time
191192
EndDate null.Time
@@ -228,13 +229,14 @@ type RegistrationEvent struct {
228229
ImageProofPayment string `json:"image_proof_payment"`
229230
PaymentDate null.Time `json:"payment_date"`
230231
Status string `json:"status"` // register, pay, approve/cancel/decline
231-
UpToYou string `json:"up_to_you"`
232-
CreatedByUserID int `json:"created_by_user_id"`
233-
UpdatedByUserID int `json:"updated_by_user_id"`
234-
DeletedByUserID int `json:"deleted_by_user_id"`
232+
UpToYou string `json:"-"`
233+
CreatedByUserID int `json:"-"`
234+
UpdatedByUserID int `json:"-"`
235+
DeletedByUserID int `json:"-"`
235236
CreatedAt time.Time `json:"created_at"`
236-
UpdatedAt null.Time `json:"updated_at"`
237-
DeletedAt null.Time `json:"deleted_at"`
237+
UpdatedAt null.Time `json:"-"`
238+
DeletedAt null.Time `json:"-"`
239+
Event Event `json:"event_detail" gorm:"foreignKey:EventID"`
238240
}
239241

240242
func (RegistrationEvent) TableName() string {

0 commit comments

Comments
 (0)