private void MarkDeleted <T>(ResourceType resourceType, int entityId) where T : ContentBase { var collection = _db.GetCollection <T>(resourceType.Name); var entity = collection.AsQueryable().First(x => x.Id == entityId); entity.Status = ContentStatus.Deleted; collection.ReplaceOne(x => x.Id == entityId, entity); }
/// <summary> /// Gets a new, never-used-before ID for a new entity of the specified type. /// </summary> public int NextId(ResourceType entityType) { lock (_lockObject) { var info = GetOrCreateEntityTypeInfo(entityType); return(++info.MaximumId); } }
/// <summary> /// Determines whether an entity with the specified type and ID exists. /// </summary> public bool Exists(ResourceType entityType, int id) { lock (_lockObject) { var info = GetOrCreateEntityTypeInfo(entityType); return(info.Entities.TryGetValue(id, out var entity) && entity.Status != ContentStatus.Deleted); } }
private EntityTypeInfo GetOrCreateEntityTypeInfo(ResourceType entityType) { if (_types.TryGetValue(entityType, out var info)) { return(info); } return(_types[entityType] = new EntityTypeInfo()); }
/// <summary> /// Get UserId of an entity owner /// </summary> public string Owner(ResourceType entityType, int id) { var info = GetOrCreateEntityTypeInfo(entityType); if (info.Entities.TryGetValue(id, out var entity)) { return(entity.UserId); } return(null); }
/// <summary> /// Gets the current status of an entity given its type and ID. /// </summary> public ContentStatus?Status(ResourceType entityType, int id) { lock (_lockObject) { var info = GetOrCreateEntityTypeInfo(entityType); if (info.Entities.TryGetValue(id, out var entity)) { return(entity.Status); } return(null); } }
/// <summary> /// Gets the IDs of all entities of the given type and status. /// </summary> public IReadOnlyCollection <int> AllIds(ResourceType entityType, ContentStatus status, IIdentity user) { lock (_lockObject) { bool isAllowedGetAll = UserPermissions.IsAllowedToGetAll(user, status); string userId = user.GetUserIdentity(); var info = GetOrCreateEntityTypeInfo(entityType); return(info.Entities .AsQueryable() .FilterIf(!isAllowedGetAll, x => ((status == ContentStatus.All) && (x.Value.Status == ContentStatus.Published)) || (x.Value.UserId == userId)) .FilterIf(status == ContentStatus.All, x => x.Value.Status != ContentStatus.Deleted) .Where(x => status == ContentStatus.All || x.Value.Status == status) .Select(x => x.Key) .ToList()); } }
/// <summary> /// Returns a timestamp indicating the last time the entity with the specified type and ID or /// any directly or indirectly referenced entity was modified (i.e. created or updated). /// </summary> public DateTimeOffset LastModificationCascading(ResourceType type, int id) =>
/// <summary> /// Returns the IDs of the entities that reference the entity with the specified type and ID. /// </summary> public IReadOnlyCollection <EntityId> ReferencesTo(ResourceType type, int id) { var key = new EntityId(type, id); return(_referencesTo.TryGetValue(key, out var set) ? set.ToArray() : Array.Empty <EntityId>()); }
/// <summary> /// Checks whether an entity is referenced by any other entities. /// </summary> /// <returns>True if there are references to the entity</returns> public bool IsUsed(ResourceType type, int id) { var key = new EntityId(type, id); return(_referencesTo.TryGetValue(key, out var set) && set.Count > 0); }