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

Commit

Permalink
Support syncing event cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
zdevaty committed Apr 9, 2024
1 parent b9961a3 commit 6b1d538
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
3 changes: 3 additions & 0 deletions apiserver/model_booking.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ type Booking struct {

// The name of the organizer.
OrganizerName string `json:"organizerName,omitempty"`

// Indicates that the event was cancelled.
Cancelled bool `json:"cancelled,omitempty"`
}

// AssertBookingRequired checks if the required fields are not zero-ed
Expand Down
3 changes: 3 additions & 0 deletions apiserver/model_bookings_listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type BookingsListen struct {
// The name of the organizer.
OrganizerName string `json:"organizerName,omitempty"`

// Indicates that the event was cancelled.
Cancelled bool `json:"cancelled,omitempty"`

// The status code expecting when actually perform the operation. Some values are - 200: updated (ok) - 201: created - 204: deleted (no content) - 304: unchanged (not modified) - 400: problem (bad request) - 404: not found - 409: duplicated (conflict) - 422: unprocessable
StatusCode int32 `json:"statusCode,omitempty"`
}
Expand Down
10 changes: 9 additions & 1 deletion apiservices/api_booking_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,19 @@ func NewBookingAPIService() apiserver.BookingAPIServicer {

// BookingsBookingIdDelete - Cancel a booking
func (s *BookingAPIService) BookingsBookingIdDelete(ctx context.Context, bookingId int32) (apiserver.ImplResponse, error) {
if err := conf.CancelEvent(ctx, int64(bookingId)); errors.Is(err, conf.ErrNotFound) {
booking, 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
}

for _, assetID := range booking.AssetIds {
for _, subscriber := range assetSubscriptions[assetID] {
subscriber.conn.WriteJSON(booking)
}
}

return apiserver.Response(http.StatusOK, nil), nil
}

Expand Down
16 changes: 10 additions & 6 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func dbEventToAPIBooking(ctx context.Context, e appdb.Event) (apiserver.Booking,
Start: e.StartTime,
End: e.EndTime,
OrganizerID: e.Organizer,
Cancelled: e.CancelledAt.Valid,
}, nil
}

Expand All @@ -220,24 +221,27 @@ func dbEventToAPIBooking(ctx context.Context, e appdb.Event) (apiserver.Booking,
// return events, nil
// }

func CancelEvent(ctx context.Context, eventID int64) error {
func CancelEvent(ctx context.Context, eventID int64) (apiserver.Booking, error) {
event, err := appdb.Events(
appdb.EventWhere.ID.EQ(eventID),
).OneG(ctx)
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFound
return apiserver.Booking{}, ErrNotFound
}
if err != nil {
return fmt.Errorf("fetching event to cancel: %v", err)
return apiserver.Booking{}, fmt.Errorf("fetching event to cancel: %v", err)
}

event.CancelledAt = null.TimeFrom(time.Now())

if _, err := event.UpdateG(ctx, boil.Whitelist(appdb.EventColumns.CancelledAt)); err != nil {
return fmt.Errorf("updating event to mark as cancelled: %v", err)
return apiserver.Booking{}, fmt.Errorf("updating event to mark as cancelled: %v", err)
}

return nil
apiBooking, err := dbEventToAPIBooking(ctx, *event)
if err != nil {
return apiserver.Booking{}, fmt.Errorf("converting db event to api booking: %v", err)
}
return apiBooking, nil
}

// func GetCurrentEvents(ctx context.Context) ([]apiserver.Booking, error) {
Expand Down
3 changes: 3 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ components:
organizerName:
type: string
description: The name of the organizer.
cancelled:
type: boolean
description: Indicates that the event was cancelled.
BookingRequest:
type: object
properties:
Expand Down

0 comments on commit 6b1d538

Please sign in to comment.