Пример #1
0
        public void AddExistingTagShouldNotModifyCollection()
        {
            // Arrange
            var entity = new Remark(id, name, nameOnApplication, description, icon);
            var tag    = new Tag(Guid.NewGuid().ToString());

            entity.AddTag(tag);

            // Act
            entity.AddTag(tag);

            // Assert
            entity.Tags.Count.Should().Be(1);
            entity.Tags.ElementAt(0).Should().Be(tag);
        }
Пример #2
0
            public static Remark CreateRemark(Guid id, string name, string nameOnApplication, string description, RemarkIcon icon, IEnumerable <DefaultRemark> defaultRemarks, IEnumerable <Tag> tags)
            {
                // prepare
                var writeRepository = new RemarkWriteRepository(new DataContext(new PersistenceConfiguration(RepositoryTestsHelper.ConnectionString)));
                var readRepository  = new RemarkReadRepository(new DataContext(new PersistenceConfiguration(RepositoryTestsHelper.ConnectionString)));

                // create
                var remark = new Remark(id, name, nameOnApplication, description, icon);

                foreach (var defaultRemark in defaultRemarks)
                {
                    remark.AddDefaultRemark(defaultRemark);
                }
                foreach (var tag in tags)
                {
                    remark.AddTag(tag);
                }

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

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

                return(result);
            }
        public async Task CreateAsync(Guid id, string userId, string category,
                                      Location location, string description = null, IEnumerable <string> tags = null,
                                      Guid?groupId       = null, decimal?price    = null, string currency = null,
                                      DateTime?startDate = null, DateTime?endDate = null)
        {
            Logger.Debug($"Create remark, id:{id}, userId: {userId}, category: {category}, " +
                         $"latitude: {location.Latitude}, longitude: {location.Longitude}.");
            var user = await _userRepository.GetOrFailAsync(userId);

            var remarkCategory = await _categoryRepository.GetByNameAsync(category);

            if (remarkCategory.HasNoValue)
            {
                throw new ServiceException(OperationCodes.CategoryNotFound,
                                           $"Category: '{category}' does not exist!");
            }
            var   encodedDescription = description.Empty() ? description : WebUtility.HtmlEncode(description);
            Group group = null;

            if (groupId != null)
            {
                group = await _groupRepository.GetOrFailAsync(groupId.Value);
            }
            var remark = new Remark(id, user, remarkCategory.Value, location, encodedDescription, group);

            if (price.HasValue)
            {
                remark.SetOffering(Offering.Create(price.Value, currency, startDate, endDate));
            }
            if (tags == null || !tags.Any())
            {
                await _remarkRepository.AddAsync(remark);

                return;
            }
            var availableTags = await _tagRepository.BrowseAsync(new BrowseTags
            {
                Results = 1000
            });

            if (availableTags.HasValue)
            {
                var selectedTags = availableTags.Value.Items
                                   .Select(x => x.Name)
                                   .Intersect(tags);

                foreach (var tag in selectedTags)
                {
                    remark.AddTag(tag);
                }
            }
            await _remarkRepository.AddAsync(remark);
        }
Пример #4
0
        public void RemoveTagShouldSucceed()
        {
            // Arrange
            var entity = new Remark(id, name, nameOnApplication, description, icon);
            var tag    = new Tag(Guid.NewGuid().ToString());

            entity.AddTag(tag);

            // Act
            entity.RemoveTag(tag);

            // Assert
            entity.Tags.Count.Should().Be(0);
        }
Пример #5
0
        public void RemoveUnexistingTagShouldPassSilentlyWithoutAnyImpactOnCollection()
        {
            // Arrange
            var entity = new Remark(id, name, nameOnApplication, description, icon);
            var tag    = new Tag(Guid.NewGuid().ToString());

            entity.AddTag(tag);
            var tagToRemove = new Tag(Guid.NewGuid().ToString());

            // Act
            entity.RemoveTag(tagToRemove);

            // Assert
            entity.Tags.Count.Should().Be(1);
            entity.Tags.ElementAt(0).Should().Be(tag);
        }
        public async Task CreateShouldSuccess()
        {
            // Arrange
            var id   = Guid.NewGuid();
            var name = "name";
            var nameOnApplication = "nameOnApplication";
            var description       = "description";
            var icon           = new RemarkIcon(Guid.NewGuid());
            var defaultRemark  = new DefaultRemark("defaultRemark1");
            var defaultRemarks = new List <DefaultRemark>()
            {
                defaultRemark
            };
            var tag  = new Tag("tag1");
            var tags = new List <Tag>()
            {
                tag
            };

            var remark = new Remark(id, name, nameOnApplication, description, icon);

            defaultRemarks.ForEach(x => remark.AddDefaultRemark(x));
            tags.ForEach(x => remark.AddTag(x));

            // Act
            await _repository.CreateAsync(remark);

            // Assert
            var data = RepositoryHelper.ForRemark.GetRemarks();

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

            result.Name.Should().Be(name);
            result.NameOnApplication.Should().Be(nameOnApplication);
            result.Description.Should().Be(description);
            result.Icon.Should().Be(icon);

            result.DefaultRemarks.Should().HaveCount(1);
            result.DefaultRemarks.First().Should().Be(defaultRemark);

            result.Tags.Should().HaveCount(1);
            result.Tags.First().Should().Be(tag);
        }
Пример #7
0
        public void CreateShouldSuccess()
        {
            // Arrange

            // Act
            var result = new Remark(id, name, nameOnApplication, description, icon);

            tags.ForEach(x => result.AddTag(x));
            defaultRemarks.ForEach(x => result.AddDefaultRemark(x));

            // Assert
            result.Should().NotBeNull();
            result.Tags.Should().AllBeOfType(typeof(Tag));
            result.DefaultRemarks.Should().AllBeOfType(typeof(DefaultRemark));
            result.Name.Should().Be(name);
            result.Id.Should().Be(id);
            result.Description.Should().Be(description);
            result.Icon.Should().Be(icon);
        }
Пример #8
0
        public async Task <Result> Handle(CreateRemarkCommand request, CancellationToken cancellationToken)
        {
            var id = _identifierProvider.Generate();

            var remarkToCreate = new Remark(id, request.Name, request.NameOnApplication, request.Description, new RemarkIcon(request.Icon));

            request.Tags.ToList().ForEach(x => remarkToCreate.AddTag(new Tag(x)));
            request.DefaultRemarks.ToList().ForEach(x => remarkToCreate.AddDefaultRemark(new DefaultRemark(x)));

            remarkToCreate.Version = _versionProvider.Generate();

            Result result;

            try
            {
                await _remarkWriteRepository.CreateAsync(remarkToCreate);

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

            return(result);
        }
Пример #9
0
        public async Task <Result> Handle(UpdateRemarkCommand request, CancellationToken cancellationToken)
        {
            Result result;

            try
            {
                var icon = await _remarkReadRepository.GetAsync(request.Id);

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

                var updatedRemark = new Remark(request.Id, request.Name, request.NameOnApplication, request.Description, new RemarkIcon(request.Icon));
                request.Tags.ToList().ForEach(x => updatedRemark.AddTag(new Tag(x)));
                request.DefaultRemarks.ToList().ForEach(x => updatedRemark.AddDefaultRemark(new DefaultRemark(x)));

                updatedRemark.Version = _versionProvider.Generate();
                await _remarkWriteRepository.UpdateAsync(updatedRemark);

                result = Result.Ok(updatedRemark.Version);
            }
            catch (EntityNotFoundDbException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.NotFound.Name,
                        Message = string.Format(HandlerFailures.NotFound, "Remark"),
                        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.UpdateRemarkFailure);
            }

            return(result);
        }