Exemple #1
0
        public async Task <bool> Commit()
        {
            OperationSuccesful = await _uow.CommitAsync();

            AddModelError(_uow.GetOperationMessage());

            return(_uow.IsSucess());
        }
Exemple #2
0
        public void UnitOfWorkUpdateItemTest()
        {
            var roomType = _unitOfWork.RoomTypeRepository.Get(p => p.RoomTypeName == "Room Type A");

            Assert.NotNull(roomType);

            roomType.Modified(Guid.NewGuid());
            _unitOfWork.RoomTypeRepository.Update(roomType);

            Assert.True(_unitOfWork.CommitAsync().Result);
        }
        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 <CreateNoteCommandResponse> Handle(CreateNoteCommand request, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var currentInvoice = await _applicationUnitOfWork.Invoices
                                 .SingleOrDefaultAsync(invoice => invoice.InvoiceId == request.Content.InvoiceId);

            if (currentInvoice == null)
            {
                return(new CreateNoteCommandResponse(
                           HttpStatusCode.NotFound,
                           new ApplicationError(ApplicationConstants.ErrorCodes.BusinessValidationError,
                                                string.Format(ApplicationConstants.ErrorMessages.InvoiceNotFound, request.Content.InvoiceId))));
            }

            var noteEntity = _mapper.Map <Note>(request.Content);

            try
            {
                var createdNote = await _applicationUnitOfWork.Notes.AddAsync(noteEntity);

                await _applicationUnitOfWork.CommitAsync(cancellationToken);

                var createNoteDto = _mapper.Map <NoteDto>(createdNote);

                return(new CreateNoteCommandResponse(HttpStatusCode.Created, createNoteDto));
            }
            catch (Exception exc)
            {
                _logger.LogError(exc, exc.Message);

                return(new CreateNoteCommandResponse(
                           HttpStatusCode.InternalServerError,
                           new ApplicationError(ApplicationConstants.ErrorCodes.CreateNoteError, 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)));
            }
        }