|
| 1 | +// This work is licensed under the terms of the MIT license. |
| 2 | +// For a copy, see <https://opensource.org/licenses/MIT>. |
| 3 | + |
| 4 | +namespace GuestLineSDK.Api; |
| 5 | + |
| 6 | +using System.Text.Json.Serialization; |
| 7 | + |
| 8 | +using FluentValidation; |
| 9 | + |
| 10 | +using GuestLineSDK.Ari; |
| 11 | +using GuestLineSDK.Primitives; |
| 12 | + |
| 13 | +partial interface IGuestLineApiClient |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Reservation Operations |
| 17 | + /// </summary> |
| 18 | + IReservationOperations Reservation { get; } |
| 19 | +} |
| 20 | + |
| 21 | +partial class GuestLineApiClient |
| 22 | +{ |
| 23 | + Lazy<IReservationOperations>? _reservation; |
| 24 | + public IReservationOperations Reservation => (_reservation ??= Defer<IReservationOperations>( |
| 25 | + c => new ReservationOperations(c))).Value; |
| 26 | +} |
| 27 | + |
| 28 | +public partial interface IReservationOperations |
| 29 | +{ |
| 30 | + Task<GuestLineResponse<ReservationRef>> ProcessReservationAsync( |
| 31 | + GuestLineReservationRequest request, |
| 32 | + CancellationToken cancellationToken = default); |
| 33 | +} |
| 34 | + |
| 35 | +public class ReservationOperations(ApiClient client) : IReservationOperations |
| 36 | +{ |
| 37 | + readonly ApiClient _client = client; |
| 38 | + |
| 39 | + public async Task<GuestLineResponse<ReservationRef>> ProcessReservationAsync(GuestLineReservationRequest request, CancellationToken cancellationToken = default) |
| 40 | + { |
| 41 | + |
| 42 | + Ensure.IsNotNull(request, nameof(request)); |
| 43 | + |
| 44 | + request.ApiKey ??= _client.Settings.ApiKey; |
| 45 | + request.Version ??= _client.Settings.Version; |
| 46 | + |
| 47 | + request.Validate(); |
| 48 | + |
| 49 | + var req = new GuestLineRequest<GuestLineReservationRequest>( |
| 50 | + GuestLineService.Reservation, |
| 51 | + HttpMethod.Post, |
| 52 | + PathString.Empty, |
| 53 | + request); |
| 54 | + |
| 55 | + return await _client.FetchAsync<GuestLineReservationRequest, ReservationRef>(req, cancellationToken) |
| 56 | + .ConfigureAwait(false); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +public record GuestLineReservationRequest( |
| 61 | + [property: JsonPropertyName("propertyid")] string PropertyId, |
| 62 | + [property: JsonIgnore] ReservationRequest[] ReservationItems) |
| 63 | + : GuestLineRequestBase<GuestLineReservationRequest>("reservation_info") |
| 64 | +{ |
| 65 | + [JsonPropertyName("reservations")] |
| 66 | + public GuestLineReservationSet Reservations => new(ReservationItems); |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Validates the current instance. |
| 70 | + /// </summary> |
| 71 | + public override IValidator<GuestLineReservationRequest> GetValidator() |
| 72 | + => GuestLineReservationRequestValidator.Instance; |
| 73 | +} |
| 74 | + |
| 75 | +public record GuestLineReservationSet( |
| 76 | + [property: JsonPropertyName("reservation")] ReservationRequest[] Reservations); |
| 77 | + |
| 78 | +public class GuestLineReservationRequestValidator : GuestLineRequestBaseValidator<GuestLineReservationRequest> |
| 79 | +{ |
| 80 | + public static readonly GuestLineReservationRequestValidator Instance = new(); |
| 81 | + |
| 82 | + public GuestLineReservationRequestValidator() |
| 83 | + { |
| 84 | + RuleFor(s => s.PropertyId) |
| 85 | + .NotEmpty() |
| 86 | + .WithMessage("The Property ID parameter must not be empty."); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +public class ReservationRef : Result<ReservationRef> |
| 91 | +{ |
| 92 | + [JsonPropertyName("bookingId")] |
| 93 | + public string? BookingId { get; set; } |
| 94 | +} |
| 95 | + |
| 96 | +public record ReservationRequest( |
| 97 | + [property: JsonPropertyName("reservation_datetime"), JsonConverter(typeof(DateTimeJsonConverter))] DateTime ReservationDateTime, |
| 98 | + [property: JsonPropertyName("reservation_id")] string ReservationId, |
| 99 | + [property: JsonPropertyName("payment_required")] decimal PaymentRequired, |
| 100 | + [property: JsonPropertyName("payment_type")] string PaymentType, |
| 101 | + [property: JsonPropertyName("totalamountaftertax")] decimal TotalAmountAfterTax, |
| 102 | + [property: JsonPropertyName("totaltax")] decimal TotalTax, |
| 103 | + [property: JsonPropertyName("currencycode")] string CurrencyCode, |
| 104 | + [property: JsonPropertyName("status")] string Status, |
| 105 | + [property: JsonPropertyName("customer")] ReservationCustomer Customer, |
| 106 | + [property: JsonPropertyName("room")] ReservationRoom[] Rooms, |
| 107 | + [property: JsonPropertyName("POS")] string Source); |
| 108 | + |
| 109 | +public class ReservationCustomer |
| 110 | +{ |
| 111 | + [JsonPropertyName("address")] |
| 112 | + public string? Address { get; set; } |
| 113 | + |
| 114 | + [JsonPropertyName("city")] |
| 115 | + public string? City { get; set; } |
| 116 | + |
| 117 | + [JsonPropertyName("country")] |
| 118 | + public string? Country { get; set; } |
| 119 | + |
| 120 | + [JsonPropertyName("email")] |
| 121 | + public string? Email { get; set; } |
| 122 | + |
| 123 | + [JsonPropertyName("salutation")] |
| 124 | + public string? Salutation { get; set; } |
| 125 | + |
| 126 | + [JsonPropertyName("first_name")] |
| 127 | + public string? FirstName { get; set; } |
| 128 | + |
| 129 | + [JsonPropertyName("last_name")] |
| 130 | + public string? LastName { get; set; } |
| 131 | + |
| 132 | + [JsonPropertyName("remarks")] |
| 133 | + public string? Remarks { get; set; } |
| 134 | + |
| 135 | + [JsonPropertyName("telephone")] |
| 136 | + public string? Telephone { get; set; } |
| 137 | + |
| 138 | + [JsonPropertyName("zip")] |
| 139 | + public string? Zip { get; set; } |
| 140 | + |
| 141 | +} |
| 142 | + |
| 143 | +public record ReservationRoom( |
| 144 | + [property: JsonPropertyName("arrival_date"), JsonConverter(typeof(DateOnlyJsonConverter))] DateTime ArrivalDate, |
| 145 | + [property: JsonPropertyName("departure_date"), JsonConverter(typeof(DateOnlyJsonConverter))] DateTime DepatureDate, |
| 146 | + [property: JsonPropertyName("room_id")] string RoomId, |
| 147 | + [property: JsonPropertyName("price")] ReservationRate[] Price, |
| 148 | + [property: JsonPropertyName("first_name")] string FirstName, |
| 149 | + [property: JsonPropertyName("last_name")] string LastName, |
| 150 | + [property: JsonPropertyName("remarks")] string? Remarks, |
| 151 | + [property: JsonPropertyName("amountaftertax")] decimal AmountAfterTax, |
| 152 | + [property: JsonIgnore] int Adults, |
| 153 | + [property: JsonIgnore] int Children) |
| 154 | +{ |
| 155 | + [JsonPropertyName("GuestCount")] |
| 156 | + public ReservationGuestGrouping[]? Guests => GetGroupings().ToArray(); |
| 157 | + |
| 158 | + IEnumerable<ReservationGuestGrouping> GetGroupings() |
| 159 | + { |
| 160 | + if (Adults > 0) |
| 161 | + { |
| 162 | + yield return new ReservationGuestGrouping(10, Adults); |
| 163 | + } |
| 164 | + if (Children > 0) |
| 165 | + { |
| 166 | + yield return new ReservationGuestGrouping(8, Children); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +public record ReservationRate( |
| 172 | + [property: JsonPropertyName("date"), JsonConverter(typeof(DateOnlyJsonConverter))] DateTime Date, |
| 173 | + [property: JsonPropertyName("rate_id")] string RateId, |
| 174 | + [property: JsonPropertyName("amountaftertax")] decimal AmountAfterTax); |
| 175 | + |
| 176 | +public record ReservationGuestGrouping( |
| 177 | + [property: JsonPropertyName("AgeQualifyingCode")] int AgeQualifyingCode, |
| 178 | + [property: JsonPropertyName("Count")] int Count); |
0 commit comments