From 8b5a3f274ebc09ef12cf7e173c7bddc72f7ecfea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20ANDRE?= Date: Wed, 3 May 2023 14:23:07 +0200 Subject: [PATCH] Add controler, domain and service about "Tunnel" --- Back/skydiveLogs-api.Domain/CacheType.cs | 3 +- Back/skydiveLogs-api.Domain/Tunnel.cs | 19 +++++ .../DropZoneService.cs | 7 +- .../Interfaces/ITunnelService.cs | 16 ++++ .../TunnelService.cs | 67 ++++++++++++++++ Back/skydiveLogs-api.Ioc/IocService.cs | 1 + .../Controllers/TunnelController.cs | 76 +++++++++++++++++++ .../skydiveLogs-api/DataContract/TunnelReq.cs | 21 +++++ .../DataContract/TunnelResp.cs | 19 +++++ Back/skydiveLogs-api/Mapper/ModelProfile.cs | 2 + Back/skydiveLogs-api/skydiveLogs-api.csproj | 2 +- 11 files changed, 230 insertions(+), 3 deletions(-) create mode 100644 Back/skydiveLogs-api.Domain/Tunnel.cs create mode 100644 Back/skydiveLogs-api.DomainBusiness/Interfaces/ITunnelService.cs create mode 100644 Back/skydiveLogs-api.DomainBusiness/TunnelService.cs create mode 100644 Back/skydiveLogs-api/Controllers/TunnelController.cs create mode 100644 Back/skydiveLogs-api/DataContract/TunnelReq.cs create mode 100644 Back/skydiveLogs-api/DataContract/TunnelResp.cs diff --git a/Back/skydiveLogs-api.Domain/CacheType.cs b/Back/skydiveLogs-api.Domain/CacheType.cs index 95602a6..45cef1f 100644 --- a/Back/skydiveLogs-api.Domain/CacheType.cs +++ b/Back/skydiveLogs-api.Domain/CacheType.cs @@ -5,6 +5,7 @@ Gear, JumpType, Aircraft, - DropZone + DropZone, + Tunnel } } \ No newline at end of file diff --git a/Back/skydiveLogs-api.Domain/Tunnel.cs b/Back/skydiveLogs-api.Domain/Tunnel.cs new file mode 100644 index 0000000..132f538 --- /dev/null +++ b/Back/skydiveLogs-api.Domain/Tunnel.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; + +namespace skydiveLogs_api.Domain +{ + public class Tunnel + { + #region Public Properties + + public string Address { get; set; } + public string Email { get; set; } + public int Id { get; set; } + public string Latitude { get; set; } + public string Longitude { get; set; } + public string Name { get; set; } + public string Website { get; set; } + + #endregion Public Properties + } +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/DropZoneService.cs b/Back/skydiveLogs-api.DomainBusiness/DropZoneService.cs index dd7085b..53dccaa 100644 --- a/Back/skydiveLogs-api.DomainBusiness/DropZoneService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/DropZoneService.cs @@ -87,6 +87,7 @@ namespace skydiveLogs_api.DomainBusiness public bool RemoveToFavorite(int dzId) { var result = _favoriteDropZoneRepository.Delete(dzId, _identityService.ConnectedUser.Id); + _cacheService.Delete(CacheType.DropZone); return result > 0; } @@ -95,7 +96,11 @@ namespace skydiveLogs_api.DomainBusiness { dropZone.Id = id; - return _dropZoneRepository.Update(dropZone); + var result = _dropZoneRepository.Update(dropZone); + if (result) + _cacheService.Delete(CacheType.DropZone); + + return result; } private IEnumerable GetAllRefDzs() diff --git a/Back/skydiveLogs-api.DomainBusiness/Interfaces/ITunnelService.cs b/Back/skydiveLogs-api.DomainBusiness/Interfaces/ITunnelService.cs new file mode 100644 index 0000000..4f18a62 --- /dev/null +++ b/Back/skydiveLogs-api.DomainBusiness/Interfaces/ITunnelService.cs @@ -0,0 +1,16 @@ +using skydiveLogs_api.Domain; +using System.Collections.Generic; + +namespace skydiveLogs_api.DomainBusiness.Interfaces +{ + public interface ITunnelService + { + #region Public Methods + + IEnumerable GetAllTunnels(); + + Tunnel GetTunnelById(int id); + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/TunnelService.cs b/Back/skydiveLogs-api.DomainBusiness/TunnelService.cs new file mode 100644 index 0000000..13eace2 --- /dev/null +++ b/Back/skydiveLogs-api.DomainBusiness/TunnelService.cs @@ -0,0 +1,67 @@ +using skydiveLogs_api.Domain; +using skydiveLogs_api.DomainBusiness.Interfaces; +using skydiveLogs_api.DomainService.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace skydiveLogs_api.DomainBusiness +{ + public class TunnelService : ITunnelService + { + #region Public Constructors + + public TunnelService(IDropZoneRepository dropZoneRepository, + ICacheService cacheService) + { + _dropZoneRepository = dropZoneRepository; + _cacheService = cacheService; + } + + #endregion Public Constructors + + #region Public Methods + + public IEnumerable GetAllTunnels() + { + return GetAllRefTunnels(); + } + + public Tunnel GetTunnelById(int id) + { + var allTunnels = GetAllRefTunnels(); + return allTunnels.Single(g => g.Id == id); + } + + private IEnumerable GetAllRefTunnels() + { + if (!_cacheService.Contains(CacheType.Tunnel)) + { + var tmp = _dropZoneRepository.GetAll() + .Where(d => d.Type.Contains("tunnel")) + .Select(t => new Tunnel + { + Id = t.Id, + Name = t.Name, + Website = t.Website, + Address = t.Address, + Email = t.Email, + Latitude = t.Latitude, + Longitude = t.Longitude + }); + _cacheService.Put(CacheType.Tunnel, tmp, 5 * 60 * 1000); + } + + return _cacheService.Get>(CacheType.Tunnel); + } + + #endregion Public Methods + + #region Private Fields + + private readonly ICacheService _cacheService; + private readonly IDropZoneRepository _dropZoneRepository; + + #endregion Private Fields + } +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.Ioc/IocService.cs b/Back/skydiveLogs-api.Ioc/IocService.cs index addf781..f947c45 100644 --- a/Back/skydiveLogs-api.Ioc/IocService.cs +++ b/Back/skydiveLogs-api.Ioc/IocService.cs @@ -36,6 +36,7 @@ namespace skydiveLogs_api.Ioc _services.AddScoped(); _services.AddScoped(); _services.AddScoped(); + _services.AddScoped(); _services.AddSingleton(); _services.AddScoped(); diff --git a/Back/skydiveLogs-api/Controllers/TunnelController.cs b/Back/skydiveLogs-api/Controllers/TunnelController.cs new file mode 100644 index 0000000..e543a28 --- /dev/null +++ b/Back/skydiveLogs-api/Controllers/TunnelController.cs @@ -0,0 +1,76 @@ +using AutoMapper; +using Microsoft.AspNetCore.Cors; +using Microsoft.AspNetCore.Mvc; +using skydiveLogs_api.DataContract; +using skydiveLogs_api.DomainBusiness.Interfaces; +using System.Collections.Generic; + +namespace skydiveLogs_api.Controllers +{ + public class TunnelController : Base + { + #region Public Constructors + + public TunnelController(ITunnelService tunnelService, + IMapper mapper) + { + _tunnelService = tunnelService; + _mapper = mapper; + } + + #endregion Public Constructors + + #region Public Methods + + // DELETE: api/Tunnel/5 + //[HttpDelete("{id}")] + //[EnableCors] + //public void Delete(int id) + //{ + // _tunnelService.DeleteTunnelById(id); + //} + + // GET: api/Tunnel + [HttpGet] + [EnableCors] + public IEnumerable Get() + { + var result = _tunnelService.GetAllTunnels(); + return _mapper.Map>(result); + } + + // GET: api/Tunnel/5 + [HttpGet("{id}")] + [EnableCors] + public TunnelResp Get(int id) + { + var result = _tunnelService.GetTunnelById(id); + return _mapper.Map(result); + } + + // POST: api/Tunnel + //[HttpPost] + //[EnableCors] + //public void Post([FromBody] TunnelReq value) + //{ + // _tunnelService.AddNewJumpType(_mapper.Map(value)); + //} + + // PUT: api/Tunnel/5 + //[HttpPut("{id}")] + //[EnableCors] + //public void Put(int id, [FromBody] TunnelReq value) + //{ + // _tunnelService.UpdateJumpType(id, _mapper.Map(value)); + //} + + #endregion Public Methods + + #region Private Fields + + private readonly ITunnelService _tunnelService; + private readonly IMapper _mapper; + + #endregion Private Fields + } +} \ No newline at end of file diff --git a/Back/skydiveLogs-api/DataContract/TunnelReq.cs b/Back/skydiveLogs-api/DataContract/TunnelReq.cs new file mode 100644 index 0000000..5317878 --- /dev/null +++ b/Back/skydiveLogs-api/DataContract/TunnelReq.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; + +namespace skydiveLogs_api.DataContract +{ + public class TunnelReq + { + public int Id { get; set; } + + public string Latitude { get; set; } + + public string Longitude { get; set; } + + public string Name { get; set; } + + public string Address { get; set; } + + public string Website { get; set; } + + public string Email { get; set; } + } +} diff --git a/Back/skydiveLogs-api/DataContract/TunnelResp.cs b/Back/skydiveLogs-api/DataContract/TunnelResp.cs new file mode 100644 index 0000000..14ec23c --- /dev/null +++ b/Back/skydiveLogs-api/DataContract/TunnelResp.cs @@ -0,0 +1,19 @@ +namespace skydiveLogs_api.DataContract +{ + public class TunnelResp + { + public int Id { get; set; } + + public string Latitude { get; set; } + + public string Longitude { get; set; } + + public string Name { get; set; } + + public string Address { get; set; } + + public string Website { get; set; } + + public string Email { get; set; } + } +} diff --git a/Back/skydiveLogs-api/Mapper/ModelProfile.cs b/Back/skydiveLogs-api/Mapper/ModelProfile.cs index 8c19e20..4ca296b 100644 --- a/Back/skydiveLogs-api/Mapper/ModelProfile.cs +++ b/Back/skydiveLogs-api/Mapper/ModelProfile.cs @@ -17,6 +17,7 @@ namespace skydiveLogs_api.Mapper CreateMap(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); CreateMap().ForMember(dest => dest.AircraftId, opt => opt.MapFrom(s => s.Aircraft.Id)) @@ -31,6 +32,7 @@ namespace skydiveLogs_api.Mapper CreateMap(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); } diff --git a/Back/skydiveLogs-api/skydiveLogs-api.csproj b/Back/skydiveLogs-api/skydiveLogs-api.csproj index ae4c71b..92a9e5f 100644 --- a/Back/skydiveLogs-api/skydiveLogs-api.csproj +++ b/Back/skydiveLogs-api/skydiveLogs-api.csproj @@ -22,7 +22,7 @@ - +