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]
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
///
/// User Id
/// Provider to create
[ProducesResponseType(typeof(ProviderDTO), 200)]
[HttpPost]
public ObjectResult Create(string userId, [FromBody] ProviderDTO providerDTO)
{
try
{
if (userId == null)
throw new InvalidOperationException("User not found");
if (!UserService.IsExist(_UserDatabaseService, userId))
throw new KeyNotFoundException("User not found");
if (providerDTO == null)
throw new KeyNotFoundException("Provider is null");
if (_ProviderDatabaseService.AlreadyExistForUser(userId, providerDTO.Name))
throw new ArgumentException("Provider already exists");
ProviderDTO providerCreated = ProviderService.CreateOrUpdate(this._ProviderDatabaseService, 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
///
/// User Id
/// Provider to update
[ProducesResponseType(typeof(DeviceDetailDTO), 200)]
[HttpPut("{deviceId}")]
public ObjectResult Update(string userId, [FromBody] ProviderDTO providerDTO)
{
try
{
if (userId == null)
throw new InvalidOperationException("User not found");
if (!UserService.IsExist(_UserDatabaseService, userId))
throw new KeyNotFoundException("User not found");
if (!ProviderService.IsExist(this._ProviderDatabaseService, userId, providerDTO.Id))
throw new KeyNotFoundException("Provider does not exist");
ProviderDTO providerUpdated = ProviderService.CreateOrUpdate(this._ProviderDatabaseService, 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
{
// Check if exist
// TODO
// ProviderDatabaseService.Remove(providerId);
return new OkObjectResult(201);
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
}
}