using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using DevHive.Services.Interfaces;
using DevHive.Services.Models.Technology;
using DevHive.Web.Models.Technology;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace DevHive.Web.Controllers
{
///
/// All endpoints for interacting with the technology layer
///
[ApiController]
[Route("/api/[controller]")]
public class TechnologyController
{
private readonly ITechnologyService _technologyService;
private readonly IMapper _technologyMapper;
public TechnologyController(ITechnologyService technologyService, IMapper technologyMapper)
{
this._technologyService = technologyService;
this._technologyMapper = technologyMapper;
}
///
/// Create a new technology, so users can have a choice. Admin only!
///
/// Data for the new technology
/// The new technology's Id
[HttpPost]
[Authorize(Roles = "Admin")]
public async Task Create([FromBody] CreateTechnologyWebModel createTechnologyWebModel)
{
CreateTechnologyServiceModel technologyServiceModel = this._technologyMapper.Map(createTechnologyWebModel);
Guid id = await this._technologyService.CreateTechnology(technologyServiceModel);
return id == Guid.Empty ?
new BadRequestObjectResult($"Could not create technology {createTechnologyWebModel.Name}") :
new OkObjectResult(new { Id = id });
}
///
/// Get technology's data by it's Id
///
/// The technology's Id
/// The technology's full data
[HttpGet]
[AllowAnonymous]
public async Task GetById(Guid id)
{
ReadTechnologyServiceModel readTechnologyServiceModel = await this._technologyService.GetTechnologyById(id);
ReadTechnologyWebModel readTechnologyWebModel = this._technologyMapper.Map(readTechnologyServiceModel);
return new OkObjectResult(readTechnologyWebModel);
}
///
/// Get all technologies from our database
///
/// All technologies
[HttpGet]
[Route("GetTechnologies")]
[Authorize(Roles = "User,Admin")]
public IActionResult GetAllExistingTechnologies()
{
HashSet technologyServiceModels = this._technologyService.GetTechnologies();
HashSet languageWebModels = this._technologyMapper.Map>(technologyServiceModels);
return new OkObjectResult(languageWebModels);
}
///
/// Alter a technology's parameters. Admin only!
///
/// Technology's Id
/// The new parametars
/// Ok result
[HttpPut]
[Authorize(Roles = "Admin")]
public async Task Update(Guid id, [FromBody] UpdateTechnologyWebModel updateModel)
{
UpdateTechnologyServiceModel updateTechnologyServiceModel = this._technologyMapper.Map(updateModel);
updateTechnologyServiceModel.Id = id;
bool result = await this._technologyService.UpdateTechnology(updateTechnologyServiceModel);
if (!result)
return new BadRequestObjectResult("Could not update Technology");
return new OkResult();
}
///
/// Delete a etchnology from the database. Admin only!
///
/// The technology's Id
/// Ok result
[HttpDelete]
[Authorize(Roles = "Admin")]
public async Task Delete(Guid id)
{
bool result = await this._technologyService.DeleteTechnology(id);
if (!result)
return new BadRequestObjectResult("Could not delete Technology");
return new OkResult();
}
}
}