MC Creation of DTOs, Device controller + update models (add required bson + ToDTO function) + add device service

This commit is contained in:
Thomas Fransolet 2020-03-26 12:41:45 +01:00
parent fb21254805
commit 8376acb662
27 changed files with 544 additions and 63 deletions

View File

@ -0,0 +1,154 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using MyCore.Models;
using MyCore.Models.MyControlPanel;
using MyCore.Services;
using MyCore.Services.Devices;
using MyCore.Services.MyControlPanel;
namespace MyCore.Controllers.Devices
{
[Authorize(Roles = "Admin")]
[Route("api/device")]
[ApiController]
public class DeviceController : ControllerBase
{
private readonly DeviceDatabaseService _DeviceDatabaseService;
private readonly ProviderDatabaseService _ProviderDatabaseService;
private readonly LocationDatabaseService _LocationDatabaseService;
public DeviceController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService)
{
_DeviceDatabaseService = DeviceDatabaseService;
_ProviderDatabaseService = ProviderDatabaseService;
_LocationDatabaseService = LocationDatabaseService;
}
// GET: Devices
/// <summary>
/// Get all devices summary
/// </summary>
[ProducesResponseType(typeof(List<DeviceSummaryDTO>), 200)]
[HttpGet]
public ObjectResult GetAll()
{
try
{
List<Device> Devices = _DeviceDatabaseService.GetAll();
List<DeviceSummaryDTO> devicesSummaryDTO = Devices.Select(d => d.ToSummaryDTO()).ToList();
return new OkObjectResult(devicesSummaryDTO);
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Get a specific device info
/// </summary>
/// <param name="deviceId">id of device</param>
[ProducesResponseType(typeof(DeviceDetailDTO), 200)]
[HttpGet("{deviceId}")]
public ObjectResult GetDetail(string deviceId)
{
try
{
Device device = _DeviceDatabaseService.GetById(deviceId);
return new OkObjectResult(device.ToDTO());
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Create a device
/// </summary>
/// <param name="deviceDetailDTO">Device to create</param>
[ProducesResponseType(typeof(DeviceDetailDTO), 200)]
[HttpPost]
public ObjectResult Create([FromBody] DeviceDetailDTO deviceDetailDTO)
{
try
{
if (deviceDetailDTO == null)
throw new KeyNotFoundException("Device is null");
DeviceDetailDTO deviceCreated = DeviceService.CreateOrUpdate(deviceDetailDTO, true);
return new OkObjectResult(deviceCreated);
}
catch (KeyNotFoundException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Update a device
/// </summary>
/// <param name="deviceId">Device Id</param>
/// <param name="deviceDetailDTO">Device to update</param>
[ProducesResponseType(typeof(DeviceDetailDTO), 200)]
[HttpPut("{deviceId}")]
public ObjectResult Update(string deviceId, [FromBody] DeviceDetailDTO deviceDetailDTO)
{
try
{
if (!_DeviceDatabaseService.IsExist(deviceId))
throw new KeyNotFoundException("Location does not exist");
DeviceDetailDTO deviceUpdated = DeviceService.CreateOrUpdate(deviceDetailDTO, false);
return new OkObjectResult(deviceUpdated);
}
catch (KeyNotFoundException ex)
{
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Delete a device
/// </summary>
/// <param name="deviceId">Id of device to delete</param>
[HttpDelete("{deviceId}")]
public ObjectResult Delete(string deviceId)
{
try
{
// Check if exist
_DeviceDatabaseService.Remove(deviceId);
return new OkObjectResult(201);
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
}
}

View File

@ -16,11 +16,11 @@ namespace MyCore.Controllers
[ApiController] [ApiController]
public class ScreenDeviceController : ControllerBase public class ScreenDeviceController : ControllerBase
{ {
private readonly ScreenDeviceService _ScreenDeviceService; private readonly ScreenDeviceDatabaseService _ScreenDeviceDatabaseService;
public ScreenDeviceController(ScreenDeviceService ScreenDeviceService) public ScreenDeviceController(ScreenDeviceDatabaseService ScreenDeviceDatabaseService)
{ {
_ScreenDeviceService = ScreenDeviceService; _ScreenDeviceDatabaseService = ScreenDeviceDatabaseService;
} }
// GET: Devices // GET: Devices
@ -33,7 +33,7 @@ namespace MyCore.Controllers
{ {
try try
{ {
List<ScreenDevice> screenDevices = _ScreenDeviceService.GetAll(); List<ScreenDevice> screenDevices = _ScreenDeviceDatabaseService.GetAll();
return new OkObjectResult(screenDevices); return new OkObjectResult(screenDevices);
} }
@ -47,14 +47,14 @@ namespace MyCore.Controllers
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="idScreenDevice">Id of the screen device you want to get information</param> /// <param name="screenDeviceId">Id of the screen device you want to get information</param>
[ProducesResponseType(typeof(ScreenDevice), 200)] [ProducesResponseType(typeof(ScreenDevice), 200)]
[HttpGet("{idScreenDevice}")] [HttpGet("{screenDeviceId}")]
public ObjectResult GetDeviceInfo(string idScreenDevice) public ObjectResult GetDeviceInfo(string screenDeviceId)
{ {
try try
{ {
ScreenDevice screenDevice = _ScreenDeviceService.GetInfo(idScreenDevice); ScreenDevice screenDevice = _ScreenDeviceDatabaseService.GetInfo(screenDeviceId);
return new OkObjectResult(screenDevice); return new OkObjectResult(screenDevice);
} }
@ -73,7 +73,7 @@ namespace MyCore.Controllers
{ {
try try
{ {
_ScreenDeviceService.Create(screenDevice); _ScreenDeviceDatabaseService.Create(screenDevice);
return new OkObjectResult(201); return new OkObjectResult(201);
} }
@ -87,12 +87,12 @@ namespace MyCore.Controllers
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
[HttpPut("{idScreenDevice}")] [HttpPut("{screenDeviceId}")]
public ObjectResult UpdateDevice(int idScreenDevice, [FromBody] ScreenDevice screenDevice) public ObjectResult UpdateDevice(int screenDeviceId, [FromBody] ScreenDevice screenDevice)
{ {
try try
{ {
_ScreenDeviceService.Update(screenDevice.Id, screenDevice); _ScreenDeviceDatabaseService.Update(screenDevice.Id, screenDevice);
return new OkObjectResult(201); return new OkObjectResult(201);
} }
@ -106,12 +106,12 @@ namespace MyCore.Controllers
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
[HttpDelete("{idDevice}")] [HttpDelete("{deviceId}")]
public ObjectResult DeleteDevice(int idDevice, [FromBody] string deviceId) public ObjectResult DeleteDevice(string deviceId)
{ {
try try
{ {
_ScreenDeviceService.Remove(deviceId); _ScreenDeviceDatabaseService.Remove(deviceId);
return new OkObjectResult(201); return new OkObjectResult(201);
} }

View File

@ -22,9 +22,9 @@ namespace MyCore.Controllers
public class TokenController : ControllerBase public class TokenController : ControllerBase
{ {
private TokenService _tokenService; private TokenService _tokenService;
private UserService _userService; private UserDatabaseService _userService;
public TokenController(TokenService tokenService, UserService userService) public TokenController(TokenService tokenService, UserDatabaseService userService)
{ {
_tokenService = tokenService; _tokenService = tokenService;
_userService = userService; _userService = userService;

View File

@ -17,10 +17,10 @@ namespace MyCore.Controllers
[ApiController] [ApiController]
public class UserController : ControllerBase public class UserController : ControllerBase
{ {
private UserService _userService; private UserDatabaseService _userService;
private TokenService _tokenService; private TokenService _tokenService;
public UserController(UserService userService, TokenService tokenService) public UserController(UserDatabaseService userService, TokenService tokenService)
{ {
_userService = userService; _userService = userService;
_tokenService = tokenService; _tokenService = tokenService;

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.DTO.MyControlPanel
{
public class AutomationDTO
{
public string Id { get; set; }
public string Name { get; set; }
}
}

View File

@ -0,0 +1,59 @@
using MyCore.DTO.Common;
using MyCore.Models.MyControlPanel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.DTO.MyControlPanel
{
public class DeviceSummaryDTO
{
public string Id { get; set; }
public string Name { get; set; }
public ConnectionStatus ConnectionStatus { get; set; }
public LocationDTO Location { get; set; }
public bool Battery { get; set; }
public int BatteryStatus { get; set; }
}
public class DeviceDetailDTO
{
public string Id { get; set; }
public string Name { get; set; }
public ConnectionStatus ConnectionStatus { get; set; }
public string LocationId { get; set; }
public LocationDTO Location { get; set; }
public List<MeansOfCommunication> MeansOfCommunications { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime UpdatedDate { get; set; }
public string LastMessage { get; set; } // TODO UNIFORMISATION ?
public DateTime LastMessageDate { get; set; }
public string IpAddress { get; set; }
public bool Battery { get; set; }
public int BatteryStatus { get; set; }
public string ProviderId { get; set; }
public List<string> Groups { get; set; }
public List<InformationDTO> Informations { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.DTO.MyControlPanel
{
public class GroupDTO
{
public string Id { get; set; }
public string Name { get; set; }
public List<string> Devices { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.DTO.MyControlPanel
{
public class InformationDTO
{
public string Id { get; set; }
public string Name { get; set; }
public object Value { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.DTO.MyControlPanel
{
public class LocationDTO
{
public string Id { get; set; }
public string Name { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.DTO.MyControlPanel
{
public class ProviderDTO
{
public string Id { get; set; }
public string Name { get; set; }
public string Username { get; set; }
public string Password { get; set; } // TODO ENCRYPTED
public string ApiKey { get; set; } // TODO ENCRYPTED
}
}

View File

@ -1,6 +1,7 @@
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using MyCore.DTO.Common; using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -16,6 +17,7 @@ namespace MyCore.Models
public string Id { get; set; } public string Id { get; set; }
[BsonElement("Name")] [BsonElement("Name")]
[BsonRequired]
public string Name { get; set; } public string Name { get; set; }
[BsonElement("Triggers")] [BsonElement("Triggers")]
@ -26,6 +28,18 @@ namespace MyCore.Models
[BsonElement("Actions")] [BsonElement("Actions")]
public List<Action> Actions { get; set; } public List<Action> Actions { get; set; }
public AutomationDTO ToDTO()
{
return new AutomationDTO()
{
Id = Id,
Name = Name,
//Triggers = Triggers
//Conditions = Conditions
//Actions = Actions
};
}
} }
public class Trigger public class Trigger

View File

@ -1,8 +1,11 @@
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using MyCore.DTO.Common; using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using MyCore.Services.MyControlPanel;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace MyCore.Models.MyControlPanel namespace MyCore.Models.MyControlPanel
{ {
@ -11,18 +14,21 @@ namespace MyCore.Models.MyControlPanel
/// </summary> /// </summary>
public class Device public class Device
{ {
private LocationDatabaseService _LocationDatabaseService;
[BsonId] [BsonId]
[BsonRepresentation(BsonType.ObjectId)] [BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; } public string Id { get; set; }
[BsonElement("Name")] [BsonElement("Name")]
[BsonRequired]
public string Name { get; set; } public string Name { get; set; }
[BsonElement("ConnectionStatus")] [BsonElement("ConnectionStatus")]
public ConnectionStatus ConnectionStatus { get; set; } public ConnectionStatus ConnectionStatus { get; set; }
[BsonElement("Location")] [BsonElement("LocationId")]
public Location Location { get; set; } [BsonRequired]
public string LocationId { get; set; }
[BsonElement("MeansOfCommunications")] [BsonElement("MeansOfCommunications")]
public List<MeansOfCommunication> MeansOfCommunications { get; set; } public List<MeansOfCommunication> MeansOfCommunications { get; set; }
@ -39,19 +45,58 @@ namespace MyCore.Models.MyControlPanel
[BsonElement("LastMessageDate")] [BsonElement("LastMessageDate")]
public DateTime LastMessageDate { get; set; } public DateTime LastMessageDate { get; set; }
[BsonElement("IpAddress")]
public string IpAddress { get; set; }
[BsonElement("Battery")] [BsonElement("Battery")]
public bool Battery { get; set; } public bool Battery { get; set; }
[BsonElement("BatteryStatus")] [BsonElement("BatteryStatus")]
public int BatteryStatus { get; set; } public int BatteryStatus { get; set; }
[BsonElement("Provider")] [BsonElement("ProviderId")]
public Provider provider { get; set; } [BsonRequired]
public string ProviderId { get; set; }
[BsonElement("Groups")] [BsonElement("GroupIds")]
public List<Group> Groups { get; set; } public List<string> GroupIds { get; set; }
[BsonElement("Informations")] [BsonElement("Informations")]
public List<Information> Informations { get; set; } public List<Information> Information { get; set; }
public DeviceSummaryDTO ToSummaryDTO()
{
return new DeviceSummaryDTO()
{
Id = Id,
Name = Name,
ConnectionStatus = ConnectionStatus,
Location = _LocationDatabaseService.GetById(LocationId).ToDTO(), // Check if correct way
Battery = Battery,
BatteryStatus = BatteryStatus
};
}
public DeviceDetailDTO ToDTO()
{
return new DeviceDetailDTO()
{
Id = Id,
Name = Name,
ConnectionStatus = ConnectionStatus,
Location = _LocationDatabaseService.GetById(LocationId).ToDTO(), // Check if correct way
MeansOfCommunications = MeansOfCommunications,
CreatedDate = CreatedDate,
UpdatedDate = UpdatedDate,
LastMessage = LastMessage,
LastMessageDate = LastMessageDate,
IpAddress = IpAddress,
//Provider = Provider.ToDTO(device.Provider),
//Groups = device.Groups.Select(i => i.ToDTO(i)).ToList(),
//Informations = device.Informations.Select(i => i.ToDTO(i)).ToList(),
Battery = Battery,
BatteryStatus = BatteryStatus
};
}
} }
} }

View File

@ -1,6 +1,7 @@
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using MyCore.DTO.Common; using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace MyCore.Models.MyControlPanel namespace MyCore.Models.MyControlPanel
@ -15,9 +16,20 @@ namespace MyCore.Models.MyControlPanel
public string Id { get; set; } public string Id { get; set; }
[BsonElement("Name")] [BsonElement("Name")]
[BsonRequired]
public string Name { get; set; } public string Name { get; set; }
[BsonElement("Devices")] [BsonElement("Devices")]
public List<Device> Devices { get; set; } public List<string> Devices { get; set; }
public GroupDTO ToDTO()
{
return new GroupDTO()
{
Id = Id,
Name = Name,
Devices = Devices
};
}
} }
} }

View File

@ -1,6 +1,7 @@
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using MyCore.DTO.Common; using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -16,9 +17,21 @@ namespace MyCore.Models.MyControlPanel
public string Id { get; set; } public string Id { get; set; }
[BsonElement("Name")] [BsonElement("Name")]
[BsonRequired]
public string Name { get; set; } public string Name { get; set; }
[BsonElement("Value")] [BsonElement("Value")]
[BsonRequired]
public object Value { get; set; } public object Value { get; set; }
public InformationDTO ToDTO()
{
return new InformationDTO()
{
Id = Id,
Name = Name,
Value = Value
};
}
} }
} }

View File

@ -1,5 +1,6 @@
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using MyCore.DTO.MyControlPanel;
namespace MyCore.Models.MyControlPanel namespace MyCore.Models.MyControlPanel
{ {
@ -13,6 +14,16 @@ namespace MyCore.Models.MyControlPanel
public string Id { get; set; } public string Id { get; set; }
[BsonElement("Name")] [BsonElement("Name")]
[BsonRequired]
public string Name { get; set; } public string Name { get; set; }
public LocationDTO ToDTO()
{
return new LocationDTO()
{
Id = Id,
Name = Name
};
}
} }
} }

View File

@ -1,6 +1,7 @@
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using MyCore.DTO.Common; using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -16,15 +17,27 @@ namespace MyCore.Models.MyControlPanel
public string Id { get; set; } public string Id { get; set; }
[BsonElement("Name")] [BsonElement("Name")]
[BsonRequired]
public string Name { get; set; } public string Name { get; set; }
[BsonElement("Username")] [BsonElement("Username")]
[BsonRequired]
public string Username { get; set; } public string Username { get; set; }
[BsonElement("Password")] [BsonElement("Password")]
[BsonRequired]
public string Password { get; set; } // TODO ENCRYPTED public string Password { get; set; } // TODO ENCRYPTED
[BsonElement("ApiKey")] [BsonElement("ApiKey")]
public string ApiKey { get; set; } // TODO ENCRYPTED public string ApiKey { get; set; } // TODO ENCRYPTED
public ProviderDTO ToDTO()
{
return new ProviderDTO()
{
Id = Id,
Name = Name
};
}
} }
} }

View File

@ -22,18 +22,23 @@ namespace MyCore.Models
public string Role { get; set; } public string Role { get; set; }
[BsonElement("Email")] [BsonElement("Email")]
[BsonRequired]
public string Email { get; set; } // UNIQUE !.. public string Email { get; set; } // UNIQUE !..
[BsonElement("Password")] [BsonElement("Password")]
[BsonRequired]
public string Password { get; set; } public string Password { get; set; }
[BsonElement("FirstName")] [BsonElement("FirstName")]
[BsonRequired]
public string FirstName { get; set; } public string FirstName { get; set; }
[BsonElement("LastName")] [BsonElement("LastName")]
[BsonRequired]
public string LastName { get; set; } public string LastName { get; set; }
[BsonElement("Token")] [BsonElement("Token")]
[BsonRequired]
public string Token { get; set; } public string Token { get; set; }
[BsonElement("Birthday")] [BsonElement("Birthday")]
@ -55,6 +60,7 @@ namespace MyCore.Models
public string Country { get; set; } public string Country { get; set; }
[BsonElement("Language")] [BsonElement("Language")]
[BsonRequired]
public string Language { get; set; } public string Language { get; set; }
[BsonElement("TimeZone")] [BsonElement("TimeZone")]

View File

@ -0,0 +1,58 @@
using Microsoft.AspNetCore.Mvc;
using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using MyCore.Models.MyControlPanel;
using MyCore.Services.MyControlPanel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.Services.Devices
{
public class DeviceService
{
private static DeviceDatabaseService _DeviceDatabaseService;
private static ProviderDatabaseService _ProviderDatabaseService;
private static LocationDatabaseService _LocationDatabaseService;
public static DeviceDetailDTO CreateOrUpdate(DeviceDetailDTO deviceDetailDTO, bool create)
{
Device device;
if (create)
device = new Device();
else
{
device = _DeviceDatabaseService.GetById(deviceDetailDTO.Id);
}
device.Name = deviceDetailDTO.Name;
if (_ProviderDatabaseService.IsExist(deviceDetailDTO.ProviderId))
device.ProviderId = deviceDetailDTO.ProviderId;
else
throw new KeyNotFoundException("Provider does not exist");
if (_LocationDatabaseService.IsExist(deviceDetailDTO.LocationId))
device.LocationId = deviceDetailDTO.LocationId;
else
throw new KeyNotFoundException("Location does not exist");
device.ConnectionStatus = ConnectionStatus.Unknown;
device.CreatedDate = DateTime.Now;
device.UpdatedDate = DateTime.Now;
device.MeansOfCommunications = deviceDetailDTO.MeansOfCommunications;
device.IpAddress = deviceDetailDTO.IpAddress;
device.Battery = deviceDetailDTO.Battery;
device.BatteryStatus = deviceDetailDTO.BatteryStatus;
device.GroupIds = device.GroupIds;
// Todo structure informations
device.Information = device.Information;
if (create)
return _DeviceDatabaseService.Create(device).ToDTO();
else
return _DeviceDatabaseService.Update(device.Id, device).ToDTO();
}
}
}

View File

@ -13,6 +13,8 @@ namespace MyCore.Services
public async Task<List<Device>> GetDevices() public async Task<List<Device>> GetDevices()
{ {
devices = await DeviceLocator.Discover(); devices = await DeviceLocator.Discover();
//Toggle(devices[1]);
return devices; return devices;
} }

View File

@ -9,27 +9,27 @@ using MyCore.Models.MyControlPanel;
namespace MyCore.Services.MyControlPanel namespace MyCore.Services.MyControlPanel
{ {
public class AutomationService public class AutomationDatabaseService
{ {
private readonly IMongoCollection<Automation> _Automations; private readonly IMongoCollection<Automation> _Automations;
public AutomationService(IConfiguration config) public AutomationDatabaseService(IConfiguration config)
{ {
var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb"); var database = client.GetDatabase("MyCoreDb");
_Automations = database.GetCollection<Automation>("Automations"); _Automations = database.GetCollection<Automation>("Automations");
} }
public List<Automation> GetAutomations() public List<Automation> GetAll()
{ {
return _Automations.Find(d => true).ToList(); return _Automations.Find(d => true).ToList();
} }
public Automation GetAutomationById(string id) public Automation GetById(string id)
{ {
return _Automations.Find<Automation>(a => a.Id == id).FirstOrDefault(); return _Automations.Find<Automation>(a => a.Id == id).FirstOrDefault();
} }
public Automation CreateAutomation(Automation automation) public Automation Create(Automation automation)
{ {
_Automations.InsertOne(automation); _Automations.InsertOne(automation);
return automation; return automation;

View File

@ -9,27 +9,32 @@ using MyCore.Models.MyControlPanel;
namespace MyCore.Services.MyControlPanel namespace MyCore.Services.MyControlPanel
{ {
public class DeviceService public class DeviceDatabaseService
{ {
private readonly IMongoCollection<Device> _Devices; private readonly IMongoCollection<Device> _Devices;
public DeviceService(IConfiguration config) public DeviceDatabaseService(IConfiguration config)
{ {
var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb"); var database = client.GetDatabase("MyCoreDb");
_Devices = database.GetCollection<Device>("Devices"); _Devices = database.GetCollection<Device>("Devices");
} }
public List<Device> GetDevices() public List<Device> GetAll()
{ {
return _Devices.Find(d => true).ToList(); return _Devices.Find(d => true).ToList();
} }
public Device GetDeviceById(string id) public Device GetById(string id)
{ {
return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault(); return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault();
} }
public Device CreateDevice(Device device) public bool IsExist(string id)
{
return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault() != null ? true : false;
}
public Device Create(Device device)
{ {
_Devices.InsertOne(device); _Devices.InsertOne(device);
return device; return device;

View File

@ -9,27 +9,27 @@ using MyCore.Models.MyControlPanel;
namespace MyCore.Services.MyControlPanel namespace MyCore.Services.MyControlPanel
{ {
public class GroupService public class GroupDatabaseService
{ {
private readonly IMongoCollection<Group> _Groups; private readonly IMongoCollection<Group> _Groups;
public GroupService(IConfiguration config) public GroupDatabaseService(IConfiguration config)
{ {
var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb"); var database = client.GetDatabase("MyCoreDb");
_Groups = database.GetCollection<Group>("Groups"); _Groups = database.GetCollection<Group>("Groups");
} }
public List<Group> GetGroups() public List<Group> GetAll()
{ {
return _Groups.Find(d => true).ToList(); return _Groups.Find(d => true).ToList();
} }
public Group GetGroupById(string id) public Group GetById(string id)
{ {
return _Groups.Find<Group>(g => g.Id == id).FirstOrDefault(); return _Groups.Find<Group>(g => g.Id == id).FirstOrDefault();
} }
public Group CreateGroup(Group group) public Group Create(Group group)
{ {
_Groups.InsertOne(group); _Groups.InsertOne(group);
return group; return group;

View File

@ -9,27 +9,32 @@ using MyCore.Models.MyControlPanel;
namespace MyCore.Services.MyControlPanel namespace MyCore.Services.MyControlPanel
{ {
public class LocationService public class LocationDatabaseService
{ {
private readonly IMongoCollection<Location> _Locations; private readonly IMongoCollection<Location> _Locations;
public LocationService(IConfiguration config) public LocationDatabaseService(IConfiguration config)
{ {
var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb"); var database = client.GetDatabase("MyCoreDb");
_Locations = database.GetCollection<Location>("Locations"); _Locations = database.GetCollection<Location>("Locations");
} }
public List<Location> GetLocations() public List<Location> GetAll()
{ {
return _Locations.Find(l => true).ToList(); return _Locations.Find(l => true).ToList();
} }
public Location GetLocationById(string id) public Location GetById(string id)
{ {
return _Locations.Find<Location>(l => l.Id == id).FirstOrDefault(); return _Locations.Find<Location>(l => l.Id == id).FirstOrDefault();
} }
public Location CreateLocation(Location location) public bool IsExist(string id)
{
return _Locations.Find<Location>(p => p.Id == id).FirstOrDefault() != null ? true : false;
}
public Location Create(Location location)
{ {
_Locations.InsertOne(location); _Locations.InsertOne(location);
return location; return location;

View File

@ -8,27 +8,37 @@ using MongoDB.Driver;
using MyCore.Models.MyControlPanel; using MyCore.Models.MyControlPanel;
namespace MyCore.Services.MyControlPanel namespace MyCore.Services.MyControlPanel
{ {
public class ProviderService public class ProviderDatabaseService
{ {
private readonly IMongoCollection<Provider> _Providers; private readonly IMongoCollection<Provider> _Providers;
public ProviderService(IConfiguration config) public ProviderDatabaseService(IConfiguration config)
{ {
var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb"); var database = client.GetDatabase("MyCoreDb");
_Providers = database.GetCollection<Provider>("Providers"); _Providers = database.GetCollection<Provider>("Providers");
} }
public List<Provider> GetProviders() public List<Provider> GetAll()
{ {
return _Providers.Find(p => true).ToList(); return _Providers.Find(p => true).ToList();
} }
public Provider GetProviderById(string id) public Provider GetById(string id)
{ {
return _Providers.Find<Provider>(p => p.Id == id).FirstOrDefault(); return _Providers.Find<Provider>(p => p.Id == id).FirstOrDefault();
} }
public Provider CreateProvider(Provider provider) public Provider GetByName(string name)
{
return _Providers.Find<Provider>(p => p.Name == name).FirstOrDefault();
}
public bool IsExist(string id)
{
return _Providers.Find<Provider>(p => p.Id == id).FirstOrDefault() != null ? true : false;
}
public Provider Create(Provider provider)
{ {
_Providers.InsertOne(provider); _Providers.InsertOne(provider);
return provider; return provider;

View File

@ -8,11 +8,11 @@ using MongoDB.Driver;
namespace MyCore.Services namespace MyCore.Services
{ {
public class ScreenDeviceService public class ScreenDeviceDatabaseService
{ {
private readonly IMongoCollection<ScreenDevice> _screenDevices; private readonly IMongoCollection<ScreenDevice> _screenDevices;
public ScreenDeviceService(IConfiguration config) public ScreenDeviceDatabaseService(IConfiguration config)
{ {
var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb"); var database = client.GetDatabase("MyCoreDb");

View File

@ -8,32 +8,32 @@ using MongoDB.Driver;
namespace MyCore.Services namespace MyCore.Services
{ {
public class UserService public class UserDatabaseService
{ {
private readonly IMongoCollection<UserInfo> _Users; private readonly IMongoCollection<UserInfo> _Users;
public UserService(IConfiguration config) public UserDatabaseService(IConfiguration config)
{ {
var client = new MongoClient(config.GetConnectionString("MyCoreDb")); var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb"); var database = client.GetDatabase("MyCoreDb");
_Users = database.GetCollection<UserInfo>("Users"); _Users = database.GetCollection<UserInfo>("Users");
} }
public List<UserInfo> GetUsers() public List<UserInfo> GetAll()
{ {
return _Users.Find(m => true).ToList(); return _Users.Find(m => true).ToList();
} }
public UserInfo GetUserByEmail(string email) public UserInfo GetByEmail(string email)
{ {
return _Users.Find<UserInfo>(m => m.Email == email).FirstOrDefault(); return _Users.Find<UserInfo>(m => m.Email == email).FirstOrDefault();
} }
public UserInfo GetUserById(string id) public UserInfo GetById(string id)
{ {
return _Users.Find<UserInfo>(m => m.Id == id).FirstOrDefault(); return _Users.Find<UserInfo>(m => m.Id == id).FirstOrDefault();
} }
public UserInfo CreateUser(UserInfo user) public UserInfo Create(UserInfo user)
{ {
_Users.InsertOne(user); _Users.InsertOne(user);
return user; return user;

View File

@ -31,6 +31,10 @@ namespace MyCore
//MerossService merossService = new MerossService(); //MerossService merossService = new MerossService();
//ArloService arloService = new ArloService(); //ArloService arloService = new ArloService();
YeelightService yeelighService = new YeelightService();
yeelighService.GetDevices();
} }
public IConfiguration Configuration { get; } public IConfiguration Configuration { get; }
@ -41,9 +45,9 @@ namespace MyCore
// Add the service (test purpose) // Add the service (test purpose)
services.AddScoped<BookService>(); services.AddScoped<BookService>();
services.AddScoped<IoTDeviceService>(); services.AddScoped<IoTDeviceService>();
services.AddScoped<UserService>(); services.AddScoped<UserDatabaseService>();
services.AddScoped<TokenService>(); services.AddScoped<TokenService>();
services.AddScoped<DeviceService>(); services.AddScoped<ScreenDeviceDatabaseService>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);