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]
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
@ -33,7 +33,7 @@ namespace MyCore.Controllers
{
try
{
List<ScreenDevice> screenDevices = _ScreenDeviceService.GetAll();
List<ScreenDevice> screenDevices = _ScreenDeviceDatabaseService.GetAll();
return new OkObjectResult(screenDevices);
}
@ -47,14 +47,14 @@ namespace MyCore.Controllers
/// <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)]
[HttpGet("{idScreenDevice}")]
public ObjectResult GetDeviceInfo(string idScreenDevice)
[HttpGet("{screenDeviceId}")]
public ObjectResult GetDeviceInfo(string screenDeviceId)
{
try
{
ScreenDevice screenDevice = _ScreenDeviceService.GetInfo(idScreenDevice);
ScreenDevice screenDevice = _ScreenDeviceDatabaseService.GetInfo(screenDeviceId);
return new OkObjectResult(screenDevice);
}
@ -73,7 +73,7 @@ namespace MyCore.Controllers
{
try
{
_ScreenDeviceService.Create(screenDevice);
_ScreenDeviceDatabaseService.Create(screenDevice);
return new OkObjectResult(201);
}
@ -87,12 +87,12 @@ namespace MyCore.Controllers
/// <summary>
///
/// </summary>
[HttpPut("{idScreenDevice}")]
public ObjectResult UpdateDevice(int idScreenDevice, [FromBody] ScreenDevice screenDevice)
[HttpPut("{screenDeviceId}")]
public ObjectResult UpdateDevice(int screenDeviceId, [FromBody] ScreenDevice screenDevice)
{
try
{
_ScreenDeviceService.Update(screenDevice.Id, screenDevice);
_ScreenDeviceDatabaseService.Update(screenDevice.Id, screenDevice);
return new OkObjectResult(201);
}
@ -106,12 +106,12 @@ namespace MyCore.Controllers
/// <summary>
///
/// </summary>
[HttpDelete("{idDevice}")]
public ObjectResult DeleteDevice(int idDevice, [FromBody] string deviceId)
[HttpDelete("{deviceId}")]
public ObjectResult DeleteDevice(string deviceId)
{
try
{
_ScreenDeviceService.Remove(deviceId);
_ScreenDeviceDatabaseService.Remove(deviceId);
return new OkObjectResult(201);
}

View File

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

View File

@ -17,10 +17,10 @@ namespace MyCore.Controllers
[ApiController]
public class UserController : ControllerBase
{
private UserService _userService;
private UserDatabaseService _userService;
private TokenService _tokenService;
public UserController(UserService userService, TokenService tokenService)
public UserController(UserDatabaseService userService, TokenService tokenService)
{
_userService = userService;
_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.Serialization.Attributes;
using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using System;
using System.Collections.Generic;
@ -16,6 +17,7 @@ namespace MyCore.Models
public string Id { get; set; }
[BsonElement("Name")]
[BsonRequired]
public string Name { get; set; }
[BsonElement("Triggers")]
@ -26,6 +28,18 @@ namespace MyCore.Models
[BsonElement("Actions")]
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

View File

@ -1,8 +1,11 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using MyCore.Services.MyControlPanel;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyCore.Models.MyControlPanel
{
@ -10,19 +13,22 @@ namespace MyCore.Models.MyControlPanel
/// Group of devices
/// </summary>
public class Device
{
{
private LocationDatabaseService _LocationDatabaseService;
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("Name")]
[BsonRequired]
public string Name { get; set; }
[BsonElement("ConnectionStatus")]
public ConnectionStatus ConnectionStatus { get; set; }
[BsonElement("Location")]
public Location Location { get; set; }
[BsonElement("LocationId")]
[BsonRequired]
public string LocationId { get; set; }
[BsonElement("MeansOfCommunications")]
public List<MeansOfCommunication> MeansOfCommunications { get; set; }
@ -39,19 +45,58 @@ namespace MyCore.Models.MyControlPanel
[BsonElement("LastMessageDate")]
public DateTime LastMessageDate { get; set; }
[BsonElement("IpAddress")]
public string IpAddress { get; set; }
[BsonElement("Battery")]
public bool Battery { get; set; }
[BsonElement("BatteryStatus")]
public int BatteryStatus { get; set; }
[BsonElement("Provider")]
public Provider provider { get; set; }
[BsonElement("ProviderId")]
[BsonRequired]
public string ProviderId { get; set; }
[BsonElement("Groups")]
public List<Group> Groups { get; set; }
[BsonElement("GroupIds")]
public List<string> GroupIds { get; set; }
[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.Serialization.Attributes;
using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using System;
using System.Collections.Generic;
namespace MyCore.Models.MyControlPanel
@ -15,9 +16,20 @@ namespace MyCore.Models.MyControlPanel
public string Id { get; set; }
[BsonElement("Name")]
[BsonRequired]
public string Name { get; set; }
[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.Serialization.Attributes;
using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using System;
using System.Collections.Generic;
@ -16,9 +17,21 @@ namespace MyCore.Models.MyControlPanel
public string Id { get; set; }
[BsonElement("Name")]
[BsonRequired]
public string Name { get; set; }
[BsonElement("Value")]
[BsonRequired]
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.Serialization.Attributes;
using MyCore.DTO.MyControlPanel;
namespace MyCore.Models.MyControlPanel
{
@ -13,6 +14,16 @@ namespace MyCore.Models.MyControlPanel
public string Id { get; set; }
[BsonElement("Name")]
[BsonRequired]
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.Serialization.Attributes;
using MyCore.DTO.Common;
using MyCore.DTO.MyControlPanel;
using System;
using System.Collections.Generic;
@ -16,15 +17,27 @@ namespace MyCore.Models.MyControlPanel
public string Id { get; set; }
[BsonElement("Name")]
[BsonRequired]
public string Name { get; set; }
[BsonElement("Username")]
[BsonRequired]
public string Username { get; set; }
[BsonElement("Password")]
[BsonRequired]
public string Password { get; set; } // TODO ENCRYPTED
[BsonElement("ApiKey")]
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; }
[BsonElement("Email")]
[BsonRequired]
public string Email { get; set; } // UNIQUE !..
[BsonElement("Password")]
[BsonRequired]
public string Password { get; set; }
[BsonElement("FirstName")]
[BsonRequired]
public string FirstName { get; set; }
[BsonElement("LastName")]
[BsonRequired]
public string LastName { get; set; }
[BsonElement("Token")]
[BsonRequired]
public string Token { get; set; }
[BsonElement("Birthday")]
@ -55,6 +60,7 @@ namespace MyCore.Models
public string Country { get; set; }
[BsonElement("Language")]
[BsonRequired]
public string Language { get; set; }
[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()
{
devices = await DeviceLocator.Discover();
//Toggle(devices[1]);
return devices;
}

View File

@ -9,27 +9,27 @@ using MyCore.Models.MyControlPanel;
namespace MyCore.Services.MyControlPanel
{
public class AutomationService
public class AutomationDatabaseService
{
private readonly IMongoCollection<Automation> _Automations;
public AutomationService(IConfiguration config)
public AutomationDatabaseService(IConfiguration config)
{
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb");
_Automations = database.GetCollection<Automation>("Automations");
}
public List<Automation> GetAutomations()
public List<Automation> GetAll()
{
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();
}
public Automation CreateAutomation(Automation automation)
public Automation Create(Automation automation)
{
_Automations.InsertOne(automation);
return automation;

View File

@ -9,27 +9,32 @@ using MyCore.Models.MyControlPanel;
namespace MyCore.Services.MyControlPanel
{
public class DeviceService
public class DeviceDatabaseService
{
private readonly IMongoCollection<Device> _Devices;
public DeviceService(IConfiguration config)
public DeviceDatabaseService(IConfiguration config)
{
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb");
_Devices = database.GetCollection<Device>("Devices");
}
public List<Device> GetDevices()
public List<Device> GetAll()
{
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();
}
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);
return device;

View File

@ -9,27 +9,27 @@ using MyCore.Models.MyControlPanel;
namespace MyCore.Services.MyControlPanel
{
public class GroupService
public class GroupDatabaseService
{
private readonly IMongoCollection<Group> _Groups;
public GroupService(IConfiguration config)
public GroupDatabaseService(IConfiguration config)
{
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb");
_Groups = database.GetCollection<Group>("Groups");
}
public List<Group> GetGroups()
public List<Group> GetAll()
{
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();
}
public Group CreateGroup(Group group)
public Group Create(Group group)
{
_Groups.InsertOne(group);
return group;

View File

@ -9,27 +9,32 @@ using MyCore.Models.MyControlPanel;
namespace MyCore.Services.MyControlPanel
{
public class LocationService
public class LocationDatabaseService
{
private readonly IMongoCollection<Location> _Locations;
public LocationService(IConfiguration config)
public LocationDatabaseService(IConfiguration config)
{
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb");
_Locations = database.GetCollection<Location>("Locations");
}
public List<Location> GetLocations()
public List<Location> GetAll()
{
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();
}
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);
return location;

View File

@ -8,27 +8,37 @@ using MongoDB.Driver;
using MyCore.Models.MyControlPanel;
namespace MyCore.Services.MyControlPanel
{
public class ProviderService
public class ProviderDatabaseService
{
private readonly IMongoCollection<Provider> _Providers;
public ProviderService(IConfiguration config)
public ProviderDatabaseService(IConfiguration config)
{
var client = new MongoClient(config.GetConnectionString("MyCoreDb"));
var database = client.GetDatabase("MyCoreDb");
_Providers = database.GetCollection<Provider>("Providers");
}
public List<Provider> GetProviders()
public List<Provider> GetAll()
{
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();
}
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);
return provider;

View File

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

View File

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

View File

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