using Dapr; using FrameworkBLL.EventLogSubscriber; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.PubSub; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Threading.Tasks; namespace FrameworkSL.Controllers.Subscriber { [ApiController] [Route("api/[controller]")] public class EventLogSubscriberService : ControllerBase { private readonly IEventLogSubscriberBLL _eventLogSubscriberBLL; private readonly ILogger _logger; public EventLogSubscriberService(IEventLogSubscriberBLL eventLogSubscriberBLL, ILogger logger) { _eventLogSubscriberBLL = eventLogSubscriberBLL; _logger = logger; } [Topic("pubsub", "eventlog-topic")] [HttpPost("eventlog-topic")] public async Task SaveEventLog([FromBody] string jsonMessage) { _logger.LogDebug("EventLogSubscriber received message {Message}", jsonMessage); if (string.IsNullOrEmpty(jsonMessage)) return BadRequest("Received empty message."); try { var publishDTO = JsonConvert.DeserializeObject(jsonMessage); if (publishDTO == null) return BadRequest("Failed to deserialize PublishDTO."); LoginDTO loginDTO = publishDTO.Login != null ? JsonConvert.DeserializeObject(publishDTO.Login.ToString()!)! : new LoginDTO(); // fallback, or handle as needed // Call BLL to save event log var result = await _eventLogSubscriberBLL.SaveEventLogSubscriber(publishDTO, loginDTO); return Ok(new{ Message = result}); } catch (Exception ex) { return StatusCode(500, $"Error saving event log: {ex.Message}"); } } } }