diff --git a/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs b/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs index 584b2a5..ac2a2b2 100644 --- a/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs +++ b/MyCore.Interfaces/Models/MyControlPanel/Database/Automation.cs @@ -15,6 +15,10 @@ namespace MyCore.Interfaces.Models [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } + [BsonElement("UserId")] + [BsonRequired] + public string UserId { get; set; } + [BsonElement("Name")] [BsonRequired] public string Name { get; set; } diff --git a/MyCore/Controllers/AutomationController.cs b/MyCore/Controllers/AutomationController.cs new file mode 100644 index 0000000..4256d8c --- /dev/null +++ b/MyCore/Controllers/AutomationController.cs @@ -0,0 +1,181 @@ +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/automation")] + [ApiController] + public class AutomationController : ControllerBase + { + private UserDatabaseService _UserDatabaseService; + private AutomationDatabaseService _AutomationDatabaseService; + private readonly IMqttClientService _mqttClientService; + private readonly IMqttOnlineClientService _mqttOnlineClientService; + + public AutomationController(UserDatabaseService userDatabaseService, AutomationDatabaseService automationDatabaseService, MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider) + { + this._UserDatabaseService = userDatabaseService; + this._AutomationDatabaseService = automationDatabaseService; + this._mqttClientService = provider.MqttClientService; + this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService; + } + + /// + /// Get all automations for the specified user + /// + /// Id of user + [ProducesResponseType(typeof(List), 200)] + [HttpGet("{userId}")] + public ObjectResult GetAll(string userId) + { + try + { + List Automations = _AutomationDatabaseService.GetAll(userId); + + List automationDTO = Automations.Select(d => d.ToDTO()).ToList(); + + return new OkObjectResult(automationDTO); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Get detail info of a specified automation + /// + /// automation id + [ProducesResponseType(typeof(RoomDetailDTO), 200)] + [HttpGet("detail/{roomId}")] + public ObjectResult GetDetail(string automationId) + { + try + { + Automation automation = _AutomationDatabaseService.GetById(automationId); + + return new OkObjectResult(automation.ToDTO()); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /* + //// + /// Create an automation + /// + /// Automation to create + [ProducesResponseType(typeof(RoomDetailDTO), 200)] + [HttpPost] + public ObjectResult Create([FromBody] AutomationCreateOrUpdateDetailDTO automationCreateOrUpdateDetail) + { + 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 an automation + /// + /// Id of automation to delete + [HttpDelete("{automationId}")] + public ObjectResult Delete(string automationId) + { + try + { + if (automationId != null) + { + if (_AutomationDatabaseService.IsExist(automationId)) + { + _AutomationDatabaseService.Remove(automationId); + } + } + + return new OkObjectResult(201); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Delete all automation for a specified + /// + /// Id of user + [HttpDelete("user/{userId}")] + public ObjectResult DeleteAllForUser(string userId) + { + try + { + if (userId != null) + { + if (_UserDatabaseService.IsExist(userId)) + { + _AutomationDatabaseService.RemoveForUser(userId); + } + } + + return new OkObjectResult(201); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + } +} diff --git a/MyCore/Services/MyControlPanel/Database/AutomationDatabaseService.cs b/MyCore/Services/MyControlPanel/Database/AutomationDatabaseService.cs index 9e85c11..191458a 100644 --- a/MyCore/Services/MyControlPanel/Database/AutomationDatabaseService.cs +++ b/MyCore/Services/MyControlPanel/Database/AutomationDatabaseService.cs @@ -18,9 +18,9 @@ namespace MyCore.Services.MyControlPanel var database = client.GetDatabase("MyCoreDb"); _Automations = database.GetCollection("Automations"); } - public List GetAll() + public List GetAll(string userId) { - return _Automations.Find(d => true).ToList(); + return _Automations.Find(a => a.UserId == userId).ToList(); } public Automation GetById(string id) @@ -33,6 +33,11 @@ namespace MyCore.Services.MyControlPanel return _Automations.Find(a => a.Triggers.Any(t => t.ProviderId == id)).FirstOrDefault(); } + public bool IsExist(string id) + { + return _Automations.Find(d => d.Id == id).FirstOrDefault() != null ? true : false; + } + public Automation Create(Automation automation) { _Automations.InsertOne(automation); @@ -49,5 +54,10 @@ namespace MyCore.Services.MyControlPanel { _Automations.DeleteOne(automation => automation.Id == id); } + + public void RemoveForUser(string userId) + { + _Automations.DeleteMany(automation => automation.UserId == userId); + } } }