Add a cache system on the referential info

+ Add an identity service
This commit is contained in:
Sébastien André
2021-04-17 22:17:45 +02:00
parent 0bb9ed2a30
commit 143127cd01
30 changed files with 955 additions and 570 deletions

1
.gitignore vendored
View File

@@ -39,3 +39,4 @@
/Back/skydiveLogs-api/Data/JumpsDb.db /Back/skydiveLogs-api/Data/JumpsDb.db
Back/skydiveLogs-api/Data/JumpsDb.db Back/skydiveLogs-api/Data/JumpsDb.db
Back/skydiveLogs-api/Data/JumpsDb-log.db Back/skydiveLogs-api/Data/JumpsDb-log.db
/Back/skydiveLogs-api.Domain/bin/Release/net5.0/skydiveLogs-api.Domain.deps.json

View File

@@ -0,0 +1,10 @@
namespace skydiveLogs_api.Domain
{
public enum CacheType
{
Gear,
JumpType,
Aircraft,
DropZone
}
}

View File

@@ -1,23 +1,31 @@
using System; using skydiveLogs_api.Domain;
using System.Collections.Generic;
using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DomainService.Repositories; using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Domain; using System;
using System.Collections.Generic;
using System.Linq;
namespace skydiveLogs_api.DomainBusiness namespace skydiveLogs_api.DomainBusiness
{ {
public class AircraftService : IAircraftService public class AircraftService : IAircraftService
{ {
public AircraftService(IAircraftRepository aircraftRepository) #region Public Constructors
public AircraftService(IAircraftRepository aircraftRepository,
ICacheService cacheService)
{ {
_aircraftRepository = aircraftRepository; _aircraftRepository = aircraftRepository;
_cacheService = cacheService;
} }
#endregion Public Constructors
#region Public Methods
public void AddNewAircraft(Aircraft newAircraft) public void AddNewAircraft(Aircraft newAircraft)
{ {
_aircraftRepository.Add(newAircraft); _aircraftRepository.Add(newAircraft);
_cacheService.Delete(CacheType.Aircraft);
} }
public void DeleteAircraftById(int id) public void DeleteAircraftById(int id)
@@ -27,12 +35,18 @@ namespace skydiveLogs_api.DomainBusiness
public Aircraft GetAircraftById(int id) public Aircraft GetAircraftById(int id)
{ {
return _aircraftRepository.GetById(id); var allAircrafts = GetAllAircrafts();
return allAircrafts.Single(g => g.Id == id);
} }
public IEnumerable<Aircraft> GetAllAircrafts() public IEnumerable<Aircraft> GetAllAircrafts()
{ {
return _aircraftRepository.GetAll(); if (!_cacheService.Contains(CacheType.Aircraft))
_cacheService.Put(CacheType.Aircraft,
_aircraftRepository.GetAll(),
5 * 60 * 1000);
return _cacheService.Get<IEnumerable<Aircraft>>(CacheType.Aircraft);
} }
public void UpdateAircraft(int id, Aircraft aircraft) public void UpdateAircraft(int id, Aircraft aircraft)
@@ -40,6 +54,13 @@ namespace skydiveLogs_api.DomainBusiness
throw new NotImplementedException(); throw new NotImplementedException();
} }
#endregion Public Methods
#region Private Fields
private readonly IAircraftRepository _aircraftRepository; private readonly IAircraftRepository _aircraftRepository;
private readonly ICacheService _cacheService;
#endregion Private Fields
} }
} }

View File

@@ -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<T>(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
}
}

View File

@@ -1,25 +1,50 @@
using System; using skydiveLogs_api.Domain;
using System.Collections.Generic;
using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DomainService.Repositories; using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Domain; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace skydiveLogs_api.DomainBusiness namespace skydiveLogs_api.DomainBusiness
{ {
public class DropZoneService : IDropZoneService public class DropZoneService : IDropZoneService
{ {
#region Public Constructors
public DropZoneService(IDropZoneRepository dropZoneRepository, public DropZoneService(IDropZoneRepository dropZoneRepository,
IFavoriteDropZoneRepository favoriteDropZoneRepository) IFavoriteDropZoneRepository favoriteDropZoneRepository,
IIdentityService identityService,
ICacheService cacheService)
{ {
_dropZoneRepository = dropZoneRepository; _dropZoneRepository = dropZoneRepository;
_favoriteDropZoneRepository = favoriteDropZoneRepository; _favoriteDropZoneRepository = favoriteDropZoneRepository;
_identityService = identityService;
_cacheService = cacheService;
} }
#endregion Public Constructors
#region Public Methods
public void AddNewDz(DropZone newdropZone) public void AddNewDz(DropZone newdropZone)
{ {
_dropZoneRepository.Add(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) public void DeleteDzById(int id)
@@ -27,12 +52,12 @@ namespace skydiveLogs_api.DomainBusiness
throw new NotImplementedException(); throw new NotImplementedException();
} }
public IEnumerable<DropZone> GetAllDzs(User connectedUser) public IEnumerable<DropZone> GetAllDzs()
{ {
var results = Enumerable.Empty<DropZone>(); var results = Enumerable.Empty<DropZone>();
var dropzones = _dropZoneRepository.GetAll(); var dropzones = GetAllRefDzs();
var favorites = _favoriteDropZoneRepository.GetAll(connectedUser); var favorites = _favoriteDropZoneRepository.GetAll(_identityService.ConnectedUser);
results = from dropZone in dropzones results = from dropZone in dropzones
join favorite in favorites on dropZone.Id equals favorite.DropZone.Id into tmp 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) 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) public bool UpdateDz(int id, DropZone dropZone)
@@ -65,30 +98,25 @@ namespace skydiveLogs_api.DomainBusiness
return _dropZoneRepository.Update(dropZone); return _dropZoneRepository.Update(dropZone);
} }
public bool AddToFavorite(int dzId, User connectedUser) private IEnumerable<DropZone> GetAllRefDzs()
{ {
var dzToAddToFavorite = GetDzById(dzId); if (!_cacheService.Contains(CacheType.DropZone))
_cacheService.Put(CacheType.DropZone,
_dropZoneRepository.GetAll(),
5 * 60 * 1000);
var favoriteDz = new FavoriteDropZone return _cacheService.Get<IEnumerable<DropZone>>(CacheType.DropZone);
{
DropZone = dzToAddToFavorite,
User = connectedUser
};
var result = _favoriteDropZoneRepository.Add(favoriteDz);
return result > 0;
} }
public bool RemoveToFavorite(int dzId, User connectedUser) #endregion Public Methods
{
var result = _favoriteDropZoneRepository.Delete(dzId, connectedUser.Id);
return result > 0; #region Private Fields
}
private readonly ICacheService _cacheService;
private readonly IDropZoneRepository _dropZoneRepository; private readonly IDropZoneRepository _dropZoneRepository;
private readonly IFavoriteDropZoneRepository _favoriteDropZoneRepository; private readonly IFavoriteDropZoneRepository _favoriteDropZoneRepository;
private readonly IIdentityService _identityService;
#endregion Private Fields
} }
} }

