diff --git a/apiserver/api.go b/apiserver/api.go index 6941b58..3bb23ec 100644 --- a/apiserver/api.go +++ b/apiserver/api.go @@ -36,6 +36,7 @@ type ConfigurationAPIRouter interface { // pass the data to a SynchronizationAPIServicer to perform the required actions, then write the service results to the http response. type SynchronizationAPIRouter interface { SubscribeBookings(http.ResponseWriter, *http.Request) + SyncBookingsBookingIdDelete(http.ResponseWriter, *http.Request) SyncBookingsPost(http.ResponseWriter, *http.Request) } @@ -72,6 +73,7 @@ type ConfigurationAPIServicer interface { // and updated with the logic required for the API. type SynchronizationAPIServicer interface { SubscribeBookings(context.Context, SubscribeBookingsRequest) (ImplResponse, error) + SyncBookingsBookingIdDelete(context.Context, int32) (ImplResponse, error) SyncBookingsPost(context.Context, BookingRequest) (ImplResponse, error) } diff --git a/apiserver/api_synchronization.go b/apiserver/api_synchronization.go index 87e3253..a96f49e 100644 --- a/apiserver/api_synchronization.go +++ b/apiserver/api_synchronization.go @@ -13,6 +13,8 @@ import ( "encoding/json" "net/http" "strings" + + "github.com/gorilla/mux" ) // SynchronizationAPIController binds http requests to an api service and writes the service results to the http response @@ -53,6 +55,11 @@ func (c *SynchronizationAPIController) Routes() Routes { "/v1/sync/bookings-subscription", c.SubscribeBookings, }, + "SyncBookingsBookingIdDelete": Route{ + strings.ToUpper("Delete"), + "/v1/sync/bookings/{bookingId}", + c.SyncBookingsBookingIdDelete, + }, "SyncBookingsPost": Route{ strings.ToUpper("Post"), "/v1/sync/bookings", @@ -88,6 +95,27 @@ func (c *SynchronizationAPIController) SubscribeBookings(w http.ResponseWriter, EncodeJSONResponse(result.Body, &result.Code, w) } +// SyncBookingsBookingIdDelete - Cancel a booking +func (c *SynchronizationAPIController) SyncBookingsBookingIdDelete(w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + bookingIdParam, err := parseNumericParameter[int32]( + params["bookingId"], + WithRequire[int32](parseInt32), + ) + if err != nil { + c.errorHandler(w, r, &ParsingError{Err: err}, nil) + return + } + result, err := c.service.SyncBookingsBookingIdDelete(r.Context(), bookingIdParam) + // If an error occurred, encode the error with the status code + if err != nil { + c.errorHandler(w, r, err, &result) + return + } + // If no error, encode the body and the result code + EncodeJSONResponse(result.Body, &result.Code, w) +} + // SyncBookingsPost - Post booking from external service func (c *SynchronizationAPIController) SyncBookingsPost(w http.ResponseWriter, r *http.Request) { bookingRequestParam := BookingRequest{} diff --git a/apiservices/api_synchronization_service.go b/apiservices/api_synchronization_service.go index 939ae67..00ce6a4 100644 --- a/apiservices/api_synchronization_service.go +++ b/apiservices/api_synchronization_service.go @@ -17,7 +17,9 @@ package apiservices import ( "booking-app/apiserver" + "booking-app/conf" "context" + "errors" "net/http" ) @@ -28,6 +30,18 @@ func NewSynchronizationAPIService() apiserver.SynchronizationAPIServicer { return &SynchronizationAPIService{} } +// BookingsBookingIdDelete - Cancel a booking +func (s *SynchronizationAPIService) SyncBookingsBookingIdDelete(ctx context.Context, bookingId int32) (apiserver.ImplResponse, error) { + _, err := conf.CancelEvent(ctx, int64(bookingId)) + if errors.Is(err, conf.ErrNotFound) { + return apiserver.ImplResponse{Code: http.StatusNotFound}, err + } else if err != nil { + return apiserver.ImplResponse{Code: http.StatusInternalServerError}, err + } + + return apiserver.Response(http.StatusOK, nil), nil +} + // SubscribeBookings - Open a WebSocket connection to get informed about newly created bookings. func (s *SynchronizationAPIService) SubscribeBookings(ctx context.Context, subscribeBookingsRequest apiserver.SubscribeBookingsRequest) (apiserver.ImplResponse, error) { // This method should be handled by websocket instead. diff --git a/openapi.yaml b/openapi.yaml index 0fdacd3..99dd81e 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -207,6 +207,24 @@ paths: description: Asset not found. "400": description: Bad request - Eliona user not found. + /sync/bookings/{bookingId}: + delete: + tags: + - Synchronization + summary: Cancel a booking + parameters: + - name: bookingId + in: path + description: The booking ID obtained in the list of bookings. + required: true + schema: + type: integer + responses: + "204": + description: Booking cancelled successfully. + "404": + description: Booking not found. + /sync/bookings-subscription: get: tags: