Skip to content

Commit

Permalink
Photos selected as main are tagged with corresponding page if no tags…
Browse files Browse the repository at this point in the history
… exist. Closes #297.
  • Loading branch information
impworks committed Jun 15, 2024
1 parent ae85e91 commit 7f8fc70
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Bonsai/Areas/Admin/Controllers/PagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Bonsai.Areas.Admin.Controllers;
/// Controller for handling pages.
/// </summary>
[Route("admin/pages")]
public class PagesController(PagesManagerService pagesSvc, ISearchEngine search, AppDbContext db, IBackgroundJobService jobs)
public class PagesController(PagesManagerService pagesSvc, MediaManagerService mediaSvc, ISearchEngine search, AppDbContext db, IBackgroundJobService jobs)
: AdminControllerBase
{
protected override Type ListStateType => typeof(PagesListRequestVM);
Expand Down Expand Up @@ -88,6 +88,10 @@ public async Task<ActionResult> Create(PageEditorVM vm)
{
var page = await pagesSvc.CreateAsync(vm, User);
await db.SaveChangesAsync();

await mediaSvc.TryAddMainPhotoTagAsync(page.MainPhoto, page, User);
await db.SaveChangesAsync();

await search.AddPageAsync(page);
await jobs.RunAsync(JobBuilder.For<TreeLayoutJob>().SupersedeAll());

Expand Down Expand Up @@ -124,6 +128,7 @@ public async Task<ActionResult> Update(PageEditorVM vm, string tab)
try
{
var page = await pagesSvc.UpdateAsync(vm, User);
await mediaSvc.TryAddMainPhotoTagAsync(page.MainPhoto, page, User);
await db.SaveChangesAsync();
await search.AddPageAsync(page);
await jobs.RunAsync(JobBuilder.For<TreeLayoutJob>().SupersedeAll());
Expand Down
18 changes: 18 additions & 0 deletions src/Bonsai/Areas/Admin/Logic/MediaManagerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,24 @@ public async Task<IReadOnlyList<MediaUploadResultVM>> GetThumbnailsAsync(IEnumer
.FirstOrDefaultAsync();
}

/// <summary>
/// Tags the whole image with the corresponding page.
/// </summary>
public async Task TryAddMainPhotoTagAsync(Media media, Page page, ClaimsPrincipal principal)
{
if (media == null || media.Tags.Any())
return;

var vm = await RequestUpdateAsync(media.Id);
var tag = new MediaTagVM
{
PageId = page.Id,
Coordinates = "0;0;1;1"
};
vm.DepictedEntities = JsonConvert.SerializeObject(new[] {tag});
await UpdateAsync(vm, principal);
}

#endregion

#region Helpers
Expand Down
5 changes: 3 additions & 2 deletions src/Bonsai/Areas/Admin/Logic/PagesManagerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public async Task<Page> UpdateAsync(PageEditorVM vm, ClaimsPrincipal principal,
_db.Changes.Add(changeset);

_mapper.Map(vm, page);
page.MainPhotoId = (await FindMainPhotoAsync(vm.MainPhotoKey))?.Id;
page.MainPhoto = await FindMainPhotoAsync(vm.MainPhotoKey);
page.LivingBeingOverview = MapLivingBeingOverview(vm, page.LivingBeingOverview);
page.NormalizedTitle = PageHelper.NormalizeTitle(page.Title);

Expand All @@ -248,7 +248,7 @@ public async Task<Page> UpdateAsync(PageEditorVM vm, ClaimsPrincipal principal,
_cache.Remove<InfoBlockVM>(page.Key);
}

if(revertedChangeId == null)
if (revertedChangeId == null)
await DiscardPageDraftAsync(vm.Id, principal);

return page;
Expand Down Expand Up @@ -546,6 +546,7 @@ private async Task<Media> FindMainPhotoAsync(string key)
return null;

var media = await _db.Media
.Include(x => x.Tags)
.FirstOrDefaultAsync(x => x.Key == key && x.IsDeleted == false);

if(media == null)
Expand Down

0 comments on commit 7f8fc70

Please sign in to comment.