aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Web/Controllers/TechnologyController.cs
blob: e50789905978beabb7ec67cf411b4cda55560398 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
{
	[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;
		}

		[HttpPost]
		[Authorize(Roles = "Admin")]
		public async Task<IActionResult> Create([FromBody] CreateTechnologyWebModel createTechnologyWebModel)
		{
			CreateTechnologyServiceModel technologyServiceModel = this._technologyMapper.Map<CreateTechnologyServiceModel>(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 });
		}

		[HttpGet]
		[AllowAnonymous]
		public async Task<IActionResult> GetById(Guid id)
		{
			ReadTechnologyServiceModel readTechnologyServiceModel = await this._technologyService.GetTechnologyById(id);
			ReadTechnologyWebModel readTechnologyWebModel = this._technologyMapper.Map<ReadTechnologyWebModel>(readTechnologyServiceModel);

			return new OkObjectResult(readTechnologyWebModel);
		}

		[HttpGet]
		[Route("GetTechnologies")]
		[Authorize(Roles = "User,Admin")]
		public IActionResult GetAllExistingTechnologies()
		{
			HashSet<ReadTechnologyServiceModel> technologyServiceModels = this._technologyService.GetTechnologies();
			HashSet<ReadTechnologyWebModel> languageWebModels = this._technologyMapper.Map<HashSet<ReadTechnologyWebModel>>(technologyServiceModels);

			return new OkObjectResult(languageWebModels);
		}

		[HttpPut]
		[Authorize(Roles = "Admin")]
		public async Task<IActionResult> Update(Guid id, [FromBody] UpdateTechnologyWebModel updateModel)
		{
			UpdateTechnologyServiceModel updateTechnologyServiceModel = this._technologyMapper.Map<UpdateTechnologyServiceModel>(updateModel);
			updateTechnologyServiceModel.Id = id;

			bool result = await this._technologyService.UpdateTechnology(updateTechnologyServiceModel);

			if (!result)
				return new BadRequestObjectResult("Could not update Technology");

			return new OkResult();
		}

		[HttpDelete]
		[Authorize(Roles = "Admin")]
		public async Task<IActionResult> Delete(Guid id)
		{
			bool result = await this._technologyService.DeleteTechnology(id);

			if (!result)
				return new BadRequestObjectResult("Could not delete Technology");

			return new OkResult();
		}
	}
}