Skip to content

Commit

Permalink
Code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
impworks committed May 25, 2024
1 parent 9cfd22f commit e4bcadb
Show file tree
Hide file tree
Showing 22 changed files with 183 additions and 397 deletions.
17 changes: 4 additions & 13 deletions src/Bonsai/Areas/Admin/Components/AdminMenuComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,15 @@ namespace Bonsai.Areas.Admin.Components;
/// <summary>
/// Component for displaying the side menu of the admin panel.
/// </summary>
public class AdminMenuComponent: ViewComponent
public class AdminMenuComponent(UserManager<AppUser> userMgr, AppDbContext db) : ViewComponent
{
public AdminMenuComponent(UserManager<AppUser> userMgr, AppDbContext db)
{
_userMgr = userMgr;
_db = db;
}

private readonly UserManager<AppUser> _userMgr;
private readonly AppDbContext _db;

/// <summary>
/// Displays the menu.
/// </summary>
public async Task<IViewComponentResult> InvokeAsync()
{
var user = await _userMgr.GetUserAsync(HttpContext.User);
var roles = await _userMgr.GetRolesAsync(user);
var user = await userMgr.GetUserAsync(HttpContext.User);
var roles = await userMgr.GetRolesAsync(user);

var groups = new List<MenuGroupVM>();
groups.Add(
Expand All @@ -51,7 +42,7 @@ public async Task<IViewComponentResult> InvokeAsync()

if (roles.Contains(nameof(UserRole.Admin)))
{
var newUsers = await _db.Users.CountAsync(x => !x.IsValidated);
var newUsers = await db.Users.CountAsync(x => !x.IsValidated);
groups.Add(
new MenuGroupVM(
new MenuItemVM { Title = Texts.Admin_Menu_Users, Icon = "user-circle-o", Url = Url.Action("Index", "Users", new { area = "Admin" }), NotificationsCount = newUsers },
Expand Down
11 changes: 2 additions & 9 deletions src/Bonsai/Areas/Admin/Components/NotificationTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@ namespace Bonsai.Areas.Admin.Components;
/// Displays a notification if the user has not discarded it.
/// </summary>
[HtmlTargetElement("div", Attributes = "notification-id")]
public class NotificationTagHelper: TagHelper
public class NotificationTagHelper(NotificationsService notifications) : TagHelper
{
public NotificationTagHelper(NotificationsService notifications)
{
_notifications = notifications;
}

private readonly NotificationsService _notifications;

/// <summary>
/// ID of the notification.
/// </summary>
Expand All @@ -24,7 +17,7 @@ public NotificationTagHelper(NotificationsService notifications)

public override void Process(TagHelperContext context, TagHelperOutput output)
{
if(!_notifications.IsShown(NotificationId))
if(!notifications.IsShown(NotificationId))
{
output.SuppressOutput();
return;
Expand Down
23 changes: 7 additions & 16 deletions src/Bonsai/Areas/Admin/Controllers/ChangesetsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,8 @@ namespace Bonsai.Areas.Admin.Controllers;
/// Controller for managing changesets.
/// </summary>
[Route("admin/changes")]
public class ChangesetsController: AdminControllerBase
public class ChangesetsController(ChangesetsManagerService changes, AppDbContext db) : AdminControllerBase
{
public ChangesetsController(ChangesetsManagerService changes, AppDbContext db)
{
_changes = changes;
_db = db;
}

private readonly ChangesetsManagerService _changes;
private readonly AppDbContext _db;

protected override Type ListStateType => typeof(ChangesetsListRequestVM);

#region Public methods
Expand All @@ -35,7 +26,7 @@ public ChangesetsController(ChangesetsManagerService changes, AppDbContext db)
public async Task<ActionResult> Index(ChangesetsListRequestVM request)
{
PersistListState(request);
var vm = await _changes.GetChangesetsAsync(request);
var vm = await changes.GetChangesetsAsync(request);
return View(vm);
}

Expand All @@ -46,7 +37,7 @@ public async Task<ActionResult> Index(ChangesetsListRequestVM request)
[Route("details")]
public async Task<ActionResult> Details(Guid id)
{
var vm = await _changes.GetChangesetDetailsAsync(id);
var vm = await changes.GetChangesetDetailsAsync(id);
return View(vm);
}

Expand All @@ -57,7 +48,7 @@ public async Task<ActionResult> Details(Guid id)
[Route("revert")]
public async Task<ActionResult> Revert(Guid id)
{
var vm = await _changes.GetChangesetDetailsAsync(id);
var vm = await changes.GetChangesetDetailsAsync(id);
if (!vm.CanRevert)
throw new OperationException(Texts.Admin_Changesets_CannotRevertMessage);

Expand All @@ -71,12 +62,12 @@ public async Task<ActionResult> Revert(Guid id)
[Route("revert")]
public async Task<ActionResult> Revert(Guid id, bool confirm)
{
var editVm = await _changes.GetChangesetDetailsAsync(id);
var editVm = await changes.GetChangesetDetailsAsync(id);
if (!editVm.CanRevert)
throw new OperationException(Texts.Admin_Changesets_CannotRevertMessage);

await _changes.RevertChangeAsync(id, User);
await _db.SaveChangesAsync();
await changes.RevertChangeAsync(id, User);
await db.SaveChangesAsync();

return RedirectToSuccess(Texts.Admin_Changesets_RevertedMessage);
}
Expand Down
13 changes: 3 additions & 10 deletions src/Bonsai/Areas/Admin/Controllers/DashboardController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,15 @@ namespace Bonsai.Areas.Admin.Controllers;
/// Controller for the default admin page.
/// </summary>
[Route("admin/home")]
public class DashboardController: AdminControllerBase
public class DashboardController(DashboardPresenterService dash) : AdminControllerBase
{
public DashboardController(DashboardPresenterService dash)
{
_dash = dash;
}

private readonly DashboardPresenterService _dash;

/// <summary>
/// Displays the main page.
/// </summary>
[Route("")]
public async Task<ActionResult> Index()
{
var vm = await _dash.GetDashboardAsync();
var vm = await dash.GetDashboardAsync();
return View(vm);
}

Expand All @@ -34,7 +27,7 @@ public async Task<ActionResult> Index()
[Route("events")]
public async Task<ActionResult> Events([FromQuery] int page = 0)
{
var vm = await _dash.GetEventsAsync(page).ToListAsync();
var vm = await dash.GetEventsAsync(page).ToListAsync();
return View(vm);
}
}
28 changes: 9 additions & 19 deletions src/Bonsai/Areas/Admin/Controllers/DraftsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,18 @@ namespace Bonsai.Areas.Admin.Controllers;
/// Controller for handling drafts.
/// </summary>
[Route("admin/drafts")]
public class DraftsController : AdminControllerBase
public class DraftsController(PagesManagerService pagesSvc, PagePresenterService pagePresenter, AppDbContext db)
: AdminControllerBase
{
public DraftsController(PagesManagerService pages, PagePresenterService pagePresenter, AppDbContext db)
{
_pages = pages;
_pagePresenter = pagePresenter;
_db = db;
}

private readonly PagesManagerService _pages;
private readonly PagePresenterService _pagePresenter;
private readonly AppDbContext _db;

/// <summary>
/// Discards the draft of a page.
/// </summary>
[HttpPost]
[Route("update")]
public async Task<ActionResult> Update(PageEditorVM vm)
{
var info = await _pages.UpdatePageDraftAsync(vm, User);
await _db.SaveChangesAsync();
var info = await pagesSvc.UpdatePageDraftAsync(vm, User);
await db.SaveChangesAsync();

return Json(info);
}
Expand All @@ -47,8 +37,8 @@ public async Task<ActionResult> Update(PageEditorVM vm)
[Route("remove")]
public async Task<ActionResult> Remove(Guid? id, PageType? type)
{
await _pages.DiscardPageDraftAsync(id, User);
await _db.SaveChangesAsync();
await pagesSvc.DiscardPageDraftAsync(id, User);
await db.SaveChangesAsync();

if (id != null && id != Guid.Empty)
return RedirectToAction("Update", "Pages", new { id = id });
Expand All @@ -62,14 +52,14 @@ public async Task<ActionResult> Remove(Guid? id, PageType? type)
[Route("preview")]
public async Task<ActionResult> Preview(Guid? id)
{
var page = await _pages.GetPageDraftPreviewAsync(id, User);
var page = await pagesSvc.GetPageDraftPreviewAsync(id, User);
if (page == null)
return NotFound();

var vm = new PageVM<PageDescriptionVM>
{
Body = await _pagePresenter.GetPageDescriptionAsync(page),
InfoBlock = await _pagePresenter.GetPageInfoBlockAsync(page)
Body = await pagePresenter.GetPageDescriptionAsync(page),
InfoBlock = await pagePresenter.GetPageInfoBlockAsync(page)
};
ViewBag.IsPreview = true;
ViewBag.DisableSearch = true;
Expand Down
32 changes: 9 additions & 23 deletions src/Bonsai/Areas/Admin/Controllers/DynamicConfigController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,17 @@ namespace Bonsai.Areas.Admin.Controllers;
/// Controller for managing the global configuration.
/// </summary>
[Route("admin/config")]
public class DynamicConfigController: AdminControllerBase
public class DynamicConfigController(DynamicConfigManagerService configMgr, BonsaiConfigService configSvc, IBackgroundJobService jobsScv, AppDbContext db, CacheService cache)
: AdminControllerBase
{
public DynamicConfigController(DynamicConfigManagerService configMgr, BonsaiConfigService config, IBackgroundJobService jobs, AppDbContext db, CacheService cache)
{
_configMgr = configMgr;
_config = config;
_jobs = jobs;
_db = db;
_cache = cache;
}

private readonly DynamicConfigManagerService _configMgr;
private readonly BonsaiConfigService _config;
private readonly IBackgroundJobService _jobs;
private readonly AppDbContext _db;
private readonly CacheService _cache;

/// <summary>
/// Displays the form and edits the config.
/// </summary>
[HttpGet]
[Route("")]
public async Task<ActionResult> Index()
{
var vm = await _configMgr.RequestUpdateAsync();
var vm = await configMgr.RequestUpdateAsync();
return View(vm);
}

Expand All @@ -52,16 +38,16 @@ public async Task<ActionResult> Index()
[Route("")]
public async Task<ActionResult> Update(UpdateDynamicConfigVM vm)
{
var oldValue = await _configMgr.RequestUpdateAsync();
await _configMgr.UpdateAsync(vm);
await _db.SaveChangesAsync();
var oldValue = await configMgr.RequestUpdateAsync();
await configMgr.UpdateAsync(vm);
await db.SaveChangesAsync();

_config.ResetCache();
configSvc.ResetCache();

if (IsTreeConfigChanged())
{
_cache.Remove<PageTreeVM>();
await _jobs.RunAsync(JobBuilder.For<TreeLayoutJob>().SupersedeAll());
cache.Remove<PageTreeVM>();
await jobsScv.RunAsync(JobBuilder.For<TreeLayoutJob>().SupersedeAll());
}

return RedirectToSuccess(Texts.Admin_Config_SavedMessage);
Expand Down
44 changes: 16 additions & 28 deletions src/Bonsai/Areas/Admin/Controllers/MediaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,9 @@ namespace Bonsai.Areas.Admin.Controllers;
/// Controller for managing media files.
/// </summary>
[Route("admin/media")]
public class MediaController: AdminControllerBase
public class MediaController(MediaManagerService mediaSvc, PagesManagerService pagesSvc, IBackgroundJobService jobs, AppDbContext db)
: AdminControllerBase
{
public MediaController(MediaManagerService media, PagesManagerService pages, IBackgroundJobService jobs, AppDbContext db)
{
_media = media;
_pages = pages;
_jobs = jobs;
_db = db;
}

private readonly MediaManagerService _media;
private readonly PagesManagerService _pages;
private readonly IBackgroundJobService _jobs;
private readonly AppDbContext _db;

protected override Type ListStateType => typeof(MediaListRequestVM);

/// <summary>
Expand All @@ -50,7 +38,7 @@ public MediaController(MediaManagerService media, PagesManagerService pages, IBa
public async Task<ActionResult> Index(MediaListRequestVM request)
{
PersistListState(request);
var vm = await _media.GetMediaAsync(request);
var vm = await mediaSvc.GetMediaAsync(request);
return View(vm);
}

Expand All @@ -74,13 +62,13 @@ public async Task<ActionResult> Upload(MediaUploadRequestVM vm, IFormFile file)
{
try
{
var result = await _media.UploadAsync(vm, file, User);
var result = await mediaSvc.UploadAsync(vm, file, User);
result.ThumbnailPath = Url.Content(result.ThumbnailPath);

await _db.SaveChangesAsync();
await db.SaveChangesAsync();

if (!result.IsProcessed)
await _jobs.RunAsync(JobBuilder.For<MediaEncoderJob>().WithArgs(result.Id));
await jobs.RunAsync(JobBuilder.For<MediaEncoderJob>().WithArgs(result.Id));

return Json(result);
}
Expand All @@ -101,7 +89,7 @@ public async Task<ActionResult> Upload(MediaUploadRequestVM vm, IFormFile file)
[Route("thumbs")]
public async Task<ActionResult> GetThumbnails(IEnumerable<Guid> ids)
{
var vms = await _media.GetThumbnailsAsync(ids);
var vms = await mediaSvc.GetThumbnailsAsync(ids);

foreach(var vm in vms)
vm.ThumbnailPath = Url.Content(vm.ThumbnailPath);
Expand All @@ -116,7 +104,7 @@ public async Task<ActionResult> GetThumbnails(IEnumerable<Guid> ids)
[Route("update")]
public async Task<ActionResult> Update(Guid id, MediaEditorSaveAction? saveAction = null)
{
var vm = await _media.RequestUpdateAsync(id);
var vm = await mediaSvc.RequestUpdateAsync(id);

if (saveAction != null)
vm.SaveAction = saveAction.Value;
Expand All @@ -136,14 +124,14 @@ public async Task<ActionResult> Update(MediaEditorVM vm)

try
{
await _media.UpdateAsync(vm, User);
await _db.SaveChangesAsync();
await mediaSvc.UpdateAsync(vm, User);
await db.SaveChangesAsync();

ShowMessage(Texts.Admin_Media_UpdatedMessage);

if (vm.SaveAction == MediaEditorSaveAction.SaveAndShowNext)
{
var nextId = await _media.GetNextUntaggedMediaAsync();
var nextId = await mediaSvc.GetNextUntaggedMediaAsync();
if (nextId != null)
return RedirectToAction("Update", new {id = nextId.Value, saveAction = vm.SaveAction});
}
Expand All @@ -164,7 +152,7 @@ public async Task<ActionResult> Update(MediaEditorVM vm)
[Route("remove")]
public async Task<ActionResult> Remove(Guid id)
{
ViewBag.Info = await _media.RequestRemoveAsync(id, User);
ViewBag.Info = await mediaSvc.RequestRemoveAsync(id, User);
return View(new RemoveEntryRequestVM { Id = id });
}

Expand All @@ -176,11 +164,11 @@ public async Task<ActionResult> Remove(Guid id)
public async Task<ActionResult> Remove(RemoveEntryRequestVM vm)
{
if (vm.RemoveCompletely)
await _media.RemoveCompletelyAsync(vm.Id, User);
await mediaSvc.RemoveCompletelyAsync(vm.Id, User);
else
await _media.RemoveAsync(vm.Id, User);
await mediaSvc.RemoveAsync(vm.Id, User);

await _db.SaveChangesAsync();
await db.SaveChangesAsync();

return RedirectToSuccess(Texts.Admin_Media_RemovedMessage);
}
Expand All @@ -197,7 +185,7 @@ private async Task<ActionResult> ViewEditorFormAsync(MediaEditorVM vm)
.Concat(new[] {vm.Location, vm.Event}.Select(x => x.TryParse<Guid?>()))
.ToList();

var pageLookup = await _pages.FindPagesByIdsAsync(ids);
var pageLookup = await pagesSvc.FindPagesByIdsAsync(ids);

foreach(var depicted in depictedEntities)
if (depicted.PageId != null && pageLookup.TryGetValue(depicted.PageId.Value, out var depPage))
Expand Down
Loading

0 comments on commit e4bcadb

Please sign in to comment.