2020-11-01 12:25:45 +01:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
using Birdmap.API.DTOs;
|
|
|
|
|
using Birdmap.BLL.Interfaces;
|
|
|
|
|
using Birdmap.DAL.Entities;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Birdmap.API.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Authorize]
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
public class ServiceController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceService _service;
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
private readonly ILogger<ServiceController> _logger;
|
|
|
|
|
|
|
|
|
|
public ServiceController(IServiceService service, IMapper mapper, ILogger<ServiceController> logger)
|
|
|
|
|
{
|
|
|
|
|
_service = service;
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
public async Task<ActionResult<List<ServiceInfo>>> GetAsync()
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"Getting all services from db...");
|
|
|
|
|
var serviceInfos = (await _service.GetAllServicesAsync())
|
|
|
|
|
.Select(s => new ServiceInfo { Service = _mapper.Map<ServiceRequest>(s) });
|
|
|
|
|
|
|
|
|
|
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-01 12:25:45 +01:00
|
|
|
|
si.StatusCode = System.Net.HttpStatusCode.ServiceUnavailable;
|
|
|
|
|
si.Response = ex.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return serviceInfos.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
|
|
|
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}].");
|
|
|
|
|
|
|
|
|
|
return CreatedAtAction(
|
|
|
|
|
nameof(GetAsync),
|
|
|
|
|
_mapper.Map<ServiceRequest>(created));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPut]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
|
public async Task<IActionResult> DeleteAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"Deleting service [{id}]...");
|
|
|
|
|
|
|
|
|
|
await _service.DeleteServiceAsync(id);
|
|
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|