示例#1
0
        public async Task <bool> Delete <TDataType>(int id) where TDataType : DataEntity
        {
            using (Logger.BeginScope("{Operation} is {Action} {DataType} with Id ({Id})", nameof(DataManager),
                                     "deleting", typeof(TDataType).Name, id))
            {
                var data = await Read <TDataType>(id);

                if (data is null)
                {
                    Logger.LogWarning("Unable to find the data it may already be deleted");
                    return(false);
                }

                if (typeof(TDataType).IsDefined(typeof(AuditAttribute), true))
                {
                    Logger.LogTrace("Data is audited");
                    if (await AuditWorker.DeleteEvent(data))
                    {
                        Logger.LogTrace("Delete audit record created");
                        if (await DataStreamer.Delete <TDataType>(id))
                        {
                            Logger.LogTrace("Data deleted from store");
                            if (await AuditWorker.CommitEvents(data))
                            {
                                Logger.LogTrace("Audit committed to store");
                                if (await IndexWorker.Unindex(data))
                                {
                                    Logger.LogInformation("Data deleted from store");
                                    return(true);
                                }

                                Logger.LogWarning("Failed to remove indexes for data");
                                await AuditWorker.RollbackEvent(data);

                                Logger.LogInformation("Added rollback audit record");
                            }
                            else
                            {
                                Logger.LogWarning("Failed to commit audit to store");
                            }

                            await DataStreamer.Rollback(id, data);

                            Logger.LogInformation("Rolled data back");
                        }
                        else
                        {
                            Logger.LogWarning("Failed to delete data");
                            await AuditWorker.DiscardEvents(data);

                            Logger.LogTrace("Discarded audit record");
                        }

                        return(false);
                    }

                    Logger.LogWarning("Failed to create audit record for delete");
                    return(false);
                }

                Logger.LogTrace("Data is not audited");
                if (await DataStreamer.Delete <TDataType>(id))
                {
                    Logger.LogInformation("Deleted data");
                    if (await IndexWorker.Unindex(data))
                    {
                        Logger.LogInformation("Removed any indexes for data");
                        return(true);
                    }

                    Logger.LogWarning("Failed to remove indexes for data");
                    await DataStreamer.Rollback(id, data);

                    Logger.LogInformation("Rolled data back");
                }

                Logger.LogWarning("Failed to delete data");
                return(false);
            }
        }
示例#2
0
        public async Task <bool> Restore <TDataType>(int id) where TDataType : DataEntity
        {
            using (Logger.BeginScope("{Operation} is {Action} {DataType} with Id ({Id})", nameof(DataManager),
                                     "restoring", typeof(TDataType).Name, id))
            {
                var data = await Read <TDataType>(id);

                if (data is null)
                {
                    Logger.LogTrace("Data could be deleted");
                    if (await DataStreamer.Undelete <TDataType>(id))
                    {
                        Logger.LogTrace("Data restored to store");
                        data = await Read <TDataType>(id);

                        if (typeof(TDataType).IsDefined(typeof(AuditAttribute), true))
                        {
                            Logger.LogTrace("Data is audited");
                            if (await AuditWorker.UndeleteEvent(data))
                            {
                                if (await AuditWorker.CommitEvents(data))
                                {
                                    Logger.LogTrace("Audit committed to store");
                                    if (await IndexWorker.Index(data))
                                    {
                                        Logger.LogInformation("Restored data with audit");
                                        return(true);
                                    }

                                    Logger.LogWarning("Failed to index data");
                                    await AuditWorker.RollbackEvent(data);
                                }
                                else
                                {
                                    Logger.LogWarning("Failed to commit audit for data to store");
                                    await AuditWorker.DiscardEvents(data);

                                    Logger.LogTrace("Discarded audit record");
                                }
                            }
                            else
                            {
                                Logger.LogWarning("Failed to create undelete audit record for data");
                            }
                        }
                        else
                        {
                            if (await IndexWorker.Index(data))
                            {
                                Logger.LogInformation("Restored data");
                                return(true);
                            }

                            Logger.LogWarning("Failed to index data");
                        }

                        await DataStreamer.Delete <TDataType>(id);

                        Logger.LogInformation("Rolled back restore");
                    }

                    Logger.LogWarning("Failed to restore data");
                    return(false);
                }

                Logger.LogWarning("Data didn't need restoring");
                return(true);
            }
        }
示例#3
0
        public async Task <bool> Write <TDataType>(TDataType data) where TDataType : DataEntity
        {
            using (Logger.BeginScope("{Operation} is {Action} {DataType} with Id ({Id})", nameof(DataManager),
                                     "writing", typeof(TDataType).Name, data.Id))
            {
                Logger.LogTrace("Checking if data exists");
                var currentData = await Read <TDataType>(data.Id);

                Logger.LogInformation(currentData is null ? "Creating data" : "Updating data");
                if (typeof(TDataType).IsDefined(typeof(AuditAttribute), true))
                {
                    Logger.LogTrace("{DataType} is audited", typeof(TDataType).Name);
                    var writtenData = await DataStreamer.Write(data.Id, data);

                    if (writtenData)
                    {
                        Logger.LogTrace("Data ready to commit to store");
                        var auditResult = currentData is null
                            ? await AuditWorker.CreationEvent(data)
                            : await AuditWorker.UpdateEvent(data, currentData);

                        if (auditResult)
                        {
                            Logger.LogTrace("Audit record created");
                            writtenData = await DataStreamer.Commit <TDataType>(data.Id);

                            if (writtenData)
                            {
                                Logger.LogTrace("Data saved to store");
                                var committedAudit = await AuditWorker.CommitEvents(data);

                                if (committedAudit)
                                {
                                    Logger.LogTrace("Audit saved to store");
                                    var indexed = await IndexWorker.Index(data);

                                    if (indexed)
                                    {
                                        Logger.LogInformation("Data, Audit saved successfully");
                                        return(true);
                                    }

                                    Logger.LogWarning("Failed to index data");
                                    await AuditWorker.RollbackEvent(data);

                                    Logger.LogInformation("Added rollback to audit");
                                    Logger.LogTrace("Rolled back data");
                                }

                                await DataStreamer.Rollback(data.Id, currentData);

                                Logger.LogInformation("Rolled data back");
                                return(false);
                            }

                            await AuditWorker.DiscardEvents(data);

                            return(false);
                        }

                        Logger.LogWarning("Failed to create audit record for data");
                        await DataStreamer.DiscardChanges <TDataType>(data.Id);

                        Logger.LogTrace("Discard changes");
                    }

                    Logger.LogWarning("Failed to save data to the store");
                    return(false);
                }

                Logger.LogTrace("Data is not audited");
                var writeAndCommit = await DataStreamer.WriteAndCommit(data.Id, data);

                if (writeAndCommit)
                {
                    var indexed = await IndexWorker.Index(data);

                    if (!indexed)
                    {
                        Logger.LogWarning("Failed to index data");
                        await DataStreamer.Rollback(data.Id, currentData);

                        Logger.LogInformation("Rolled data back");
                        return(false);
                    }

                    Logger.LogTrace("Data was indexed");
                    Logger.LogInformation("Data saved successfully");
                }
                else
                {
                    Logger.LogWarning("Failed to save data");
                }

                return(writeAndCommit);
            }
        }