Beispiel #1
0
        public async Task CreateShouldSucceed()
        {
            // Arrange
            var id          = Guid.NewGuid();
            var name        = "name";
            var description = "any description";
            var icon        = "iconUrl";
            var content     = "any content";
            var image       = "any imageUrl";
            var video       = "any videoUrl";
            var instruction = new Instruction(id, name, description, icon, content, image, video);
            var tag         = new Tag("tag");

            instruction.AddTag(tag);

            // Act
            await _repository.CreateAsync(instruction);

            // Assert
            var data = RepositoryHelper.ForInstruction.GetInstructions();

            data.Should().HaveCount(1);
            var result = data.First();

            result.Name.Should().Be(name);
            result.Description.Should().Be(description);
            result.Icon.Should().Be(icon);
            result.Content.Should().Be(content);
            result.Image.Should().Be(image);
            result.Video.Should().Be(video);
            result.Tags.Should().HaveCount(1);
            result.Tags.First().Should().Be(tag);
        }
            public static Instruction CreateInstruction(Guid id, string name, string description, string icon, string content, string image, string video, IEnumerable <Tag> tags)
            {
                // prepare
                var writeRepository = new InstructionWriteRepository(new DataContext(new PersistenceConfiguration(RepositoryTestsHelper.ConnectionString)));
                var readRepository  = new InstructionReadRepository(new DataContext(new PersistenceConfiguration(RepositoryTestsHelper.ConnectionString)));

                // create
                var instruction = new Instruction(id, name, description, icon, content, image, video);

                foreach (var tag in tags)
                {
                    instruction.AddTag(tag);
                }

                writeRepository.CreateAsync(instruction).GetAwaiter().GetResult();

                // result
                var result = readRepository.GetAsync(id).Result;

                return(result);
            }
Beispiel #3
0
        public async Task <Result> Handle(CreateInstructionCommand request, CancellationToken cancellationToken)
        {
            var id = _identifierProvider.Generate();
            var instructionToCreate = new Instruction(id, request.Name, request.Description, request.Icon, request.Content, request.Image, request.Video);

            foreach (var tag in request.Tags)
            {
                instructionToCreate.AddTag(new Tag(tag));
            }
            instructionToCreate.Version = _versionProvider.Generate();

            Result result;

            try
            {
                await _instructionWriteRepository.CreateAsync(instructionToCreate);

                result = Result.Ok(id, instructionToCreate.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.CreateInstructionFailure);
            }

            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);
        }