Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
Support cancelling events from external service
Browse files Browse the repository at this point in the history
  • Loading branch information
zdevaty committed Apr 16, 2024
1 parent 6b1d538 commit b2af15e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apiserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
28 changes: 28 additions & 0 deletions apiserver/api_synchronization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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{}
Expand Down
14 changes: 14 additions & 0 deletions apiservices/api_synchronization_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package apiservices

import (
"booking-app/apiserver"
"booking-app/conf"
"context"
"errors"
"net/http"
)

Expand All @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b2af15e

Please sign in to comment.