public async Task <IActionResult> PutAuditLog([FromRoute] int id, [FromBody] AuditLog auditLog)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != auditLog.Id)
            {
                return(BadRequest());
            }

            _context.Entry(auditLog).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AuditLogExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
示例#2
0
        public async Task <long> Handle(CreateAuditCommand request, CancellationToken cancellationToken)
        {
            var audit = _mapper.Map <CreateAuditCommand, AuditEntity>(request);

            //Get Client Id from Token and lookup Application
            var clientId = _httpContext.User.Claims.FirstOrDefault(c => c.Type == "client_id")?.Value;

            if (clientId == null)
            {
                throw new Exception(NoClientIdFound);
            }

            var auditApplication = _dbContext.AuditApplications.FirstOrDefault(a => a.ClientId == clientId);

            if (auditApplication == null)
            {
                throw new Exception(NoApplicationFound);
            }

            audit.AuditApplication = auditApplication;

            _dbContext.Audits.Add(audit);

            await _dbContext.SaveChangesAsync().ConfigureAwait(false);

            return(audit.Id);
        }
示例#3
0
        public async Task <IActionResult> PutAuditHeader(int id, AuditHeaderAll auditHeaderAll)
        {
            if (id != auditHeaderAll.HeaderId)
            {
                return(BadRequest());
            }
            _context.Entry(auditHeaderAll).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();

                return(Ok(auditHeaderAll));
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AuditHeaderAllExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
        }
示例#4
0
        public async Task <IActionResult> UploadFile(List <IFormFile> files, [FromForm] Int32 LineID, [FromForm] string OriginalName, [FromForm] DateTime CreationDate, [FromForm] string CreatedBy, [FromForm] string Extension)
        {
            // string _root = "D:\\Projects\\EAudit\\";
            string _root = "D:\\NewFlowTest\\App\\";
            // string _root = _configuration["UploadRootPathProd"];
            string _folder = _root + "\\Files\\" + LineID + "\\";

            Directory.CreateDirectory(_folder);

            var size = files.Sum(f => f.Length);

            foreach (var file in files)
            {
                if (file.Length > 0)
                {
                    string guidNumber = Guid.NewGuid().ToString();
                    // var path1 = $@"{_folder}\{file.FileName}";
                    var extension    = Path.GetExtension(file.FileName);
                    var path         = $@"{_folder}\{guidNumber}{extension}";
                    var path_checked = CheckFileExistRename(path);
                    using (var stream = new FileStream(path_checked, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);

                        await stream.FlushAsync();
                    };



                    _context.AuditAttchments.Add(new AuditAttchment
                    {
                        LineId       = LineID,
                        OriginalName = file.FileName,
                        Name         = $@"{guidNumber}{extension}",
                        Path         = _folder,
                        CreationDate = CreationDate,
                        CreatedBy    = CreatedBy
                    });

                    await _context.SaveChangesAsync();
                }
            }



            return(Ok(new
            {
                count = files.Count,
                size,
                aa = LineID
            }));
        }
示例#5
0
        public async Task <bool> CreateEntryAsync(Operation operation, T before, T after)
        {
            int updatedCount = 0;

            var entry = new AuditEvent()
            {
                Id        = Guid.NewGuid().ToString(),
                EventType = operation,
                Occured   = DateTime.UtcNow
            };

            var propertyList = typeof(T).GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var property in propertyList)
            {
                var auditEventItem = new AuditEventItem()
                {
                    TableName = typeof(T).Name,
                    FieldName = property.Name
                };

                switch (operation)
                {
                case Operation.Create:
                    auditEventItem.AfterChangeValue = property.GetValue(after)?.ToString();
                    break;

                case Operation.Delete:
                    auditEventItem.BeforeChangeValue = property.GetValue(before)?.ToString();
                    break;

                case Operation.Update:
                    auditEventItem.AfterChangeValue  = property.GetValue(after)?.ToString();
                    auditEventItem.BeforeChangeValue = property.GetValue(before)?.ToString();
                    break;
                }

                if (auditEventItem.BeforeChangeValue != auditEventItem.AfterChangeValue)
                {
                    entry.Changes.Add(auditEventItem);
                }
            }

            if (entry.Changes.Any())
            {
                context.AuditEvents.Add(entry);
                updatedCount = await context.SaveChangesAsync();
            }

            return(updatedCount > 0);
        }
        public async Task <long> Handle(CreateAuditApplicationCommand request, CancellationToken cancellationToken)
        {
            var app = _mapper.Map <CreateAuditApplicationCommand, AuditApplication>(request);

            //Overwrite ClientId in context for Application as it will be supplied,
            //not derived from Token, as Token will be an Admin Token
            _dbContext.ClientId = request.ClientId;

            _dbContext.AuditApplications.Add(app);

            await _dbContext.SaveChangesAsync().ConfigureAwait(false);

            return(app.Id);
        }
