Website Summary Files Commits Branches @commit @diff @stats
path: root / src / DevHive.Services / Services / TechnologyService.cs
diff options
context:
space:
mode:
Commitf4a70c6430db923af9fa9958a11c2d6612cb52cc (patch)
Author
 Victor S <57849063+transtrike@users.noreply.github.com>
2021-02-05 10:54:49 -0800
Committer
 GitHub <noreply@github.com>
2021-02-05 10:54:49 -0800
Filesca0ea403ba5500df20bc8854ec50529a25c64245 /src/DevHive.Services/Services/TechnologyService.cs
DevHive-f4a70c6430db923af9fa9958a11c2d6612cb52cc.tar
DevHive-f4a70c6430db923af9fa9958a11c2d6612cb52cc.tar.gz
DevHive-f4a70c6430db923af9fa9958a11c2d6612cb52cc.zip
Parent1ccdefdac025b1b986ad2bd0bc3eda7505d6e7c3 (diff)
Parent2269b5aa6c8d3dcb407c34fa256200bdc573585a (diff)
Merge pull request #18 from Team-Kaleidoscope/dev v0.1
First stage: Complete. Awaiting further progress...
1 files changed, 90 insertions, 0 deletions
-rw-r--r--src/DevHive.Services/Services/TechnologyService.cs90
diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs
new file mode 100644
index 0000000..6dd6286
--- /dev/null
+++ b/src/DevHive.Services/Services/TechnologyService.cs
@@ -0,0 +1,90 @@
+using System;
+using System.Collections.Generic;
+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<Guid> CreateTechnology(CreateTechnologyServiceModel technologyServiceModel)
+ {
+ if (await this._technologyRepository.DoesTechnologyNameExistAsync(technologyServiceModel.Name))
+ throw new ArgumentException("Technology already exists!");
+
+ Technology technology = this._technologyMapper.Map<Technology>(technologyServiceModel);
+ bool success = await this._technologyRepository.AddAsync(technology);
+
+ if (success)
+ {
+ Technology newTechnology = await this._technologyRepository.GetByNameAsync(technologyServiceModel.Name);
+ return newTechnology.Id;
+ }
+ else
+ return Guid.Empty;
+ }
+ #endregion
+
+ #region Read
+ public async Task<ReadTechnologyServiceModel> GetTechnologyById(Guid id)
+ {
+ Technology technology = await this._technologyRepository.GetByIdAsync(id);
+
+ if (technology == null)
+ throw new ArgumentException("The technology does not exist");
+
+ return this._technologyMapper.Map<ReadTechnologyServiceModel>(technology);
+ }
+
+ public HashSet<ReadTechnologyServiceModel> GetTechnologies()
+ {
+ HashSet<Technology> technologies = this._technologyRepository.GetTechnologies();
+
+ return this._technologyMapper.Map<HashSet<ReadTechnologyServiceModel>>(technologies);
+ }
+ #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(updateTechnologyServiceModel.Id, technology);
+
+ return result;
+ }
+ #endregion
+
+ #region Delete
+ public async Task<bool> DeleteTechnology(Guid id)
+ {
+ if (!await this._technologyRepository.DoesTechnologyExistAsync(id))
+ throw new ArgumentException("Technology does not exist!");
+
+ Technology technology = await this._technologyRepository.GetByIdAsync(id);
+ bool result = await this._technologyRepository.DeleteAsync(technology);
+
+ return result;
+ }
+ #endregion
+ }
+}