From 14db95231af5034b26e782f2f2ff04bd4792ff75 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Fri, 18 Dec 2020 09:10:00 +0200 Subject: Added: TechnologyService, Technology Service models, Technology mappings for the service and fixed laguage mappings for the service --- src/DevHive.Services/Services/TechnologyService.cs | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/DevHive.Services/Services/TechnologyService.cs (limited to 'src/DevHive.Services/Services/TechnologyService.cs') diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs new file mode 100644 index 0000000..5617e9b --- /dev/null +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -0,0 +1,67 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Models; +using DevHive.Data.Repositories; +using DevHive.Services.Models.Technology; + +namespace DevHive.Services.Services +{ + public class TechnologyService + { + private readonly TechnologyRepository _technologyRepository; + private readonly IMapper _technologyMapper; + + public TechnologyService(DevHiveContext context, IMapper technologyMapper) + { + this._technologyRepository = new TechnologyRepository(context); + this._technologyMapper = technologyMapper; + } + + public async Task Create(TechnologyServiceModel technologyServiceModel) + { + if (!await this._technologyRepository.DoesTechnologyNameExist(technologyServiceModel.Name)) + throw new ArgumentException("Technology already exists!"); + + Technology technology = this._technologyMapper.Map(technologyServiceModel); + bool result = await this._technologyRepository.AddAsync(technology); + + return result; + } + + public async Task 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(technology); + } + + public async Task UpdateTechnology(UpdateTechnologyServiceModel updateTechnologyServiceModel) + { + if (!await this._technologyRepository.DoesTechnologyExist(updateTechnologyServiceModel.Id)) + throw new ArgumentException("Technology does not exist!"); + + if (!await this._technologyRepository.DoesTechnologyNameExist(updateTechnologyServiceModel.Name)) + throw new ArgumentException("Technology name already exists!"); + + Technology technology = this._technologyMapper.Map(updateTechnologyServiceModel); + bool result = await this._technologyRepository.EditAsync(technology); + + return result; + } + + public async Task DeleteLanguage(Guid id) + { + if (!await this._technologyRepository.DoesTechnologyExist(id)) + throw new ArgumentException("Technology does not exist!"); + + Technology language = await this._technologyRepository.GetByIdAsync(id); + bool result = await this._technologyRepository.DeleteAsync(language); + + return result; + } + } +} \ No newline at end of file -- cgit v1.2.3 From 7acec816fe021db4a3922b70ce9b3f50e7600c79 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Fri, 18 Dec 2020 09:42:13 +0200 Subject: Adding Technology Web layer --- src/DevHive.Services/Services/TechnologyService.cs | 6 +- .../Configurations/Mapping/TechnolofyMappings.cs | 16 +++++ .../Controllers/TechnologyController.cs | 72 ++++++++++++++++++++++ .../Models/Technology/TechnologyWebModel.cs | 7 +++ .../Models/Technology/UpdateTechnologyWebModel.cs | 9 +++ 5 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 src/DevHive.Web/Configurations/Mapping/TechnolofyMappings.cs create mode 100644 src/DevHive.Web/Controllers/TechnologyController.cs create mode 100644 src/DevHive.Web/Models/Technology/TechnologyWebModel.cs create mode 100644 src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs (limited to 'src/DevHive.Services/Services/TechnologyService.cs') diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index 5617e9b..246ad2c 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -53,13 +53,13 @@ namespace DevHive.Services.Services return result; } - public async Task DeleteLanguage(Guid id) + public async Task DeleteTechnology(Guid id) { if (!await this._technologyRepository.DoesTechnologyExist(id)) throw new ArgumentException("Technology does not exist!"); - Technology language = await this._technologyRepository.GetByIdAsync(id); - bool result = await this._technologyRepository.DeleteAsync(language); + Technology technology = await this._technologyRepository.GetByIdAsync(id); + bool result = await this._technologyRepository.DeleteAsync(technology); return result; } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnolofyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnolofyMappings.cs new file mode 100644 index 0000000..25ce5ca --- /dev/null +++ b/src/DevHive.Web/Configurations/Mapping/TechnolofyMappings.cs @@ -0,0 +1,16 @@ +using AutoMapper; +using DevHive.Web.Models.Technology; +using DevHive.Services.Models.Technology; + +namespace DevHive.Web.Configurations.Mapping +{ + public class TechnologyMappings : Profile + { + public TechnologyMappings() + { + CreateMap(); + CreateMap(); + CreateMap(); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs new file mode 100644 index 0000000..9e3cf3c --- /dev/null +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -0,0 +1,72 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Repositories; +using DevHive.Services.Models.Technology; +using DevHive.Services.Services; +using DevHive.Web.Models.Technology; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class TechnologyController + { + private readonly TechnologyService _technologyService; + private readonly IMapper _technologyMapper; + + public TechnologyController(DevHiveContext context, IMapper mapper) + { + this._technologyService = new TechnologyService(context, mapper); + this._technologyMapper = mapper; + } + + [HttpPost] + public async Task Create([FromBody] TechnologyWebModel technologyWebModel) + { + TechnologyServiceModel technologyServiceModel = this._technologyMapper.Map(technologyWebModel); + + bool result = await this._technologyService.Create(technologyServiceModel); + + if(!result) + return new BadRequestObjectResult("Could not create the Technology"); + + return new OkResult(); + } + + [HttpGet] + public async Task GetById(Guid id) + { + TechnologyServiceModel technologyServiceModel = await this._technologyService.GetTechnologyById(id); + TechnologyWebModel technologyWebModel = this._technologyMapper.Map(technologyServiceModel); + + return new OkObjectResult(technologyWebModel); + } + + [HttpPut] + public async Task Update(Guid id, [FromBody] UpdateTechnologyWebModel updateModel) + { + UpdateTechnologyServiceModel updateTechnologyWebModel = this._technologyMapper.Map(updateModel); + updateTechnologyWebModel.Id = id; + + bool result = await this._technologyService.UpdateTechnology(updateTechnologyWebModel); + + if(!result) + return new BadRequestObjectResult("Could not update Technology"); + + return new OkResult(); + } + + [HttpDelete] + 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(); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs new file mode 100644 index 0000000..cb6b998 --- /dev/null +++ b/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Web.Models.Technology +{ + public class TechnologyWebModel + { + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs new file mode 100644 index 0000000..b94496e --- /dev/null +++ b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Web.Models.Technology +{ + public class UpdateTechnologyWebModel : TechnologyWebModel + { + public Guid Id { get; set; } + } +} \ No newline at end of file -- cgit v1.2.3