Skip to content

Commit b83330b

Browse files
committed
Improvements to upgrade and notifications
1 parent 8f3176a commit b83330b

File tree

11 files changed

+134
-39
lines changed

11 files changed

+134
-39
lines changed

src/App/Pages/Admin/AdminPageModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace App.Pages.Admin
88
public class AdminPageModel : PageModel
99
{
1010
public bool IsAdmin { get; set; }
11+
public bool HasNewVersion { get; set; }
1112

1213
public IEnumerable<Notification> Notifications { get; set; }
1314

src/App/Pages/Admin/Shared/_Layout.cshtml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,21 @@
7878
{
7979
foreach (var note in _notifications)
8080
{
81-
<div class="alert alert-primary" role="alert">
82-
@Html.Raw(note.Content)
83-
<button type="button" class="close" onclick="notificationsController.remove('@note.Id')" data-dismiss="alert" aria-label="Close">
84-
<span aria-hidden="true">&times;</span>
85-
</button>
86-
</div>
81+
@if (note.AlertType == AlertType.Sticky)
82+
{
83+
<div class="alert alert-warning" role="alert">
84+
@Html.Raw(note.Content)
85+
</div>
86+
}
87+
else
88+
{
89+
<div class="alert alert-primary" role="alert">
90+
@Html.Raw(note.Content)
91+
<button type="button" class="close" onclick="notificationsController.remove('@note.Id')" data-dismiss="alert" aria-label="Close">
92+
<span aria-hidden="true">&times;</span>
93+
</button>
94+
</div>
95+
}
8796
}
8897
}
8998
</div>

src/App/Pages/Admin/Upgrade/Index.cshtml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
@model App.Pages.Admin.Upgrade.UpgradeModel
33
@{
44
Layout = null;
5+
var repoLink = "https://github.com/blogifierdotnet/Blogifier/releases/tag/v" + Model.NewVersion;
56
}
67
<!DOCTYPE html>
78
<html>
@@ -14,13 +15,17 @@
1415
<br />
1516
<form method="post" asp-antiforgery="true">
1617
<div class="jumbotron">
17-
<h1>Upgrade to version 2.1</h1>
18+
<h1>Upgrade to version @Model.NewVersion</h1>
1819
<p>
19-
You running Blogifier version 2.0. There new version 2.1 is <a href="#">available in the repository</a>. You can upgrade to new version manually or just click button below and run in-place upgrade process.
20+
You running Blogifier version @Model.OldVersion. There new version @Model.NewVersion is <a href="@repoLink" target="_blank">available in the repository</a>. You can upgrade to new version manually or just click button below and run in-place upgrade process.
2021
</p>
21-
<p>
22-
Please do <b>back up your data</b> before running upgrade, manual or automatic - if something goes wrong it's always good to have a fallback plan!
23-
</p>
22+
23+
<div class="alert alert-warning" role="alert">
24+
<p>
25+
Please do <b>back up your data</b> before running upgrade, manual or automatic.
26+
</p>
27+
Upgrade will turn off your web service before replacing files. You may need to manually turn it on again from your hosting admin panel.
28+
</div>
2429
@if (Model.IsUpgrading)
2530
{
2631
<div class="alert alert-success" role="alert">

src/App/Pages/Admin/Upgrade/Index.cshtml.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Core.Services;
1+
using Core;
2+
using Core.Services;
23
using Microsoft.AspNetCore.Hosting;
34
using System;
45
using System.Diagnostics;
@@ -10,18 +11,29 @@ public class UpgradeModel : AdminPageModel
1011
{
1112
private IApplicationLifetime _app;
1213
private IWebService _ws;
14+
private IDataService _db;
1315

1416
public bool IsUpgrading = false;
17+
public string OldVersion;
18+
public string NewVersion;
1519

16-
public UpgradeModel(IApplicationLifetime app, IWebService ws)
20+
public UpgradeModel(IApplicationLifetime app, IWebService ws, IDataService db)
1721
{
1822
_app = app;
1923
_ws = ws;
24+
_db = db;
2025
}
2126

2227
public void OnGet()
2328
{
24-
// check for newer version
29+
OldVersion = AppSettings.Version.Substring(0, 3);
30+
31+
var field = _db.CustomFields.Single(f => f.Name == Constants.NewestVersion && f.AuthorId == 0);
32+
33+
if(field != null)
34+
{
35+
NewVersion = field.Content.Substring(0, 1) + "." + field.Content.Substring(1, 1);
36+
}
2537
}
2638

2739
public async Task OnPost()
@@ -31,14 +43,14 @@ public async Task OnPost()
3143
if (string.IsNullOrEmpty(await _ws.DownloadLatestRelease()))
3244
{
3345
// start upgrade process
34-
//Process p = new Process();
35-
//p.StartInfo.FileName = "dotnet";
36-
//p.StartInfo.Arguments = "Upgrade.dll";
37-
//p.StartInfo.UseShellExecute = false;
38-
//p.StartInfo.CreateNoWindow = false;
39-
//p.Start();
40-
41-
//_app.StopApplication();
46+
Process p = new Process();
47+
p.StartInfo.FileName = "dotnet";
48+
p.StartInfo.Arguments = "Upgrade.dll";
49+
p.StartInfo.UseShellExecute = false;
50+
p.StartInfo.CreateNoWindow = false;
51+
p.Start();
52+
53+
_app.StopApplication();
4254

4355
//Program.Main(null);
4456
//Process.GetCurrentProcess().Kill();

src/Core/Constants.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Core
2+
{
3+
public class Constants
4+
{
5+
public static string NewestVersion = "last-version";
6+
public static string UpgradeDirectory = "_upgrade";
7+
}
8+
}

src/Core/Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
5-
<Version>2.0.2.2</Version>
5+
<Version>2.0.2.3</Version>
66
</PropertyGroup>
77

88
<ItemGroup>

src/Core/Data/Domain/Notification.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public class Notification
1515

1616
public enum AlertType
1717
{
18-
Primary, Success, Warning, Error
18+
Primary, Success, Warning, Error, Sticky
1919
}
2020
}

src/Core/Services/DataService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public interface IDataService : IDisposable
99
IAuthorRepository Authors { get; }
1010
INotificationRepository Notifications { get; }
1111
IHtmlWidgetRepository HtmlWidgets { get; }
12+
ICustomFieldRepository CustomFields { get; }
1213

1314
int Complete();
1415
}
@@ -25,12 +26,14 @@ public DataService(AppDbContext db)
2526
Authors = new AuthorRepository(_db);
2627
Notifications = new NotificationRepository(_db);
2728
HtmlWidgets = new HtmlWidgetRepository(_db);
29+
CustomFields = new CustomFieldRepository(_db);
2830
}
2931

3032
public IPostRepository BlogPosts { get; private set; }
3133
public IAuthorRepository Authors { get; private set; }
3234
public INotificationRepository Notifications { get; private set; }
3335
public IHtmlWidgetRepository HtmlWidgets { get; private set; }
36+
public ICustomFieldRepository CustomFields { get; private set; }
3437

3538
public int Complete()
3639
{

src/Core/Services/NotificationService.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,33 @@ public async Task<IEnumerable<Notification>> GetNotifications(int authorId)
6060
var notes = _db.Notifications
6161
.Find(n => n.Active && (n.AuthorId == 0 || n.AuthorId == authorId))
6262
.OrderByDescending(n => n.DateNotified)
63-
.Take(5);
63+
.Take(5).ToList();
64+
65+
// add notification if newer version exists
66+
if(authorId > 0)
67+
{
68+
var author = _db.Authors.Single(a => a.Id == authorId);
69+
if(author != null && author.IsAdmin)
70+
{
71+
var field = _db.CustomFields.Single(f => f.Name == Constants.NewestVersion && f.AuthorId == 0);
72+
if(field != null)
73+
{
74+
int current, latest;
75+
76+
int.TryParse(AppSettings.Version.Replace(".", "").Substring(0, 2), out current);
77+
int.TryParse(field.Content, out latest);
78+
79+
if(current < latest)
80+
{
81+
notes.Add(new Notification
82+
{
83+
AlertType = AlertType.Sticky,
84+
Content = $"Upgrade to <a href=\"upgrade\">version {field.Content.Substring(0,1)}.{field.Content.Substring(1, 1)}</a>"
85+
});
86+
}
87+
}
88+
}
89+
}
6490

6591
return await Task.FromResult(notes);
6692
}

src/Core/Services/WebService.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public async Task<string> CheckForLatestRelease()
4646
{
4747
var dwnUrl = repo.assets[0].browser_download_url;
4848
result = $"The new Blogifier <a href='{repo.html_url}' class='alert-link' target='_blank'>{repo.name}</a> is available for download";
49+
50+
var field = _db.CustomFields.Single(f => f.Name == Constants.NewestVersion && f.AuthorId == 0);
51+
if(field == null || (field != null && int.Parse(field.Content) < latest))
52+
{
53+
_db.CustomFields.Add(new Data.CustomField
54+
{
55+
AuthorId = 0,
56+
Name = Constants.NewestVersion,
57+
Content = latest.ToString()
58+
});
59+
_db.Complete();
60+
}
4961
}
5062
}
5163

0 commit comments

Comments
 (0)