View File

@@ -1,28 +1,39 @@
using System; using skydiveLogs_api.Domain;
using System.Collections.Generic;
using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DomainService.Repositories; using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Domain; using System;
using System.Collections.Generic;
using System.Linq;
namespace skydiveLogs_api.DomainBusiness namespace skydiveLogs_api.DomainBusiness
{ {
public class GearService : IGearService public class GearService : IGearService
{ {
public GearService(IGearRepository gearRepository) #region Public Constructors
public GearService(IGearRepository gearRepository,
ICacheService cacheService,
IIdentityService identityService)
{ {
_gearRepository = gearRepository; _gearRepository = gearRepository;
_cacheService = cacheService;
_identityService = identityService;
} }
public void AddNewGear(Gear newGear, #endregion Public Constructors
User connectedUser)
#region Public Methods
public void AddNewGear(Gear newGear)
{ {
newGear.User = connectedUser; newGear.User = _identityService.ConnectedUser;
_gearRepository.Add(newGear); _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 var rentalGear = new Gear
{ {
@@ -33,7 +44,7 @@ namespace skydiveLogs_api.DomainBusiness
MaxSize = 280, MaxSize = 280,
MinSize = 190, MinSize = 190,
ReserveCanopy = "?", ReserveCanopy = "?",
User = connectedUser User = newUser
}; };
_gearRepository.Add(rentalGear); _gearRepository.Add(rentalGear);
@@ -44,21 +55,39 @@ namespace skydiveLogs_api.DomainBusiness
throw new NotImplementedException(); throw new NotImplementedException();
} }
public IEnumerable<Gear> 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<IEnumerable<Gear>>(CacheType.Gear, id: userId);
}
public Gear GetGearById(int id) public Gear GetGearById(int id)
{ {
return _gearRepository.GetById(id); var allGears = GetAllGears();
return allGears.Single(g => g.Id == id);
} }
public IEnumerable<Gear> 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) #endregion Public Methods
{
throw new NotImplementedException();
}
#region Private Fields
private readonly ICacheService _cacheService;
private readonly IGearRepository _gearRepository; private readonly IGearRepository _gearRepository;
private readonly IIdentityService _identityService;
#endregion Private Fields
} }
} }

View File

@@ -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
}
}

View File

@@ -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.IO;
using System.Text.Json; using System.Text.Json;
using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.Domain;
namespace skydiveLogs_api.DomainBusiness namespace skydiveLogs_api.DomainBusiness
{ {
public class InitDbService : IInitDbService public class InitDbService : IInitDbService
{ {
#region Public Constructors
public InitDbService(IAircraftService aircraftService, public InitDbService(IAircraftService aircraftService,
IJumpTypeService jumpTypeService, IJumpTypeService jumpTypeService,
IDropZoneService dropZoneService, IDropZoneService dropZoneService,
@@ -21,6 +21,10 @@ namespace skydiveLogs_api.DomainBusiness
_userService = userService; _userService = userService;
} }
#endregion Public Constructors
#region Public Methods
public void GenerateDb() public void GenerateDb()
{ {
LoadAircrafts(); LoadAircrafts();
@@ -29,6 +33,39 @@ namespace skydiveLogs_api.DomainBusiness
AddAdmin(); 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<List<Aircraft>>(jsonString, options);
foreach (var item in jsonModel)
{
_aircraftService.AddNewAircraft(item);
}
}
private void LoadDropZones() private void LoadDropZones()
{ {
var jsonString = File.ReadAllText("Init/dropZone.json"); var jsonString = File.ReadAllText("Init/dropZone.json");
@@ -61,41 +98,16 @@ namespace skydiveLogs_api.DomainBusiness
} }
} }
private void LoadAircrafts() #endregion Private Methods
{
var jsonString = File.ReadAllText("Init/aircraft.json");
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
var jsonModel = JsonSerializer.Deserialize<List<Aircraft>>(jsonString, options);
foreach (var item in jsonModel) #region Private Fields
{
_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);
}
private readonly IAircraftService _aircraftService; private readonly IAircraftService _aircraftService;
private readonly IJumpTypeService _jumpTypeService;
private readonly IDropZoneService _dropZoneService; private readonly IDropZoneService _dropZoneService;
private readonly IJumpTypeService _jumpTypeService;
private readonly IUserService _userService; private readonly IUserService _userService;
#endregion Private Fields
} }
} }

View File

@@ -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<T>(CacheType type, int id = 0);
void Put(CacheType type, object value, int duration, int id = 0);
#endregion Public Methods
}
}

View File

@@ -1,24 +1,26 @@
using System.Collections.Generic; using skydiveLogs_api.Domain;
using System.Collections.Generic;
using skydiveLogs_api.Domain;
namespace skydiveLogs_api.DomainBusiness.Interfaces namespace skydiveLogs_api.DomainBusiness.Interfaces
{ {
public interface IDropZoneService public interface IDropZoneService
{ {
IEnumerable<DropZone> GetAllDzs(User connectedUser); #region Public Methods
DropZone GetDzById(int id);
void DeleteDzById(int id);
bool UpdateDz(int id, DropZone dropZone);
void AddNewDz(DropZone dropZone); 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<DropZone> GetAllDzs();
DropZone GetDzById(int id);
bool RemoveToFavorite(int dzId);
bool UpdateDz(int id, DropZone dropZone);
#endregion Public Methods
} }
} }

