public override async Task <int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken()) { var userId = _currentUserService.GetUserId(); var time = _dateTimeService.GetTime(); AddAuditInformation(userId, time); return(await base.SaveChangesAsync(cancellationToken)); }
public async Task <UpdateNoteCommandResponse> Handle(UpdateNoteCommand request, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var updateContent = request.Content; var targetNote = await _applicationUnitOfWork.Notes .SingleOrDefaultAsync(note => note.NoteId == updateContent.NoteId); if (targetNote == null) { return(new UpdateNoteCommandResponse( HttpStatusCode.NotFound, new ApplicationError(ApplicationConstants.ErrorCodes.BusinessValidationError, string.Format(ApplicationConstants.ErrorMessages.NoteWithIdDoesNotExist, updateContent.NoteId)))); } if (targetNote.CreatedBy != _userService.GetUserId()) { return(new UpdateNoteCommandResponse( HttpStatusCode.Forbidden, new ApplicationError(ApplicationConstants.ErrorCodes.AuthorizationError, string.Format(ApplicationConstants.ErrorMessages.NoteCreatedByDifferentUser, updateContent.NoteId)))); } try { targetNote.Text = updateContent.Text; _applicationUnitOfWork.Notes.Update(targetNote); await _applicationUnitOfWork.CommitAsync(cancellationToken); return(new UpdateNoteCommandResponse(HttpStatusCode.OK)); } catch (Exception exc) { _logger.LogError(exc, exc.Message); return(new UpdateNoteCommandResponse( HttpStatusCode.InternalServerError, new ApplicationError(ApplicationConstants.ErrorCodes.UpdateNoteError, exc.Message))); } }
public async Task <UpdateInvoiceCommandResponse> Handle(UpdateInvoiceCommand request, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var updateContent = request.Content; var targetInvoice = await _applicationUnitOfWork.Invoices .SingleOrDefaultAsync(invoice => invoice.InvoiceId == updateContent.InvoiceId); if (targetInvoice == null) { return(new UpdateInvoiceCommandResponse( HttpStatusCode.NotFound, new ApplicationError(ApplicationConstants.ErrorCodes.BusinessValidationError, string.Format(ApplicationConstants.ErrorMessages.InvoiceWithIdDoesNotExist, updateContent.InvoiceId)))); } if (targetInvoice.CreatedBy != _userService.GetUserId()) { return(new UpdateInvoiceCommandResponse( HttpStatusCode.Forbidden, new ApplicationError(ApplicationConstants.ErrorCodes.AuthorizationError, string.Format(ApplicationConstants.ErrorMessages.InvoiceCreatedByDifferentUser, updateContent.InvoiceId)))); } var otherInvoiceWithSameIdentifier = await _applicationUnitOfWork.Invoices .SingleOrDefaultAsync(invoice => invoice.Identifier == updateContent.Identifier && invoice.InvoiceId != updateContent.InvoiceId); if (otherInvoiceWithSameIdentifier != null) { return(new UpdateInvoiceCommandResponse( HttpStatusCode.BadRequest, new ApplicationError(ApplicationConstants.ErrorCodes.BusinessValidationError, string.Format(ApplicationConstants.ErrorMessages.DuplicateInvoiceIdentifier, updateContent.Identifier)))); } try { if (updateContent.Identifier != null) { targetInvoice.Identifier = updateContent.Identifier; } if (updateContent.Amount.HasValue) { targetInvoice.Amount = updateContent.Amount.Value; } _applicationUnitOfWork.Invoices.Update(targetInvoice); await _applicationUnitOfWork.CommitAsync(cancellationToken); return(new UpdateInvoiceCommandResponse(HttpStatusCode.OK)); } catch (Exception exc) { _logger.LogError(exc, exc.Message); return(new UpdateInvoiceCommandResponse( HttpStatusCode.InternalServerError, new ApplicationError(ApplicationConstants.ErrorCodes.UpdateInvoiceError, exc.Message))); } }