using Microsoft.Extensions.Configuration; using MyCore.DTO.MyControlPanel; using MyCore.Models.MyControlPanel; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace MyCore.Services.MyControlPanel { public class ProviderService { static List supportedProviders = new List() { "Arlo", "Meross", "Yeelight", "ZigBee" }; private readonly ProviderDatabaseService _ProviderDatabaseService; public ProviderService(ProviderDatabaseService ProviderDatabaseService) { _ProviderDatabaseService = ProviderDatabaseService; } public static bool IsExist(string userId, string providerId) { return _ProviderDatabaseService.GetById(userId, providerId) != null ? true : false; } public static List GetAll(string userId) { return _ProviderDatabaseService.GetAll(userId); } public static ProviderDTO CreateOrUpdate(string userId, ProviderDTO providerDTO, bool create) { Provider provider; if (create) provider = new Provider(); else { provider = _ProviderDatabaseService.GetById(userId, providerDTO.Id); } if (!IsProviderSupported(providerDTO.Name)) throw new KeyNotFoundException("Provider is not yet supported"); provider.Name = providerDTO.Name; provider.UserId = providerDTO.UserId; provider.Username = providerDTO.Username; provider.Password = providerDTO.Password; provider.ApiKey = providerDTO.ApiKey; if (create) return _ProviderDatabaseService.Create(provider).ToDTO(); else return _ProviderDatabaseService.Update(provider.Id, provider).ToDTO(); } public static Provider GetProviderById(string userId, string providerId) { return _ProviderDatabaseService.GetById(userId, providerId); } // TODO Get supported services public static List GetSupportedProvider() { return supportedProviders; } public static bool IsProviderSupported(string providerName) { return supportedProviders.Contains(providerName) ? true : false; } } }