View File

@@ -1,22 +1,24 @@
using System.Collections.Generic; using skydiveLogs_api.Domain;
using System.Collections.Generic;
using skydiveLogs_api.Domain;
namespace skydiveLogs_api.DomainBusiness.Interfaces namespace skydiveLogs_api.DomainBusiness.Interfaces
{ {
public interface IGearService public interface IGearService
{ {
IEnumerable<Gear> GetAllGears(User connectedUser); #region Public Methods
Gear GetGearById(int id); void AddNewGear(Gear gear);
void AddRentalGear(User newUser);
void DeleteGearById(int id); void DeleteGearById(int id);
void UpdateGear(int id, Gear gear); IEnumerable<Gear> GetAllGears();
void AddNewGear(Gear gear, User connectedUser); Gear GetGearById(int id);
void AddRentalGear(User connectedUser); bool UpdateGear(int id, Gear gear);
#endregion Public Methods
} }
} }

View File

@@ -0,0 +1,9 @@
using skydiveLogs_api.Domain;
namespace skydiveLogs_api.DomainBusiness.Interfaces
{
public interface IIdentityService
{
User ConnectedUser { get; }
}
}

View File

@@ -2,24 +2,26 @@
using skydiveLogs_api.Domain; using skydiveLogs_api.Domain;
namespace skydiveLogs_api.DomainBusiness.Interfaces namespace skydiveLogs_api.DomainBusiness.Interfaces
{ {
public interface IJumpService public interface IJumpService
{ {
IEnumerable<Jump> GetAllJumps(User connectedUser); #region Public Methods
Jump GetJumpById(int id);
void AddNewJump(int aircraftId, void AddNewJump(int aircraftId,
int dzId, int dzId,
int jumpTypeId, int jumpTypeId,
int gearId, int gearId,
Jump jump, Jump jump);
User connectedUser);
void DeleteJumpById(int id);
IEnumerable<Jump> GetAllJumps();
Jump GetJumpById(int id);
void UpdateJump(int id, Jump jump); void UpdateJump(int id, Jump jump);
void DeleteJumpById(int id); #endregion Public Methods
} }
} }

View File

@@ -1,30 +1,32 @@
using System.Collections.Generic; using skydiveLogs_api.Domain;
using System.Collections.Generic;
using skydiveLogs_api.Domain;
namespace skydiveLogs_api.DomainBusiness.Interfaces namespace skydiveLogs_api.DomainBusiness.Interfaces
{ {
public interface IStatsService public interface IStatsService
{ {
IEnumerable<Statistic> GetStatsByDz(User connectedUser); #region Public Methods
IEnumerable<Statistic> GetStatsByAircraft(User connectedUser); SimpleSummary GetSimpleSummary();
IEnumerable<Statistic> GetStatsByJumpType(User connectedUser); IEnumerable<Statistic> GetStatsByAircraft();
IEnumerable<Statistic> GetStatsByGear(User connectedUser); IEnumerable<Statistic> GetStatsByDz();
IEnumerable<Statistic> GetStatsByYear(User connectedUser); IEnumerable<Statistic> GetStatsByGear();
IEnumerable<Statistic> GetStatsForLastYearByDz(User connectedUser); IEnumerable<Statistic> GetStatsByJumpType();
IEnumerable<Statistic> GetStatsForLastYearByJumpType(User connectedUser); IEnumerable<Statistic> GetStatsByYear();
IEnumerable<Statistic> GetStatsForLastMonthByDz(User connectedUser); IEnumerable<Statistic> GetStatsForLastMonthByDz();
IEnumerable<Statistic> GetStatsForLastMonthByJumpType(User connectedUser); IEnumerable<Statistic> GetStatsForLastMonthByJumpType();
SimpleSummary GetSimpleSummary(User connectedUser); IEnumerable<Statistic> GetStatsForLastYearByDz();
IEnumerable<Statistic> GetStatsForLastYearByJumpType();
#endregion Public Methods
} }
} }

View File

@@ -2,19 +2,22 @@
using skydiveLogs_api.Domain; using skydiveLogs_api.Domain;
namespace skydiveLogs_api.DomainBusiness.Interfaces namespace skydiveLogs_api.DomainBusiness.Interfaces
{ {
public interface IUserImageService public interface IUserImageService
{ {
IEnumerable<UserImage> GetAllImages(User connectedUser); #region Public Methods
void AddNewImage(UserImage image);
void DeleteImageById(int id);
IEnumerable<UserImage> GetAllImages();
UserImage GetImageById(int id); UserImage GetImageById(int id);
void AddNewImage(UserImage image, User connectedUser);
void UpdateImage(int id, UserImage image); void UpdateImage(int id, UserImage image);
void DeleteImageById(int id); #endregion Public Methods
} }
} }

View File

