Cucumber/Cloud/Controllers/GreengouseController.cs

160 lines
5.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Cloud.Models;
using Cloud.Requests;
using Cloud.Services.Broker;
using Cloud.Services.Broker.Support;
using Cloud.Services.Domain;
using Microsoft.AspNetCore.Mvc;
namespace Cloud.Controllers
{
[ApiController]
[Route("api/farm/{farmId}/greenhouse")]
public class GreenhouseController : ControllerBase
{
private readonly IGreenhouseService _greenhouseService;
public GreenhouseController(IGreenhouseService greenhouseService)
{
_greenhouseService = greenhouseService;
}
/// <summary>
/// Возвращает текущую информацию о всех теплицах пользователя
/// </summary>
/// <param name="farmId"></param>
/// <returns></returns>
[HttpGet]
public async Task<ActionResult<List<GreenhouseInfo>>> GetAll(int farmId)
{
try
{
var greenhouses = _greenhouseService.GetAll(farmId);
if (greenhouses == null) return NotFound("Greenhouses is not found");
return Ok(greenhouses);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
/// <summary>
/// Возвращает текущую информацию о конкретной теплице
/// </summary>
/// <param name="farmId"></param>
/// <param name="greenhouseId"></param>
/// <returns></returns>
[HttpGet("{greenhouseId}")]
public async Task<ActionResult<GreenhouseInfo>> Get(int farmId, int greenhouseId)
{
try
{
var greenhouses = _greenhouseService.GetGreenhouseInfo(greenhouseId, farmId);
return Ok(greenhouses);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
/// <summary>
/// Возвращает сохраненные данные для автоматизации теплицы
/// </summary>
/// <param name="farmId"></param>
/// <param name="greenhouseId"></param>
/// <returns></returns>
[HttpGet("{greenhouseId}/settings")]
public async Task<ActionResult<Greenhouse>> GetGreenhouse(int farmId, int greenhouseId)
{
try
{
var greenhouse = await _greenhouseService.GetGreenhouse(greenhouseId);
if (greenhouse == null) return NotFound("Greenhouses is not found");
return Ok(greenhouse);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
/// <summary>
/// Сохраняет в базе данных API данные для автоматизации теплицы
/// </summary>
/// <param name="farmId"></param>
/// <param name="greenhouse"></param>
/// <returns></returns>
[HttpPost]
public async Task<ActionResult<Greenhouse>> SaveToDatabase(int farmId, GreenhouseRequest greenhouse)
{
try
{
var greenhouseEntity = new Greenhouse()
{
RecomendedTemperature = greenhouse.RecomendedTemperature,
WateringMode = greenhouse.WateringMode,
HeatingMode = greenhouse.HeatingMode
};
var result = await _greenhouseService.Create(greenhouseEntity);
return Ok(result);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
/// <summary>
/// Обновляет в базе данных API данные для автоматизации теплицы
/// </summary>
/// <param name="farmId">ID фермы</param>
/// <param name="greenhouseId">ID теплицы</param>
/// <param name="greenhouse">Данные для обновления</param>
/// <returns>Обновленный объект Greenhouse</returns>
[HttpPut("{greenhouseId}/settings")]
public async Task<ActionResult<Greenhouse>> Update(int farmId, int greenhouseId, GreenhouseRequest greenhouse)
{
try
{
var greenhouseEntity = new Greenhouse()
{
Id = greenhouseId,
FarmId = farmId,
WateringMode = greenhouse.WateringMode,
HeatingMode = greenhouse.HeatingMode,
RecomendedTemperature = greenhouse.RecomendedTemperature
};
var result = await _greenhouseService.Update(greenhouseEntity);
return Ok(result);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
/// <summary>
/// Удаляет из базы данных API запись настроек автоматизации теплицы
/// </summary>
/// <param name="farmId"></param>
/// <param name="greenhouseId"></param>
/// <returns></returns>
[HttpDelete("{greenhouseId}")]
public async Task<ActionResult> Delete(int farmId, int greenhouseId)
{
try
{
_ = await _greenhouseService.Delete(greenhouseId);
return Ok();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
}
}