using System.Collections.Generic;
using System.Threading.Tasks;
using Dapr;
using Dapr.Client;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace FrameworkSL.Controllers.Events
{
[ApiController]
[Route("[controller]")]
public class EventsService : ControllerBase
{
private readonly IEventRegistryService _registry;
private readonly NotificationService _notifier;
private readonly EventPublisher _publisher;
public EventsService(IEventRegistryService registry,NotificationService notifier,EventPublisher publisher)
{
_registry = registry;
_notifier = notifier;
_publisher = publisher;
}
///
/// Publishes an event to the pubsub component.
///
[HttpPost("events")]
public async Task PublishEvent([FromBody] EventMessage message)
{
await _publisher.PublishAsync(message);
return Ok(new { status = "Event published" });
}
///
/// Handles user registration events.
///
[Topic("pubsub", "user.registered")]
[HttpPost("user-registered")]
public async Task HandleUserRegistered([FromBody] EventMessage message)
{
if (_registry.IsSubscriptionEnabled(message.EventType))
{
await _notifier.NotifyAsync($"User registered: {message.Payload}");
}
return Ok();
}
///
/// Handles order creation events.
///
[Topic("pubsub", "order.created")]
[HttpPost("order-created")]
public async Task HandleOrderCreated([FromBody] EventMessage message)
{
if (_registry.IsSubscriptionEnabled(message.EventType))
{
await _notifier.NotifyAsync($"Order created: {message.Payload}");
}
return Ok();
}
}
public class EventPublisher
{
private readonly DaprClient _daprClient;
private readonly IEventRegistryService _registry;
public EventPublisher(DaprClient daprClient, IEventRegistryService registry)
{
_daprClient = daprClient;
_registry = registry;
}
public async Task PublishAsync(EventMessage message)
{
if (_registry.IsPublishingEnabled(message.EventType))
{
await _daprClient.PublishEventAsync("pubsub", message.EventType, message);
}
}
}
public class EventMessage
{
public string EventType { get; set; }
public string Payload { get; set; }
}
public interface IEventRegistryService
{
bool IsPublishingEnabled(string eventType);
bool IsSubscriptionEnabled(string eventType);
void SetEventSettings(string eventType, bool publishEnabled, bool subscribeEnabled);
}
public class InMemoryEventRegistryService : IEventRegistryService
{
private readonly Dictionary _eventSettings = new();
public InMemoryEventRegistryService()
{
_eventSettings["user.registered"] = (true, true);
_eventSettings["order.created"] = (true, true);
}
public bool IsPublishingEnabled(string eventType) =>
_eventSettings.TryGetValue(eventType, out var s) && s.PublishEnabled;
public bool IsSubscriptionEnabled(string eventType) =>
_eventSettings.TryGetValue(eventType, out var s) && s.SubscribeEnabled;
public void SetEventSettings(string eventType, bool publishEnabled, bool subscribeEnabled) =>
_eventSettings[eventType] = (publishEnabled, subscribeEnabled);
}
public class NotificationService
{
private readonly ILogger _logger;
public NotificationService(ILogger logger)
{
_logger = logger;
}
public Task NotifyAsync(string message)
{
_logger.LogDebug("Notification: {Message}", message);
return Task.CompletedTask;
}
}
}