diff --git a/zero.Core/Api/Setup/SetupApi.cs b/zero.Core/Api/Setup/SetupApi.cs index c81f2018..80f14ed2 100644 --- a/zero.Core/Api/Setup/SetupApi.cs +++ b/zero.Core/Api/Setup/SetupApi.cs @@ -1,13 +1,20 @@ using FluentValidation.Results; using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using Raven.Client.Documents; using Raven.Client.Documents.Conventions; using Raven.Client.Documents.Session; using System; +using System.Collections.Generic; +using System.IO; using System.Threading.Tasks; using zero.Core.Entities; using zero.Core.Entities.Setup; using zero.Core.Validation; +using Microsoft.AspNetCore.Hosting; + namespace zero.Core.Api { @@ -53,6 +60,8 @@ namespace zero.Core.Api return Constants.Database.CollectionPrefix + DocumentConventions.DefaultGetCollectionName(type); }; + raven.Conventions.IdentityPartsSeparator = "."; + raven.Initialize(); // create application @@ -86,6 +95,9 @@ namespace zero.Core.Api await session.StoreAsync(user); + // update settings file. if this fails the changes won't be stored + UpdateSettingsFile(model); + await session.SaveChangesAsync(); } } @@ -102,6 +114,30 @@ namespace zero.Core.Api return EntityChangeResult.Success(model); } + + + /// + /// Updates the settings file with the new data (database connection and version) + /// + void UpdateSettingsFile(SetupModel model) + { + var filePath = Path.Combine(model.ContentRootPath, "zeroSettings.json"); + string json = File.ReadAllText(filePath); + + ZeroConfiguration config = JsonConvert.DeserializeObject(json); + + config.Raven = new RavenConfig() + { + Database = model.Database.Name, + Url = model.Database.Url + }; + + config.ZeroVersion = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString(); + + json = JsonConvert.SerializeObject(config, Formatting.Indented); + + File.WriteAllText(filePath, json); + } } diff --git a/zero.Core/Entities/Setup/SetupModel.cs b/zero.Core/Entities/Setup/SetupModel.cs index aa632e4c..a4b7d2a9 100644 --- a/zero.Core/Entities/Setup/SetupModel.cs +++ b/zero.Core/Entities/Setup/SetupModel.cs @@ -7,6 +7,8 @@ public SetupUserModel User { get; set; } public SetupDatabaseModel Database { get; set; } + + public string ContentRootPath { get; set; } } diff --git a/zero.Web/Controllers/SetupController.cs b/zero.Web/Controllers/SetupController.cs index 493a06f8..d56210a6 100644 --- a/zero.Web/Controllers/SetupController.cs +++ b/zero.Web/Controllers/SetupController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using System; using System.Linq; @@ -7,6 +8,7 @@ using zero.Core; using zero.Core.Api; using zero.Core.Entities; using zero.Core.Entities.Setup; +using zero.Core.Extensions; using zero.Web.Controllers; namespace zero.Web.Setup @@ -16,20 +18,30 @@ namespace zero.Web.Setup { protected ISetupApi Api { get; private set; } + protected IWebHostEnvironment Environment { get; private set; } - public SetupController(IZeroConfiguration config, ISetupApi api) : base(config) + + public SetupController(IZeroConfiguration config, ISetupApi api, IWebHostEnvironment env) : base(config) { Api = api; + Environment = env; } public IActionResult Index() { + if (!Configuration.ZeroVersion.IsNullOrEmpty()) + { + return RedirectToAction("Index", "Index"); + } + return View("/Views/Setup.cshtml"); } [HttpPost] public async Task Install([FromBody] SetupModel model) { + model.ContentRootPath = Environment.ContentRootPath; + EntityChangeResult result = await Api.Install(model); if (result.IsSuccess) diff --git a/zero.Web/zero.Web.csproj b/zero.Web/zero.Web.csproj index 9f067469..0b756f7a 100644 --- a/zero.Web/zero.Web.csproj +++ b/zero.Web/zero.Web.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 InProcess - 1.0.0 + 1.0.0-alpha false Embedded Tobias Klika diff --git a/zero.Web/zeroSettings.json b/zero.Web/zeroSettings.json index 893eff95..c3a54276 100644 --- a/zero.Web/zeroSettings.json +++ b/zero.Web/zeroSettings.json @@ -1,17 +1,15 @@ -{ - "ZeroVersion": null, +{ + "ZeroVersion": "1.0.0.0", "DefaultLanguage": "en-US", "Raven": { - "Url": "http://127.0.0.1:9800", + "Url": "http://localhost:9800", "Database": "zero" }, "Logging": { - "Json": true, "IncludeScopes": false, - "PathFormat": "Logs/log-{Date}.txt", "LogLevel": { "Default": "Warning", "System": "Warning",