diff --git a/.gitignore b/.gitignore index 275a7fe..8881c6d 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,4 @@ /Back/skydiveLogs-api/Data/JumpsDb.db Back/skydiveLogs-api/Data/JumpsDb.db Back/skydiveLogs-api/Data/JumpsDb-log.db +/Back/skydiveLogs-api.Domain/bin/Release/net5.0/skydiveLogs-api.Domain.deps.json diff --git a/Back/skydiveLogs-api.Domain/CacheType.cs b/Back/skydiveLogs-api.Domain/CacheType.cs new file mode 100644 index 0000000..95602a6 --- /dev/null +++ b/Back/skydiveLogs-api.Domain/CacheType.cs @@ -0,0 +1,10 @@ +namespace skydiveLogs_api.Domain +{ + public enum CacheType + { + Gear, + JumpType, + Aircraft, + DropZone + } +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/AircraftService.cs b/Back/skydiveLogs-api.DomainBusiness/AircraftService.cs index 659f917..9815fd9 100644 --- a/Back/skydiveLogs-api.DomainBusiness/AircraftService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/AircraftService.cs @@ -1,23 +1,31 @@ -using System; -using System.Collections.Generic; - +using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainService.Repositories; -using skydiveLogs_api.Domain; - +using System; +using System.Collections.Generic; +using System.Linq; namespace skydiveLogs_api.DomainBusiness { public class AircraftService : IAircraftService { - public AircraftService(IAircraftRepository aircraftRepository) + #region Public Constructors + + public AircraftService(IAircraftRepository aircraftRepository, + ICacheService cacheService) { _aircraftRepository = aircraftRepository; + _cacheService = cacheService; } + #endregion Public Constructors + + #region Public Methods + public void AddNewAircraft(Aircraft newAircraft) { _aircraftRepository.Add(newAircraft); + _cacheService.Delete(CacheType.Aircraft); } public void DeleteAircraftById(int id) @@ -27,12 +35,18 @@ namespace skydiveLogs_api.DomainBusiness public Aircraft GetAircraftById(int id) { - return _aircraftRepository.GetById(id); + var allAircrafts = GetAllAircrafts(); + return allAircrafts.Single(g => g.Id == id); } public IEnumerable GetAllAircrafts() { - return _aircraftRepository.GetAll(); + if (!_cacheService.Contains(CacheType.Aircraft)) + _cacheService.Put(CacheType.Aircraft, + _aircraftRepository.GetAll(), + 5 * 60 * 1000); + + return _cacheService.Get>(CacheType.Aircraft); } public void UpdateAircraft(int id, Aircraft aircraft) @@ -40,6 +54,13 @@ namespace skydiveLogs_api.DomainBusiness throw new NotImplementedException(); } + #endregion Public Methods + + #region Private Fields + private readonly IAircraftRepository _aircraftRepository; + private readonly ICacheService _cacheService; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/CacheService.cs b/Back/skydiveLogs-api.DomainBusiness/CacheService.cs new file mode 100644 index 0000000..8eeb702 --- /dev/null +++ b/Back/skydiveLogs-api.DomainBusiness/CacheService.cs @@ -0,0 +1,51 @@ +using skydiveLogs_api.Domain; +using skydiveLogs_api.DomainBusiness.Interfaces; +using System; +using System.Runtime.Caching; + +namespace skydiveLogs_api.DomainBusiness +{ + public class CacheService : ICacheService + { + #region Public Methods + + public bool Contains(CacheType type, int id = 0) + { + var key = GetKey(id, type); + return MemoryCache.Default[key.ToString()] != null; + } + + public void Delete(CacheType type, int id = 0) + { + var key = GetKey(id, type); + MemoryCache.Default.Remove(key.ToString()); + } + + public T Get(CacheType type, int id = 0) + { + var key = GetKey(id, type); + return (T)MemoryCache.Default[key.ToString()]; + } + + public void Put(CacheType type, object value, int duration, int id = 0) + { + var key = GetKey(id, type); + if (duration <= 0) + throw new ArgumentException("Duration cannot be less or equal to zero", "duration"); + + var policy = new CacheItemPolicy + { + AbsoluteExpiration = DateTime.Now.AddMilliseconds(duration) + }; + + MemoryCache.Default.Set(key.ToString(), value, policy); + } + + private string GetKey(int id, CacheType type) + { + return $"{id}-{type}"; + } + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/DropZoneService.cs b/Back/skydiveLogs-api.DomainBusiness/DropZoneService.cs index 4e4d7fc..dd7085b 100644 --- a/Back/skydiveLogs-api.DomainBusiness/DropZoneService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/DropZoneService.cs @@ -1,25 +1,50 @@ -using System; -using System.Collections.Generic; - +using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainService.Repositories; -using skydiveLogs_api.Domain; +using System; +using System.Collections.Generic; using System.Linq; namespace skydiveLogs_api.DomainBusiness { public class DropZoneService : IDropZoneService { + #region Public Constructors + public DropZoneService(IDropZoneRepository dropZoneRepository, - IFavoriteDropZoneRepository favoriteDropZoneRepository) + IFavoriteDropZoneRepository favoriteDropZoneRepository, + IIdentityService identityService, + ICacheService cacheService) { _dropZoneRepository = dropZoneRepository; _favoriteDropZoneRepository = favoriteDropZoneRepository; + _identityService = identityService; + _cacheService = cacheService; } + #endregion Public Constructors + + #region Public Methods + public void AddNewDz(DropZone newdropZone) { _dropZoneRepository.Add(newdropZone); + _cacheService.Delete(CacheType.DropZone); + } + + public bool AddToFavorite(int dzId) + { + var dzToAddToFavorite = GetDzById(dzId); + + var favoriteDz = new FavoriteDropZone + { + DropZone = dzToAddToFavorite, + User = _identityService.ConnectedUser + }; + + var result = _favoriteDropZoneRepository.Add(favoriteDz); + + return result > 0; } public void DeleteDzById(int id) @@ -27,12 +52,12 @@ namespace skydiveLogs_api.DomainBusiness throw new NotImplementedException(); } - public IEnumerable GetAllDzs(User connectedUser) + public IEnumerable GetAllDzs() { var results = Enumerable.Empty(); - var dropzones = _dropZoneRepository.GetAll(); - var favorites = _favoriteDropZoneRepository.GetAll(connectedUser); + var dropzones = GetAllRefDzs(); + var favorites = _favoriteDropZoneRepository.GetAll(_identityService.ConnectedUser); results = from dropZone in dropzones join favorite in favorites on dropZone.Id equals favorite.DropZone.Id into tmp @@ -55,7 +80,15 @@ namespace skydiveLogs_api.DomainBusiness public DropZone GetDzById(int id) { - return _dropZoneRepository.GetById(id); + var allDzs = GetAllRefDzs(); + return allDzs.Single(g => g.Id == id); + } + + public bool RemoveToFavorite(int dzId) + { + var result = _favoriteDropZoneRepository.Delete(dzId, _identityService.ConnectedUser.Id); + + return result > 0; } public bool UpdateDz(int id, DropZone dropZone) @@ -65,30 +98,25 @@ namespace skydiveLogs_api.DomainBusiness return _dropZoneRepository.Update(dropZone); } - public bool AddToFavorite(int dzId, User connectedUser) + private IEnumerable GetAllRefDzs() { - var dzToAddToFavorite = GetDzById(dzId); + if (!_cacheService.Contains(CacheType.DropZone)) + _cacheService.Put(CacheType.DropZone, + _dropZoneRepository.GetAll(), + 5 * 60 * 1000); - var favoriteDz = new FavoriteDropZone - { - DropZone = dzToAddToFavorite, - User = connectedUser - }; - - var result = _favoriteDropZoneRepository.Add(favoriteDz); - - return result > 0; + return _cacheService.Get>(CacheType.DropZone); } - public bool RemoveToFavorite(int dzId, User connectedUser) - { - var result = _favoriteDropZoneRepository.Delete(dzId, connectedUser.Id); + #endregion Public Methods - return result > 0; - } + #region Private Fields + private readonly ICacheService _cacheService; private readonly IDropZoneRepository _dropZoneRepository; - private readonly IFavoriteDropZoneRepository _favoriteDropZoneRepository; + private readonly IIdentityService _identityService; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/GearService.cs b/Back/skydiveLogs-api.DomainBusiness/GearService.cs index 279af7d..ce47765 100644 --- a/Back/skydiveLogs-api.DomainBusiness/GearService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/GearService.cs @@ -1,28 +1,39 @@ -using System; -using System.Collections.Generic; - +using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainService.Repositories; -using skydiveLogs_api.Domain; - +using System; +using System.Collections.Generic; +using System.Linq; namespace skydiveLogs_api.DomainBusiness { public class GearService : IGearService { - public GearService(IGearRepository gearRepository) + #region Public Constructors + + public GearService(IGearRepository gearRepository, + ICacheService cacheService, + IIdentityService identityService) { _gearRepository = gearRepository; + _cacheService = cacheService; + _identityService = identityService; } - public void AddNewGear(Gear newGear, - User connectedUser) + #endregion Public Constructors + + #region Public Methods + + public void AddNewGear(Gear newGear) { - newGear.User = connectedUser; + newGear.User = _identityService.ConnectedUser; _gearRepository.Add(newGear); + + var userId = _identityService.ConnectedUser.Id; + _cacheService.Delete(CacheType.Gear, id: userId); } - public void AddRentalGear(User connectedUser) + public void AddRentalGear(User newUser) { var rentalGear = new Gear { @@ -33,7 +44,7 @@ namespace skydiveLogs_api.DomainBusiness MaxSize = 280, MinSize = 190, ReserveCanopy = "?", - User = connectedUser + User = newUser }; _gearRepository.Add(rentalGear); @@ -44,21 +55,39 @@ namespace skydiveLogs_api.DomainBusiness throw new NotImplementedException(); } + public IEnumerable GetAllGears() + { + var userId = _identityService.ConnectedUser.Id; + if (!_cacheService.Contains(CacheType.Gear, id: userId)) + _cacheService.Put(CacheType.Gear, + _gearRepository.GetAll(_identityService.ConnectedUser), + 5 * 60 * 1000, + id: userId); + + return _cacheService.Get>(CacheType.Gear, id: userId); + } + public Gear GetGearById(int id) { - return _gearRepository.GetById(id); + var allGears = GetAllGears(); + return allGears.Single(g => g.Id == id); } - public IEnumerable GetAllGears(User connectedUser) + public bool UpdateGear(int id, Gear gear) { - return _gearRepository.GetAll(connectedUser); + gear.Id = id; + + return _gearRepository.Update(gear); } - public void UpdateGear(int id, Gear Gear) - { - throw new NotImplementedException(); - } + #endregion Public Methods + #region Private Fields + + private readonly ICacheService _cacheService; private readonly IGearRepository _gearRepository; + private readonly IIdentityService _identityService; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/IdentityService.cs b/Back/skydiveLogs-api.DomainBusiness/IdentityService.cs new file mode 100644 index 0000000..678aa2b --- /dev/null +++ b/Back/skydiveLogs-api.DomainBusiness/IdentityService.cs @@ -0,0 +1,34 @@ +using skydiveLogs_api.Domain; +using skydiveLogs_api.DomainBusiness.Interfaces; +using System; +using System.Linq; +using System.Security.Claims; + +namespace skydiveLogs_api.DomainBusiness +{ + public class IdentityService : IIdentityService + { + #region Public Constructors + + public IdentityService(ClaimsPrincipal user) + { + if (user != null + && user.Claims.Any()) + { + var claims = user.Claims; + + ConnectedUser = new User(); + ConnectedUser.Login = claims.Single(c => c.Type == ClaimTypes.Name).Value; + ConnectedUser.Id = Convert.ToInt32(claims.Single(c => c.Type == ClaimTypes.UserData).Value); + } + } + + #endregion Public Constructors + + #region Public Properties + + public User ConnectedUser { get; init; } + + #endregion Public Properties + } +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/InitDbService.cs b/Back/skydiveLogs-api.DomainBusiness/InitDbService.cs index 362ff7a..e5a4437 100644 --- a/Back/skydiveLogs-api.DomainBusiness/InitDbService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/InitDbService.cs @@ -1,15 +1,15 @@ -using System.Collections.Generic; +using skydiveLogs_api.Domain; +using skydiveLogs_api.DomainBusiness.Interfaces; +using System.Collections.Generic; using System.IO; using System.Text.Json; -using skydiveLogs_api.DomainBusiness.Interfaces; -using skydiveLogs_api.Domain; - - namespace skydiveLogs_api.DomainBusiness { public class InitDbService : IInitDbService { + #region Public Constructors + public InitDbService(IAircraftService aircraftService, IJumpTypeService jumpTypeService, IDropZoneService dropZoneService, @@ -21,6 +21,10 @@ namespace skydiveLogs_api.DomainBusiness _userService = userService; } + #endregion Public Constructors + + #region Public Methods + public void GenerateDb() { LoadAircrafts(); @@ -29,6 +33,39 @@ namespace skydiveLogs_api.DomainBusiness AddAdmin(); } + #endregion Public Methods + + #region Private Methods + + private void AddAdmin() + { + var adminUser = new User + { + FirstName = "Admin", + LastName = "Admin", + Login = "administrator", + Password = "logsadmin", + Email = "admin@nomail.com" + }; + _userService.AddNewUser(adminUser, true); + } + + private void LoadAircrafts() + { + var jsonString = File.ReadAllText("Init/aircraft.json"); + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = true + }; + var jsonModel = JsonSerializer.Deserialize>(jsonString, options); + + foreach (var item in jsonModel) + { + _aircraftService.AddNewAircraft(item); + } + } + private void LoadDropZones() { var jsonString = File.ReadAllText("Init/dropZone.json"); @@ -61,41 +98,16 @@ namespace skydiveLogs_api.DomainBusiness } } - private void LoadAircrafts() - { - var jsonString = File.ReadAllText("Init/aircraft.json"); - var options = new JsonSerializerOptions - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - WriteIndented = true - }; - var jsonModel = JsonSerializer.Deserialize>(jsonString, options); + #endregion Private Methods - foreach (var item in jsonModel) - { - _aircraftService.AddNewAircraft(item); - } - } - - private void AddAdmin() - { - var adminUser = new User - { - FirstName = "Admin", - LastName = "Admin", - Login = "administrator", - Password = "logsadmin", - Email = "admin@nomail.com" - }; - _userService.AddNewUser(adminUser, true); - } + #region Private Fields private readonly IAircraftService _aircraftService; - private readonly IJumpTypeService _jumpTypeService; - private readonly IDropZoneService _dropZoneService; - + private readonly IJumpTypeService _jumpTypeService; private readonly IUserService _userService; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/Interfaces/ICacheService.cs b/Back/skydiveLogs-api.DomainBusiness/Interfaces/ICacheService.cs new file mode 100644 index 0000000..3bb8805 --- /dev/null +++ b/Back/skydiveLogs-api.DomainBusiness/Interfaces/ICacheService.cs @@ -0,0 +1,19 @@ +using skydiveLogs_api.Domain; + +namespace skydiveLogs_api.DomainBusiness.Interfaces +{ + public interface ICacheService + { + #region Public Methods + + bool Contains(CacheType type, int id = 0); + + void Delete(CacheType type, int id = 0); + + T Get(CacheType type, int id = 0); + + void Put(CacheType type, object value, int duration, int id = 0); + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IDropZoneService.cs b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IDropZoneService.cs index b7eb96f..3acc3ac 100644 --- a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IDropZoneService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IDropZoneService.cs @@ -1,24 +1,26 @@ -using System.Collections.Generic; - -using skydiveLogs_api.Domain; - +using skydiveLogs_api.Domain; +using System.Collections.Generic; namespace skydiveLogs_api.DomainBusiness.Interfaces { public interface IDropZoneService { - IEnumerable GetAllDzs(User connectedUser); - - DropZone GetDzById(int id); - - void DeleteDzById(int id); - - bool UpdateDz(int id, DropZone dropZone); + #region Public Methods void AddNewDz(DropZone dropZone); - bool AddToFavorite(int dzId, User connectedUser); + bool AddToFavorite(int dzId); - bool RemoveToFavorite(int dzId, User connectedUser); + void DeleteDzById(int id); + + IEnumerable GetAllDzs(); + + DropZone GetDzById(int id); + + bool RemoveToFavorite(int dzId); + + bool UpdateDz(int id, DropZone dropZone); + + #endregion Public Methods } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IGearService.cs b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IGearService.cs index ca3ed49..43f8c0b 100644 --- a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IGearService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IGearService.cs @@ -1,22 +1,24 @@ -using System.Collections.Generic; - -using skydiveLogs_api.Domain; - +using skydiveLogs_api.Domain; +using System.Collections.Generic; namespace skydiveLogs_api.DomainBusiness.Interfaces { public interface IGearService { - IEnumerable GetAllGears(User connectedUser); + #region Public Methods - Gear GetGearById(int id); + void AddNewGear(Gear gear); + + void AddRentalGear(User newUser); void DeleteGearById(int id); - void UpdateGear(int id, Gear gear); + IEnumerable GetAllGears(); - void AddNewGear(Gear gear, User connectedUser); + Gear GetGearById(int id); - void AddRentalGear(User connectedUser); + bool UpdateGear(int id, Gear gear); + + #endregion Public Methods } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IIdentityService.cs b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IIdentityService.cs new file mode 100644 index 0000000..b6aeba2 --- /dev/null +++ b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IIdentityService.cs @@ -0,0 +1,9 @@ +using skydiveLogs_api.Domain; + +namespace skydiveLogs_api.DomainBusiness.Interfaces +{ + public interface IIdentityService + { + User ConnectedUser { get; } + } +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IJumpService.cs b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IJumpService.cs index 9b597b6..ed7dd65 100644 --- a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IJumpService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IJumpService.cs @@ -2,24 +2,26 @@ using skydiveLogs_api.Domain; - namespace skydiveLogs_api.DomainBusiness.Interfaces { public interface IJumpService { - IEnumerable GetAllJumps(User connectedUser); - - Jump GetJumpById(int id); + #region Public Methods void AddNewJump(int aircraftId, int dzId, int jumpTypeId, int gearId, - Jump jump, - User connectedUser); + Jump jump); + + void DeleteJumpById(int id); + + IEnumerable GetAllJumps(); + + Jump GetJumpById(int id); void UpdateJump(int id, Jump jump); - void DeleteJumpById(int id); + #endregion Public Methods } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IStatsService.cs b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IStatsService.cs index 18ad624..4626db1 100644 --- a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IStatsService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IStatsService.cs @@ -1,30 +1,32 @@ -using System.Collections.Generic; - -using skydiveLogs_api.Domain; - +using skydiveLogs_api.Domain; +using System.Collections.Generic; namespace skydiveLogs_api.DomainBusiness.Interfaces { public interface IStatsService { - IEnumerable GetStatsByDz(User connectedUser); + #region Public Methods - IEnumerable GetStatsByAircraft(User connectedUser); + SimpleSummary GetSimpleSummary(); - IEnumerable GetStatsByJumpType(User connectedUser); + IEnumerable GetStatsByAircraft(); - IEnumerable GetStatsByGear(User connectedUser); + IEnumerable GetStatsByDz(); - IEnumerable GetStatsByYear(User connectedUser); + IEnumerable GetStatsByGear(); - IEnumerable GetStatsForLastYearByDz(User connectedUser); + IEnumerable GetStatsByJumpType(); - IEnumerable GetStatsForLastYearByJumpType(User connectedUser); + IEnumerable GetStatsByYear(); - IEnumerable GetStatsForLastMonthByDz(User connectedUser); + IEnumerable GetStatsForLastMonthByDz(); - IEnumerable GetStatsForLastMonthByJumpType(User connectedUser); + IEnumerable GetStatsForLastMonthByJumpType(); - SimpleSummary GetSimpleSummary(User connectedUser); + IEnumerable GetStatsForLastYearByDz(); + + IEnumerable GetStatsForLastYearByJumpType(); + + #endregion Public Methods } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IUserImageService.cs b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IUserImageService.cs index e5c6c29..6df3b72 100644 --- a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IUserImageService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IUserImageService.cs @@ -2,19 +2,22 @@ using skydiveLogs_api.Domain; - namespace skydiveLogs_api.DomainBusiness.Interfaces { public interface IUserImageService { - IEnumerable GetAllImages(User connectedUser); + #region Public Methods + + void AddNewImage(UserImage image); + + void DeleteImageById(int id); + + IEnumerable GetAllImages(); UserImage GetImageById(int id); - void AddNewImage(UserImage image, User connectedUser); - void UpdateImage(int id, UserImage image); - void DeleteImageById(int id); + #endregion Public Methods } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/JumpService.cs b/Back/skydiveLogs-api.DomainBusiness/JumpService.cs index 15bd2af..9f3c955 100644 --- a/Back/skydiveLogs-api.DomainBusiness/JumpService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/JumpService.cs @@ -1,34 +1,39 @@ -using System; -using System.Collections.Generic; - +using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainService.Repositories; -using skydiveLogs_api.Domain; - +using System; +using System.Collections.Generic; namespace skydiveLogs_api.DomainBusiness { public class JumpService : IJumpService { + #region Public Constructors + public JumpService(IJumpTypeService jumpTypeService, IAircraftService aircraftService, IDropZoneService dropZoneService, IGearService gearService, - IJumpRepository jumpRepository) + IJumpRepository jumpRepository, + IIdentityService identityService) { _jumpTypeService = jumpTypeService; _aircraftService = aircraftService; _dropZoneService = dropZoneService; _gearService = gearService; _jumpRepository = jumpRepository; + _identityService = identityService; } + #endregion Public Constructors + + #region Public Methods + public void AddNewJump(int aircraftId, int dzId, int jumpTypeId, int gearId, - Jump jump, - User connectedUser) + Jump jump) { var selectedGear = _gearService.GetGearById(gearId); var selectedJumpType = _jumpTypeService.GetJumpTypeById(jumpTypeId); @@ -39,7 +44,7 @@ namespace skydiveLogs_api.DomainBusiness jump.JumpType = selectedJumpType; jump.DropZone = selectedDropZone; jump.Gear = selectedGear; - jump.User = connectedUser; + jump.User = _identityService.ConnectedUser; _jumpRepository.Add(jump); } @@ -49,9 +54,9 @@ namespace skydiveLogs_api.DomainBusiness throw new NotImplementedException(); } - public IEnumerable GetAllJumps(User connectedUser) + public IEnumerable GetAllJumps() { - return _jumpRepository.GetAll(connectedUser); + return _jumpRepository.GetAll(_identityService.ConnectedUser); } public Jump GetJumpById(int id) @@ -64,14 +69,17 @@ namespace skydiveLogs_api.DomainBusiness throw new NotImplementedException(); } - private readonly IJumpRepository _jumpRepository; + #endregion Public Methods - private readonly IJumpTypeService _jumpTypeService; + #region Private Fields private readonly IAircraftService _aircraftService; - private readonly IDropZoneService _dropZoneService; - private readonly IGearService _gearService; + private readonly IIdentityService _identityService; + private readonly IJumpRepository _jumpRepository; + private readonly IJumpTypeService _jumpTypeService; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/JumpTypeService.cs b/Back/skydiveLogs-api.DomainBusiness/JumpTypeService.cs index d3d4855..1180fc2 100644 --- a/Back/skydiveLogs-api.DomainBusiness/JumpTypeService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/JumpTypeService.cs @@ -1,23 +1,31 @@ -using System; -using System.Collections.Generic; - +using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainService.Repositories; -using skydiveLogs_api.Domain; - +using System; +using System.Collections.Generic; +using System.Linq; namespace skydiveLogs_api.DomainBusiness { public class JumpTypeService : IJumpTypeService { - public JumpTypeService(IJumpTypeRepository jumpTypeRepository) + #region Public Constructors + + public JumpTypeService(IJumpTypeRepository jumpTypeRepository, + ICacheService cacheService) { _jumpTypeRepository = jumpTypeRepository; + _cacheService = cacheService; } + #endregion Public Constructors + + #region Public Methods + public void AddNewJumpType(JumpType newJumpType) { _jumpTypeRepository.Add(newJumpType); + _cacheService.Delete(CacheType.JumpType); } public void DeleteJumpTypeById(int id) @@ -27,12 +35,18 @@ namespace skydiveLogs_api.DomainBusiness public IEnumerable GetAllJumpTypes() { - return _jumpTypeRepository.GetAll(); + if (!_cacheService.Contains(CacheType.JumpType)) + _cacheService.Put(CacheType.JumpType, + _jumpTypeRepository.GetAll(), + 5 * 60 * 1000); + + return _cacheService.Get>(CacheType.JumpType); } public JumpType GetJumpTypeById(int id) { - return _jumpTypeRepository.GetById(id); + var allJumpTypes = GetAllJumpTypes(); + return allJumpTypes.Single(g => g.Id == id); } public void UpdateJumpType(int id, JumpType value) @@ -40,6 +54,13 @@ namespace skydiveLogs_api.DomainBusiness throw new NotImplementedException(); } + #endregion Public Methods + + #region Private Fields + + private readonly ICacheService _cacheService; private readonly IJumpTypeRepository _jumpTypeRepository; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/StatsService.cs b/Back/skydiveLogs-api.DomainBusiness/StatsService.cs index fbc58ef..a40554b 100644 --- a/Back/skydiveLogs-api.DomainBusiness/StatsService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/StatsService.cs @@ -1,220 +1,26 @@ -using System.Collections.Generic; -using System.Linq; - +using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; -using skydiveLogs_api.Domain; - +using System.Collections.Generic; +using System.Linq; namespace skydiveLogs_api.DomainBusiness { public class StatsService : IStatsService { + #region Public Constructors + public StatsService(IJumpService jumpService) { _jumpService = jumpService; } - public IEnumerable GetStatsByAircraft(User connectedUser) + #endregion Public Constructors + + #region Public Methods + + public SimpleSummary GetSimpleSummary() { - var allJumps = _jumpService.GetAllJumps(connectedUser); - var results = new List(); - - if (allJumps.Any()) - { - results = allJumps.GroupBy(j => j.Aircraft.Name, - j => j, - (groupby, jumps) => new Statistic - { - Label = groupby.ToString(), - Nb = jumps.Count() - }) - .ToList(); - } - - return results; - } - - public IEnumerable GetStatsByDz(User connectedUser) - { - var allJumps = _jumpService.GetAllJumps(connectedUser); - var results = new List(); - - if (allJumps.Any()) - { - results = allJumps.GroupBy(j => j.DropZone.Name, - j => j, - (groupby, jumps) => new Statistic - { - Label = groupby.ToString(), - Nb = jumps.Count() - }) - .ToList(); - } - - return results; - } - - public IEnumerable GetStatsByJumpType(User connectedUser) - { - var allJumps = _jumpService.GetAllJumps(connectedUser); - var results = new List(); - - if (allJumps.Any()) - { - results = allJumps.GroupBy(j => j.JumpType.Name, - j => j, - (groupby, jumps) => new Statistic - { - Label = groupby.ToString(), - Nb = jumps.Count() - }) - .ToList(); - } - - return results; - } - - public IEnumerable GetStatsByGear(User connectedUser) - { - var allJumps = _jumpService.GetAllJumps(connectedUser); - var results = new List(); - - if (allJumps.Any()) - { - results = allJumps.GroupBy(j => j.Gear.Name, - j => j, - (groupby, jumps) => new Statistic - { - Label = groupby.ToString(), - Nb = jumps.Count() - }) - .ToList(); - } - - return results; - } - - public IEnumerable GetStatsByYear(User connectedUser) - { - var allJumps = _jumpService.GetAllJumps(connectedUser); - var results = new List(); - - if (allJumps.Any()) - { - results = allJumps.GroupBy(j => j.JumpDate.Year, - j => j, - (groupby, jumps) => new Statistic - { - Label = groupby.ToString(), - Nb = jumps.Count() - }) - .ToList(); - } - - return results; - } - - public IEnumerable GetStatsForLastYearByDz(User connectedUser) - { - var allJumps = _jumpService.GetAllJumps(connectedUser); - var results = new List(); - - if (allJumps.Any()) - { - var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First(); - var yearOfLastJump = lastJump.JumpDate.Year; - - results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump) - .GroupBy(j => j.DropZone.Name, - j => j, - (groupby, jumps) => new Statistic - { - Label = groupby.ToString(), - Nb = jumps.Count() - }) - .ToList(); - } - - return results; - } - - public IEnumerable GetStatsForLastYearByJumpType(User connectedUser) - { - var allJumps = _jumpService.GetAllJumps(connectedUser); - var results = new List(); - - if (allJumps.Any()) - { - var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First(); - var yearOfLastJump = lastJump.JumpDate.Year; - - results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump) - .GroupBy(j => j.JumpType.Name, - j => j, - (groupby, jumps) => new Statistic - { - Label = groupby.ToString(), - Nb = jumps.Count() - }) - .ToList(); - } - - return results; - } - - public IEnumerable GetStatsForLastMonthByDz(User connectedUser) - { - var allJumps = _jumpService.GetAllJumps(connectedUser); - var results = new List(); - - if (allJumps.Any()) - { - var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First(); - var yearOfLastJump = lastJump.JumpDate.Year; - var monthOfLastJump = lastJump.JumpDate.Month; - - results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump && j.JumpDate.Month == monthOfLastJump) - .GroupBy(j => j.DropZone.Name, - j => j, - (groupby, jumps) => new Statistic - { - Label = groupby.ToString(), - Nb = jumps.Count() - }) - .ToList(); - } - - return results; - } - - public IEnumerable GetStatsForLastMonthByJumpType(User connectedUser) - { - var allJumps = _jumpService.GetAllJumps(connectedUser); - var results = new List(); - - if (allJumps.Any()) - { - var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First(); - var yearOfLastJump = lastJump.JumpDate.Year; - var monthOfLastJump = lastJump.JumpDate.Month; - - results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump && j.JumpDate.Month == monthOfLastJump) - .GroupBy(j => j.JumpType.Name, - j => j, - (groupby, jumps) => new Statistic - { - Label = groupby.ToString(), - Nb = jumps.Count() - }) - .ToList(); - } - - return results; - } - - public SimpleSummary GetSimpleSummary(User connectedUser) - { - var allJumps = _jumpService.GetAllJumps(connectedUser); + var allJumps = _jumpService.GetAllJumps(); var results = new SimpleSummary(); if (allJumps.Any()) @@ -232,6 +38,210 @@ namespace skydiveLogs_api.DomainBusiness return results; } + public IEnumerable GetStatsByAircraft() + { + var allJumps = _jumpService.GetAllJumps(); + var results = new List(); + + if (allJumps.Any()) + { + results = allJumps.GroupBy(j => j.Aircraft.Name, + j => j, + (groupby, jumps) => new Statistic + { + Label = groupby.ToString(), + Nb = jumps.Count() + }) + .ToList(); + } + + return results; + } + + public IEnumerable GetStatsByDz() + { + var allJumps = _jumpService.GetAllJumps(); + var results = new List(); + + if (allJumps.Any()) + { + results = allJumps.GroupBy(j => j.DropZone.Name, + j => j, + (groupby, jumps) => new Statistic + { + Label = groupby.ToString(), + Nb = jumps.Count() + }) + .ToList(); + } + + return results; + } + + public IEnumerable GetStatsByGear() + { + var allJumps = _jumpService.GetAllJumps(); + var results = new List(); + + if (allJumps.Any()) + { + results = allJumps.GroupBy(j => j.Gear.Name, + j => j, + (groupby, jumps) => new Statistic + { + Label = groupby.ToString(), + Nb = jumps.Count() + }) + .ToList(); + } + + return results; + } + + public IEnumerable GetStatsByJumpType() + { + var allJumps = _jumpService.GetAllJumps(); + var results = new List(); + + if (allJumps.Any()) + { + results = allJumps.GroupBy(j => j.JumpType.Name, + j => j, + (groupby, jumps) => new Statistic + { + Label = groupby.ToString(), + Nb = jumps.Count() + }) + .ToList(); + } + + return results; + } + + public IEnumerable GetStatsByYear() + { + var allJumps = _jumpService.GetAllJumps(); + var results = new List(); + + if (allJumps.Any()) + { + results = allJumps.GroupBy(j => j.JumpDate.Year, + j => j, + (groupby, jumps) => new Statistic + { + Label = groupby.ToString(), + Nb = jumps.Count() + }) + .ToList(); + } + + return results; + } + + public IEnumerable GetStatsForLastMonthByDz() + { + var allJumps = _jumpService.GetAllJumps(); + var results = new List(); + + if (allJumps.Any()) + { + var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First(); + var yearOfLastJump = lastJump.JumpDate.Year; + var monthOfLastJump = lastJump.JumpDate.Month; + + results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump && j.JumpDate.Month == monthOfLastJump) + .GroupBy(j => j.DropZone.Name, + j => j, + (groupby, jumps) => new Statistic + { + Label = groupby.ToString(), + Nb = jumps.Count() + }) + .ToList(); + } + + return results; + } + + public IEnumerable GetStatsForLastMonthByJumpType() + { + var allJumps = _jumpService.GetAllJumps(); + var results = new List(); + + if (allJumps.Any()) + { + var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First(); + var yearOfLastJump = lastJump.JumpDate.Year; + var monthOfLastJump = lastJump.JumpDate.Month; + + results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump && j.JumpDate.Month == monthOfLastJump) + .GroupBy(j => j.JumpType.Name, + j => j, + (groupby, jumps) => new Statistic + { + Label = groupby.ToString(), + Nb = jumps.Count() + }) + .ToList(); + } + + return results; + } + + public IEnumerable GetStatsForLastYearByDz() + { + var allJumps = _jumpService.GetAllJumps(); + var results = new List(); + + if (allJumps.Any()) + { + var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First(); + var yearOfLastJump = lastJump.JumpDate.Year; + + results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump) + .GroupBy(j => j.DropZone.Name, + j => j, + (groupby, jumps) => new Statistic + { + Label = groupby.ToString(), + Nb = jumps.Count() + }) + .ToList(); + } + + return results; + } + + public IEnumerable GetStatsForLastYearByJumpType() + { + var allJumps = _jumpService.GetAllJumps(); + var results = new List(); + + if (allJumps.Any()) + { + var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First(); + var yearOfLastJump = lastJump.JumpDate.Year; + + results = allJumps.Where(j => j.JumpDate.Year == yearOfLastJump) + .GroupBy(j => j.JumpType.Name, + j => j, + (groupby, jumps) => new Statistic + { + Label = groupby.ToString(), + Nb = jumps.Count() + }) + .ToList(); + } + + return results; + } + + #endregion Public Methods + + #region Private Fields + private readonly IJumpService _jumpService; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/UserImageService.cs b/Back/skydiveLogs-api.DomainBusiness/UserImageService.cs index 5c8c966..dce1997 100644 --- a/Back/skydiveLogs-api.DomainBusiness/UserImageService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/UserImageService.cs @@ -1,23 +1,29 @@ -using System; -using System.Collections.Generic; - +using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; -using skydiveLogs_api.Domain; using skydiveLogs_api.DomainService.Repositories; - +using System; +using System.Collections.Generic; namespace skydiveLogs_api.DomainBusiness { public class UserImageService : IUserImageService { - public UserImageService(IUserImageRepository imageRepository) + #region Public Constructors + + public UserImageService(IUserImageRepository imageRepository, + IIdentityService identityService) { _imageRepository = imageRepository; + _identityService = identityService; } - public void AddNewImage(UserImage newImage, User connectedUser) + #endregion Public Constructors + + #region Public Methods + + public void AddNewImage(UserImage newImage) { - newImage.User = connectedUser; + newImage.User = _identityService.ConnectedUser; _imageRepository.Add(newImage); } @@ -26,21 +32,28 @@ namespace skydiveLogs_api.DomainBusiness throw new NotImplementedException(); } + public IEnumerable GetAllImages() + { + return _imageRepository.GetAll(_identityService.ConnectedUser); + } + public UserImage GetImageById(int id) { return _imageRepository.GetById(id); } - public IEnumerable GetAllImages(User connectedUser) - { - return _imageRepository.GetAll(connectedUser); - } - public void UpdateImage(int id, UserImage Image) { throw new NotImplementedException(); } + #endregion Public Methods + + #region Private Fields + + private readonly IIdentityService _identityService; private readonly IUserImageRepository _imageRepository; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/UserService.cs b/Back/skydiveLogs-api.DomainBusiness/UserService.cs index 56a038b..9dd0bdb 100644 --- a/Back/skydiveLogs-api.DomainBusiness/UserService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/UserService.cs @@ -1,17 +1,17 @@ -using System.Security.Cryptography; -using System.Text; -using System.IO; -using System; - +using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainService.Repositories; -using skydiveLogs_api.Domain; - +using System; +using System.IO; +using System.Security.Cryptography; +using System.Text; namespace skydiveLogs_api.DomainBusiness { public class UserService : IUserService { + #region Public Constructors + public UserService(IUserRepository userRepository, IGearService gearService) { @@ -19,15 +19,9 @@ namespace skydiveLogs_api.DomainBusiness _gearService = gearService; } - public User GetById(int userId) - { - return _userRepository.GetById(userId); - } + #endregion Public Constructors - public User GetByLogin(string login, string password) - { - return _userRepository.GetByLogin(login, EncryptPassword(password)); - } + #region Public Methods public bool AddNewUser(User newUser, bool isAdmin = false) { @@ -51,9 +45,23 @@ namespace skydiveLogs_api.DomainBusiness return result; } + public User GetById(int userId) + { + return _userRepository.GetById(userId); + } + + public User GetByLogin(string login, string password) + { + return _userRepository.GetByLogin(login, EncryptPassword(password)); + } + + #endregion Public Methods + + #region Private Methods + private string EncryptPassword(string password) { - var encryptionKey = "skydivelogsangular"; //we can change the code converstion key as per our requirement + var encryptionKey = "skydivelogsangular"; //we can change the code converstion key as per our requirement byte[] clearBytes = Encoding.Unicode.GetBytes(password); var encryptedPassword = string.Empty; @@ -78,8 +86,13 @@ namespace skydiveLogs_api.DomainBusiness return encryptedPassword; } - private readonly IUserRepository _userRepository; + #endregion Private Methods + + #region Private Fields private readonly IGearService _gearService; + private readonly IUserRepository _userRepository; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.DomainBusiness/skydiveLogs-api.DomainBusiness.csproj b/Back/skydiveLogs-api.DomainBusiness/skydiveLogs-api.DomainBusiness.csproj index 0475f09..fd52fdb 100644 --- a/Back/skydiveLogs-api.DomainBusiness/skydiveLogs-api.DomainBusiness.csproj +++ b/Back/skydiveLogs-api.DomainBusiness/skydiveLogs-api.DomainBusiness.csproj @@ -5,6 +5,10 @@ skydiveLogs_api.DomainBusiness + + + + diff --git a/Back/skydiveLogs-api.Infrastructure/JumpRepository.cs b/Back/skydiveLogs-api.Infrastructure/JumpRepository.cs index a0ea709..7683194 100644 --- a/Back/skydiveLogs-api.Infrastructure/JumpRepository.cs +++ b/Back/skydiveLogs-api.Infrastructure/JumpRepository.cs @@ -1,37 +1,24 @@ -using System.Collections.Generic; - -using LiteDB; - +using LiteDB; using skydiveLogs_api.Domain; using skydiveLogs_api.DomainService.Repositories; using skydiveLogs_api.Infrastructure.Interfaces; - +using System.Collections.Generic; namespace skydiveLogs_api.Infrastructure { public class JumpRepository : IJumpRepository { + #region Public Constructors + public JumpRepository(IDataProvider dataProvider) { _dataProvider = dataProvider; _col = _dataProvider.CollOfJump; } - public IEnumerable GetAll(User user) - { - return _col.Include(x => x.Aircraft) - .Include(x => x.DropZone) - .Include(x => x.Gear) - .Include(x => x.JumpType) - .Query() - .Where(j => j.User.Id == user.Id) - .ToList(); - } + #endregion Public Constructors - public Jump GetById(int id) - { - return _col.FindById(new BsonValue(id)); - } + #region Public Methods public int Add(Jump newJump) { @@ -50,9 +37,15 @@ namespace skydiveLogs_api.Infrastructure return result; } - public bool Update(Jump updatedJump) + public IEnumerable GetAll(User user) { - throw new System.NotImplementedException(); + return _col.Include(x => x.Aircraft) + .Include(x => x.DropZone) + .Include(x => x.Gear) + .Include(x => x.JumpType) + .Query() + .Where(j => j.User.Id == user.Id) + .ToList(); } public IEnumerable GetAll() @@ -60,8 +53,23 @@ namespace skydiveLogs_api.Infrastructure throw new System.NotImplementedException(); } - private readonly IDataProvider _dataProvider; + public Jump GetById(int id) + { + return _col.FindById(new BsonValue(id)); + } + + public bool Update(Jump updatedJump) + { + throw new System.NotImplementedException(); + } + + #endregion Public Methods + + #region Private Fields private readonly ILiteCollection _col; + private readonly IDataProvider _dataProvider; + + #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 9e1bce2..b650673 100644 --- a/Back/skydiveLogs-api.Ioc/IocService.cs +++ b/Back/skydiveLogs-api.Ioc/IocService.cs @@ -1,17 +1,18 @@ -using Microsoft.Extensions.Configuration; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; - +using skydiveLogs_api.DomainBusiness; +using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainService.Repositories; using skydiveLogs_api.Infrastructure; -using skydiveLogs_api.DomainBusiness.Interfaces; -using skydiveLogs_api.DomainBusiness; using skydiveLogs_api.Infrastructure.Interfaces; - namespace skydiveLogs_api.Ioc { public class IocService { + #region Public Constructors + public IocService(IServiceCollection services, IConfiguration configuration) { @@ -19,6 +20,10 @@ namespace skydiveLogs_api.Ioc _configuration = configuration; } + #endregion Public Constructors + + #region Public Methods + public void Configure() { _services.AddScoped(); @@ -31,6 +36,10 @@ namespace skydiveLogs_api.Ioc _services.AddScoped(); _services.AddScoped(); + _services.AddSingleton(); + _services.AddScoped(); + _services.AddScoped(s => s.GetService()?.HttpContext.User); + _services.AddScoped(); _services.AddScoped(); _services.AddScoped(); @@ -39,13 +48,18 @@ namespace skydiveLogs_api.Ioc _services.AddScoped(); _services.AddScoped(); _services.AddScoped(); - + string connectionString = _configuration.GetConnectionString("DefaultConnection"); _services.AddSingleton(c => new LiteDbProvider(connectionString)); } - private readonly IServiceCollection _services; + #endregion Public Methods + + #region Private Fields private readonly IConfiguration _configuration; + private readonly IServiceCollection _services; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.Ioc/skydiveLogs-api.Ioc.csproj b/Back/skydiveLogs-api.Ioc/skydiveLogs-api.Ioc.csproj index c18f35c..4656617 100644 --- a/Back/skydiveLogs-api.Ioc/skydiveLogs-api.Ioc.csproj +++ b/Back/skydiveLogs-api.Ioc/skydiveLogs-api.Ioc.csproj @@ -5,6 +5,10 @@ skydiveLogs_api.Ioc + + + + diff --git a/Back/skydiveLogs-api/Controllers/DropZoneController.cs b/Back/skydiveLogs-api/Controllers/DropZoneController.cs index 4738492..36afcd1 100644 --- a/Back/skydiveLogs-api/Controllers/DropZoneController.cs +++ b/Back/skydiveLogs-api/Controllers/DropZoneController.cs @@ -8,11 +8,12 @@ using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DataContract; - namespace skydiveLogs_api.Controllers { public class DropZoneController : Base { + #region Public Constructors + public DropZoneController(IDropZoneService dropZoneService, IMapper mapper) { @@ -20,12 +21,32 @@ namespace skydiveLogs_api.Controllers _mapper = mapper; } + #endregion Public Constructors + + #region Public Methods + + // PUT: api/DropZone/AddToFavorite/5 + [HttpPut("AddToFavorite/{id}")] + [EnableCors] + public bool AddToFavorite(int id) + { + return _dropZoneService.AddToFavorite(id); + } + + // DELETE: api/ApiWithActions + [HttpDelete("{id}")] + [EnableCors] + public void Delete(int id) + { + _dropZoneService.DeleteDzById(id); + } + // GET: api/DropZone [HttpGet] [EnableCors] public IEnumerable Get() { - var result = _dropZoneService.GetAllDzs(ConnectedUser); + var result = _dropZoneService.GetAllDzs(); return _mapper.Map>(result); } @@ -56,31 +77,21 @@ namespace skydiveLogs_api.Controllers return _dropZoneService.UpdateDz(id, _mapper.Map(value)); } - // DELETE: api/ApiWithActions - [HttpDelete("{id}")] - [EnableCors] - public void Delete(int id) - { - _dropZoneService.DeleteDzById(id); - } - - // PUT: api/DropZone/AddToFavorite/5 - [HttpPut("AddToFavorite/{id}")] - [EnableCors] - public bool AddToFavorite(int id) - { - return _dropZoneService.AddToFavorite(id, ConnectedUser); - } - // PUT: api/DropZone/RemoveToFavorite/15 [HttpPut("RemoveToFavorite/{id}")] [EnableCors] public bool RemoveToFavorite(int id) { - return _dropZoneService.RemoveToFavorite(id, ConnectedUser); + return _dropZoneService.RemoveToFavorite(id); } + #endregion Public Methods + + #region Private Fields + private readonly IDropZoneService _dropZoneService; private readonly IMapper _mapper; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api/Controllers/GearController.cs b/Back/skydiveLogs-api/Controllers/GearController.cs index c3d0e62..4486ff5 100644 --- a/Back/skydiveLogs-api/Controllers/GearController.cs +++ b/Back/skydiveLogs-api/Controllers/GearController.cs @@ -8,11 +8,12 @@ using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DataContract; - namespace skydiveLogs_api.Controllers { public class GearController : Base { + #region Public Constructors + public GearController(IGearService gearService, IMapper mapper) { @@ -20,12 +21,25 @@ namespace skydiveLogs_api.Controllers _mapper = mapper; } + #endregion Public Constructors + + #region Public Methods + + // DELETE: api/ApiWithActions/5 + [HttpDelete("{id}")] + [EnableCors] + public void Delete(int id) + { + _gearService.DeleteGearById(id); + } + // GET: api/Gear [HttpGet] [EnableCors] public IEnumerable Get() { - var result = _gearService.GetAllGears(ConnectedUser); + //var result = _gearService.GetAllGears(ConnectedUser); + var result = _gearService.GetAllGears(); return _mapper.Map>(result); } @@ -43,7 +57,7 @@ namespace skydiveLogs_api.Controllers [EnableCors] public void Post([FromBody] GearReq value) { - _gearService.AddNewGear(_mapper.Map(value), ConnectedUser); + _gearService.AddNewGear(_mapper.Map(value)); } // PUT: api/Gear/5 @@ -54,16 +68,14 @@ namespace skydiveLogs_api.Controllers _gearService.UpdateGear(id, _mapper.Map(value)); } - // DELETE: api/ApiWithActions/5 - [HttpDelete("{id}")] - [EnableCors] - public void Delete(int id) - { - _gearService.DeleteGearById(id); - } + #endregion Public Methods + + #region Private Fields private readonly IGearService _gearService; private readonly IMapper _mapper; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api/Controllers/ImageController.cs b/Back/skydiveLogs-api/Controllers/ImageController.cs index eb82344..93826c7 100644 --- a/Back/skydiveLogs-api/Controllers/ImageController.cs +++ b/Back/skydiveLogs-api/Controllers/ImageController.cs @@ -8,11 +8,12 @@ using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DataContract; - namespace skydiveLogs_api.Controllers { public class ImageController : Base { + #region Public Constructors + public ImageController(IUserImageService imageService, IMapper mapper) { @@ -20,12 +21,24 @@ namespace skydiveLogs_api.Controllers _mapper = mapper; } + #endregion Public Constructors + + #region Public Methods + + // DELETE: api/Image/5 + [HttpDelete("{id}")] + [EnableCors] + public void Delete(int id) + { + _imageService.DeleteImageById(id); + } + // GET: api/Image [HttpGet] [EnableCors] public IEnumerable Get() { - var result = _imageService.GetAllImages(ConnectedUser); + var result = _imageService.GetAllImages(); return _mapper.Map>(result); } @@ -43,7 +56,7 @@ namespace skydiveLogs_api.Controllers [EnableCors] public void Post([FromBody] ImageReq value) { - _imageService.AddNewImage(_mapper.Map(value), ConnectedUser); + _imageService.AddNewImage(_mapper.Map(value)); } // PUT: api/Image/5 @@ -54,15 +67,13 @@ namespace skydiveLogs_api.Controllers _imageService.UpdateImage(id, _mapper.Map(value)); } - // DELETE: api/Image/5 - [HttpDelete("{id}")] - [EnableCors] - public void Delete(int id) - { - _imageService.DeleteImageById(id); - } + #endregion Public Methods + + #region Private Fields private readonly IUserImageService _imageService; private readonly IMapper _mapper; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api/Controllers/JumpController.cs b/Back/skydiveLogs-api/Controllers/JumpController.cs index f81d47a..883eb1e 100644 --- a/Back/skydiveLogs-api/Controllers/JumpController.cs +++ b/Back/skydiveLogs-api/Controllers/JumpController.cs @@ -1,18 +1,17 @@ -using System.Collections.Generic; -using Microsoft.AspNetCore.Mvc; +using AutoMapper; using Microsoft.AspNetCore.Cors; - -using AutoMapper; - +using Microsoft.AspNetCore.Mvc; +using skydiveLogs_api.DataContract; using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; -using skydiveLogs_api.DataContract; - +using System.Collections.Generic; namespace skydiveLogs_api.Controllers { public class JumpController : Base { + #region Public Constructors + public JumpController(IJumpService jumpService, IMapper mapper) { @@ -20,12 +19,24 @@ namespace skydiveLogs_api.Controllers _mapper = mapper; } + #endregion Public Constructors + + #region Public Methods + + // DELETE: api/Jump/5 + [HttpDelete("{id}")] + [EnableCors] + public void Delete(int id) + { + _jumpService.DeleteJumpById(id); + } + // GET: api/Jump [HttpGet] [EnableCors] public IEnumerable Get() { - var result = _jumpService.GetAllJumps(ConnectedUser); + var result = _jumpService.GetAllJumps(); return _mapper.Map>(result); } @@ -48,8 +59,7 @@ namespace skydiveLogs_api.Controllers value.DropZoneId, value.JumpTypeId, value.GearId, - _mapper.Map(value), - ConnectedUser); + _mapper.Map(value)); } // PUT: api/Jump/5 @@ -60,15 +70,13 @@ namespace skydiveLogs_api.Controllers _jumpService.UpdateJump(id, _mapper.Map(value)); } - // DELETE: api/Jump/5 - [HttpDelete("{id}")] - [EnableCors] - public void Delete(int id) - { - _jumpService.DeleteJumpById(id); - } + #endregion Public Methods + + #region Private Fields private readonly IJumpService _jumpService; private readonly IMapper _mapper; + + #endregion Private Fields } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api/Controllers/StatsController.cs b/Back/skydiveLogs-api/Controllers/StatsController.cs index 81103ab..5a50ffc 100644 --- a/Back/skydiveLogs-api/Controllers/StatsController.cs +++ b/Back/skydiveLogs-api/Controllers/StatsController.cs @@ -1,17 +1,16 @@ -using System.Collections.Generic; -using Microsoft.AspNetCore.Mvc; +using AutoMapper; using Microsoft.AspNetCore.Cors; - -using AutoMapper; - -using skydiveLogs_api.DomainBusiness.Interfaces; +using Microsoft.AspNetCore.Mvc; using skydiveLogs_api.DataContract; - +using skydiveLogs_api.DomainBusiness.Interfaces; +using System.Collections.Generic; namespace skydiveLogs_api.Controllers { public class StatsController : Base { + #region Public Constructors + public StatsController(IStatsService statsService, IMapper mapper) { @@ -19,38 +18,24 @@ namespace skydiveLogs_api.Controllers _mapper = mapper; } - [HttpGet("Simple")] - [EnableCors] - public SimpleSummaryResp Simple() - { - var result = _statsService.GetSimpleSummary(ConnectedUser); + #endregion Public Constructors - return _mapper.Map(result); + #region Public Methods + + [HttpGet("ByAircraft")] + [EnableCors] + public IEnumerable ByAircraft() + { + var result = _statsService.GetStatsByAircraft(); + + return _mapper.Map>(result); } [HttpGet("ByDz")] [EnableCors] public IEnumerable ByDz() { - var result = _statsService.GetStatsByDz(ConnectedUser); - - return _mapper.Map>(result); - } - - [HttpGet("ByAircraft")] - [EnableCors] - public IEnumerable ByAircraft() - { - var result = _statsService.GetStatsByAircraft(ConnectedUser); - - return _mapper.Map>(result); - } - - [HttpGet("ByJumpType")] - [EnableCors] - public IEnumerable ByJumpType() - { - var result = _statsService.GetStatsByJumpType(ConnectedUser); + var result = _statsService.GetStatsByDz(); return _mapper.Map>(result); } @@ -59,7 +44,16 @@ namespace skydiveLogs_api.Controllers [EnableCors] public IEnumerable ByGear() { - var result = _statsService.GetStatsByGear(ConnectedUser); + var result = _statsService.GetStatsByGear(); + + return _mapper.Map>(result); + } + + [HttpGet("ByJumpType")] + [EnableCors] + public IEnumerable ByJumpType() + { + var result = _statsService.GetStatsByJumpType(); return _mapper.Map>(result); } @@ -68,31 +62,17 @@ namespace skydiveLogs_api.Controllers [EnableCors] public IEnumerable ByYear() { - var result = _statsService.GetStatsByYear(ConnectedUser); + var result = _statsService.GetStatsByYear(); return _mapper.Map>(result); } - [HttpGet("ForLastYear")] - [EnableCors] - public StatisticForLastYearResp ForLastYear() - { - var resultByDz = _statsService.GetStatsForLastYearByDz(ConnectedUser); - var resultByJumpType = _statsService.GetStatsForLastYearByJumpType(ConnectedUser); - - var result = new StatisticForLastYearResp(); - result.ByDz = _mapper.Map>(resultByDz); - result.ByJumpType = _mapper.Map>(resultByJumpType); - - return result; - } - [HttpGet("ForLastMonth")] [EnableCors] public StatisticForLastMonthResp ForLastMonth() { - var resultByDz = _statsService.GetStatsForLastMonthByDz(ConnectedUser); - var resultByJumpType = _statsService.GetStatsForLastMonthByJumpType(ConnectedUser); + var resultByDz = _statsService.GetStatsForLastMonthByDz(); + var resultByJumpType = _statsService.GetStatsForLastMonthByJumpType(); var result = new StatisticForLastMonthResp(); result.ByDz = _mapper.Map>(resultByDz); @@ -101,10 +81,36 @@ namespace skydiveLogs_api.Controllers return result; } + [HttpGet("ForLastYear")] + [EnableCors] + public StatisticForLastYearResp ForLastYear() + { + var resultByDz = _statsService.GetStatsForLastYearByDz(); + var resultByJumpType = _statsService.GetStatsForLastYearByJumpType(); + + var result = new StatisticForLastYearResp(); + result.ByDz = _mapper.Map>(resultByDz); + result.ByJumpType = _mapper.Map>(resultByJumpType); + + return result; + } + + [HttpGet("Simple")] + [EnableCors] + public SimpleSummaryResp Simple() + { + var result = _statsService.GetSimpleSummary(); + + return _mapper.Map(result); + } + + #endregion Public Methods + #region Private properties - private readonly IStatsService _statsService; private readonly IMapper _mapper; - #endregion + private readonly IStatsService _statsService; + + #endregion Private properties } -} +} \ No newline at end of file diff --git a/Back/skydiveLogs-api/Startup.cs b/Back/skydiveLogs-api/Startup.cs index d0d82d1..8a278a9 100644 --- a/Back/skydiveLogs-api/Startup.cs +++ b/Back/skydiveLogs-api/Startup.cs @@ -13,22 +13,48 @@ using skydiveLogs_api.Ioc; using skydiveLogs_api.Settings; using skydiveLogs_api.DomainBusiness.Interfaces; - namespace skydiveLogs_api { public class Startup { + #region Public Constructors + public Startup(IConfiguration configuration) { Configuration = configuration; } + #endregion Public Constructors + + #region Public Methods + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.EnvironmentName == "Development") + { + app.UseDeveloperExceptionPage(); + } + else + { + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); + } + + app.UseCors(); + app.UseAuthentication(); + + app.UseMvc(); + } + // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.EnableEndpointRouting = false; }) .SetCompatibilityVersion(CompatibilityVersion.Version_3_0); + services.AddHttpContextAccessor(); + // JWT var jwtSection = Configuration.GetSection("JWT"); services.Configure(jwtSection); @@ -77,24 +103,9 @@ namespace skydiveLogs_api CheckAndInitDb(services); } - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.EnvironmentName == "Development") - { - app.UseDeveloperExceptionPage(); - } - else - { - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseHsts(); - } + #endregion Public Methods - app.UseCors(); - app.UseAuthentication(); - - app.UseMvc(); - } + #region Private Methods private void CheckAndInitDb(IServiceCollection services) { @@ -109,6 +120,12 @@ namespace skydiveLogs_api } } + #endregion Private Methods + + #region Public Properties + public IConfiguration Configuration { get; } + + #endregion Public Properties } -} +} \ No newline at end of file