aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services/Services/TechnologyService.cs
blob: c0882819c88630c47579cf648db7c3964eec3ee5 (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
using System;
using System.Threading.Tasks;
using AutoMapper;
using DevHive.Data.Interfaces.Repositories;
using DevHive.Data.Models;
using DevHive.Services.Interfaces;
using DevHive.Services.Models.Technology;

namespace DevHive.Services.Services
{
	public class TechnologyService : ITechnologyService
	{
		private readonly ITechnologyRepository _technologyRepository;
		private readonly IMapper _technologyMapper;

		public TechnologyService(ITechnologyRepository technologyRepository, IMapper technologyMapper)
		{
			this._technologyRepository = technologyRepository;
			this._technologyMapper = technologyMapper;
		}

		#region Create

		public async Task<bool> Create(CreateTechnologyServiceModel technologyServiceModel)
		{
			if (await this._technologyRepository.DoesTechnologyNameExistAsync(technologyServiceModel.Name))
				throw new ArgumentException("Technology already exists!");

			Technology technology = this._technologyMapper.Map<Technology>(technologyServiceModel);
			bool result = await this._technologyRepository.AddAsync(technology);

			return result;
		}
		#endregion

		#region Read

		public async Task<CreateTechnologyServiceModel> GetTechnologyById(Guid technologyId)
		{
			Technology technology = await this._technologyRepository.GetByIdAsync(technologyId);

			if (technology == null)
				throw new ArgumentException("The technology does not exist");

			return this._technologyMapper.Map<CreateTechnologyServiceModel>(technology);
		}
		#endregion

		#region Update

		public async Task<bool> UpdateTechnology(UpdateTechnologyServiceModel updateTechnologyServiceModel)
		{
			if (!await this._technologyRepository.DoesTechnologyExistAsync(updateTechnologyServiceModel.Id))
				throw new ArgumentException("Technology does not exist!");

			if (await this._technologyRepository.DoesTechnologyNameExistAsync(updateTechnologyServiceModel.Name))
				throw new ArgumentException("Technology name already exists!");

			Technology technology = this._technologyMapper.Map<Technology>(updateTechnologyServiceModel);
			bool result = await this._technologyRepository.EditAsync(technology);

			return result;
		}
		#endregion

		#region Delete
		public async Task<bool> DeleteTechnology(Guid technologyId)
		{
			if (!await this._technologyRepository.DoesTechnologyExistAsync(technologyId))
				throw new ArgumentException("Technology does not exist!");

			Technology technology = await this._technologyRepository.GetByIdAsync(technologyId);
			bool result = await this._technologyRepository.DeleteAsync(technology);

			return result;
		}
		#endregion
	}
}