Skip to content

Commit 2e3eff5

Browse files
authored
Merge pull request #29 from hammercode-dev/BE-17/create-and-adjust-api-detail-event-admin
feat(event-admin): add routes create image by admin, and create event…
2 parents 93abdc7 + 1764d76 commit 2e3eff5

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

app/events/delivery/http/event.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (h Handler) GetDetail(w http.ResponseWriter, r *http.Request) {
181181
return
182182
}
183183

184-
data, err := h.usecase.GetEventByID(r.Context(), uint(id))
184+
data, err := h.usecase.GetEventByIDAdmin(r.Context(), uint(id))
185185
if err != nil {
186186
ngelog.Error(r.Context(), "failed to get event by id", err)
187187
resp := utils.CustomErrorResponse(err)

app/events/usecase/get_event_general.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,16 @@ func (uc usecase) GetEventByID(ctx context.Context, id uint) (resp domain.EventD
2121

2222
return event.ToDTO(), err
2323
}
24+
25+
func (uc usecase) GetEventByIDAdmin(ctx context.Context, id uint) (resp domain.EventAdminDTO, err error) {
26+
event, err := uc.repository.GetEvent(ctx, id)
27+
if err != nil {
28+
err = utils.NewInternalServerError(ctx, err)
29+
return resp, err
30+
}
31+
baseURL := config.GetConfig().BaseURL
32+
33+
event.Image = fmt.Sprintf("%s/api/v1/public/storage/images/%s", baseURL, event.Image)
34+
35+
return event.ToAdminDTO(), err
36+
}

cmd/serve_http.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ func registerHandler(app app.App) *mux.Router {
197197
protectedV1AdminRoute.HandleFunc("/events/{id}", app.EventHandler.GetDetail).Methods(http.MethodGet)
198198
protectedV1AdminRoute.HandleFunc("/users", app.UserHandler.GetUsers).Methods(http.MethodGet)
199199

200+
protectedV1AdminRoute.HandleFunc("/images", app.ImageHandler.UploadImage).Methods(http.MethodPost)
201+
protectedV1AdminRoute.HandleFunc("/images/{id}", app.ImageHandler.UpdateImage).Methods(http.MethodPut)
202+
200203
return router
201204
}
202205

domain/event.go

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type EventUsecase interface {
3838
CreateRegistrationEvent(ctx context.Context, payload RegisterEventPayload) (RegisterEventResponse, error)
3939
CreateEventPay(ctx context.Context, payload EventPayPayload) error
4040
GetEventByID(ctx context.Context, id uint) (resp EventDTO, err error)
41+
GetEventByIDAdmin(ctx context.Context, id uint) (resp EventAdminDTO, err error)
4142
DeleteEvent(ctx context.Context, id uint) (err error)
4243
RegistrationStatus(ctx context.Context, orderNo string) (resp RegisterStatusResponse, err error)
4344
ListRegistration(ctx context.Context, filter EventFilter) (resp []RegistrationEvent, pagination Pagination, err error)
@@ -121,7 +122,6 @@ type CreateEventPayload struct {
121122
Author string `json:"author" validate:"required"`
122123
FileName string `json:"file_name" validate:"required"`
123124
Slug string `json:"slug" validate:"required"`
124-
IsOnline string `json:"is_online" validate:"required"`
125125
Date null.Time `json:"date" validate:"required"`
126126
Type constants.EventType `json:"type" validate:"required"`
127127
Location string `json:"location" validate:"required"`
@@ -135,7 +135,7 @@ type CreateEventPayload struct {
135135
ReservationStartDate null.Time `json:"reservation_start_date"`
136136
ReservationEndDate null.Time `json:"reservation_end_date"`
137137
AdditionalLink string `json:"additional_link"`
138-
SessionType string `json:"session_type"`
138+
SessionType string `json:"session_type" validate:"required"`
139139
}
140140

141141
type UpdateEventPayload struct {
@@ -144,7 +144,6 @@ type UpdateEventPayload struct {
144144
Author string `json:"author" validate:"required"`
145145
FileName string `json:"file_name" validate:"required"`
146146
Slug string `json:"slug" validate:"required"`
147-
IsOnline string `json:"is_online" validate:"required"`
148147
Date null.Time `json:"date" validate:"required"`
149148
Type constants.EventType `json:"type" validate:"required"`
150149
Location string `json:"location" validate:"required"`
@@ -158,7 +157,7 @@ type UpdateEventPayload struct {
158157
ReservationStartDate null.Time `json:"reservation_start_date"`
159158
ReservationEndDate null.Time `json:"reseveration_end_date"`
160159
AdditionalLink string `json:"additional_link"`
161-
SessionType string `json:"session_type"`
160+
SessionType string `json:"session_type" validate:"required"`
162161
}
163162

164163
type EventDTO struct {
@@ -178,6 +177,29 @@ type EventDTO struct {
178177
Status string `json:"status"`
179178
}
180179

180+
type EventAdminDTO struct {
181+
ID int `json:"id"`
182+
Title string `json:"title"`
183+
Description string `json:"description"`
184+
Author string `json:"author"`
185+
FileName string `json:"file_name"`
186+
Slug string `json:"slug"`
187+
Date null.Time `json:"date"`
188+
Type constants.EventType `json:"type"`
189+
Location string `json:"location"`
190+
Duration string `json:"duration"`
191+
Status string `json:"status"`
192+
Capacity int `json:"capacity"`
193+
Price float64 `json:"price"`
194+
RegistrationLink string `json:"registration_link"`
195+
Tags []string `json:"tags"`
196+
Speakers []string `json:"speakers"`
197+
ReservationStartDate null.Time `json:"reservation_start_date"`
198+
ReservationEndDate null.Time `json:"reservation_end_date"`
199+
// AdditionalLink string `json:"additional_link"`
200+
SessionType string `json:"session_type"`
201+
}
202+
181203
type UpdateEvenPayload struct {
182204
Title string `json:"title"`
183205
Description string `json:"description"`
@@ -294,3 +316,40 @@ func (e Event) ToDTO() EventDTO {
294316
SessionType: e.SessionType,
295317
}
296318
}
319+
320+
func (e Event) ToAdminDTO() EventAdminDTO {
321+
id := int(e.ID)
322+
323+
tags := make([]string, len(e.Tags))
324+
for i, tag := range e.Tags {
325+
tags[i] = tag.Tag
326+
}
327+
328+
speakers := make([]string, len(e.Speakers))
329+
for i, speaker := range e.Speakers {
330+
speakers[i] = speaker.Name
331+
}
332+
333+
return EventAdminDTO{
334+
ID: id,
335+
Title: e.Title,
336+
Description: e.Description,
337+
Author: e.Author.Username,
338+
FileName: e.Image,
339+
Slug: e.Slug,
340+
Date: e.Date,
341+
Type: e.Type,
342+
Location: e.Location,
343+
Duration: e.Duration,
344+
Status: e.Status,
345+
Capacity: e.Capacity,
346+
Price: e.Price,
347+
RegistrationLink: e.RegistrationLink,
348+
Tags: tags,
349+
Speakers: speakers,
350+
ReservationStartDate: e.ReservationStartDate,
351+
ReservationEndDate: e.ReservationEndDate,
352+
// AdditionalLink: e.AdditionalLink,
353+
SessionType: e.SessionType,
354+
}
355+
}

0 commit comments

Comments
 (0)