Skip to content

Commit cea11ee

Browse files
committed
Merge branch 'development' of github.com:hammercode-dev/lms-be into BE-14/refactoring-payload-and-domain
2 parents cac4988 + ef26486 commit cea11ee

File tree

8 files changed

+92
-45
lines changed

8 files changed

+92
-45
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,12 +4,17 @@ import (
44
"context"
55
"errors"
66

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

1213
func (uc usecase) CreateEvent(ctx context.Context, payload domain.CreateEventPayload) error {
14+
if !constants.IsValidEventType(payload.Type) {
15+
return utils.NewBadRequestError(ctx, "Sorry, invalid event type", errors.New("event type is not valid"))
16+
}
17+
1318
dataImage, err := uc.imageRepository.GetImage(ctx, payload.FileName)
1419
if err != nil {
1520
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +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
contextkey "github.com/hammer-code/lms-be/pkg/context_key"
911
"github.com/hammer-code/lms-be/utils"
1012
"gopkg.in/guregu/null.v4"
1113
)
1214

1315
func (uc usecase) UpdateEvent(ctx context.Context, id uint, payload domain.UpdateEventPayload) error {
14-
15-
userData := ctx.Value(contextkey.UserKey).(domain.User)
16+
if !constants.IsValidEventType(payload.Type) {
17+
return utils.NewBadRequestError(ctx, "Sorry, invalid event type", errors.New("event type is not valid"))
18+
}
1619

1720
err := uc.repository.UpdateEvent(ctx, domain.Event{
1821
ID: id,

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: 32 additions & 36 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

@@ -68,21 +69,23 @@ type Event struct {
6869
Author User `gorm:"foreignKey:AuthorID;references:ID"` // Ensure foreign key is correctly referenced
6970
Image string `json:"image"`
7071
Date null.Time `json:"date"`
71-
Type string `json:"type"`
72+
Type constants.EventType `json:"type"`
7273
Location string `json:"location"`
7374
Duration string `json:"duration"`
7475
Capacity int `json:"capacity"`
75-
Status string `json:"status"` // Conference, Tech Talk, Workshop, Webinar, etc.
76-
Tags []EventTag `gorm:"foreignKey:EventID;constraint:OnDelete:CASCADE;"` // Ensure foreign key is correctly referenced
77-
Speakers []EventSpeaker `gorm:"foreignKey:EventID;constraint:OnDelete:CASCADE;"` // Ensure foreign key is correctly referenced
78-
SessionType string `json:"session_type"` // online, offline, hybrid
76+
Status string `json:"status"`
77+
Tags []EventTag `json:"tags" gorm:"foreignKey:EventID;constraint:OnDelete:CASCADE;"`
78+
Speakers []EventSpeaker `json:"speakers" gorm:"foreignKey:EventID;constraint:OnDelete:CASCADE;"`
7979
RegistrationLink string `json:"registration_link"`
8080
Price float64 `json:"price"` // 0 == free
81+
CreatedBy int `json:"-"`
82+
UpdatedBy int `json:"-"`
83+
DeletedBy int `json:"-"`
8184
ReservationStartDate null.Time `json:"reservation_start_date"`
8285
ReservationEndDate null.Time `json:"reservation_end_date"`
8386
CreatedAt time.Time `json:"created_at"`
84-
UpdatedAt null.Time `json:"updated_at"`
85-
DeletedAt null.Time `json:"deleted_at"`
87+
UpdatedAt null.Time `json:"-"`
88+
DeletedAt null.Time `json:"-"`
8689
AdditionalLink string `json:"additional_link"`
8790
}
8891

@@ -118,7 +121,7 @@ type CreateEventPayload struct {
118121
Slug string `json:"slug" validate:"required"`
119122
IsOnline string `json:"is_online" validate:"required"`
120123
Date null.Time `json:"date" validate:"required"`
121-
Type string `json:"type" validate:"required"`
124+
Type constants.EventType `json:"type" validate:"required"`
122125
Location string `json:"location" validate:"required"`
123126
Duration string `json:"duration" validate:"required"`
124127
Status string `json:"status" validate:"required"`
@@ -140,7 +143,7 @@ type UpdateEventPayload struct {
140143
Slug string `json:"slug" validate:"required"`
141144
IsOnline string `json:"is_online" validate:"required"`
142145
Date null.Time `json:"date" validate:"required"`
143-
Type string `json:"type" validate:"required"`
146+
Type constants.EventType `json:"type" validate:"required"`
144147
Location string `json:"location" validate:"required"`
145148
Duration string `json:"duration" validate:"required"`
146149
Status string `json:"status" validate:"required"`
@@ -155,29 +158,17 @@ type UpdateEventPayload struct {
155158
}
156159

157160
type EventDTO struct {
158-
ID uint `json:"id" gorm:"primarykey"`
159-
Title string `json:"title" `
160-
Description string `json:"description"`
161-
Slug string `json:"slug"`
162-
Author string `json:"author"`
163-
Image string `json:"image"`
164-
Date null.Time `json:"date"`
165-
Type string `json:"type"`
166-
Location string `json:"location"`
167-
Duration string `json:"duration"`
168-
Capacity int `json:"capacity"`
169-
Status string `json:"status"` // Conference, Tech Talk, Workshop, Webinar, etc.
170-
Tags []EventTag `gorm:"foreignKey:EventID;constraint:OnDelete:CASCADE;"` // Ensure foreign key is correctly referenced
171-
Speakers []EventSpeaker `gorm:"foreignKey:EventID;constraint:OnDelete:CASCADE;"` // Ensure foreign key is correctly referenced
172-
SessionType string `json:"session_type"` // online, offline, hybrid
173-
RegistrationLink string `json:"registration_link"`
174-
Price float64 `json:"price"` // 0 == free
175-
ReservationStartDate null.Time `json:"reservation_start_date"`
176-
ReservationEndDate null.Time `json:"reservation_end_date"`
177-
CreatedAt time.Time `json:"created_at"`
178-
UpdatedAt null.Time `json:"updated_at"`
179-
DeletedAt null.Time `json:"deleted_at"`
180-
AdditionalLink string `json:"additional_link"`
161+
ID int `json:"id"`
162+
Title string `json:"title"`
163+
Description string `json:"description"`
164+
Author string `json:"author"`
165+
ImageEvent string `json:"image_event"`
166+
DateEvent null.Time `json:"date_event"`
167+
Type constants.EventType `json:"type"`
168+
Location string `json:"location"`
169+
Duration string `json:"duration"`
170+
Capacity int `json:"capacity"`
171+
RegistrationLink string `json:"registration_link"`
181172
}
182173

183174
type UpdateEvenPayload struct {
@@ -186,7 +177,7 @@ type UpdateEvenPayload struct {
186177
Author string `json:"author"`
187178
ImageEvent string `json:"image_event"`
188179
DateEvent null.Time `json:"date_event"`
189-
Type string `json:"type"`
180+
Type constants.EventType `json:"type"`
190181
Location string `json:"location"`
191182
Duration string `json:"duration"`
192183
Capacity int `json:"capacity"`
@@ -196,7 +187,7 @@ type UpdateEvenPayload struct {
196187
type EventFilter struct {
197188
ID uint
198189
Title string
199-
Type string
190+
Type constants.EventType
200191
Status string
201192
StartDate null.Time
202193
EndDate null.Time
@@ -236,9 +227,14 @@ type RegistrationEvent struct {
236227
ImageProofPayment string `json:"image_proof_payment"`
237228
PaymentDate null.Time `json:"payment_date"`
238229
Status string `json:"status"` // register, pay, approve/cancel/decline
230+
UpToYou string `json:"-"`
231+
CreatedByUserID int `json:"-"`
232+
UpdatedByUserID int `json:"-"`
233+
DeletedByUserID int `json:"-"`
239234
CreatedAt time.Time `json:"created_at"`
240-
UpdatedAt null.Time `json:"updated_at"`
241-
DeletedAt null.Time `json:"deleted_at"`
235+
UpdatedAt null.Time `json:"-"`
236+
DeletedAt null.Time `json:"-"`
237+
Event Event `json:"event_detail" gorm:"foreignKey:EventID"`
242238
}
243239

244240
func (RegistrationEvent) TableName() string {

0 commit comments

Comments
 (0)