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.Interfaces.DTO; using MyCore.Interfaces.Models; using MyCore.Services; using MyCore.Services.Devices; using MyCore.Services.MyControlPanel; namespace MyCore.Controllers { //[Authorize(Roles = "Admin")] [Authorize] [Route("api/provider")] [ApiController] public class ProviderController : ControllerBase { private DeviceDatabaseService _DeviceDatabaseService; private ProviderDatabaseService _ProviderDatabaseService; private LocationDatabaseService _LocationDatabaseService; private UserDatabaseService _UserDatabaseService; public ProviderController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService) { this._DeviceDatabaseService = DeviceDatabaseService; this._ProviderDatabaseService = ProviderDatabaseService; this._LocationDatabaseService = LocationDatabaseService; this._UserDatabaseService = UserDatabaseService; } // GET: Devices /// /// Get all user providers /// [ProducesResponseType(typeof(List), 200)] [HttpGet("{userId}")] public ObjectResult GetAll(string userId) { try { if (userId == null) throw new InvalidOperationException("User not found"); if (!UserService.IsExist(_UserDatabaseService, userId)) throw new KeyNotFoundException("User not found"); List providers = ProviderService.GetAll(this._ProviderDatabaseService, userId); List providersDTO = providers.Select(p => p.ToDTO()).ToList(); return new OkObjectResult(providersDTO); } catch (InvalidOperationException ex) { return new BadRequestObjectResult(ex.Message) { StatusCode = 400 }; } catch (KeyNotFoundException ex) { return new BadRequestObjectResult(ex.Message) { StatusCode = 404 }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Create a provider /// /// Provider to create [ProducesResponseType(typeof(ProviderDTO), 200)] [HttpPost] public ObjectResult Create([FromBody] ProviderDTO providerDTO) { try { if (providerDTO.UserId == null) throw new InvalidOperationException("User not found"); if (!UserService.IsExist(_UserDatabaseService, providerDTO.UserId)) throw new KeyNotFoundException("User not found"); if (providerDTO == null) throw new KeyNotFoundException("Provider is null"); if (_ProviderDatabaseService.AlreadyExistForUser(providerDTO.UserId, providerDTO.Name)) throw new ArgumentException("Provider already exists"); ProviderDTO providerCreated = ProviderService.CreateOrUpdate(this._ProviderDatabaseService, providerDTO.UserId, providerDTO, true); return new OkObjectResult(providerCreated); } catch (InvalidOperationException ex) { return new BadRequestObjectResult(ex.Message) { StatusCode = 400 }; } catch (KeyNotFoundException ex) { return new BadRequestObjectResult(ex.Message) { StatusCode = 404 }; } catch (ArgumentException ex) { return new BadRequestObjectResult(ex.Message) { StatusCode = 409 }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Update a provider /// /// Provider to update [ProducesResponseType(typeof(DeviceDetailDTO), 200)] [HttpPut] public ObjectResult Update([FromBody] ProviderDTO providerDTO) { try { if (providerDTO.UserId == null) throw new InvalidOperationException("User not found"); if (!UserService.IsExist(_UserDatabaseService, providerDTO.UserId)) throw new KeyNotFoundException("User not found"); if (!ProviderService.IsExist(this._ProviderDatabaseService, providerDTO.UserId, providerDTO.Id)) throw new KeyNotFoundException("Provider does not exist"); ProviderDTO providerUpdated = ProviderService.CreateOrUpdate(this._ProviderDatabaseService, providerDTO.UserId, providerDTO, false); return new OkObjectResult(providerUpdated); } catch (InvalidOperationException ex) { return new BadRequestObjectResult(ex.Message) { StatusCode = 400 }; } catch (KeyNotFoundException ex) { return new BadRequestObjectResult(ex.Message) { StatusCode = 404 }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Delete a provider /// /// Id of provider to delete [HttpDelete("{providerId}")] public ObjectResult Delete(string providerId) { try { if (providerId == null) throw new InvalidOperationException("Provider is null"); if (!_ProviderDatabaseService.IsExist(providerId)) throw new InvalidOperationException("Provider is null"); _ProviderDatabaseService.Remove(providerId); return new OkObjectResult(201); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } } }