mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
180 lines
6.9 KiB
C#
180 lines
6.9 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.Devices
|
|
{
|
|
//[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
|
|
/// <summary>
|
|
/// Get all user providers
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(List<ProviderDTO>), 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<Provider> providers = ProviderService.GetAll(this._ProviderDatabaseService, userId);
|
|
|
|
List<ProviderDTO> 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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a provider
|
|
/// </summary>
|
|
/// <param name="userId">User Id</param>
|
|
/// <param name="providerDTO">Provider to create</param>
|
|
[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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a provider
|
|
/// </summary>
|
|
/// <param name="userId">User Id</param>
|
|
/// <param name="providerDTO">Provider to update</param>
|
|
[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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete a provider
|
|
/// </summary>
|
|
/// <param name="providerId">Id of provider to delete</param>
|
|
[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 };
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|