using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Security.Authentication; 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.Models.MyControlPanel.Database; 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 DeviceDatabaseService _DeviceDatabaseService; private ProviderDatabaseService _ProviderDatabaseService; private LocationDatabaseService _LocationDatabaseService; private UserDatabaseService _UserDatabaseService; public DeviceController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService) { this._DeviceDatabaseService = DeviceDatabaseService; this._ProviderDatabaseService = ProviderDatabaseService; this._LocationDatabaseService = LocationDatabaseService; this._UserDatabaseService = UserDatabaseService; } // GET: Devices /// /// Get all devices summary /// [ProducesResponseType(typeof(List), 200)] [HttpGet] public ObjectResult GetAll() { try { List Devices = _DeviceDatabaseService.GetAll(); List devicesSummaryDTO = Devices.Select(d => d.ToSummaryDTO()).ToList(); return new OkObjectResult(devicesSummaryDTO); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Get a specific device info /// /// id of device [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 }; } } /// /// Create a device /// /// Device to create [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(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, deviceDetailDTO, true); return new OkObjectResult(deviceCreated); } catch (DuplicateWaitObjectException 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 devices from provider /// /// User Id /// Id of Provider [ProducesResponseType(typeof(List), 200)] [HttpPost("fromProvider/{userId}")] public async Task CreateDevicesFromProvider(string userId, string providerId) { try { if (userId == null) throw new InvalidOperationException("User not found"); if (!UserService.IsExist(_UserDatabaseService, userId)) throw new KeyNotFoundException("User not found"); Provider provider = ProviderService.GetProviderById(this._ProviderDatabaseService, userId, providerId); if (provider == null) throw new KeyNotFoundException("Provider id is null"); List devicesCreated = await DeviceService.CreateFromProvider(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, provider); return new OkObjectResult(devicesCreated); } catch (InvalidOperationException ex) { return new BadRequestObjectResult(ex.Message) { StatusCode = 400 }; } catch (KeyNotFoundException ex) { return new BadRequestObjectResult(ex.Message) { StatusCode = 404 }; } catch (AuthenticationException ex) { return new BadRequestObjectResult(ex.Message) { StatusCode = 401 }; } catch (HttpRequestException ex) // Not ok if not http request.. { return new BadRequestObjectResult(ex.Message) { StatusCode = 421 }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Update a device /// /// Device Id /// Device to update [ProducesResponseType(typeof(DeviceDetailDTO), 200)] [HttpPut("{deviceId}")] public ObjectResult Update(string deviceId, [FromBody] DeviceDetailDTO deviceDetailDTO) { try { if (!_DeviceDatabaseService.IsExist(deviceId)) throw new KeyNotFoundException("Device does not exist"); DeviceDetailDTO deviceUpdated = DeviceService.CreateOrUpdate(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, 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 }; } } /// /// Delete a device /// /// Id of device to delete [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 }; } } } }