2020-11-01 12:25:45 +01:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
using Birdmap.API.DTOs;
|
|
|
|
|
using Birdmap.BLL.Interfaces;
|
2020-12-03 18:52:25 +01:00
|
|
|
|
using Birdmap.BLL.Services.CommunicationServices.Hubs;
|
|
|
|
|
using Birdmap.BLL.Services.CommunicationServices.Mqtt;
|
2020-11-01 12:25:45 +01:00
|
|
|
|
using Birdmap.DAL.Entities;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-11-19 20:43:01 +01:00
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
2020-11-01 12:25:45 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-11-19 20:43:01 +01:00
|
|
|
|
using System.Net;
|
2020-11-01 12:25:45 +01:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Birdmap.API.Controllers
|
|
|
|
|
{
|
2020-11-11 16:55:50 +01:00
|
|
|
|
[Authorize(Roles = "User, Admin")]
|
2020-11-01 12:25:45 +01:00
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
2020-11-01 14:08:08 +01:00
|
|
|
|
public class ServicesController : ControllerBase
|
2020-11-01 12:25:45 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly IServiceService _service;
|
|
|
|
|
private readonly IMapper _mapper;
|
2020-11-19 20:43:01 +01:00
|
|
|
|
private readonly IMqttClientService _mqttClientService;
|
|
|
|
|
private readonly IHubContext<ServicesHub, IServicesHubClient> _hubContext;
|
2020-11-01 14:08:08 +01:00
|
|
|
|
private readonly ILogger<ServicesController> _logger;
|
2020-11-01 12:25:45 +01:00
|
|
|
|
|
2020-11-19 20:43:01 +01:00
|
|
|
|
public ServicesController(IServiceService service, IMapper mapper, MqttClientServiceProvider mqttClientProvider,
|
|
|
|
|
IHubContext<ServicesHub, IServicesHubClient> hubContext, ILogger<ServicesController> logger)
|
2020-11-01 12:25:45 +01:00
|
|
|
|
{
|
|
|
|
|
_service = service;
|
|
|
|
|
_mapper = mapper;
|
2020-11-19 20:43:01 +01:00
|
|
|
|
_mqttClientService = mqttClientProvider.MqttClientService;
|
|
|
|
|
_hubContext = hubContext;
|
2020-11-01 12:25:45 +01:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 20:43:01 +01:00
|
|
|
|
[HttpGet("count"), ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
public async Task<ActionResult<int>> GetCountAsync()
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"Getting service count from db...");
|
|
|
|
|
|
|
|
|
|
return await _service.GetServiceCountAsync() + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 14:19:29 +01:00
|
|
|
|
[HttpGet, ProducesResponseType(StatusCodes.Status200OK)]
|
2020-11-01 12:25:45 +01:00
|
|
|
|
public async Task<ActionResult<List<ServiceInfo>>> GetAsync()
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"Getting all services from db...");
|
|
|
|
|
var serviceInfos = (await _service.GetAllServicesAsync())
|
2020-11-19 20:43:01 +01:00
|
|
|
|
.Select(s => new ServiceInfo { Service = _mapper.Map<ServiceRequest>(s) }).ToList();
|
2020-11-01 12:25:45 +01:00
|
|
|
|
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
foreach (var si in serviceInfos)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-11-01 12:28:33 +01:00
|
|
|
|
_logger.LogInformation($"Sending a request to service [{si.Service.Name}] with url [{si.Service.Uri}]...");
|
2020-11-01 12:25:45 +01:00
|
|
|
|
var response = await client.GetAsync(si.Service.Uri);
|
|
|
|
|
si.StatusCode = response.StatusCode;
|
|
|
|
|
si.Response = await response.Content.ReadAsStringAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-11-01 12:28:33 +01:00
|
|
|
|
_logger.LogWarning($"Requesting service [{si.Service.Name}] faulted.");
|
2020-11-19 20:43:01 +01:00
|
|
|
|
si.StatusCode = HttpStatusCode.ServiceUnavailable;
|
2020-11-01 12:25:45 +01:00
|
|
|
|
si.Response = ex.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 20:43:01 +01:00
|
|
|
|
serviceInfos.Add(new()
|
|
|
|
|
{
|
|
|
|
|
Service = new()
|
|
|
|
|
{
|
|
|
|
|
Id = 0,
|
|
|
|
|
Name = "Mqtt Client Service",
|
|
|
|
|
Uri = "localhost",
|
|
|
|
|
},
|
|
|
|
|
Response = $"IsConnected: {_mqttClientService.IsConnected}",
|
|
|
|
|
StatusCode = _mqttClientService.IsConnected ? HttpStatusCode.OK : HttpStatusCode.ServiceUnavailable,
|
|
|
|
|
});
|
|
|
|
|
|
2020-11-01 12:25:45 +01:00
|
|
|
|
return serviceInfos.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 16:55:50 +01:00
|
|
|
|
[Authorize(Roles = "Admin")]
|
2020-11-07 14:19:29 +01:00
|
|
|
|
[HttpPost, ProducesResponseType(StatusCodes.Status201Created)]
|
2020-11-01 12:25:45 +01:00
|
|
|
|
public async Task<ActionResult<ServiceRequest>> PostAsync(ServiceRequest request)
|
|
|
|
|
{
|
2020-11-01 12:28:33 +01:00
|
|
|
|
_logger.LogInformation($"Creating service [{request.Name}]...");
|
2020-11-01 12:25:45 +01:00
|
|
|
|
var created = await _service.CreateServiceAsync(
|
|
|
|
|
_mapper.Map<Service>(request));
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation($"Created service [{created.Id}].");
|
2020-11-19 20:43:01 +01:00
|
|
|
|
await _hubContext.Clients.All.NotifyUpdatedAsync();
|
2020-11-01 12:25:45 +01:00
|
|
|
|
|
|
|
|
|
return CreatedAtAction(
|
|
|
|
|
nameof(GetAsync),
|
|
|
|
|
_mapper.Map<ServiceRequest>(created));
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 16:55:50 +01:00
|
|
|
|
[Authorize(Roles = "Admin")]
|
2020-11-07 14:19:29 +01:00
|
|
|
|
[HttpPut, ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-11-01 12:25:45 +01:00
|
|
|
|
public async Task<IActionResult> PutAsync(ServiceRequest request)
|
|
|
|
|
{
|
2020-11-01 12:28:33 +01:00
|
|
|
|
_logger.LogInformation($"Updating service [{request.Name}]...");
|
2020-11-01 12:25:45 +01:00
|
|
|
|
var service = _mapper.Map<Service>(request);
|
|
|
|
|
service.IsFromConfig = false;
|
|
|
|
|
|
|
|
|
|
await _service.UpdateServiceAsync(service);
|
2020-11-19 20:43:01 +01:00
|
|
|
|
await _hubContext.Clients.All.NotifyUpdatedAsync();
|
2020-11-01 12:25:45 +01:00
|
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 16:55:50 +01:00
|
|
|
|
[Authorize(Roles = "Admin")]
|
2020-11-07 14:19:29 +01:00
|
|
|
|
[HttpDelete("{id}"), ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-11-01 12:25:45 +01:00
|
|
|
|
public async Task<IActionResult> DeleteAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"Deleting service [{id}]...");
|
|
|
|
|
|
|
|
|
|
await _service.DeleteServiceAsync(id);
|
2020-11-19 20:43:01 +01:00
|
|
|
|
await _hubContext.Clients.All.NotifyUpdatedAsync();
|
2020-11-01 12:25:45 +01:00
|
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|