MC automation controller update and create + clean dto

This commit is contained in:
Thomas Fransolet 2021-01-19 23:59:24 +01:00
parent db89f0a753
commit e34bc3763e
6 changed files with 105 additions and 65 deletions

View File

@ -1,4 +1,5 @@
using System; using MyCore.Interfaces.Models;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -9,5 +10,24 @@ namespace MyCore.Interfaces.DTO
{ {
public string Id { get; set; } public string Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string UserId { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime UpdatedDate { get; set; }
}
public class AutomationDetailDTO : AutomationDTO
{
public List<Trigger> Triggers { get; set; }
public List<Condition> Conditions { get; set; }
public List<MyCore.Interfaces.Models.Action> Actions { get; set; }
public List<string> DeviceIds { get; set; }
}
public class AutomationCreateOrUpdateDetailDTO : AutomationDTO
{
public List<Trigger> Triggers { get; set; }
public List<Condition> Conditions { get; set; }
public List<MyCore.Interfaces.Models.Action> Actions { get; set; }
public List<string> DeviceIds { get; set; }
} }
} }

View File

@ -38,33 +38,14 @@ namespace MyCore.Interfaces.DTO
public int BatteryStatus { get; set; } public int BatteryStatus { get; set; }
} }
public class DeviceDetailDTO public class DeviceDetailDTO : DeviceSummaryDTO
{ {
public string Id { get; set; }
public string UserId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Model { get; set; }
public DeviceType Type { get; set; }
public string FirmwareVersion { get; set; } public string FirmwareVersion { get; set; }
public string HardwareVersion { get; set; } public string HardwareVersion { get; set; }
public int Port { get; set; } public int Port { get; set; }
public ConnectionStatus ConnectionStatus { get; set; }
public bool Status { get; set; }
public string LocationId { get; set; }
public LocationDTO Location { get; set; }
public List<MeansOfCommunication> MeansOfCommunications { get; set; } public List<MeansOfCommunication> MeansOfCommunications { get; set; }
public DateTime CreatedDate { get; set; } public DateTime CreatedDate { get; set; }
@ -73,20 +54,10 @@ namespace MyCore.Interfaces.DTO
public string LastState { get; set; } // TODO UNIFORMISATION ? public string LastState { get; set; } // TODO UNIFORMISATION ?
public DateTime LastStateDate { get; set; }
public string IpAddress { get; set; } public string IpAddress { get; set; }
public string ServiceIdentification { get; set; } public string ServiceIdentification { get; set; }
public bool Battery { get; set; }
public int BatteryStatus { get; set; }
public string ProviderId { get; set; }
public string ProviderName { get; set; }
public string ManufacturerName { get; set; } public string ManufacturerName { get; set; }
public List<string> GroupIds { get; set; } public List<string> GroupIds { get; set; }

View File

@ -14,14 +14,8 @@ namespace MyCore.Interfaces.DTO
public string Name { get; set; } public string Name { get; set; }
} }
public class RoomDetailDTO public class RoomDetailDTO : RoomSummaryDTO
{ {
public string Id { get; set; }
public string UserId { get; set; }
public string Name { get; set; }
public DateTime CreatedDate { get; set; } public DateTime CreatedDate { get; set; }
public DateTime UpdatedDate { get; set; } public DateTime UpdatedDate { get; set; }
@ -29,14 +23,8 @@ namespace MyCore.Interfaces.DTO
public List<string> DeviceIds { get; set; } public List<string> DeviceIds { get; set; }
} }
public class RoomCreateOrUpdateDetailDTO public class RoomCreateOrUpdateDetailDTO : RoomSummaryDTO
{ {
public string Id { get; set; }
public string UserId { get; set; }
public string Name { get; set; }
public List<string> DeviceIds { get; set; } public List<string> DeviceIds { get; set; }
} }
} }

View File

