//public static ILogger Logger = null;

        /// <summary>
        /// Write an entire entity wrapper as-is, setting all associated ids.
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static async Task WriteAsync(this IEntityProvider provider, EntityPackage entity)
        {
            var allWrite = new List <EntityBase>();

            allWrite.AddRange(entity.Values);
            allWrite.AddRange(entity.Relations);

            if (entity.Entity.id > 0)
            {
                //If this is an update entity, it's easy. Just save everything all
                //at once, making the whole dang thing atomic
                allWrite.Add(entity.Entity);
                await provider.WriteAsync(allWrite.ToArray());
            }
            else
            {
                //If this is a NEW entity, try to write it first then update the values and relations.
                //If something goes wrong with the values/relations, delete the entity we added.
                await provider.WriteAsync(entity.Entity);

                try
                {
                    entity.Values.ForEach(x => x.entityId     = entity.Entity.id);
                    entity.Relations.ForEach(x => x.entityId2 = entity.Entity.id); //Assume relations are all parents. a user has perms ON this entity, a category OWNS this entity, etc.
                    await provider.WriteAsync(allWrite.ToArray());
                }
                catch
                {
                    await provider.DeleteAsync(entity.Entity);

                    throw;
                }
            }
        }
        /// <summary>
        /// Update the given existing entity with the new entity, preserving the history for the original.
        /// </summary>
        /// <param name="updateData"></param>
        /// <param name="originalData"></param>
        /// <returns></returns>
        public async Task UpdateWithHistoryAsync(EntityPackage updateData, long user, EntityPackage originalData = null)
        {
            logger.LogTrace($"WriteHistoric called for entity {updateData.Entity.id}");

            //The original isn't necessary; we can find it using the id from our apparently updated data
            if (originalData == null)
            {
                originalData = await provider.FindByIdAsync(updateData.Entity.id);
            }

            var history = await CreateHistoricCopyAsync(originalData.Entity);

            try
            {
                //Bring all the existing over to this historic entity
                history.Relink(originalData.Values, originalData.Relations);

                //WE have to link the new stuff to US because we want to write everything all at once
                originalData.Entity.Relink(updateData.Values, updateData.Relations);

                //Add the historic link back to the history copy from the
                originalData.Relations.Add(NewHistoryLink(updateData.Entity, history));

                //A special thing: the values and relations need to be NEW for the update data
                updateData.Relations.ForEach(x => x.id = 0);
                updateData.Values.ForEach(x => x.id    = 0);

                //We're writing the entirety of the "update" data.
                var writes = updateData.FlattenPackage();

                //Also writing the relinked original stuff.
                writes.AddRange(originalData.Values);
                writes.AddRange(originalData.Relations);

                writes.Add(activityService.MakeActivity(updateData.Entity, user, Keys.UpdateAction, history.id.ToString()));

                await provider.WriteAsync(writes.ToArray());
            }
            catch
            {
                logger.LogError("Failure during historic update, trying to undo... Exception bubbling...");
                await provider.DeleteAsync(history);

                throw;
            }
        }
        public static Task DeleteAsync(this IEntityProvider provider, EntityPackage package)
        {
            var deletes = new List <EntityBase>();

            deletes.Add(package.Entity);
            deletes.AddRange(package.Values);
            deletes.AddRange(package.Relations);
            return(provider.DeleteAsync(deletes.ToArray()));
        }
 public Task <ActionResult <string> > Delete([FromRoute] string key)
 {
     return(ThrowToAction(() =>
     {
         var uid = GetRequesterUid();
         lock (userlocks.GetOrAdd(uid, l => new object()))
         {
             var result = GetVariable(key).Result;
             if (result == null)
             {
                 throw new NotFoundException();
             }
             provider.DeleteAsync(result).Wait();
             return Task.FromResult(result.value);
         }
     }));
 }
Example #5
0
 public virtual async Task <ActionResult> Delete(Guid id)
 {
     return(await _entityProvider.DeleteAsync(id) ? new NoContentResult() : new ConflictResult() as ActionResult);
 }