mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
181 lines
6.0 KiB
C#
181 lines
6.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Security.Authentication;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using MongoDB.Bson;
|
|
using Mqtt.Client.AspNetCore.Services;
|
|
using MyCore.Interfaces.DTO;
|
|
using MyCore.Interfaces.Models;
|
|
using MyCore.Services;
|
|
using MyCore.Services.Devices;
|
|
using MyCore.Services.MyControlPanel;
|
|
|
|
namespace MyCore.Service.Controllers
|
|
{
|
|
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
|
[Route("api/room")]
|
|
[ApiController]
|
|
public class RoomController : ControllerBase
|
|
{
|
|
private UserDatabaseService _UserDatabaseService;
|
|
private RoomDatabaseService _RoomDatabaseService;
|
|
private readonly IMqttClientService _mqttClientService;
|
|
private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
|
|
|
public RoomController(UserDatabaseService userDatabaseService, RoomDatabaseService roomDatabaseService, MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider)
|
|
{
|
|
this._UserDatabaseService = userDatabaseService;
|
|
this._RoomDatabaseService = roomDatabaseService;
|
|
this._mqttClientService = provider.MqttClientService;
|
|
this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all rooms for the specified user
|
|
/// </summary>
|
|
/// <param name="userId">Id of user</param>
|
|
[ProducesResponseType(typeof(List<RoomSummaryDTO>), 200)]
|
|
[HttpGet("{userId}")]
|
|
public ObjectResult GetAll(string userId)
|
|
{
|
|
try
|
|
{
|
|
List<Room> Rooms = _RoomDatabaseService.GetAll(userId);
|
|
|
|
List<RoomSummaryDTO> roomsSummaryDTO = Rooms.Select(d => d.ToSummaryDTO()).ToList();
|
|
|
|
return new OkObjectResult(roomsSummaryDTO);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get detail info of a specified room
|
|
/// </summary>
|
|
/// <param name="roomId">room id</param>
|
|
[ProducesResponseType(typeof(RoomDetailDTO), 200)]
|
|
[HttpGet("detail/{roomId}")]
|
|
public ObjectResult GetDetail(string roomId)
|
|
{
|
|
try
|
|
{
|
|
Room room = _RoomDatabaseService.GetById(roomId);
|
|
|
|
return new OkObjectResult(room.ToDTO());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a room
|
|
/// </summary>
|
|
/// <param name="roomCreateOrUpdateDetail">Room to create</param>
|
|
[ProducesResponseType(typeof(RoomDetailDTO), 200)]
|
|
[HttpPost]
|
|
public ObjectResult Create([FromBody] RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetail)
|
|
{
|
|
try
|
|
{
|
|
if (roomCreateOrUpdateDetail == null)
|
|
throw new KeyNotFoundException("Room is null");
|
|
|
|
RoomDetailDTO roomCreated = RoomService.CreateOrUpdate(this._RoomDatabaseService, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, true);
|
|
|
|
return new OkObjectResult(roomCreated);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a room
|
|
/// </summary>
|
|
/// <param name="roomCreateOrUpdateDetail">room to update</param>
|
|
[ProducesResponseType(typeof(RoomCreateOrUpdateDetailDTO), 200)]
|
|
[HttpPut]
|
|
public ObjectResult Update([FromBody] RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetail)
|
|
{
|
|
try
|
|
{
|
|
if (!_RoomDatabaseService.IsExist(roomCreateOrUpdateDetail.Id))
|
|
throw new KeyNotFoundException("Room does not exist");
|
|
|
|
RoomDetailDTO roomUpdated = RoomService.CreateOrUpdate(this._RoomDatabaseService, roomCreateOrUpdateDetail.UserId, roomCreateOrUpdateDetail, false);
|
|
|
|
return new OkObjectResult(roomUpdated);
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete a room
|
|
/// </summary>
|
|
/// <param name="roomId">Id of room to delete</param>
|
|
[HttpDelete("{roomId}")]
|
|
public ObjectResult Delete(string roomId)
|
|
{
|
|
try
|
|
{
|
|
if (roomId != null)
|
|
{
|
|
if (_RoomDatabaseService.IsExist(roomId))
|
|
{
|
|
_RoomDatabaseService.Remove(roomId);
|
|
}
|
|
}
|
|
|
|
return new OkObjectResult(201);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete all room for a specified
|
|
/// </summary>
|
|
/// <param name="userId">Id of user</param>
|
|
[HttpDelete("user/{userId}")]
|
|
public ObjectResult DeleteAllForUser(string userId)
|
|
{
|
|
try
|
|
{
|
|
if (userId != null)
|
|
{
|
|
if (_UserDatabaseService.IsExist(userId))
|
|
{
|
|
_RoomDatabaseService.RemoveForUser(userId);
|
|
}
|
|
}
|
|
|
|
return new OkObjectResult(201);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
}
|
|
}
|