@@ -1,34 +1,39 @@
using System; using skydiveLogs_api.Domain;
using System.Collections.Generic;
using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DomainService.Repositories; using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Domain; using System;
using System.Collections.Generic;
namespace skydiveLogs_api.DomainBusiness namespace skydiveLogs_api.DomainBusiness
{ {
public class JumpService : IJumpService public class JumpService : IJumpService
{ {
#region Public Constructors
public JumpService(IJumpTypeService jumpTypeService, public JumpService(IJumpTypeService jumpTypeService,
IAircraftService aircraftService, IAircraftService aircraftService,
IDropZoneService dropZoneService, IDropZoneService dropZoneService,
IGearService gearService, IGearService gearService,
IJumpRepository jumpRepository) IJumpRepository jumpRepository,
IIdentityService identityService)
{ {
_jumpTypeService = jumpTypeService; _jumpTypeService = jumpTypeService;
_aircraftService = aircraftService; _aircraftService = aircraftService;
_dropZoneService = dropZoneService; _dropZoneService = dropZoneService;
_gearService = gearService; _gearService = gearService;
_jumpRepository = jumpRepository; _jumpRepository = jumpRepository;
_identityService = identityService;
} }
#endregion Public Constructors
#region Public Methods
public void AddNewJump(int aircraftId, public void AddNewJump(int aircraftId,
int dzId, int dzId,
int jumpTypeId, int jumpTypeId,
int gearId, int gearId,
Jump jump, Jump jump)
User connectedUser)
{ {
var selectedGear = _gearService.GetGearById(gearId); var selectedGear = _gearService.GetGearById(gearId);
var selectedJumpType = _jumpTypeService.GetJumpTypeById(jumpTypeId); var selectedJumpType = _jumpTypeService.GetJumpTypeById(jumpTypeId);
@@ -39,7 +44,7 @@ namespace skydiveLogs_api.DomainBusiness
jump.JumpType = selectedJumpType; jump.JumpType = selectedJumpType;
jump.DropZone = selectedDropZone; jump.DropZone = selectedDropZone;
jump.Gear = selectedGear; jump.Gear = selectedGear;
jump.User = connectedUser; jump.User = _identityService.ConnectedUser;
_jumpRepository.Add(jump); _jumpRepository.Add(jump);
} }
@@ -49,9 +54,9 @@ namespace skydiveLogs_api.DomainBusiness
throw new NotImplementedException(); throw new NotImplementedException();
} }
public IEnumerable<Jump> GetAllJumps(User connectedUser) public IEnumerable<Jump> GetAllJumps()
{ {
return _jumpRepository.GetAll(connectedUser); return _jumpRepository.GetAll(_identityService.ConnectedUser);
} }
public Jump GetJumpById(int id) public Jump GetJumpById(int id)
@@ -64,14 +69,17 @@ namespace skydiveLogs_api.DomainBusiness
throw new NotImplementedException(); throw new NotImplementedException();
} }
private readonly IJumpRepository _jumpRepository; #endregion Public Methods
private readonly IJumpTypeService _jumpTypeService; #region Private Fields
private readonly IAircraftService _aircraftService; private readonly IAircraftService _aircraftService;
private readonly IDropZoneService _dropZoneService; private readonly IDropZoneService _dropZoneService;
private readonly IGearService _gearService; private readonly IGearService _gearService;
private readonly IIdentityService _identityService;
private readonly IJumpRepository _jumpRepository;
private readonly IJumpTypeService _jumpTypeService;
#endregion Private Fields
} }
} }

View File

@@ -1,23 +1,31 @@
using System; using skydiveLogs_api.Domain;
using System.Collections.Generic;
using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DomainService.Repositories; using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Domain; using System;
using System.Collections.Generic;
using System.Linq;
namespace skydiveLogs_api.DomainBusiness namespace skydiveLogs_api.DomainBusiness
{ {
public class JumpTypeService : IJumpTypeService public class JumpTypeService : IJumpTypeService
{ {
public JumpTypeService(IJumpTypeRepository jumpTypeRepository) #region Public Constructors
public JumpTypeService(IJumpTypeRepository jumpTypeRepository,
ICacheService cacheService)
{ {
_jumpTypeRepository = jumpTypeRepository; _jumpTypeRepository = jumpTypeRepository;
_cacheService = cacheService;
} }
#endregion Public Constructors
#region Public Methods
public void AddNewJumpType(JumpType newJumpType) public void AddNewJumpType(JumpType newJumpType)
{ {
_jumpTypeRepository.Add(newJumpType); _jumpTypeRepository.Add(newJumpType);
_cacheService.Delete(CacheType.JumpType);
} }
public void DeleteJumpTypeById(int id) public void DeleteJumpTypeById(int id)
@@ -27,12 +35,18 @@ namespace skydiveLogs_api.DomainBusiness
public IEnumerable<JumpType> GetAllJumpTypes() public IEnumerable<JumpType> GetAllJumpTypes()
{ {
return _jumpTypeRepository.GetAll(); if (!_cacheService.Contains(CacheType.JumpType))
_cacheService.Put(CacheType.JumpType,
_jumpTypeRepository.GetAll(),
5 * 60 * 1000);
return _cacheService.Get<IEnumerable<JumpType>>(CacheType.JumpType);
} }
public JumpType GetJumpTypeById(int id) 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) public void UpdateJumpType(int id, JumpType value)
@@ -40,6 +54,13 @@ namespace skydiveLogs_api.DomainBusiness
throw new NotImplementedException(); throw new NotImplementedException();
} }
#endregion Public Methods
#region Private Fields
private readonly ICacheService _cacheService;
private readonly IJumpTypeRepository _jumpTypeRepository; private readonly IJumpTypeRepository _jumpTypeRepository;
#endregion Private Fields
} }
} }

View File

