Exemple #1
0
        public async Task CreateShouldSucceed()
        {
            // Arrange
            var id          = Guid.NewGuid();
            var name        = "name";
            var description = "description";
            var icon        = Guid.NewGuid();
            var instruction = "instruction";
            var input       = new Input(id, name, description, icon, instruction);

            var tag = new Tag("tag");

            input.AddTag(tag);

            var image = new Image("image", "url");

            input.AddImage(image);

            var video = new Video("image", "url");

            input.AddVideo(video);

            var inputAttributeName                 = "any name";
            var inputAttributeNotMandatory         = true;
            var inputAttributeScanningOnly         = true;
            var inputAttributeMinLenght            = new Random().Next();
            var inputAttributeMaxLenght            = new Random().Next();
            var inputAttributePrefix               = "any prefix";
            var inputAttributeCheckLastXCharacters = new Random().Next();
            var inputAttribute = new InputAttribute(inputAttributeName, inputAttributeNotMandatory, inputAttributeScanningOnly, inputAttributeMinLenght, inputAttributeMaxLenght, inputAttributePrefix, inputAttributeCheckLastXCharacters);

            input.AddInputAttribute(inputAttribute);

            // Act
            await _repository.CreateAsync(input);

            // Assert
            var data = RepositoryHelper.ForInput.GetInputs();

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

            result.Name.Should().Be(name);
            result.Description.Should().Be(description);
            result.Icon.Should().Be(icon);
            result.Instruction.Should().Be(instruction);
            result.Tags.Should().HaveCount(1);
            result.Tags.First().Should().Be(tag);
            result.Images.Should().HaveCount(1);
            result.Images.First().Should().Be(image);
            result.Videos.Should().HaveCount(1);
            result.Videos.First().Should().Be(video);
            result.InputAttributes.Should().HaveCount(1);
            result.InputAttributes.First().Should().Be(inputAttribute);
        }
        public async Task <Result> Handle(CreateInputCommand request, CancellationToken cancellationToken)
        {
            var id = _identifierProvider.Generate();

            var inputToCreate = new Input(id, request.Name, request.Description, request.Icon, request.Instruction);

            foreach (var image in request.Images)
            {
                inputToCreate.AddImage(new Image(image.Name, "some image url"));
            }
            foreach (var video in request.Videos)
            {
                inputToCreate.AddVideo(new Video(video.Name, "some image url"));
            }
            foreach (var tag in request.Tags)
            {
                inputToCreate.AddTag(new Tag(tag));
            }

            inputToCreate.Version = _versionProvider.Generate();

            Result result;

            try
            {
                await _inputWriteRepository.CreateAsync(inputToCreate);

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