Manage the update of some data at the

start of the database
This commit is contained in:
Sébastien ANDRE
2023-08-23 20:30:53 +02:00
parent bf05e1c668
commit 2b880231a1
15 changed files with 150 additions and 62 deletions

View File

@@ -49,9 +49,15 @@ namespace skydiveLogs_api.DomainBusiness
return _cacheService.Get<IEnumerable<Aircraft>>(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

View File

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

View File

@@ -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<List<Aircraft>>(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<List<DropZone>>(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<List<JumpType>>(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
}

View File

@@ -15,7 +15,7 @@ namespace skydiveLogs_api.DomainBusiness.Interfaces
IEnumerable<Aircraft> GetAllAircrafts();
void UpdateAircraft(int id, Aircraft aircraft);
bool UpdateAircraft(int id, Aircraft aircraft, bool resetCache = true);
#endregion Public Methods
}

View File

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

View File

@@ -5,6 +5,7 @@
#region Public Methods
public void GenerateDb();
public void Update();
#endregion Public Methods
}

View File

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

View File

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