using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Security.Authentication; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using MongoDB.Bson; using Mqtt.Client.AspNetCore.Services; using MyCore.Interfaces.DTO; using MyCore.Interfaces.Models; using MyCore.Services; using MyCore.Services.Devices; using MyCore.Services.MyControlPanel; namespace MyCore.Service.Controllers { [Authorize] // TODO Add ROLES (Roles = "Admin") [Route("api/event")] [ApiController] public class EventController : ControllerBase { private HomeDatabaseService _HomeDatabaseService; private RoomDatabaseService _RoomDatabaseService; private EventDatabaseService _EventDatabaseService; private DeviceDatabaseService _DeviceDatabaseService; private readonly IMqttClientService _mqttClientService; //private readonly IMqttOnlineClientService _mqttOnlineClientService; public EventController(HomeDatabaseService homeDatabaseService, RoomDatabaseService roomDatabaseService, EventDatabaseService eventDatabaseService, DeviceDatabaseService deviceDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider) { this._HomeDatabaseService = homeDatabaseService; this._RoomDatabaseService = roomDatabaseService; this._EventDatabaseService = eventDatabaseService; this._DeviceDatabaseService = deviceDatabaseService; this._mqttClientService = provider.MqttClientService; //this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService; } /// /// Get events for the specified home /// /// Home Id /// Filter params [ProducesResponseType(typeof(ListResponse), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet("{homeId}")] public ObjectResult Get(string homeId, [FromQuery] EventHomeFilter eventHomeFilter) // TODO Add filter by date etc { try { IQueryable events = _EventDatabaseService.GetAll(homeId); // To check if best if (eventHomeFilter.EventType != null) { events = events.Where(e => e.Type == eventHomeFilter.EventType).OrderByDescending(e => e.Date); } if (eventHomeFilter.DeviceType != null) { events = events.Where(e => e.DeviceState.DeviceType == eventHomeFilter.DeviceType).OrderByDescending(e => e.Date); } if (eventHomeFilter.DeviceId != null) { events = events.Where(e => e.DeviceState.DeviceId == eventHomeFilter.DeviceId).OrderByDescending(e => e.Date); } if (eventHomeFilter.RoomId != null) { events = events.Where(e => e.RoomId == eventHomeFilter.RoomId).OrderByDescending(e => e.Date); } if (eventHomeFilter.DateStart != null && eventHomeFilter.DateEnd != null) { events = events.Where(e => e.Date >= eventHomeFilter.DateStart && e.Date < eventHomeFilter.DateEnd).OrderByDescending(e => e.Date); } var totalCount = events.Count(); events = events.Skip(eventHomeFilter.StartIndex).Take(eventHomeFilter.Count); List eventDTOs = events.ToList().Select(d => d.ToDetailDTO()).OrderByDescending(e => e.Date).ToList(); var lr = new ListResponse(eventDTOs, eventHomeFilter); lr.TotalCount = totalCount; lr.ActualCount = eventDTOs.Count(); return new OkObjectResult(lr); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Get detail info of a specified event /// /// event id [ProducesResponseType(typeof(EventDetailDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("detail/{eventId}")] public ObjectResult GetDetail(string eventId) { try { if (eventId == null) throw new ArgumentNullException("Incorrect parameters"); Event evt = _EventDatabaseService.GetById(eventId); if (evt == null) throw new KeyNotFoundException("Event does not exist"); return new OkObjectResult(evt.ToDetailDTO()); } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Create an event /// /// Room to create /*[ProducesResponseType(typeof(RoomDetailDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 500)] [HttpPost] public ObjectResult Create([FromBody] RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetail) { try { if (roomCreateOrUpdateDetail == null) throw new ArgumentNullException("Incorrect parameters"); RoomDetailDTO roomCreated = RoomService.CreateOrUpdate(this._RoomDatabaseService, this._DeviceDatabaseService, roomCreateOrUpdateDetail.HomeId, roomCreateOrUpdateDetail, true); return new OkObjectResult(roomCreated); } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } }*/ /*/// /// Update an event /// /// room to update /*[ProducesResponseType(typeof(RoomCreateOrUpdateDetailDTO), 200)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpPut] public ObjectResult Update([FromBody] RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetail) { try { if (!_RoomDatabaseService.IsExist(roomCreateOrUpdateDetail.Id)) throw new KeyNotFoundException("Room does not exist"); RoomDetailDTO roomUpdated = RoomService.CreateOrUpdate(this._RoomDatabaseService, this._DeviceDatabaseService, roomCreateOrUpdateDetail.HomeId, roomCreateOrUpdateDetail, false); return new OkObjectResult(roomUpdated); } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) {}; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } }*/ /// /// Delete an event /// /// Id of event to delete [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpDelete("{eventId}")] public ObjectResult Delete(string eventId) { try { if (eventId == null) throw new ArgumentNullException("Incorrect parameters"); if (!_EventDatabaseService.IsExist(eventId)) throw new KeyNotFoundException("Event does not exist"); _EventDatabaseService.Remove(eventId); return new OkObjectResult("Event has been successfully deleted") { StatusCode = 202 }; } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Delete all events for a specified home /// /// Home Id [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpDelete("home/{homeId}")] public ObjectResult DeleteAllForHome(string homeId) { try { if (homeId == null) throw new ArgumentNullException("Incorrect parameters"); if (!_HomeDatabaseService.IsExist(homeId)) throw new KeyNotFoundException("Home does not exist"); _EventDatabaseService.RemoveForHome(homeId); return new OkObjectResult("All events associated to specified home has been removed") { StatusCode = 202 }; } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } } }