-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExternalServices.cs
More file actions
57 lines (49 loc) · 1.98 KB
/
ExternalServices.cs
File metadata and controls
57 lines (49 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
namespace Saga;
/// <summary>
/// Simulates external booking APIs (flight, hotel, car rental).
/// In a real application, these would call external REST/gRPC services.
/// Each booking can fail — the saga pattern ensures compensating actions undo
/// any previously completed steps on failure.
/// </summary>
public static class BookingApi
{
public static Task<string> BookFlight(FlightRequest request)
{
Console.WriteLine(
$" [API] Booking flight {request.From} -> {request.To} on {request.Date}"
);
return Task.FromResult($"FL-{Guid.NewGuid().ToString()[..8].ToUpperInvariant()}");
}
public static Task CancelFlight(string confirmationId)
{
Console.WriteLine($" [API] Cancelling flight {confirmationId}");
return Task.CompletedTask;
}
public static Task<string> BookHotel(HotelRequest request)
{
Console.WriteLine(
$" [API] Booking hotel in {request.City} from {request.CheckIn} to {request.CheckOut}"
);
return Task.FromResult($"HT-{Guid.NewGuid().ToString()[..8].ToUpperInvariant()}");
}
public static Task CancelHotel(string confirmationId)
{
Console.WriteLine($" [API] Cancelling hotel {confirmationId}");
return Task.CompletedTask;
}
public static Task<string> BookCarRental(CarRentalRequest request)
{
Console.WriteLine(
$" [API] Booking car in {request.City} from {request.PickUp} to {request.DropOff}"
);
// Simulate occasional failures to demonstrate compensation
if (Random.Shared.Next(100) < 20)
throw new InvalidOperationException("Car rental service temporarily unavailable");
return Task.FromResult($"CR-{Guid.NewGuid().ToString()[..8].ToUpperInvariant()}");
}
public static Task CancelCarRental(string confirmationId)
{
Console.WriteLine($" [API] Cancelling car rental {confirmationId}");
return Task.CompletedTask;
}
}