@ -23,24 +23,46 @@ namespace MyCore.Interfaces.Models
[BsonRequired] [BsonRequired]
public string Name { get; set; } public string Name { get; set; }
[BsonElement("CreatedDate")]
public DateTime CreatedDate { get; set; }
[BsonElement("UpdatedDate")]
public DateTime UpdatedDate { get; set; }
[BsonElement("Triggers")] [BsonElement("Triggers")]
public List<Trigger> Triggers { get; set; } public List<Trigger> Triggers { get; set; }
[BsonElement("Conditions")] [BsonElement("Conditions")]
public List<Condition> Conditions { get; set; } public List<Condition> Conditions { get; set; }
[BsonElement("DevicesIds")]
public List<string> DevicesIds { get; set; }
[BsonElement("Actions")] [BsonElement("Actions")]
public List<Action> Actions { get; set; } public List<Action> Actions { get; set; }
[BsonElement("DevicesIds")]
public List<string> DevicesIds { get; set; }
public AutomationDTO ToDTO() public AutomationDTO ToDTO()
{ {
return new AutomationDTO() return new AutomationDTO()
{ {
Id = Id, Id = Id,
Name = Name, Name = Name,
UserId = UserId,
//Triggers = Triggers
//Conditions = Conditions
//Actions = Actions
};
}
public AutomationDetailDTO ToDetailDTO()
{
return new AutomationDetailDTO()
{
Id = Id,
Name = Name,
UserId = UserId,
CreatedDate = CreatedDate,
UpdatedDate = UpdatedDate,
//Triggers = Triggers //Triggers = Triggers
//Conditions = Conditions //Conditions = Conditions
//Actions = Actions //Actions = Actions

View File

@ -11,6 +11,7 @@ using MongoDB.Bson;
using Mqtt.Client.AspNetCore.Services; using Mqtt.Client.AspNetCore.Services;
using MyCore.Interfaces.DTO; using MyCore.Interfaces.DTO;
using MyCore.Interfaces.Models; using MyCore.Interfaces.Models;
using MyCore.Service.Services;
using MyCore.Services; using MyCore.Services;
using MyCore.Services.Devices; using MyCore.Services.Devices;
using MyCore.Services.MyControlPanel; using MyCore.Services.MyControlPanel;
@ -61,7 +62,7 @@ namespace MyCore.Service.Controllers
/// Get detail info of a specified automation /// Get detail info of a specified automation
/// </summary> /// </summary>
/// <param name="automationId">automation id</param> /// <param name="automationId">automation id</param>
[ProducesResponseType(typeof(RoomDetailDTO), 200)] [ProducesResponseType(typeof(AutomationDetailDTO), 200)]
[HttpGet("detail/{roomId}")] [HttpGet("detail/{roomId}")]
public ObjectResult GetDetail(string automationId) public ObjectResult GetDetail(string automationId)
{ {
@ -69,7 +70,7 @@ namespace MyCore.Service.Controllers
{ {
Automation automation = _AutomationDatabaseService.GetById(automationId); Automation automation = _AutomationDatabaseService.GetById(automationId);
return new OkObjectResult(automation.ToDTO()); return new OkObjectResult(automation.ToDetailDTO());
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -77,23 +78,23 @@ namespace MyCore.Service.Controllers
} }
} }
/*
//// <summary> //// <summary>
/// Create an automation /// Create an automation
/// </summary> /// </summary>
/// <param name="automationCreateOrUpdateDetail">Automation to create</param> /// <param name="automationCreateOrUpdateDetail">Automation to create</param>
[ProducesResponseType(typeof(RoomDetailDTO), 200)] [ProducesResponseType(typeof(AutomationDTO), 200)]
[HttpPost] [HttpPost]
public ObjectResult Create([FromBody] AutomationCreateOrUpdateDetailDTO automationCreateOrUpdateDetail) public ObjectResult Create([FromBody] AutomationCreateOrUpdateDetailDTO automationCreateOrUpdateDetail)
{ {
try try
{ {
if (roomCreateOrUpdateDetail == null) if (automationCreateOrUpdateDetail == null)
throw new KeyNotFoundException("Room is null"); throw new KeyNotFoundException("Automation is null");
RoomDetailDTO roomCreated = RoomService.CreateOrUpdate(this._RoomDatabaseService, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, true); AutomationDTO automationCreated = AutomationService.CreateOrUpdate(this._AutomationDatabaseService, automationCreateOrUpdateDetail.UserId, automationCreateOrUpdateDetail, true);
return new OkObjectResult(roomCreated); return new OkObjectResult(automationCreated);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -101,22 +102,23 @@ namespace MyCore.Service.Controllers
} }
} }
/// <summary> /// <summary>
/// Update a room /// Update an automation
/// </summary> /// </summary>
/// <param name="roomCreateOrUpdateDetail">room to update</param> /// <param name="automationCreateOrUpdateDetail">automation to update</param>
[ProducesResponseType(typeof(RoomCreateOrUpdateDetailDTO), 200)] [ProducesResponseType(typeof(AutomationCreateOrUpdateDetailDTO), 200)]
[HttpPut] [HttpPut]
public ObjectResult Update([FromBody] RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetail) public ObjectResult Update([FromBody] AutomationCreateOrUpdateDetailDTO automationCreateOrUpdateDetail)
{ {
try try
{ {
if (!_RoomDatabaseService.IsExist(roomCreateOrUpdateDetail.Id)) if (!_AutomationDatabaseService.IsExist(automationCreateOrUpdateDetail.Id))
throw new KeyNotFoundException("Room does not exist"); throw new KeyNotFoundException("Automation does not exist");
RoomDetailDTO roomUpdated = RoomService.CreateOrUpdate(this._RoomDatabaseService, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, false); AutomationDTO automationUpdated = AutomationService.CreateOrUpdate(this._AutomationDatabaseService, automationCreateOrUpdateDetail.UserId, automationCreateOrUpdateDetail, false);
return new OkObjectResult(roomUpdated); return new OkObjectResult(automationUpdated);
} }
catch (KeyNotFoundException ex) catch (KeyNotFoundException ex)
{ {
@ -126,7 +128,7 @@ namespace MyCore.Service.Controllers
{ {
return new ObjectResult(ex.Message) { StatusCode = 500 }; return new ObjectResult(ex.Message) { StatusCode = 500 };
} }
}*/ }
/// <summary> /// <summary>
/// Delete an automation /// Delete an automation

View File

@ -0,0 +1,37 @@
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.Services
{
public class AutomationService
{
public static AutomationDTO CreateOrUpdate(AutomationDatabaseService _AutomationDatabaseService, string userId, AutomationCreateOrUpdateDetailDTO automationCreateOrUpdateDetailDTO, bool create)
{
Automation automation;
if (create)
automation = new Automation();
else
{
automation = _AutomationDatabaseService.GetById(automationCreateOrUpdateDetailDTO.Id);
}
automation.UserId = userId;
automation.Name = automationCreateOrUpdateDetailDTO.Name;
automation.CreatedDate = DateTime.Now;
automation.UpdatedDate = DateTime.Now;
automation.Triggers = automationCreateOrUpdateDetailDTO.Triggers;
automation.Conditions = automationCreateOrUpdateDetailDTO.Conditions;
automation.Actions = automationCreateOrUpdateDetailDTO.Actions;
if (create)
return _AutomationDatabaseService.Create(automation).ToDTO();
else
return _AutomationDatabaseService.Update(automation.Id, automation).ToDTO();
}
}
}