add last modified date to all entities
This commit is contained in:
@@ -211,6 +211,7 @@ namespace zero.Core.Api
|
||||
{
|
||||
zeroEntity.Alias = Safenames.Alias(zeroEntity.Name);
|
||||
zeroEntity.LastModifiedById = userId;
|
||||
zeroEntity.LastModifiedDate = DateTimeOffset.Now;
|
||||
zeroEntity.CreatedById ??= userId;
|
||||
}
|
||||
|
||||
|
||||
@@ -66,12 +66,10 @@ namespace zero.Core.Api
|
||||
// create revision objects
|
||||
foreach (T item in items)
|
||||
{
|
||||
DateTime? date = session.Advanced.GetLastModifiedFor(item);
|
||||
|
||||
Revision revision = new Revision()
|
||||
{
|
||||
ChangeVector = session.Advanced.GetChangeVectorFor(item),
|
||||
Date = date.HasValue ? new DateTimeOffset(date.Value) : item.CreatedDate,
|
||||
Date = item.LastModifiedDate,
|
||||
Json = includeContent ? JsonConvert.SerializeObject(item) : null
|
||||
};
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace zero.Core.Database.Indexes
|
||||
|
||||
public string AppId { get; set; }
|
||||
|
||||
public DateTime LastModified { get; set; }
|
||||
public DateTimeOffset LastModified { get; set; }
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace zero.Core.Database.Indexes
|
||||
{
|
||||
Id = x.Id,
|
||||
AppId = x.AppId,
|
||||
LastModified = MetadataFor(x).Value<DateTime>("@last-modified")
|
||||
LastModified = x.LastModifiedDate
|
||||
});
|
||||
|
||||
StoreAllFields(FieldStorage.Yes);
|
||||
|
||||
@@ -36,9 +36,6 @@ namespace zero.Core.Entities
|
||||
/// <inheritdoc />
|
||||
public MediaImageMeta ImageMeta { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTimeOffset LastModifiedDate { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public MediaFocalPoint FocalPoint { get; set; }
|
||||
|
||||
|
||||
@@ -24,6 +24,9 @@ namespace zero.Core.Entities
|
||||
/// <inheritdoc/>
|
||||
public string LastModifiedById { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public DateTimeOffset LastModifiedDate { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string CreatedById { get; set; }
|
||||
|
||||
@@ -63,6 +66,11 @@ namespace zero.Core.Entities
|
||||
/// </summary>
|
||||
public string LastModifiedById { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Date of last modification
|
||||
/// </summary>
|
||||
DateTimeOffset LastModifiedDate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Backoffice user who created this content
|
||||
/// </summary>
|
||||
@@ -72,7 +80,6 @@ namespace zero.Core.Entities
|
||||
/// <summary>
|
||||
/// Date of creation
|
||||
/// </summary>
|
||||
[Overwrite]
|
||||
DateTimeOffset CreatedDate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -9,41 +9,6 @@ namespace zero.Core.Extensions
|
||||
{
|
||||
public static class RavenDocumentSessionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Get revision list (without content JSON) for an entity
|
||||
/// </summary>
|
||||
public static async Task<ListResult<Revision>> GetRevisions<T>(this IAsyncDocumentSession session, string id, int pageNumber = 1, int pageSize = 30) where T : IZeroEntity
|
||||
{
|
||||
List<T> items = await session.Advanced.Revisions.GetForAsync<T>(id, pageNumber - 1, pageSize);
|
||||
List<Revision> revisions = new List<Revision>();
|
||||
|
||||
string[] userIds = items.Select(x => x.LastModifiedById).Distinct().ToArray();
|
||||
Dictionary<string, User> users = await session.LoadAsync<User>(userIds);
|
||||
|
||||
foreach (T item in items)
|
||||
{
|
||||
DateTime? date = session.Advanced.GetLastModifiedFor(item);
|
||||
|
||||
Revision revision = new Revision()
|
||||
{
|
||||
ChangeVector = session.Advanced.GetChangeVectorFor(item),
|
||||
Date = date.HasValue ? new DateTimeOffset(date.Value) : item.CreatedDate
|
||||
};
|
||||
|
||||
if (!item.LastModifiedById.IsNullOrEmpty() && users.TryGetValue(item.LastModifiedById, out User user))
|
||||
{
|
||||
revision.User = new RevisionUser()
|
||||
{
|
||||
AvatarId = user.AvatarId,
|
||||
Id = user.Id,
|
||||
Name = user.Name
|
||||
};
|
||||
}
|
||||
|
||||
revisions.Add(revision);
|
||||
}
|
||||
|
||||
return new ListResult<Revision>(revisions, revisions.Count, pageNumber, pageSize);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
using FluentValidation;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Raven.Client.Documents;
|
||||
using Raven.Client.Documents.Session;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using zero.Commerce.Api;
|
||||
using zero.Commerce.Entities;
|
||||
using zero.Core;
|
||||
using zero.Core.Api;
|
||||
@@ -18,7 +12,6 @@ using zero.Core.Attributes;
|
||||
using zero.Core.Entities;
|
||||
using zero.Core.Extensions;
|
||||
using zero.Core.Utils;
|
||||
using zero.Web.Filters;
|
||||
|
||||
namespace zero.Debug.Controllers
|
||||
{
|
||||
@@ -78,21 +71,23 @@ namespace zero.Debug.Controllers
|
||||
using (IAsyncDocumentSession session = Raven.OpenAsyncSession())
|
||||
{
|
||||
items = await session.Query<T>().ToListAsync();
|
||||
}
|
||||
|
||||
foreach (T item in items)
|
||||
{
|
||||
if (await Save(item))
|
||||
foreach (T item in items)
|
||||
{
|
||||
changedIds.Add(item.Id);
|
||||
if (Update(item, session.Advanced.GetLastModifiedFor(item)))
|
||||
{
|
||||
changedIds.Add(item.Id);
|
||||
}
|
||||
}
|
||||
|
||||
await session.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return changedIds;
|
||||
}
|
||||
|
||||
|
||||
async Task<bool> Save<T>(T model) where T : IZeroIdEntity
|
||||
bool Update<T>(T model, DateTime? modifiedDate) where T : IZeroIdEntity
|
||||
{
|
||||
IAppAwareEntity appAwareEntity = model as IAppAwareEntity;
|
||||
IZeroEntity zeroEntity = model as IZeroEntity;
|
||||
@@ -119,40 +114,37 @@ namespace zero.Debug.Controllers
|
||||
|
||||
string userId = HttpContext.User.Claims.FirstOrDefault(x => x.Type == Constants.Auth.Claims.UserId)?.Value;
|
||||
|
||||
// set default properties
|
||||
if (zeroEntity != null && zeroEntity.CreatedById == null)
|
||||
if (zeroEntity != null)
|
||||
{
|
||||
hasChange = true;
|
||||
zeroEntity.CreatedDate = zeroEntity.CreatedDate == default ? DateTimeOffset.Now : zeroEntity.CreatedDate;
|
||||
zeroEntity.CreatedById = userId;
|
||||
if (zeroEntity.CreatedById == null)
|
||||
{
|
||||
hasChange = true;
|
||||
zeroEntity.CreatedDate = zeroEntity.CreatedDate == default ? DateTimeOffset.Now : zeroEntity.CreatedDate;
|
||||
zeroEntity.CreatedById = userId;
|
||||
}
|
||||
if (model is ITranslation && zeroEntity.Name.IsNullOrEmpty())
|
||||
{
|
||||
hasChange = true;
|
||||
zeroEntity.Name = ((ITranslation)model).Key;
|
||||
}
|
||||
if (zeroEntity.Alias.IsNullOrEmpty())
|
||||
{
|
||||
hasChange = true;
|
||||
zeroEntity.Alias = Safenames.Alias(zeroEntity.Name);
|
||||
}
|
||||
if (zeroEntity.LastModifiedById == default)
|
||||
{
|
||||
hasChange = true;
|
||||
zeroEntity.LastModifiedById = userId;
|
||||
}
|
||||
if (zeroEntity.LastModifiedDate == default)
|
||||
{
|
||||
hasChange = true;
|
||||
zeroEntity.LastModifiedDate = modifiedDate.HasValue ? new DateTimeOffset(modifiedDate.Value) : zeroEntity.CreatedDate;
|
||||
}
|
||||
}
|
||||
|
||||
if (zeroEntity != null && model is ITranslation)
|
||||
{
|
||||
zeroEntity.Name = ((ITranslation)model).Key;
|
||||
}
|
||||
|
||||
// update name alias and last modified
|
||||
if (zeroEntity != null && zeroEntity.Alias.IsNullOrEmpty())
|
||||
{
|
||||
hasChange = true;
|
||||
zeroEntity.Alias = Safenames.Alias(zeroEntity.Name);
|
||||
}
|
||||
if (zeroEntity != null && zeroEntity.LastModifiedById == default)
|
||||
{
|
||||
hasChange = true;
|
||||
zeroEntity.LastModifiedById = userId;
|
||||
}
|
||||
|
||||
if (!hasChange)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using IAsyncDocumentSession session = Raven.OpenAsyncSession();
|
||||
await session.StoreAsync(model);
|
||||
await session.SaveChangesAsync();
|
||||
return true;
|
||||
return hasChange;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,9 @@
|
||||
<ui-property v-if="value.id" label="@ui.id" :is-text="true">
|
||||
{{value.id}}
|
||||
</ui-property>
|
||||
<ui-property v-if="value.id && value.lastModifiedDate" label="@ui.modifiedDate" :is-text="true">
|
||||
<ui-date v-model="value.lastModifiedDate" />
|
||||
</ui-property>
|
||||
<ui-property v-if="value.id" label="@ui.createdDate" :is-text="true">
|
||||
<ui-date v-model="value.createdDate" />
|
||||
</ui-property>
|
||||
|
||||
Reference in New Issue
Block a user