Skip to content

Commit 622815b

Browse files
committed
feat: add validation event type list
1 parent 1082f60 commit 622815b

4 files changed

Lines changed: 43 additions & 5 deletions

File tree

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/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: 6 additions & 5 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,7 +68,7 @@ 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"`
@@ -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"`

0 commit comments

Comments
 (0)