mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
155 lines
5.0 KiB
C#
155 lines
5.0 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.DTO.Common;
|
|
using MyCore.DTO.MyControlPanel;
|
|
using MyCore.Models;
|
|
using MyCore.Models.MyControlPanel;
|
|
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 readonly DeviceDatabaseService _DeviceDatabaseService;
|
|
private readonly ProviderDatabaseService _ProviderDatabaseService;
|
|
private readonly LocationDatabaseService _LocationDatabaseService;
|
|
|
|
public DeviceController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService)
|
|
{
|
|
_DeviceDatabaseService = DeviceDatabaseService;
|
|
_ProviderDatabaseService = ProviderDatabaseService;
|
|
_LocationDatabaseService = LocationDatabaseService;
|
|
}
|
|
|
|
// GET: Devices
|
|
/// <summary>
|
|
/// Get all devices summary
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(List<DeviceSummaryDTO>), 200)]
|
|
[HttpGet]
|
|
public ObjectResult GetAll()
|
|
{
|
|
try
|
|
{
|
|
List<Device> Devices = _DeviceDatabaseService.GetAll();
|
|
|
|
List<DeviceSummaryDTO> devicesSummaryDTO = Devices.Select(d => d.ToSummaryDTO()).ToList();
|
|
|
|
return new OkObjectResult(devicesSummaryDTO);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a specific device info
|
|
/// </summary>
|
|
/// <param name="deviceId">id of device</param>
|
|
[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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a device
|
|
/// </summary>
|
|
/// <param name="deviceDetailDTO">Device to create</param>
|
|
[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(deviceDetailDTO, true);
|
|
|
|
return new OkObjectResult(deviceCreated);
|
|
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a device
|
|
/// </summary>
|
|
/// <param name="deviceId">Device Id</param>
|
|
/// <param name="deviceDetailDTO">Device to update</param>
|
|
[ProducesResponseType(typeof(DeviceDetailDTO), 200)]
|
|
[HttpPut("{deviceId}")]
|
|
public ObjectResult Update(string deviceId, [FromBody] DeviceDetailDTO deviceDetailDTO)
|
|
{
|
|
try
|
|
{
|
|
if (!_DeviceDatabaseService.IsExist(deviceId))
|
|
throw new KeyNotFoundException("Location does not exist");
|
|
|
|
DeviceDetailDTO deviceUpdated = DeviceService.CreateOrUpdate(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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete a device
|
|
/// </summary>
|
|
/// <param name="deviceId">Id of device to delete</param>
|
|
[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 };
|
|
}
|
|
}
|
|
}
|
|
}
|