public async Task GetByIdShouldSucceed()
        {
            // Arrange
            var        id          = Guid.NewGuid();
            var        name        = "name";
            string     description = "description";
            string     icon        = "iconUrl";
            string     content     = "content";
            string     image       = "imageUrl";
            string     video       = "videoUrl";
            List <Tag> tags        = new List <Tag>()
            {
                new Tag("tag1"), new Tag("tag2")
            };

            RepositoryHelper.ForInstruction.CreateInstruction(id, name, description, icon, content, image, video, tags);

            // Act
            var result = await _repository.GetAsync(id);

            // Assert
            result.Should().NotBeNull();
            result.Name.Should().Be(name);
            result.Description.Should().Be(description);
            result.Icon.Should().Be(icon);
            result.Content.Should().Be(content);
            result.Tags.Count.Should().Be(2);
            result.Image.Should().Be(image);
            result.Video.Should().Be(video);
        }
示例#2
0
        public async Task <Result> Handle(GetInstructionQuery request, CancellationToken cancellationToken)
        {
            Result result;

            try
            {
                var instruction = await _instructionReadRepository.GetAsync(request.Id);

                var instructionModel = _mapper.Map <InstructionModel>(instruction);

                result = Result.Ok(instructionModel, instruction.Version);
            }
            catch (EntityNotFoundDbException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.NotFound.Name,
                        Message = string.Format(HandlerFailures.NotFound, "Instruction"),
                        Target  = "id"
                    }
                }
                                     );
            }
            catch
            {
                result = Result.Fail(CustomFailures.GetInstructionFailure);
            }

            return(result);
        }
        public async Task <Result> Handle(UpdateInstructionCommand request, CancellationToken cancellationToken)
        {
            Result result;

            try
            {
                var instruction = await _instructionReadRepository.GetAsync(request.Id);

                if (instruction.Version != request.Version)
                {
                    throw new CommandVersionException();
                }

                var updatedInstruction = new Instruction(request.Id, request.Name, request.Description, request.Icon, request.Content, request.Image, request.Video);
                foreach (var tag in request.Tags)
                {
                    updatedInstruction.AddTag(new Tag(tag));
                }

                updatedInstruction.Version = _versionProvider.Generate();
                await _instructionWriteRepository.UpdateAsync(updatedInstruction);

                result = Result.Ok(updatedInstruction.Version);
            }
            catch (EntityNotFoundDbException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.NotFound.Name,
                        Message = string.Format(HandlerFailures.NotFound, "Instruction"),
                        Target  = "id"
                    }
                }
                                     );
            }
            catch (CommandVersionException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.NotMet.Name,
                        Message = HandlerFailures.NotMet,
                        Target  = "version"
                    }
                }
                                     );
            }
            catch (UniqueKeyException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.Conflict.Name,
                        Message = HandlerFailures.Conflict,
                        Target  = "name"
                    }
                }
                                     );
            }
            catch
            {
                result = Result.Fail(CustomFailures.UpdateInstructionFailure);
            }

            return(result);
        }