From 2dcfa0ebe87732b3a4ad9734bba307422917c774 Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Mon, 4 Jan 2021 16:33:58 +0100 Subject: [PATCH] Room controller + update device controller --- .../DTO/MyControlPanel/RoomDTO.cs | 42 ++++ .../Models/MyControlPanel/Database/Room.cs | 60 ++++++ .../Controllers/Devices/DeviceController.cs | 30 +-- .../Controllers/Devices/ProviderController.cs | 2 +- MyCore/Controllers/RoomController.cs | 180 ++++++++++++++++++ .../Database/RoomDatabaseService.cs | 58 ++++++ .../Database/UserDatabaseService.cs | 5 + MyCore/Services/RoomService.cs | 34 ++++ MyCore/Startup.cs | 1 + 9 files changed, 400 insertions(+), 12 deletions(-) create mode 100644 MyCore.Interfaces/DTO/MyControlPanel/RoomDTO.cs create mode 100644 MyCore.Interfaces/Models/MyControlPanel/Database/Room.cs create mode 100644 MyCore/Controllers/RoomController.cs create mode 100644 MyCore/Services/MyControlPanel/Database/RoomDatabaseService.cs create mode 100644 MyCore/Services/RoomService.cs diff --git a/MyCore.Interfaces/DTO/MyControlPanel/RoomDTO.cs b/MyCore.Interfaces/DTO/MyControlPanel/RoomDTO.cs new file mode 100644 index 0000000..cc5dac3 --- /dev/null +++ b/MyCore.Interfaces/DTO/MyControlPanel/RoomDTO.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyCore.Interfaces.DTO +{ + public class RoomSummaryDTO + { + public string Id { get; set; } + + public string UserId { get; set; } + + public string Name { get; set; } + } + + public class RoomDetailDTO + { + public string Id { get; set; } + + public string UserId { get; set; } + + public string Name { get; set; } + + public DateTime CreatedDate { get; set; } + + public DateTime UpdatedDate { get; set; } + + public List DeviceIds { get; set; } + } + + public class RoomCreateOrUpdateDetailDTO + { + public string Id { get; set; } + + public string UserId { get; set; } + + public string Name { get; set; } + + public List DeviceIds { get; set; } + } +} diff --git a/MyCore.Interfaces/Models/MyControlPanel/Database/Room.cs b/MyCore.Interfaces/Models/MyControlPanel/Database/Room.cs new file mode 100644 index 0000000..0a45908 --- /dev/null +++ b/MyCore.Interfaces/Models/MyControlPanel/Database/Room.cs @@ -0,0 +1,60 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using MyCore.Interfaces.DTO; +using MyCore.Interfaces.Models; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace MyCore.Interfaces.Models +{ + /// + /// Group of rooms + /// + public class Room + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + + [BsonElement("UserId")] + [BsonRequired] + public string UserId { get; set; } + + [BsonElement("Name")] + [BsonRequired] + public string Name { get; set; } + + [BsonElement("CreatedDate")] + public DateTime CreatedDate { get; set; } + + [BsonElement("UpdatedDate")] + public DateTime UpdatedDate { get; set; } + + [BsonElement("DevicesIds")] + public List DevicesIds { get; set; } + + public RoomSummaryDTO ToSummaryDTO() + { + return new RoomSummaryDTO() + { + Id = Id, + UserId = UserId, + Name = Name + }; + } + + public RoomDetailDTO ToDTO() + { + return new RoomDetailDTO() + { + Id = Id, + UserId = UserId, + Name = Name, + CreatedDate = CreatedDate, + UpdatedDate = UpdatedDate, + DeviceIds = DevicesIds + }; + } + } +} diff --git a/MyCore/Controllers/Devices/DeviceController.cs b/MyCore/Controllers/Devices/DeviceController.cs index 8d4cf02..db518e8 100644 --- a/MyCore/Controllers/Devices/DeviceController.cs +++ b/MyCore/Controllers/Devices/DeviceController.cs @@ -15,7 +15,7 @@ using MyCore.Services; using MyCore.Services.Devices; using MyCore.Services.MyControlPanel; -namespace MyCore.Controllers.Devices +namespace MyCore.Controllers { [Authorize] // TODO Add ROLES (Roles = "Admin") [Route("api/device")] @@ -70,11 +70,10 @@ namespace MyCore.Controllers.Devices /// /// Get a specific device info /// - /// Id of user /// id of device [ProducesResponseType(typeof(DeviceDetailDTO), 200)] [HttpGet("{deviceId}")] - public ObjectResult GetDetail(string userId, string deviceId) + public ObjectResult GetDetail(string deviceId) { try { @@ -271,18 +270,17 @@ namespace MyCore.Controllers.Devices /// /// Update a device /// - /// Device Id /// Device to update [ProducesResponseType(typeof(DeviceDetailDTO), 200)] [HttpPut("{deviceId}")] - public ObjectResult Update(string userId, [FromBody] DeviceDetailDTO deviceDetailDTO) + public ObjectResult Update([FromBody] DeviceDetailDTO deviceDetailDTO) { try { if (!_DeviceDatabaseService.IsExist(deviceDetailDTO.Id)) throw new KeyNotFoundException("Device does not exist"); - DeviceDetailDTO deviceUpdated = DeviceService.CreateOrUpdate(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, userId, deviceDetailDTO, false); + DeviceDetailDTO deviceUpdated = DeviceService.CreateOrUpdate(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, deviceDetailDTO.UserId, deviceDetailDTO, false); return new OkObjectResult(deviceUpdated); } @@ -305,8 +303,13 @@ namespace MyCore.Controllers.Devices { try { - // Check if exist - _DeviceDatabaseService.Remove(deviceId); + if (deviceId != null) + { + if (_DeviceDatabaseService.IsExist(deviceId)) + { + _DeviceDatabaseService.Remove(deviceId); + } + } return new OkObjectResult(201); } @@ -317,7 +320,7 @@ namespace MyCore.Controllers.Devices } /// - /// Delete a device + /// Delete all device for a specified /// /// Id of user [HttpDelete("user/{userId}")] @@ -325,8 +328,13 @@ namespace MyCore.Controllers.Devices { try { - // Check if exist - _DeviceDatabaseService.RemoveForUser(userId); + if (userId != null) + { + if (_UserDatabaseService.IsExist(userId)) + { + _DeviceDatabaseService.RemoveForUser(userId); + } + } return new OkObjectResult(201); } diff --git a/MyCore/Controllers/Devices/ProviderController.cs b/MyCore/Controllers/Devices/ProviderController.cs index 02a0eb8..20fbdf1 100644 --- a/MyCore/Controllers/Devices/ProviderController.cs +++ b/MyCore/Controllers/Devices/ProviderController.cs @@ -12,7 +12,7 @@ using MyCore.Services; using MyCore.Services.Devices; using MyCore.Services.MyControlPanel; -namespace MyCore.Controllers.Devices +namespace MyCore.Controllers { //[Authorize(Roles = "Admin")] [Authorize] diff --git a/MyCore/Controllers/RoomController.cs b/MyCore/Controllers/RoomController.cs new file mode 100644 index 0000000..ce3d2c5 --- /dev/null +++ b/MyCore/Controllers/RoomController.cs @@ -0,0 +1,180 @@ +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 readonly IMqttClientService _mqttClientService; + private readonly IMqttOnlineClientService _mqttOnlineClientService; + + public RoomController(UserDatabaseService userDatabaseService, RoomDatabaseService roomDatabaseService, MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider) + { + this._UserDatabaseService = userDatabaseService; + this._RoomDatabaseService = roomDatabaseService; + this._mqttClientService = provider.MqttClientService; + this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService; + } + + /// + /// Get all rooms for the specified user + /// + /// Id of user + [ProducesResponseType(typeof(List), 200)] + [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 + /// + /// room id + [ProducesResponseType(typeof(RoomDetailDTO), 200)] + [HttpGet("detail/{roomId}")] + public ObjectResult GetDetail(string roomId) + { + try + { + Room room = _RoomDatabaseService.GetById(roomId); + + return new OkObjectResult(room.ToDTO()); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Create a room + /// + /// Room to create + [ProducesResponseType(typeof(RoomDetailDTO), 200)] + [HttpPost] + public ObjectResult Create([FromBody] RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetail) + { + try + { + if (roomCreateOrUpdateDetail == null) + throw new KeyNotFoundException("Room is null"); + + RoomDetailDTO roomCreated = RoomService.CreateOrUpdate(this._RoomDatabaseService, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, true); + + return new OkObjectResult(roomCreated); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Update a room + /// + /// room to update + [ProducesResponseType(typeof(RoomCreateOrUpdateDetailDTO), 200)] + [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, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, false); + + return new OkObjectResult(roomUpdated); + } + catch (KeyNotFoundException ex) + { + return new BadRequestObjectResult(ex.Message) { StatusCode = 404 }; + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Delete a room + /// + /// Id of room to delete + [HttpDelete("{roomId}")] + public ObjectResult Delete(string roomId) + { + try + { + if (roomId != null) + { + if (_RoomDatabaseService.IsExist(roomId)) + { + _RoomDatabaseService.Remove(roomId); + } + } + + return new OkObjectResult(201); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Delete all room for a specified + /// + /// Id of user + [HttpDelete("user/{userId}")] + public ObjectResult DeleteAllForUser(string userId) + { + try + { + if (userId != null) + { + if (_UserDatabaseService.IsExist(userId)) + { + _RoomDatabaseService.RemoveForUser(userId); + } + } + + return new OkObjectResult(201); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + } +} diff --git a/MyCore/Services/MyControlPanel/Database/RoomDatabaseService.cs b/MyCore/Services/MyControlPanel/Database/RoomDatabaseService.cs new file mode 100644 index 0000000..df60abc --- /dev/null +++ b/MyCore/Services/MyControlPanel/Database/RoomDatabaseService.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using MyCore.Interfaces.Models; +using Microsoft.Extensions.Configuration; +using MongoDB.Driver; + +namespace MyCore.Services.MyControlPanel +{ + public class RoomDatabaseService + { + private readonly IMongoCollection _Rooms; + + public RoomDatabaseService(IConfiguration config) + { + var client = new MongoClient(config.GetConnectionString("MyCoreDb")); + var database = client.GetDatabase("MyCoreDb"); + _Rooms = database.GetCollection("Rooms"); + } + public List GetAll(string userId) + { + return _Rooms.Find(d => d.UserId == userId).ToList(); + } + + public Room GetById(string id) + { + return _Rooms.Find(d => d.Id == id).FirstOrDefault(); + } + + public bool IsExist(string id) + { + return _Rooms.Find(d => d.Id == id).FirstOrDefault() != null ? true : false; + } + + public Room Create(Room room) + { + _Rooms.InsertOne(room); + return room; + } + + public Room Update(string id, Room roomIn) + { + _Rooms.ReplaceOne(room => room.Id == id, roomIn); + return roomIn; + } + + public void Remove(string id) + { + _Rooms.DeleteOne(room => room.Id == id); + } + + public void RemoveForUser(string userId) + { + _Rooms.DeleteMany(room => room.UserId == userId); + } + } +} diff --git a/MyCore/Services/MyControlPanel/Database/UserDatabaseService.cs b/MyCore/Services/MyControlPanel/Database/UserDatabaseService.cs index 9bda89e..411dc68 100644 --- a/MyCore/Services/MyControlPanel/Database/UserDatabaseService.cs +++ b/MyCore/Services/MyControlPanel/Database/UserDatabaseService.cs @@ -33,6 +33,11 @@ namespace MyCore.Services return _Users.Find(m => m.Id == id).FirstOrDefault(); } + public bool IsExist(string id) + { + return _Users.Find(d => d.Id == id).FirstOrDefault() != null ? true : false; + } + public UserInfo Create(UserInfo user) { _Users.InsertOne(user); diff --git a/MyCore/Services/RoomService.cs b/MyCore/Services/RoomService.cs new file mode 100644 index 0000000..cda3c6f --- /dev/null +++ b/MyCore/Services/RoomService.cs @@ -0,0 +1,34 @@ +using MyCore.Interfaces.DTO; +using MyCore.Interfaces.Models; +using MyCore.Services.MyControlPanel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MyCore.Service +{ + public class RoomService + { + public static RoomDetailDTO CreateOrUpdate(RoomDatabaseService _RoomDatabaseService, string userId, RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetailDTO, bool create) + { + Room room; + if (create) + room = new Room(); + else + { + room = _RoomDatabaseService.GetById(roomCreateOrUpdateDetailDTO.Id); + } + + room.UserId = userId; + room.Name = roomCreateOrUpdateDetailDTO.Name; + room.CreatedDate = DateTime.Now; + room.UpdatedDate = DateTime.Now; + + if (create) + return _RoomDatabaseService.Create(room).ToDTO(); + else + return _RoomDatabaseService.Update(room.Id, room).ToDTO(); + } + } +} diff --git a/MyCore/Startup.cs b/MyCore/Startup.cs index 1a63328..2c9e1e3 100644 --- a/MyCore/Startup.cs +++ b/MyCore/Startup.cs @@ -202,6 +202,7 @@ namespace MyCore services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddMqttClientHostedService(); // Todo client files (a lot are useless) services.AddMqttClientOnlineHostedService();