diff --git a/.gitignore b/.gitignore index 9783354..8d57e6e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,45 +2,28 @@ # This .gitignore file was automatically created by Microsoft(R) Visual Studio. ################################################################################ -/Back/skydiveLogs-api/obj -/Back/.vs/skydiveLogs-api/v15 -/Back/skydiveLogs-api/bin -/Back/.vs -/Back/skydiveLogs-api.DomainBusiness/obj -/Back/skydiveLogs-api.Infrastructure/obj -/Back/skydiveLogs-api.Model/obj -/Back/skydiveLogs-api.Model/bin -/Back/skydiveLogs-api.Ioc/bin -/Back/skydiveLogs-api.Ioc/obj -/Back/skydiveLogs-api.Infrastructure/bin -/Back/skydiveLogs-api.DomainBusiness/bin -/Back/dist +/Back/.vs/ +/Back/skydiveLogs-api/obj/ +/Back/skydiveLogs-api/bin/ +/Back/skydiveLogs-api.Model/obj/ +/Back/skydiveLogs-api.Model/bin/ +/Back/skydiveLogs-api.Ioc/bin/ +/Back/skydiveLogs-api.Ioc/obj/ +/Back/skydiveLogs-api.Infrastructure/bin/ +/Back/skydiveLogs-api.Infrastructure/obj/ +/Back/dist/ /Back/skydiveLogs-api.v3.ncrunchsolution.user -/Back/skydiveLogs-api/Init/twinOtter.jpg -/Back/skydiveLogs-api/Init/skyvan.jpg -/Back/skydiveLogs-api/Init/Pilatus.png -/Back/skydiveLogs-api/Init/pac.jpg -/Back/skydiveLogs-api/Init/dornier.jpg -/Back/skydiveLogs-api/Init/casa.jpg -/Back/skydiveLogs-api/Init/caravan.jpg -/Back/skydiveLogs-api/Init/206.jpg -/Back/skydiveLogs-api/Data -/Back/skydiveLogs-api.DomainService/bin -/Back/skydiveLogs-api.DomainService/obj -/Back/skydiveLogs-api.Data/obj -/Back/skydiveLogs-api.Business/obj -/Back/skydiveLogs-api.Domain/obj -/Back/skydiveLogs-api.Domain/bin/Debug/net5.0 -/Back/skydiveLogs-api.Data/bin/Debug/net5.0 -/Back/skydiveLogs-api.Domain/bin/Release/net5.0/ref/skydiveLogs-api.Domain.dll -/Back/skydiveLogs-api.Domain/bin/Release/net5.0/skydiveLogs-api.Domain.dll -/Back/skydiveLogs-api.Domain/bin/Release/net5.0/skydiveLogs-api.Domain.pdb -/Back/skydiveLogs-api/Data/JumpsDb-log.db -/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 -/Back/skydiveLogs-api.Domain/bin/Debug/net6.0 -/Back/skydiveLogs-api.Domain/bin/Debug/net7.0 -Back/skydiveLogs-api/Data/JumpsDb.db -/Back/skydiveLogs-api/Data/JumpsDb.db +/Back/skydiveLogs-api/Init/*.jpg +/Back/skydiveLogs-api/Update/ +/Back/skydiveLogs-api.DomainService/bin/ +/Back/skydiveLogs-api.DomainService/obj/ +/Back/skydiveLogs-api.Business/bin/ +/Back/skydiveLogs-api.Business/obj/ +/Back/skydiveLogs-api.Data/bin/ +/Back/skydiveLogs-api.Data/obj/ +/Back/skydiveLogs-api.Domain/bin/ +/Back/skydiveLogs-api.Domain/obj/ +/Back/skydiveLogs-api/Data/ +/Back/skydiveLogs-api/users.txt +/Back/skydiveLogs-api.DomainBusiness/bin/ +/Back/skydiveLogs-api.DomainBusiness/obj/ diff --git a/Back/skydiveLogs-api.DomainBusiness/AircraftService.cs b/Back/skydiveLogs-api.DomainBusiness/AircraftService.cs index 9815fd9..c26b042 100644 --- a/Back/skydiveLogs-api.DomainBusiness/AircraftService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/AircraftService.cs @@ -49,9 +49,15 @@ namespace skydiveLogs_api.DomainBusiness return _cacheService.Get>(CacheType.Aircraft); } - public void UpdateAircraft(int id, Aircraft aircraft) + public bool UpdateAircraft(int id, Aircraft aircraft, bool resetCache = true) { - throw new NotImplementedException(); + aircraft.Id = id; + + var result = _aircraftRepository.Update(aircraft); + if (resetCache && result) + _cacheService.Delete(CacheType.JumpType); + + return result; } #endregion Public Methods diff --git a/Back/skydiveLogs-api.DomainBusiness/DropZoneService.cs b/Back/skydiveLogs-api.DomainBusiness/DropZoneService.cs index 53dccaa..c598ca3 100644 --- a/Back/skydiveLogs-api.DomainBusiness/DropZoneService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/DropZoneService.cs @@ -92,12 +92,12 @@ namespace skydiveLogs_api.DomainBusiness return result > 0; } - public bool UpdateDz(int id, DropZone dropZone) + public bool UpdateDz(int id, DropZone dropZone, bool resetCache = true) { dropZone.Id = id; var result = _dropZoneRepository.Update(dropZone); - if (result) + if (resetCache && result) _cacheService.Delete(CacheType.DropZone); return result; diff --git a/Back/skydiveLogs-api.DomainBusiness/InitDbService.cs b/Back/skydiveLogs-api.DomainBusiness/InitDbService.cs index e5a4437..541d8bc 100644 --- a/Back/skydiveLogs-api.DomainBusiness/InitDbService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/InitDbService.cs @@ -13,18 +13,31 @@ namespace skydiveLogs_api.DomainBusiness public InitDbService(IAircraftService aircraftService, IJumpTypeService jumpTypeService, IDropZoneService dropZoneService, - IUserService userService) + IUserService userService, + ICacheService cacheService) { _aircraftService = aircraftService; _jumpTypeService = jumpTypeService; _dropZoneService = dropZoneService; _userService = userService; + _cacheService = cacheService; } #endregion Public Constructors #region Public Methods + public void Update() + { + UpdateAircrafts(); + UpdateDropZones(); + UpdateJumpTypes(); + + _cacheService.Delete(CacheType.Aircraft); + _cacheService.Delete(CacheType.DropZone); + _cacheService.Delete(CacheType.JumpType); + } + public void GenerateDb() { LoadAircrafts(); @@ -50,6 +63,7 @@ namespace skydiveLogs_api.DomainBusiness _userService.AddNewUser(adminUser, true); } + #region Init tables and informations private void LoadAircrafts() { var jsonString = File.ReadAllText("Init/aircraft.json"); @@ -97,16 +111,85 @@ namespace skydiveLogs_api.DomainBusiness _jumpTypeService.AddNewJumpType(item); } } + #endregion + + #region Update the existing data + private void UpdateAircrafts() + { + var file = "Update/aircraft.json"; + if (File.Exists(file)) + { + var jsonString = File.ReadAllText(file); + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = true + }; + var jsonModel = JsonSerializer.Deserialize>(jsonString, options); + + foreach (var item in jsonModel) + { + _aircraftService.UpdateAircraft(item.Id, item, false); + } + + File.Delete(file); + } + } + + private void UpdateDropZones() + { + var file = "Update/dropZone.json"; + if (File.Exists(file)) + { + var jsonString = File.ReadAllText("Update/dropZone.json"); + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = true + }; + var jsonModel = JsonSerializer.Deserialize>(jsonString, options); + + foreach (var item in jsonModel) + { + _dropZoneService.UpdateDz(item.Id, item, false); + } + + File.Delete(file); + } + } + + private void UpdateJumpTypes() + { + var file = "Update/jumpType.json"; + if (File.Exists(file)) + { + var jsonString = File.ReadAllText("Update/jumpType.json"); + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = true + }; + var jsonModel = JsonSerializer.Deserialize>(jsonString, options); + + foreach (var item in jsonModel) + { + _jumpTypeService.UpdateJumpType(item.Id, item, false); + } + + File.Delete(file); + } + } + #endregion #endregion Private Methods #region Private Fields private readonly IAircraftService _aircraftService; - private readonly IDropZoneService _dropZoneService; private readonly IJumpTypeService _jumpTypeService; private readonly IUserService _userService; + private readonly ICacheService _cacheService; #endregion Private Fields } diff --git a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IAircraftService.cs b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IAircraftService.cs index 210f583..2441291 100644 --- a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IAircraftService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IAircraftService.cs @@ -15,7 +15,7 @@ namespace skydiveLogs_api.DomainBusiness.Interfaces IEnumerable GetAllAircrafts(); - void UpdateAircraft(int id, Aircraft aircraft); + bool UpdateAircraft(int id, Aircraft aircraft, bool resetCache = true); #endregion Public Methods } diff --git a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IDropZoneService.cs b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IDropZoneService.cs index 3acc3ac..ce68a82 100644 --- a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IDropZoneService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IDropZoneService.cs @@ -19,7 +19,7 @@ namespace skydiveLogs_api.DomainBusiness.Interfaces bool RemoveToFavorite(int dzId); - bool UpdateDz(int id, DropZone dropZone); + bool UpdateDz(int id, DropZone dropZone, bool resetCache = true); #endregion Public Methods } diff --git a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IInitDbService.cs b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IInitDbService.cs index 04bba8f..ec071d8 100644 --- a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IInitDbService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IInitDbService.cs @@ -5,6 +5,7 @@ #region Public Methods public void GenerateDb(); + public void Update(); #endregion Public Methods } diff --git a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IJumpTypeService.cs b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IJumpTypeService.cs index 2a3564d..bdd27b0 100644 --- a/Back/skydiveLogs-api.DomainBusiness/Interfaces/IJumpTypeService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/Interfaces/IJumpTypeService.cs @@ -17,7 +17,7 @@ namespace skydiveLogs_api.DomainBusiness.Interfaces JumpType GetJumpTypeById(int id); - bool UpdateJumpType(int id, JumpType value); + bool UpdateJumpType(int id, JumpType value, bool resetCache = true); #endregion Public Methods } diff --git a/Back/skydiveLogs-api.DomainBusiness/JumpTypeService.cs b/Back/skydiveLogs-api.DomainBusiness/JumpTypeService.cs index 94164de..ca4329c 100644 --- a/Back/skydiveLogs-api.DomainBusiness/JumpTypeService.cs +++ b/Back/skydiveLogs-api.DomainBusiness/JumpTypeService.cs @@ -1,7 +1,6 @@ using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainService.Repositories; -using skydiveLogs_api.Infrastructure; using System; using System.Collections.Generic; using System.Linq; @@ -55,12 +54,12 @@ namespace skydiveLogs_api.DomainBusiness return allJumpTypes.Single(g => g.Id == id); } - public bool UpdateJumpType(int id, JumpType jumpType) + public bool UpdateJumpType(int id, JumpType jumpType, bool resetCache = true) { jumpType.Id = id; var result = _jumpTypeRepository.Update(jumpType); - if (result) + if (resetCache && result) _cacheService.Delete(CacheType.JumpType); return result; diff --git a/Back/skydiveLogs-api.Ioc/IocService.cs b/Back/skydiveLogs-api.Ioc/IocService.cs index 56a8920..a08586b 100644 --- a/Back/skydiveLogs-api.Ioc/IocService.cs +++ b/Back/skydiveLogs-api.Ioc/IocService.cs @@ -42,7 +42,7 @@ namespace skydiveLogs_api.Ioc _services.AddSingleton(); _services.AddScoped(); - _services.AddScoped(s => s.GetService()?.HttpContext.User); + _services.AddScoped(s => s.GetService()?.HttpContext?.User); _services.AddScoped(); _services.AddScoped(); diff --git a/Back/skydiveLogs-api/Startup.cs b/Back/skydiveLogs-api/Startup.cs index 21f95bb..05a98a4 100644 --- a/Back/skydiveLogs-api/Startup.cs +++ b/Back/skydiveLogs-api/Startup.cs @@ -9,6 +9,7 @@ using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.Ioc; using skydiveLogs_api.Settings; using System.IO; +using System.Linq; using System.Text; namespace skydiveLogs_api @@ -106,14 +107,21 @@ namespace skydiveLogs_api private void CheckAndInitDb(IServiceCollection services) { string connectionString = Configuration.GetConnectionString("DefaultConnection"); - string dbFile = connectionString.Replace("Filename=", string.Empty); + var dictionary = connectionString.Split(';') + .Select(part => part.Split('=')) + .Where(part => part.Length == 2) + .ToDictionary(sp => sp[0], sp => sp[1]); + + string dbFile = dictionary["Filename"]; //connectionString.Replace("Filename=", string.Empty); + + + var serviceProvider = services.BuildServiceProvider(); + var initDbService = serviceProvider.GetRequiredService(); if (!File.Exists(dbFile)) - { - var serviceProvider = services.BuildServiceProvider(); - var initDbService = serviceProvider.GetRequiredService(); initDbService.GenerateDb(); - } + else + initDbService.Update(); } #endregion Private Methods diff --git a/Back/skydiveLogs-api/appsettings.Development.json b/Back/skydiveLogs-api/appsettings.Development.json index 97fbce5..363a09a 100644 --- a/Back/skydiveLogs-api/appsettings.Development.json +++ b/Back/skydiveLogs-api/appsettings.Development.json @@ -15,6 +15,6 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "DefaultConnection": "Filename=./Data/JumpsDb.db" + "DefaultConnection": "Filename=./Data/JumpsDb.db;connection=shared" } } \ No newline at end of file diff --git a/Back/skydiveLogs-api/appsettings.Release.json b/Back/skydiveLogs-api/appsettings.Release.json index 389a653..b8f33a4 100644 --- a/Back/skydiveLogs-api/appsettings.Release.json +++ b/Back/skydiveLogs-api/appsettings.Release.json @@ -13,6 +13,6 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "DefaultConnection": "Filename=./Data/JumpsDb.db" + "DefaultConnection": "Filename=./Data/JumpsDb.db;connection=shared" } } \ No newline at end of file diff --git a/Back/skydiveLogs-api/appsettings.json b/Back/skydiveLogs-api/appsettings.json index cda2c31..6e88ea9 100644 --- a/Back/skydiveLogs-api/appsettings.json +++ b/Back/skydiveLogs-api/appsettings.json @@ -15,6 +15,6 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "DefaultConnection": "Filename=./Data/JumpsDb.db" + "DefaultConnection": "Filename=./Data/JumpsDb.db;connection=shared" } } diff --git a/Back/skydiveLogs-api/skydiveLogs-api.csproj b/Back/skydiveLogs-api/skydiveLogs-api.csproj index fc044a2..d3a79fd 100644 --- a/Back/skydiveLogs-api/skydiveLogs-api.csproj +++ b/Back/skydiveLogs-api/skydiveLogs-api.csproj @@ -31,4 +31,12 @@ + + + PreserveNewest + true + PreserveNewest + + +