@@ -1,220 +1,26 @@
using System.Collections.Generic; using skydiveLogs_api.Domain;
using System.Linq;
using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.Domain; using System.Collections.Generic;
using System.Linq;
namespace skydiveLogs_api.DomainBusiness namespace skydiveLogs_api.DomainBusiness
{ {
public class StatsService : IStatsService public class StatsService : IStatsService
{ {
#region Public Constructors
public StatsService(IJumpService jumpService) public StatsService(IJumpService jumpService)
{ {
_jumpService = jumpService; _jumpService = jumpService;
} }
public IEnumerable<Statistic> GetStatsByAircraft(User connectedUser) #endregion Public Constructors
#region Public Methods
public SimpleSummary GetSimpleSummary()
{ {
var allJumps = _jumpService.GetAllJumps(connectedUser); var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
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<Statistic> GetStatsByDz(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
var results = new List<Statistic>();
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<Statistic> GetStatsByJumpType(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
var results = new List<Statistic>();
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<Statistic> GetStatsByGear(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
var results = new List<Statistic>();
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<Statistic> GetStatsByYear(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
var results = new List<Statistic>();
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<Statistic> GetStatsForLastYearByDz(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
var results = new List<Statistic>();
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<Statistic> GetStatsForLastYearByJumpType(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
var results = new List<Statistic>();
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<Statistic> GetStatsForLastMonthByDz(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
var results = new List<Statistic>();
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<Statistic> GetStatsForLastMonthByJumpType(User connectedUser)
{
var allJumps = _jumpService.GetAllJumps(connectedUser);
var results = new List<Statistic>();
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 results = new SimpleSummary(); var results = new SimpleSummary();
if (allJumps.Any()) if (allJumps.Any())
@@ -232,6 +38,210 @@ namespace skydiveLogs_api.DomainBusiness
return results; return results;
} }
public IEnumerable<Statistic> GetStatsByAircraft()
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
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<Statistic> GetStatsByDz()
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
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<Statistic> GetStatsByGear()
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
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<Statistic> GetStatsByJumpType()
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
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<Statistic> GetStatsByYear()
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
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<Statistic> GetStatsForLastMonthByDz()
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
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<Statistic> GetStatsForLastMonthByJumpType()
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
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<Statistic> GetStatsForLastYearByDz()
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
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<Statistic> GetStatsForLastYearByJumpType()
{
var allJumps = _jumpService.GetAllJumps();
var results = new List<Statistic>();
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; private readonly IJumpService _jumpService;
#endregion Private Fields
} }
} }

View File

@@ -1,23 +1,29 @@
using System; using skydiveLogs_api.Domain;
using System.Collections.Generic;
using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainService.Repositories; using skydiveLogs_api.DomainService.Repositories;
using System;
using System.Collections.Generic;
namespace skydiveLogs_api.DomainBusiness namespace skydiveLogs_api.DomainBusiness
{ {
public class UserImageService : IUserImageService public class UserImageService : IUserImageService
{ {
public UserImageService(IUserImageRepository imageRepository) #region Public Constructors
public UserImageService(IUserImageRepository imageRepository,
IIdentityService identityService)
{ {
_imageRepository = imageRepository; _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); _imageRepository.Add(newImage);
} }
@@ -26,21 +32,28 @@ namespace skydiveLogs_api.DomainBusiness
throw new NotImplementedException(); throw new NotImplementedException();
} }
public IEnumerable<UserImage> GetAllImages()
{
return _imageRepository.GetAll(_identityService.ConnectedUser);
}
public UserImage GetImageById(int id) public UserImage GetImageById(int id)
{ {
return _imageRepository.GetById(id); return _imageRepository.GetById(id);
} }
public IEnumerable<UserImage> GetAllImages(User connectedUser)
{
return _imageRepository.GetAll(connectedUser);
}
public void UpdateImage(int id, UserImage Image) public void UpdateImage(int id, UserImage Image)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
#endregion Public Methods
#region Private Fields
private readonly IIdentityService _identityService;
private readonly IUserImageRepository _imageRepository; private readonly IUserImageRepository _imageRepository;
#endregion Private Fields
} }
} }

View File

@@ -1,17 +1,17 @@
using System.Security.Cryptography; using skydiveLogs_api.Domain;
using System.Text;
using System.IO;
using System;
using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DomainService.Repositories; 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 namespace skydiveLogs_api.DomainBusiness
{ {
public class UserService : IUserService public class UserService : IUserService
{ {
#region Public Constructors
public UserService(IUserRepository userRepository, public UserService(IUserRepository userRepository,
IGearService gearService) IGearService gearService)
{ {
@@ -19,15 +19,9 @@ namespace skydiveLogs_api.DomainBusiness
_gearService = gearService; _gearService = gearService;
} }
public User GetById(int userId) #endregion Public Constructors
{
return _userRepository.GetById(userId);
}
public User GetByLogin(string login, string password) #region Public Methods
{
return _userRepository.GetByLogin(login, EncryptPassword(password));
}
public bool AddNewUser(User newUser, bool isAdmin = false) public bool AddNewUser(User newUser, bool isAdmin = false)
{ {
@@ -51,6 +45,20 @@ namespace skydiveLogs_api.DomainBusiness
return result; 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) 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
@@ -78,8 +86,13 @@ namespace skydiveLogs_api.DomainBusiness
return encryptedPassword; return encryptedPassword;
} }
private readonly IUserRepository _userRepository; #endregion Private Methods
#region Private Fields
private readonly IGearService _gearService; private readonly IGearService _gearService;
private readonly IUserRepository _userRepository;
#endregion Private Fields
} }
} }

View File

@@ -5,6 +5,10 @@
<RootNamespace>skydiveLogs_api.DomainBusiness</RootNamespace> <RootNamespace>skydiveLogs_api.DomainBusiness</RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Runtime.Caching" Version="5.0.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\skydiveLogs-api.DomainService\skydiveLogs-api.DomainService.csproj" /> <ProjectReference Include="..\skydiveLogs-api.DomainService\skydiveLogs-api.DomainService.csproj" />
<ProjectReference Include="..\skydiveLogs-api.Domain\skydiveLogs-api.Domain.csproj" /> <ProjectReference Include="..\skydiveLogs-api.Domain\skydiveLogs-api.Domain.csproj" />

View File

@@ -1,37 +1,24 @@
using System.Collections.Generic; using LiteDB;
using LiteDB;
using skydiveLogs_api.Domain; using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainService.Repositories; using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Infrastructure.Interfaces; using skydiveLogs_api.Infrastructure.Interfaces;
using System.Collections.Generic;
namespace skydiveLogs_api.Infrastructure namespace skydiveLogs_api.Infrastructure
{ {
public class JumpRepository : IJumpRepository public class JumpRepository : IJumpRepository
{ {
#region Public Constructors
public JumpRepository(IDataProvider dataProvider) public JumpRepository(IDataProvider dataProvider)
{ {
_dataProvider = dataProvider; _dataProvider = dataProvider;
_col = _dataProvider.CollOfJump; _col = _dataProvider.CollOfJump;
} }
public IEnumerable<Jump> GetAll(User user) #endregion Public Constructors
{
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 Jump GetById(int id) #region Public Methods
{
return _col.FindById(new BsonValue(id));
}
public int Add(Jump newJump) public int Add(Jump newJump)
{ {
@@ -50,9 +37,15 @@ namespace skydiveLogs_api.Infrastructure
return result; return result;
} }
public bool Update(Jump updatedJump) public IEnumerable<Jump> 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<Jump> GetAll() public IEnumerable<Jump> GetAll()
@@ -60,8 +53,23 @@ namespace skydiveLogs_api.Infrastructure
throw new System.NotImplementedException(); 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<Jump> _col; private readonly ILiteCollection<Jump> _col;
private readonly IDataProvider _dataProvider;
#endregion Private Fields
} }
} }

View File

@@ -1,17 +1,18 @@
using Microsoft.Extensions.Configuration; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using skydiveLogs_api.DomainBusiness;
using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DomainService.Repositories; using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Infrastructure; using skydiveLogs_api.Infrastructure;
using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DomainBusiness;
using skydiveLogs_api.Infrastructure.Interfaces; using skydiveLogs_api.Infrastructure.Interfaces;
namespace skydiveLogs_api.Ioc namespace skydiveLogs_api.Ioc
{ {
public class IocService public class IocService
{ {
#region Public Constructors
public IocService(IServiceCollection services, public IocService(IServiceCollection services,
IConfiguration configuration) IConfiguration configuration)
{ {
@@ -19,6 +20,10 @@ namespace skydiveLogs_api.Ioc
_configuration = configuration; _configuration = configuration;
} }
#endregion Public Constructors
#region Public Methods
public void Configure() public void Configure()
{ {
_services.AddScoped<IAircraftService, AircraftService>(); _services.AddScoped<IAircraftService, AircraftService>();
@@ -31,6 +36,10 @@ namespace skydiveLogs_api.Ioc
_services.AddScoped<IUserImageService, UserImageService>(); _services.AddScoped<IUserImageService, UserImageService>();
_services.AddScoped<IInitDbService, InitDbService>(); _services.AddScoped<IInitDbService, InitDbService>();
_services.AddSingleton<ICacheService, CacheService>();
_services.AddScoped<IIdentityService, IdentityService>();
_services.AddScoped(s => s.GetService<IHttpContextAccessor>()?.HttpContext.User);
_services.AddScoped<IAircraftRepository, AircraftRepository>(); _services.AddScoped<IAircraftRepository, AircraftRepository>();
_services.AddScoped<IDropZoneRepository, DropZoneRepository>(); _services.AddScoped<IDropZoneRepository, DropZoneRepository>();
_services.AddScoped<IJumpRepository, JumpRepository>(); _services.AddScoped<IJumpRepository, JumpRepository>();
@@ -44,8 +53,13 @@ namespace skydiveLogs_api.Ioc
_services.AddSingleton<IDataProvider>(c => new LiteDbProvider(connectionString)); _services.AddSingleton<IDataProvider>(c => new LiteDbProvider(connectionString));
} }
private readonly IServiceCollection _services; #endregion Public Methods
#region Private Fields
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly IServiceCollection _services;
#endregion Private Fields
} }
} }

View File

@@ -5,6 +5,10 @@
<RootNamespace>skydiveLogs_api.Ioc</RootNamespace> <RootNamespace>skydiveLogs_api.Ioc</RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\skydiveLogs-api.Infrastructure\skydiveLogs-api.Infrastructure.csproj" /> <ProjectReference Include="..\skydiveLogs-api.Infrastructure\skydiveLogs-api.Infrastructure.csproj" />
<ProjectReference Include="..\skydiveLogs-api.DomainBusiness\skydiveLogs-api.DomainBusiness.csproj" /> <ProjectReference Include="..\skydiveLogs-api.DomainBusiness\skydiveLogs-api.DomainBusiness.csproj" />

View File

@@ -8,11 +8,12 @@ using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DataContract; using skydiveLogs_api.DataContract;
namespace skydiveLogs_api.Controllers namespace skydiveLogs_api.Controllers
{ {
public class DropZoneController : Base public class DropZoneController : Base
{ {
#region Public Constructors
public DropZoneController(IDropZoneService dropZoneService, public DropZoneController(IDropZoneService dropZoneService,
IMapper mapper) IMapper mapper)
{ {
@@ -20,12 +21,32 @@ namespace skydiveLogs_api.Controllers
_mapper = mapper; _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 // GET: api/DropZone
[HttpGet] [HttpGet]
[EnableCors] [EnableCors]
public IEnumerable<DropZoneResp> Get() public IEnumerable<DropZoneResp> Get()
{ {
var result = _dropZoneService.GetAllDzs(ConnectedUser); var result = _dropZoneService.GetAllDzs();
return _mapper.Map<IEnumerable<DropZoneResp>>(result); return _mapper.Map<IEnumerable<DropZoneResp>>(result);
} }
@@ -56,31 +77,21 @@ namespace skydiveLogs_api.Controllers
return _dropZoneService.UpdateDz(id, _mapper.Map<DropZone>(value)); return _dropZoneService.UpdateDz(id, _mapper.Map<DropZone>(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 // PUT: api/DropZone/RemoveToFavorite/15
[HttpPut("RemoveToFavorite/{id}")] [HttpPut("RemoveToFavorite/{id}")]
[EnableCors] [EnableCors]
public bool RemoveToFavorite(int id) 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 IDropZoneService _dropZoneService;
private readonly IMapper _mapper; private readonly IMapper _mapper;
#endregion Private Fields
} }
} }

View File

@@ -8,11 +8,12 @@ using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DataContract; using skydiveLogs_api.DataContract;
namespace skydiveLogs_api.Controllers namespace skydiveLogs_api.Controllers
{ {
public class GearController : Base public class GearController : Base
{ {
#region Public Constructors
public GearController(IGearService gearService, public GearController(IGearService gearService,
IMapper mapper) IMapper mapper)
{ {
@@ -20,12 +21,25 @@ namespace skydiveLogs_api.Controllers
_mapper = mapper; _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 // GET: api/Gear
[HttpGet] [HttpGet]
[EnableCors] [EnableCors]
public IEnumerable<GearResp> Get() public IEnumerable<GearResp> Get()
{ {
var result = _gearService.GetAllGears(ConnectedUser); //var result = _gearService.GetAllGears(ConnectedUser);
var result = _gearService.GetAllGears();
return _mapper.Map<IEnumerable<GearResp>>(result); return _mapper.Map<IEnumerable<GearResp>>(result);
} }
@@ -43,7 +57,7 @@ namespace skydiveLogs_api.Controllers
[EnableCors] [EnableCors]
public void Post([FromBody] GearReq value) public void Post([FromBody] GearReq value)
{ {
_gearService.AddNewGear(_mapper.Map<Gear>(value), ConnectedUser); _gearService.AddNewGear(_mapper.Map<Gear>(value));
} }
// PUT: api/Gear/5 // PUT: api/Gear/5
@@ -54,16 +68,14 @@ namespace skydiveLogs_api.Controllers
_gearService.UpdateGear(id, _mapper.Map<Gear>(value)); _gearService.UpdateGear(id, _mapper.Map<Gear>(value));
} }
// DELETE: api/ApiWithActions/5 #endregion Public Methods
[HttpDelete("{id}")]
[EnableCors] #region Private Fields
public void Delete(int id)
{
_gearService.DeleteGearById(id);
}
private readonly IGearService _gearService; private readonly IGearService _gearService;
private readonly IMapper _mapper; private readonly IMapper _mapper;
#endregion Private Fields
} }
} }

View File

@@ -8,11 +8,12 @@ using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DataContract; using skydiveLogs_api.DataContract;
namespace skydiveLogs_api.Controllers namespace skydiveLogs_api.Controllers
{ {
public class ImageController : Base public class ImageController : Base
{ {
#region Public Constructors
public ImageController(IUserImageService imageService, public ImageController(IUserImageService imageService,
IMapper mapper) IMapper mapper)
{ {
@@ -20,12 +21,24 @@ namespace skydiveLogs_api.Controllers
_mapper = mapper; _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 // GET: api/Image
[HttpGet] [HttpGet]
[EnableCors] [EnableCors]
public IEnumerable<ImageResp> Get() public IEnumerable<ImageResp> Get()
{ {
var result = _imageService.GetAllImages(ConnectedUser); var result = _imageService.GetAllImages();
return _mapper.Map<IEnumerable<ImageResp>>(result); return _mapper.Map<IEnumerable<ImageResp>>(result);
} }
@@ -43,7 +56,7 @@ namespace skydiveLogs_api.Controllers
[EnableCors] [EnableCors]
public void Post([FromBody] ImageReq value) public void Post([FromBody] ImageReq value)
{ {
_imageService.AddNewImage(_mapper.Map<UserImage>(value), ConnectedUser); _imageService.AddNewImage(_mapper.Map<UserImage>(value));
} }
// PUT: api/Image/5 // PUT: api/Image/5
@@ -54,15 +67,13 @@ namespace skydiveLogs_api.Controllers
_imageService.UpdateImage(id, _mapper.Map<UserImage>(value)); _imageService.UpdateImage(id, _mapper.Map<UserImage>(value));
} }
// DELETE: api/Image/5 #endregion Public Methods
[HttpDelete("{id}")]
[EnableCors] #region Private Fields
public void Delete(int id)
{
_imageService.DeleteImageById(id);
}
private readonly IUserImageService _imageService; private readonly IUserImageService _imageService;
private readonly IMapper _mapper; private readonly IMapper _mapper;
#endregion Private Fields
} }
} }

View File

@@ -1,18 +1,17 @@
using System.Collections.Generic; using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using AutoMapper; using skydiveLogs_api.DataContract;
using skydiveLogs_api.Domain; using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DataContract; using System.Collections.Generic;
namespace skydiveLogs_api.Controllers namespace skydiveLogs_api.Controllers
{ {
public class JumpController : Base public class JumpController : Base
{ {
#region Public Constructors
public JumpController(IJumpService jumpService, public JumpController(IJumpService jumpService,
IMapper mapper) IMapper mapper)
{ {
@@ -20,12 +19,24 @@ namespace skydiveLogs_api.Controllers
_mapper = mapper; _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 // GET: api/Jump
[HttpGet] [HttpGet]
[EnableCors] [EnableCors]
public IEnumerable<JumpResp> Get() public IEnumerable<JumpResp> Get()
{ {
var result = _jumpService.GetAllJumps(ConnectedUser); var result = _jumpService.GetAllJumps();
return _mapper.Map<IEnumerable<JumpResp>>(result); return _mapper.Map<IEnumerable<JumpResp>>(result);
} }
@@ -48,8 +59,7 @@ namespace skydiveLogs_api.Controllers
value.DropZoneId, value.DropZoneId,
value.JumpTypeId, value.JumpTypeId,
value.GearId, value.GearId,
_mapper.Map<Jump>(value), _mapper.Map<Jump>(value));
ConnectedUser);
} }
// PUT: api/Jump/5 // PUT: api/Jump/5
@@ -60,15 +70,13 @@ namespace skydiveLogs_api.Controllers
_jumpService.UpdateJump(id, _mapper.Map<Jump>(value)); _jumpService.UpdateJump(id, _mapper.Map<Jump>(value));
} }
// DELETE: api/Jump/5 #endregion Public Methods
[HttpDelete("{id}")]
[EnableCors] #region Private Fields
public void Delete(int id)
{
_jumpService.DeleteJumpById(id);
}
private readonly IJumpService _jumpService; private readonly IJumpService _jumpService;
private readonly IMapper _mapper; private readonly IMapper _mapper;
#endregion Private Fields
} }
} }

View File

@@ -1,17 +1,16 @@
using System.Collections.Generic; using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using AutoMapper;
using skydiveLogs_api.DomainBusiness.Interfaces;
using skydiveLogs_api.DataContract; using skydiveLogs_api.DataContract;
using skydiveLogs_api.DomainBusiness.Interfaces;
using System.Collections.Generic;
namespace skydiveLogs_api.Controllers namespace skydiveLogs_api.Controllers
{ {
public class StatsController : Base public class StatsController : Base
{ {
#region Public Constructors
public StatsController(IStatsService statsService, public StatsController(IStatsService statsService,
IMapper mapper) IMapper mapper)
{ {
@@ -19,38 +18,24 @@ namespace skydiveLogs_api.Controllers
_mapper = mapper; _mapper = mapper;
} }
[HttpGet("Simple")] #endregion Public Constructors
[EnableCors]
public SimpleSummaryResp Simple()
{
var result = _statsService.GetSimpleSummary(ConnectedUser);
return _mapper.Map<SimpleSummaryResp>(result); #region Public Methods
[HttpGet("ByAircraft")]
[EnableCors]
public IEnumerable<StatisticResp> ByAircraft()
{
var result = _statsService.GetStatsByAircraft();
return _mapper.Map<IEnumerable<StatisticResp>>(result);
} }
[HttpGet("ByDz")] [HttpGet("ByDz")]
[EnableCors] [EnableCors]
public IEnumerable<StatisticResp> ByDz() public IEnumerable<StatisticResp> ByDz()
{ {
var result = _statsService.GetStatsByDz(ConnectedUser); var result = _statsService.GetStatsByDz();
return _mapper.Map<IEnumerable<StatisticResp>>(result);
}
[HttpGet("ByAircraft")]
[EnableCors]
public IEnumerable<StatisticResp> ByAircraft()
{
var result = _statsService.GetStatsByAircraft(ConnectedUser);
return _mapper.Map<IEnumerable<StatisticResp>>(result);
}
[HttpGet("ByJumpType")]
[EnableCors]
public IEnumerable<StatisticResp> ByJumpType()
{
var result = _statsService.GetStatsByJumpType(ConnectedUser);
return _mapper.Map<IEnumerable<StatisticResp>>(result); return _mapper.Map<IEnumerable<StatisticResp>>(result);
} }
@@ -59,7 +44,16 @@ namespace skydiveLogs_api.Controllers
[EnableCors] [EnableCors]
public IEnumerable<StatisticResp> ByGear() public IEnumerable<StatisticResp> ByGear()
{ {
var result = _statsService.GetStatsByGear(ConnectedUser); var result = _statsService.GetStatsByGear();
return _mapper.Map<IEnumerable<StatisticResp>>(result);
}
[HttpGet("ByJumpType")]
[EnableCors]
public IEnumerable<StatisticResp> ByJumpType()
{
var result = _statsService.GetStatsByJumpType();
return _mapper.Map<IEnumerable<StatisticResp>>(result); return _mapper.Map<IEnumerable<StatisticResp>>(result);
} }
@@ -68,31 +62,17 @@ namespace skydiveLogs_api.Controllers
[EnableCors] [EnableCors]
public IEnumerable<StatisticResp> ByYear() public IEnumerable<StatisticResp> ByYear()
{ {
var result = _statsService.GetStatsByYear(ConnectedUser); var result = _statsService.GetStatsByYear();
return _mapper.Map<IEnumerable<StatisticResp>>(result); return _mapper.Map<IEnumerable<StatisticResp>>(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<IEnumerable<StatisticResp>>(resultByDz);
result.ByJumpType = _mapper.Map<IEnumerable<StatisticResp>>(resultByJumpType);
return result;
}
[HttpGet("ForLastMonth")] [HttpGet("ForLastMonth")]
[EnableCors] [EnableCors]
public StatisticForLastMonthResp ForLastMonth() public StatisticForLastMonthResp ForLastMonth()
{ {
var resultByDz = _statsService.GetStatsForLastMonthByDz(ConnectedUser); var resultByDz = _statsService.GetStatsForLastMonthByDz();
var resultByJumpType = _statsService.GetStatsForLastMonthByJumpType(ConnectedUser); var resultByJumpType = _statsService.GetStatsForLastMonthByJumpType();
var result = new StatisticForLastMonthResp(); var result = new StatisticForLastMonthResp();
result.ByDz = _mapper.Map<IEnumerable<StatisticResp>>(resultByDz); result.ByDz = _mapper.Map<IEnumerable<StatisticResp>>(resultByDz);
@@ -101,10 +81,36 @@ namespace skydiveLogs_api.Controllers
return result; return result;
} }
[HttpGet("ForLastYear")]
[EnableCors]
public StatisticForLastYearResp ForLastYear()
{
var resultByDz = _statsService.GetStatsForLastYearByDz();
var resultByJumpType = _statsService.GetStatsForLastYearByJumpType();
var result = new StatisticForLastYearResp();
result.ByDz = _mapper.Map<IEnumerable<StatisticResp>>(resultByDz);
result.ByJumpType = _mapper.Map<IEnumerable<StatisticResp>>(resultByJumpType);
return result;
}
[HttpGet("Simple")]
[EnableCors]
public SimpleSummaryResp Simple()
{
var result = _statsService.GetSimpleSummary();
return _mapper.Map<SimpleSummaryResp>(result);
}
#endregion Public Methods
#region Private properties #region Private properties
private readonly IStatsService _statsService;
private readonly IMapper _mapper; private readonly IMapper _mapper;
#endregion private readonly IStatsService _statsService;
#endregion Private properties
} }
} }

View File

@@ -13,22 +13,48 @@ using skydiveLogs_api.Ioc;
using skydiveLogs_api.Settings; using skydiveLogs_api.Settings;
using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainBusiness.Interfaces;
namespace skydiveLogs_api namespace skydiveLogs_api
{ {
public class Startup public class Startup
{ {
#region Public Constructors
public Startup(IConfiguration configuration) public Startup(IConfiguration configuration)
{ {
Configuration = 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. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddMvc(options => { options.EnableEndpointRouting = false; }) services.AddMvc(options => { options.EnableEndpointRouting = false; })
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0); .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddHttpContextAccessor();
// JWT // JWT
var jwtSection = Configuration.GetSection("JWT"); var jwtSection = Configuration.GetSection("JWT");
services.Configure<JwtSettings>(jwtSection); services.Configure<JwtSettings>(jwtSection);
@@ -77,24 +103,9 @@ namespace skydiveLogs_api
CheckAndInitDb(services); CheckAndInitDb(services);
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. #endregion Public Methods
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(); #region Private Methods
app.UseAuthentication();
app.UseMvc();
}
private void CheckAndInitDb(IServiceCollection services) private void CheckAndInitDb(IServiceCollection services)
{ {
@@ -109,6 +120,12 @@ namespace skydiveLogs_api
} }
} }
#endregion Private Methods
#region Public Properties
public IConfiguration Configuration { get; } public IConfiguration Configuration { get; }
#endregion Public Properties
} }
} }