66using Swashbuckle . AspNetCore . Annotations ;
77using TNO . API . Areas . Services . Models . EventSchedule ;
88using TNO . API . Models ;
9+ using TNO . API . Models . SignalR ;
910using TNO . Core . Exceptions ;
1011using TNO . DAL . Services ;
12+ using TNO . Kafka ;
13+ using TNO . Kafka . SignalR ;
1114using TNO . Keycloak ;
1215
1316namespace TNO . API . Areas . Services . Controllers ;
@@ -28,19 +31,31 @@ namespace TNO.API.Areas.Services.Controllers;
2831public class EventScheduleController : ControllerBase
2932{
3033 #region Variables
31- private readonly IEventScheduleService _serviceEventSchedule ;
34+ private readonly IKafkaMessenger _kafkaMessenger ;
35+ private readonly KafkaHubConfig _kafkaHubOptions ;
36+ private readonly IUserService _userService ;
37+ private readonly IReportService _reportService ;
38+ private readonly IEventScheduleService _eventScheduleService ;
3239 private readonly JsonSerializerOptions _serializerOptions ;
3340 #endregion
3441
3542 #region Constructors
3643 /// <summary>
3744 /// Creates a new instance of a EventScheduleController object, initializes with specified parameters.
3845 /// </summary>
39- /// <param name="serviceEventSchedule"></param>
46+ /// <param name="kafkaMessenger"></param>
47+ /// <param name="kafkaConfig"></param>
48+ /// <param name="userService"></param>
49+ /// <param name="reportService"></param>
50+ /// <param name="eventScheduleService"></param>
4051 /// <param name="serializerOptions"></param>
41- public EventScheduleController ( IEventScheduleService serviceEventSchedule , IOptions < JsonSerializerOptions > serializerOptions )
52+ public EventScheduleController ( IKafkaMessenger kafkaMessenger , IOptions < KafkaHubConfig > kafkaConfig , IUserService userService , IReportService reportService , IEventScheduleService eventScheduleService , IOptions < JsonSerializerOptions > serializerOptions )
4253 {
43- _serviceEventSchedule = serviceEventSchedule ;
54+ _kafkaMessenger = kafkaMessenger ;
55+ _kafkaHubOptions = kafkaConfig . Value ;
56+ _userService = userService ;
57+ _reportService = reportService ;
58+ _eventScheduleService = eventScheduleService ;
4459 _serializerOptions = serializerOptions . Value ;
4560 }
4661 #endregion
@@ -56,7 +71,7 @@ public EventScheduleController(IEventScheduleService serviceEventSchedule, IOpti
5671 [ SwaggerOperation ( Tags = new [ ] { "EventSchedule" } ) ]
5772 public IActionResult GetEventSchedules ( )
5873 {
59- var result = _serviceEventSchedule . FindAll ( ) ;
74+ var result = _eventScheduleService . FindAll ( ) ;
6075 return new JsonResult ( result . Select ( ds => new EventScheduleModel ( ds , _serializerOptions ) ) ) ;
6176 }
6277
@@ -72,7 +87,7 @@ public IActionResult GetEventSchedules()
7287 [ SwaggerOperation ( Tags = new [ ] { "EventSchedule" } ) ]
7388 public IActionResult FindById ( int id )
7489 {
75- var result = _serviceEventSchedule . FindById ( id ) ;
90+ var result = _eventScheduleService . FindById ( id ) ;
7691 if ( result == null ) return NoContent ( ) ;
7792 return new JsonResult ( new EventScheduleModel ( result , _serializerOptions ) ) ;
7893 }
@@ -88,11 +103,32 @@ public IActionResult FindById(int id)
88103 [ ProducesResponseType ( typeof ( ErrorResponseModel ) , ( int ) HttpStatusCode . BadRequest ) ]
89104 [ ProducesResponseType ( ( int ) HttpStatusCode . NoContent ) ]
90105 [ SwaggerOperation ( Tags = new [ ] { "EventSchedule" } ) ]
91- public IActionResult Update ( [ FromBody ] EventScheduleModel model )
106+ public async Task < IActionResult > UpdateAsync ( [ FromBody ] EventScheduleModel model )
92107 {
93- _serviceEventSchedule . UpdateAndSave ( model . ToEntity ( _serializerOptions ) ) ;
108+ _eventScheduleService . UpdateAndSave ( model . ToEntity ( _serializerOptions ) ) ;
94109
95- var result = _serviceEventSchedule . FindById ( model . Id ) ?? throw new NoContentException ( ) ;
110+ var result = _eventScheduleService . FindById ( model . Id ) ?? throw new NoContentException ( ) ;
111+ if ( result . ReportId . HasValue )
112+ {
113+ var report = _reportService . FindById ( result . ReportId . Value ) ;
114+ if ( report ? . OwnerId . HasValue == true )
115+ {
116+ var instance = _reportService . GetCurrentReportInstance ( report . Id , report . OwnerId ) ;
117+ var user = _userService . FindById ( report . OwnerId . Value ) ?? throw new NotAuthorizedException ( ) ;
118+ await _kafkaMessenger . SendMessageAsync (
119+ _kafkaHubOptions . HubTopic ,
120+ new KafkaHubMessage ( HubEvent . SendUser , user . Username , new KafkaInvocationMessage ( MessageTarget . ReportStatus , new [ ] { new ReportMessageModel ( )
121+ {
122+ Id = instance ? . Id ?? 0 ,
123+ ReportId = report . Id ,
124+ Status = instance ? . Status ?? Entities . ReportStatus . Pending ,
125+ Subject = instance ? . Subject ?? report . Name ,
126+ OwnerId = user . Id ,
127+ Message = "event" ,
128+ } } ) )
129+ ) ;
130+ }
131+ }
96132 return new JsonResult ( new EventScheduleModel ( result , _serializerOptions ) ) ;
97133 }
98134 #endregion
0 commit comments