示例#7
0
    public async Task SaveChangesFailedAsync(
        DbContextErrorEventData eventData,
        CancellationToken cancellationToken = default)
    {
        using (var auditContext = new AuditContext(_connectionString))
        {
            auditContext.Attach(_audit);
            _audit.Succeeded    = false;
            _audit.EndTime      = DateTime.UtcNow;
            _audit.ErrorMessage = eventData.Exception.InnerException?.Message;

            await auditContext.SaveChangesAsync(cancellationToken);
        }
    }
        public async Task <Result <long> > Handle(CreateAuditApplicationCommand command, CancellationToken cancellationToken)
        {
            var app = _mapper.Map <CreateAuditApplicationCommand, AuditApplication>(command);

            //Overwrite ClientId in context for Application as it will be supplied,
            //not derived from Token, as Token will be an Admin Token as this is an Admin function
            _dbContext.ClientId = command.ClientId;

            _dbContext.AuditApplications.Add(app);

            await _dbContext.SaveChangesAsync().ConfigureAwait(false);

            return(Result.Ok(app.Id));
        }
示例#9
0
    public async ValueTask <InterceptionResult <int> > SavingChangesAsync(
        DbContextEventData eventData,
        InterceptionResult <int> result,
        CancellationToken cancellationToken = default)
    {
        _audit = CreateAudit(eventData.Context);

        using (var auditContext = new AuditContext(_connectionString))
        {
            auditContext.Add(_audit);
            await auditContext.SaveChangesAsync();
        }

        return(result);
    }
示例#10
0
    public async ValueTask <int> SavedChangesAsync(
        SaveChangesCompletedEventData eventData,
        int result,
        CancellationToken cancellationToken = default)
    {
        using (var auditContext = new AuditContext(_connectionString))
        {
            auditContext.Attach(_audit);
            _audit.Succeeded = true;
            _audit.EndTime   = DateTime.UtcNow;

            await auditContext.SaveChangesAsync(cancellationToken);
        }

        return(result);
    }
        public async Task CreateRequestForCommandAsync <T>(Guid id)
        {
            var exists = await ExistAsync(id).ConfigureAwait(false);

            var request = exists ?
                          throw new Exception($"Request with {id} already exists") :
                                new ClientRequest()
                                {
                                    Id   = id,
                                    Name = typeof(T).Name,
                                    Time = DateTime.UtcNow
                                };

            _context.Add(request);

            await _context.SaveChangesAsync().ConfigureAwait(false);
        }
示例#12
0
        public async Task <Result <long> > Handle(CreateAuditCommand command, CancellationToken cancellationToken)
        {
            var audit = _mapper.Map <AuditEntity>(command);

            _dbContext.ClientId = command.ClientId;

            var auditApplication = _dbContext.AuditApplications.FirstOrDefault(a => a.ClientId == command.ClientId);

            if (auditApplication == null)
            {
                return(Result.Fail <long>(Constants.ErrorMessages.NoApplicationFound));
            }

            audit.AuditApplicationId = auditApplication.Id;
            _dbContext.Audits.Add(audit);
            await _dbContext.SaveChangesAsync().ConfigureAwait(false);

            _logger.LogInformation("Audit persisted to database with Id -{0}", audit.Id);

            return(Result.Ok(audit.Id));
        }