mycorerepository/MyCore/Controllers/Devices/ScreenDeviceController.cs
2020-12-16 21:35:51 +01:00

124 lines
3.5 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;
}
// GET: Devices
/// <summary>
///
/// </summary>
[ProducesResponseType(typeof(List<ScreenDevice>), 200)]
[HttpGet]
public ObjectResult GetAllScreenDevices()
{
try
{
List<ScreenDevice> screenDevices = _ScreenDeviceDatabaseService.GetAll();
return new OkObjectResult(screenDevices);
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
// GET: ScreenDevice
/// <summary>
///
/// </summary>
/// <param name="screenDeviceId">Id of the screen device you want to get information</param>
[ProducesResponseType(typeof(ScreenDevice), 200)]
[HttpGet("{screenDeviceId}")]
public ObjectResult GetDeviceInfo(string screenDeviceId)
{
try
{
ScreenDevice screenDevice = _ScreenDeviceDatabaseService.GetInfo(screenDeviceId);
return new OkObjectResult(screenDevice);
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
// POST: Device/Create
/// <summary>
///
/// </summary>
[HttpPost]
public ObjectResult CreateDevice([FromBody] ScreenDevice screenDevice)
{
try
{
_ScreenDeviceDatabaseService.Create(screenDevice);
return new OkObjectResult(201);
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
// PUT: Device/Update
/// <summary>
///
/// </summary>
[HttpPut("{screenDeviceId}")]
public ObjectResult UpdateDevice(int screenDeviceId, [FromBody] ScreenDevice screenDevice)
{
try
{
_ScreenDeviceDatabaseService.Update(screenDevice.Id, screenDevice);
return new OkObjectResult(201);
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
// Delete: Device/Delete
/// <summary>
///
/// </summary>
[HttpDelete("{deviceId}")]
public ObjectResult DeleteDevice(string deviceId)
{
try
{
_ScreenDeviceDatabaseService.Remove(deviceId);
return new OkObjectResult(201);
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
}
}