Spaces:
Runtime error
Runtime error
| package controllers | |
| import ( | |
| "abdanhafidz.com/go-boilerplate/models/dto" | |
| "abdanhafidz.com/go-boilerplate/services" | |
| "github.com/gin-gonic/gin" | |
| ) | |
| type EventBookingController interface { | |
| BookEvent(ctx *gin.Context) | |
| GetMyEventBookings(ctx *gin.Context) | |
| GetEventBookingDetail(ctx *gin.Context) | |
| GetEventBookings(ctx *gin.Context) | |
| UpdateEventBooking(ctx *gin.Context) | |
| CancelEventBooking(ctx *gin.Context) | |
| } | |
| type eventBookingController struct { | |
| eventService services.EventService | |
| } | |
| func NewEventBookingController(eventService services.EventService) EventBookingController { | |
| return &eventBookingController{eventService: eventService} | |
| } | |
| // BookEvent godoc | |
| // @Summary Book an event | |
| // @Description Book a specific package for an approved event. Returns Xendit checkout URL if payment is required. | |
| // @Tags Event Booking | |
| // @Accept json | |
| // @Produce json | |
| // @Param request body dto.BookEventRequest true "Booking Data" | |
| // @Success 201 {object} dto.EventBookingResponse | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/bookings/events [post] | |
| func (c *eventBookingController) BookEvent(ctx *gin.Context) { | |
| userID := ParseUserId(ctx) | |
| if userID == "" { | |
| return | |
| } | |
| req, err := RequestJSON[dto.BookEventRequest](ctx) | |
| if err != nil { | |
| return | |
| } | |
| res, err := c.eventService.BookEvent(ctx.Request.Context(), userID, req) | |
| if err != nil { | |
| ResponseJSON(ctx, req, res, err) | |
| return | |
| } | |
| ctx.JSON(201, gin.H{"success": true, "message": "Event booked successfully", "data": res}) | |
| } | |
| // GetMyEventBookings godoc | |
| // @Summary Get my event bookings | |
| // @Description Returns a list of event bookings made by the authenticated user. | |
| // @Tags Event Booking | |
| // @Produce json | |
| // @Success 200 {array} dto.EventBookingResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/bookings/events/me [get] | |
| func (c *eventBookingController) GetMyEventBookings(ctx *gin.Context) { | |
| userID := ParseUserId(ctx) | |
| if userID == "" { | |
| return | |
| } | |
| res, err := c.eventService.GetEventBookingsByUser(ctx.Request.Context(), userID) | |
| ResponseJSON(ctx, "", res, err) | |
| } | |
| // GetEventBookingDetail godoc | |
| // @Summary Get event booking detail | |
| // @Description Returns full details of a specific event booking by booking ID. | |
| // @Tags Event Booking | |
| // @Produce json | |
| // @Param booking_id path string true "Booking ID" | |
| // @Success 200 {object} dto.EventBookingResponse | |
| // @Failure 404 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/bookings/events/{booking_id} [get] | |
| func (c *eventBookingController) GetEventBookingDetail(ctx *gin.Context) { | |
| bookingID := ctx.Param("booking_id") | |
| res, err := c.eventService.GetEventBookingDetail(ctx.Request.Context(), bookingID) | |
| ResponseJSON(ctx, "", res, err) | |
| } | |
| // GetEventBookings godoc | |
| // @Summary Get all bookings for an event | |
| // @Description Returns a list of all bookings for a specific event. Intended for event owners/admins. | |
| // @Tags Event Booking | |
| // @Produce json | |
| // @Param event_id path string true "Event ID" | |
| // @Success 200 {array} dto.EventBookingResponse | |
| // @Failure 401 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/bookings/events/event/{event_id} [get] | |
| func (c *eventBookingController) GetEventBookings(ctx *gin.Context) { | |
| eventID := ctx.Param("event_id") | |
| if eventID == "" { | |
| ctx.JSON(400, gin.H{"success": false, "message": "event_id is required"}) | |
| return | |
| } | |
| res, err := c.eventService.GetEventBookingsByEventID(ctx.Request.Context(), eventID) | |
| ResponseJSON(ctx, "", res, err) | |
| } | |
| // UpdateEventBooking godoc | |
| // @Summary Update event booking status | |
| // @Description Update booking or payment status of an event booking. Used by admin. | |
| // @Tags Event Booking | |
| // @Accept json | |
| // @Produce json | |
| // @Param booking_id path string true "Booking ID" | |
| // @Param request body dto.UpdateEventBookingRequest true "Fields to update" | |
| // @Success 200 {object} dto.EventBookingResponse | |
| // @Failure 400 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/bookings/events/{booking_id} [put] | |
| func (c *eventBookingController) UpdateEventBooking(ctx *gin.Context) { | |
| bookingID := ctx.Param("booking_id") | |
| req, err := RequestJSON[dto.UpdateEventBookingRequest](ctx) | |
| if err != nil { | |
| return | |
| } | |
| res, err := c.eventService.UpdateEventBooking(ctx.Request.Context(), bookingID, req) | |
| if err != nil { | |
| ResponseJSON(ctx, req, res, err) | |
| return | |
| } | |
| ctx.JSON(200, gin.H{"success": true, "message": "Booking updated successfully", "data": res}) | |
| } | |
| // CancelEventBooking godoc | |
| // @Summary Cancel event booking | |
| // @Description Cancel a booking made by the authenticated user. | |
| // @Tags Event Booking | |
| // @Produce json | |
| // @Param booking_id path string true "Booking ID" | |
| // @Success 200 {object} map[string]interface{} | |
| // @Failure 403 {object} dto.ErrorResponse | |
| // @Failure 404 {object} dto.ErrorResponse | |
| // @Security BearerAuth | |
| // @Router /api/v1/bookings/events/{booking_id} [delete] | |
| func (c *eventBookingController) CancelEventBooking(ctx *gin.Context) { | |
| userID := ParseUserId(ctx) | |
| if userID == "" { | |
| return | |
| } | |
| bookingID := ctx.Param("booking_id") | |
| err := c.eventService.CancelEventBooking(ctx.Request.Context(), bookingID, userID) | |
| if err != nil { | |
| ctx.JSON(400, gin.H{"success": false, "message": err.Error()}) | |
| return | |
| } | |
| ctx.JSON(200, gin.H{"success": true, "message": "Booking cancelled successfully"}) | |
| } | |