mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
208 lines
7.6 KiB
C#
208 lines
7.6 KiB
C#
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 RoomDatabaseService _RoomDatabaseService;
|
|
private HomeDatabaseService _HomeDatabaseService;
|
|
|
|
public ProviderController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, RoomDatabaseService RoomDatabaseService, HomeDatabaseService HomeDatabaseService)
|
|
{
|
|
this._DeviceDatabaseService = DeviceDatabaseService;
|
|
this._ProviderDatabaseService = ProviderDatabaseService;
|
|
this._RoomDatabaseService = RoomDatabaseService;
|
|
this._HomeDatabaseService = HomeDatabaseService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all home providers
|
|
/// </summary>
|
|
/// <param name="homeId">Home Id</param>
|
|
[ProducesResponseType(typeof(List<ProviderDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{homeId}")]
|
|
public ObjectResult GetAll(string homeId)
|
|
{
|
|
try
|
|
{
|
|
if (homeId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!HomeService.IsExist(_HomeDatabaseService, homeId))
|
|
throw new KeyNotFoundException("Home not found");
|
|
|
|
List<Provider> providers = ProviderService.GetAll(this._ProviderDatabaseService, homeId);
|
|
|
|
List<ProviderDTO> providersDTO = providers.Select(p => p.ToDTO()).ToList();
|
|
|
|
return new OkObjectResult(providersDTO);
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a provider
|
|
/// </summary>
|
|
/// <param name="providerDTO">Provider to create</param>
|
|
[ProducesResponseType(typeof(ProviderDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 409)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPost]
|
|
public ObjectResult Create([FromBody] ProviderDTO providerDTO)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (providerDTO == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (providerDTO.HomeId == null)
|
|
throw new ArgumentNullException("Home not found");
|
|
|
|
if (!HomeService.IsExist(_HomeDatabaseService, providerDTO.HomeId))
|
|
throw new KeyNotFoundException("Home not found");
|
|
|
|
if (_ProviderDatabaseService.AlreadyExistForHome(providerDTO.HomeId, providerDTO.Name))
|
|
throw new InvalidOperationException("Provider already exists");
|
|
|
|
ProviderDTO providerCreated = ProviderService.CreateOrUpdate(this._ProviderDatabaseService, providerDTO.HomeId, providerDTO, true);
|
|
|
|
return new OkObjectResult(providerCreated);
|
|
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) {};
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return new ConflictObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a provider
|
|
/// </summary>
|
|
/// <param name="providerDTO">Provider to update</param>
|
|
[ProducesResponseType(typeof(DeviceDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPut]
|
|
public ObjectResult Update([FromBody] ProviderDTO providerDTO)
|
|
{
|
|
try
|
|
{
|
|
if (providerDTO == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (providerDTO.HomeId == null)
|
|
throw new ArgumentNullException("Home not found");
|
|
|
|
if (!HomeService.IsExist(_HomeDatabaseService, providerDTO.HomeId))
|
|
throw new KeyNotFoundException("Home not found");
|
|
|
|
if (!ProviderService.IsExist(this._ProviderDatabaseService, providerDTO.HomeId, providerDTO.Id))
|
|
throw new KeyNotFoundException("Provider does not exist");
|
|
|
|
ProviderDTO providerUpdated = ProviderService.CreateOrUpdate(this._ProviderDatabaseService, providerDTO.HomeId, providerDTO, false);
|
|
|
|
return new OkObjectResult(providerUpdated);
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete a provider
|
|
/// </summary>
|
|
/// <param name="providerId">Id of provider to delete</param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpDelete("{providerId}")]
|
|
public ObjectResult Delete(string providerId)
|
|
{
|
|
try
|
|
{
|
|
// TODO DELETE ALL DEVICES linked
|
|
if (providerId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!_ProviderDatabaseService.IsExist(providerId))
|
|
throw new KeyNotFoundException("Provider is null");
|
|
|
|
_ProviderDatabaseService.Remove(providerId);
|
|
|
|
return new ObjectResult("The provider has been deleted") { StatusCode = 202 };
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|