mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
184 lines
6.3 KiB
C#
184 lines
6.3 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.Models;
|
|
using MyCore.Services;
|
|
|
|
namespace MyCore.Controllers
|
|
{
|
|
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
|
[Route("api/device/screen")]
|
|
[ApiController]
|
|
public class ScreenDeviceController : ControllerBase
|
|
{
|
|
private readonly ScreenDeviceDatabaseService _ScreenDeviceDatabaseService;
|
|
|
|
public ScreenDeviceController(ScreenDeviceDatabaseService ScreenDeviceDatabaseService)
|
|
{
|
|
_ScreenDeviceDatabaseService = ScreenDeviceDatabaseService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all screen devices
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(List<ScreenDevice>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet]
|
|
public ObjectResult GetAllScreenDevices()
|
|
{
|
|
try
|
|
{
|
|
List<ScreenDevice> screenDevices = _ScreenDeviceDatabaseService.GetAll();
|
|
|
|
return new OkObjectResult(screenDevices);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get screen device info
|
|
/// </summary>
|
|
/// <param name="screenDeviceId">Id of the screen device you want to get information</param>
|
|
[ProducesResponseType(typeof(ScreenDevice), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{screenDeviceId}")]
|
|
public ObjectResult GetDeviceInfo(string screenDeviceId)
|
|
{
|
|
try
|
|
{
|
|
if (screenDeviceId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
ScreenDevice screenDevice = _ScreenDeviceDatabaseService.GetInfo(screenDeviceId);
|
|
if (screenDevice == null)
|
|
throw new KeyNotFoundException("Screen device not found");
|
|
|
|
return new OkObjectResult(screenDevice);
|
|
}
|
|
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 screen device
|
|
/// </summary>
|
|
/// <param name="screenDevice">Screen device to create</param>
|
|
[ProducesResponseType(typeof(ScreenDevice), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[HttpPost]
|
|
public ObjectResult CreateDevice([FromBody] ScreenDevice screenDevice)
|
|
{
|
|
try
|
|
{
|
|
if (screenDevice == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
_ScreenDeviceDatabaseService.Create(screenDevice);
|
|
|
|
return new OkObjectResult(screenDevice);
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update screen device
|
|
/// </summary>
|
|
/// <param name="screenDevice">Screen device to update</param>
|
|
[ProducesResponseType(typeof(ScreenDevice), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPut()]
|
|
public ObjectResult UpdateDevice([FromBody] ScreenDevice screenDevice)
|
|
{
|
|
try
|
|
{
|
|
if (screenDevice == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!_ScreenDeviceDatabaseService.IsExist(screenDevice.Id))
|
|
throw new KeyNotFoundException("Screen device does not exist");
|
|
|
|
_ScreenDeviceDatabaseService.Update(screenDevice.Id, screenDevice);
|
|
|
|
return new OkObjectResult(screenDevice);
|
|
}
|
|
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 device
|
|
/// </summary>
|
|
/// <param name="deviceId">Screen device id to update</param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpDelete("{deviceId}")]
|
|
public ObjectResult DeleteDevice(string deviceId)
|
|
{
|
|
try
|
|
{
|
|
if (deviceId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!_ScreenDeviceDatabaseService.IsExist(deviceId))
|
|
throw new KeyNotFoundException("Device does not exist");
|
|
|
|
_ScreenDeviceDatabaseService.Remove(deviceId);
|
|
|
|
return new ObjectResult("The screen device has been successfully 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 };
|
|
}
|
|
}
|
|
}
|
|
} |