estatedocflow / estatedocflow.api

0 stars 0 forks source link

Booking Service API #12

Open estatedocflow opened 1 month ago

estatedocflow commented 1 month ago

Description

This API will allow customers to view available services, select desired services, and submit booking requests. Upon successful submission, the API will trigger email confirmations to the customer and the assigned runner.

Requirements

{
  "customerId": "123",
  "runnerId": "456",
  "serviceType": "Property Inspection",
  "selectedDateTime": "2024-05-15T10:00:00"
}

**Dto**

public class BookingRequestDto { public string CustomerId { get; set; } public string RunnerId { get; set; } public string ServiceType { get; set; } public DateTime SelectedDateTime { get; set; } }


**Sending message with RabbitMQ**

in Post booking endpoint, publish booking requests to RabbitMQ:

[HttpPost("/api/bookings")] public IActionResult BookService([FromBody] BookingRequestDto bookingRequest) { // Publish booking request to RabbitMQ RabbitMqService.PublishBookingRequest(bookingRequest);

return Ok(new { message = "Booking request submitted successfully." });

}


Integrate RabbitMQ into the backend service using a dedicated service class (RabbitMqService).

public class RabbitMqService { private readonly IModel _channel;

public RabbitMqService()
{
    var factory = new ConnectionFactory() { HostName = "localhost" };
    var connection = factory.CreateConnection();
    _channel = connection.CreateModel();
    _channel.QueueDeclare(queue: "bookingRequests",
                           durable: false,
                           exclusive: false,
                           autoDelete: false,
                           arguments: null);
}

public void PublishBookingRequest(BookingRequestDto bookingRequest)
{
    var message = JsonConvert.SerializeObject(bookingRequest);
    var body = Encoding.UTF8.GetBytes(message);

    _channel.BasicPublish(exchange: "",
                          routingKey: "bookingRequests",
                          basicProperties: null,
                          body: body);
}

}


Implement a background service or worker to consume booking requests from RabbitMQ and process them (e.g., send email notifications).

public class BookingRequestConsumer : BackgroundService { private readonly RabbitMqService _rabbitMqService;

public BookingRequestConsumer(RabbitMqService rabbitMqService)
{
    _rabbitMqService = rabbitMqService;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    while (!stoppingToken.IsCancellationRequested)
    {
        var consumer = new EventingBasicConsumer(_rabbitMqService.Channel);
        consumer.Received += (model, ea) =>
        {
            var body = ea.Body.ToArray();
            var message = Encoding.UTF8.GetString(body);

            var bookingRequest = JsonConvert.DeserializeObject<BookingRequestDto>(message);

            // Process the booking request (e.g., send email notifications)
            // Example: EmailService.SendBookingConfirmationEmail(bookingRequest.CustomerId);

            _rabbitMqService.Channel.BasicAck(ea.DeliveryTag, false);
        };

        _rabbitMqService.Channel.BasicConsume(queue: "bookingRequests",
                                              autoAck: false,
                                              consumer: consumer);

        await Task.Delay(1000, stoppingToken); // Delay before processing next message
    }
}

}