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/room")] [ApiController] public class RoomController : ControllerBase { private UserDatabaseService _UserDatabaseService; private RoomDatabaseService _RoomDatabaseService; private DeviceDatabaseService _DeviceDatabaseService; private readonly IMqttClientService _mqttClientService; //private readonly IMqttOnlineClientService _mqttOnlineClientService; public RoomController(UserDatabaseService userDatabaseService, RoomDatabaseService roomDatabaseService, DeviceDatabaseService deviceDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider) { this._UserDatabaseService = userDatabaseService; this._RoomDatabaseService = roomDatabaseService; this._DeviceDatabaseService = deviceDatabaseService; this._mqttClientService = provider.MqttClientService; //this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService; } /// /// Get all rooms for the specified user /// /// Id of user [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet("{userId}")] public ObjectResult GetAll(string userId) { try { List Rooms = _RoomDatabaseService.GetAll(userId); List roomsSummaryDTO = Rooms.Select(d => d.ToSummaryDTO()).ToList(); return new OkObjectResult(roomsSummaryDTO); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Get detail info of a specified room /// /// user id /// room id [ProducesResponseType(typeof(RoomDetailDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("detail/{roomId}")] public ObjectResult GetDetail(string userId, string roomId) { try { if (userId == null || roomId == null) throw new ArgumentNullException("Incorrect parameters"); Room room = _RoomDatabaseService.GetById(roomId); if (room == null) throw new KeyNotFoundException("Room does not exist"); List devices = _DeviceDatabaseService.GetByLocation(room.UserId, roomId); return new OkObjectResult(room.ToDTO(devices.Select(d => d.ToDTO()).ToList())); } 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 a room /// /// 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.UserId, 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 a room /// /// 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.UserId, 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 device from a room /// /// Id of device to delete from the room /// Id of room [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpDelete("{roomId}/device/{deviceId}")] public ObjectResult Delete(string deviceId, string roomId) { try { if (roomId == null && deviceId == null) throw new ArgumentNullException("Incorrect parameters"); if (!_RoomDatabaseService.IsExist(roomId)) throw new KeyNotFoundException("Room does not exist"); // Update room Room room = _RoomDatabaseService.GetById(roomId); room.DevicesIds = room.DevicesIds.Where(d => d != deviceId).ToList(); room.UpdatedDate = DateTime.Now; _RoomDatabaseService.Update(room); // Update device Device device = _DeviceDatabaseService.GetById(deviceId); if (device == null) throw new KeyNotFoundException("Device does not exist"); device.LocationId = null; device.UpdatedDate = DateTime.Now; _DeviceDatabaseService.Update(device); return new OkObjectResult("Device has been removed from specified room") { 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 a room /// /// Id of room to delete [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpDelete("{roomId}")] public ObjectResult Delete(string roomId) { try { if (roomId == null) throw new ArgumentNullException("Incorrect parameters"); if (!_RoomDatabaseService.IsExist(roomId)) throw new KeyNotFoundException("Room does not exist"); Room room = _RoomDatabaseService.GetById(roomId); // Delete location from all devices List devices = _DeviceDatabaseService.GetByLocation(room.UserId, roomId); foreach (var device in devices) { device.LocationId = null; device.UpdatedDate = DateTime.Now; _DeviceDatabaseService.Update(device); } // Delete room _RoomDatabaseService.Remove(roomId); return new OkObjectResult("Room 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 room for a specified user /// /// Id of user [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpDelete("user/{userId}")] public ObjectResult DeleteAllForUser(string userId) { try { if (userId == null) throw new ArgumentNullException("Incorrect parameters"); if (!_UserDatabaseService.IsExist(userId)) throw new KeyNotFoundException("User does not exist"); _RoomDatabaseService.RemoveForUser(userId); return new OkObjectResult("All room associated to specified user 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 }; } } } }