196 lines
6.0 KiB
C#
196 lines
6.0 KiB
C#
using skydiveLogs_api.Domain;
|
|
using skydiveLogs_api.DomainBusiness.Interfaces;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
|
|
namespace skydiveLogs_api.DomainBusiness
|
|
{
|
|
public class InitDbService : IInitDbService
|
|
{
|
|
#region Public Constructors
|
|
|
|
public InitDbService(IAircraftService aircraftService,
|
|
IJumpTypeService jumpTypeService,
|
|
IDropZoneService dropZoneService,
|
|
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();
|
|
LoadDropZones();
|
|
LoadJumpTypes();
|
|
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);
|
|
}
|
|
|
|
#region Init tables and informations
|
|
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()
|
|
{
|
|
var jsonString = File.ReadAllText("Init/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.AddNewDz(item);
|
|
}
|
|
}
|
|
|
|
private void LoadJumpTypes()
|
|
{
|
|
var jsonString = File.ReadAllText("Init/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.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
|
|
}
|
|
} |