public SiteGroupDetailsViewModel GetViewModel()
        {

            var siteGroupDto = new SiteGroupDto { ClientId = _clientId, Id = 0 };


            if (_siteGroupId != 0)
            {
                siteGroupDto = _siteGroupService.GetSiteGroup(_siteGroupId, _clientId);
            }

            var linkToGroupId = siteGroupDto.Parent != null && (siteGroupDto.Parent as SiteGroupDto != null)
                                    ? siteGroupDto.Parent.Id
                                    : 0;

            var linkToSiteId = siteGroupDto.Parent != null && (siteGroupDto.Parent as SiteDto != null)
                                   ? siteGroupDto.Parent.Id
                                   : 0;

            var siteDetailsViewModel = new SiteGroupDetailsViewModel
                                           {
                                               Name = siteGroupDto.Name,
                                               GroupId = siteGroupDto.Id,
                                               ClientId = _clientId,
                                               HasChildren = siteGroupDto.HasChildren,
                                               GroupLinkToGroupId = linkToGroupId,
                                               GroupLinkToSiteId = linkToSiteId,
                                               ExistingSites = GetExistingSites(siteGroupDto),
                                               ExistingGroups = GetExistingSiteGroups(siteGroupDto)
                                           };


            return siteDetailsViewModel;
        }
 private IEnumerable<AutoCompleteViewModel> GetExistingSites(SiteGroupDto siteGroupDto)
 {
     var sites = _siteService.GetByCompanyId(_clientId).OrderBy(x => x.Name);
     return sites
             .Where(x => siteGroupDto.ChildIdsThatCannotBecomeParent.Contains(x.Id) == false)
             .Select(AutoCompleteViewModel.ForSite)
             .AddDefaultOption();
 }
 public static AutoCompleteViewModel ForSiteGroup(SiteGroupDto siteGroupDto)
 {
     return new AutoCompleteViewModel(siteGroupDto.Name, siteGroupDto.Id.ToString());
 }
 public SiteGroupDto Build()
 {
     var siteGroupDto = new SiteGroupDto { Id = _groupId };
     return siteGroupDto;
 }
        public void Given_search_with_parameters_When_GetViewModel_return_only_allowed_site_groups_in_viewModel()
        {
            //Given
            const long categoryId = 123L;
            const long siteId = 324234L;
            const long siteGroupId = 2L;
            const bool deleted = true;
            var from = DateTime.Now.AddDays(-124);
            var to = DateTime.Now.AddDays(-3231);
            const string title = "title";
            IList<long> allowedSites = new List<long>() { siteGroupId };
            var target = CreateTarget();

            //When
            var siteGroup1 = new SiteGroupDto() { Id = 1L };
            var siteGroup2 = new SiteGroupDto() { Id = 2L };

            _siteGroupService
                .Setup(x => x.GetByCompanyId(It.IsAny<long>()))
                .Returns(new List<SiteGroupDto>() { siteGroup1, siteGroup2 });

            var result = target
                .WithCategoryId(categoryId)
                .WithCreatedFrom(from)
                .WithCreatedTo(to)
                .WithTitle(title)
                .WithSiteId(siteId)
                .WithSiteGroupId(siteGroupId)
                .WithShowDeleted(deleted)
                .WithAllowedSiteIds(allowedSites)
                .GetViewModel();

            //Then
            Assert.That(result.CategoryId, Is.EqualTo(categoryId));
            Assert.That(result.SiteId, Is.EqualTo(siteId));
            Assert.That(result.SiteGroupId, Is.EqualTo(siteGroupId));
            Assert.That(result.IsShowDeleted, Is.EqualTo(deleted));
            Assert.That(DateTime.Parse(result.CreatedFrom), Is.EqualTo(from.Date));
            Assert.That(DateTime.Parse(result.CreatedTo), Is.EqualTo(to.Date));
            Assert.That(result.Title, Is.EqualTo(title));
            Assert.That(result.SiteGroups.Count(), Is.EqualTo(2));
            Assert.That(result.SiteGroups.Last().value, Is.EqualTo(siteGroupId.ToString()));
        }
        public void When_get_view_model_Then_should_order_existing_site_group()
        {
            // Arrange
            var siteDto = new SiteDto { SiteId = 1 };

            siteService.Setup(ss => ss.GetByIdAndCompanyId(1, 1)).Returns(siteDto);

            _clientService.Setup(x => x.GetSite(1, 1)).Returns(new SiteAddressDto(1, "Add 1", "Add 2",
                                                                                               "Add 3", "Add 4", "Add 5", "county", "PC",
                                                                                               "tel", null));

            string expectedLastGroup = "Zoo bar";
            string expectedFirstGroup = "Alph bar";

            IList<SiteGroupDto> existingGroups = new List<SiteGroupDto>();
            existingGroups.Add(new SiteGroupDto{Id = 1, Name = expectedLastGroup});
            existingGroups.Add(new SiteGroupDto{Id = 1, Name = "Random"});
            existingGroups.Add(new SiteGroupDto { Id = 1, Name = expectedFirstGroup });
            siteGroupService.Setup(x => x.GetByCompanyIdExcludingSiteGroup(1, 1)).Returns(existingGroups);

            siteService.Setup(x => x.GetByCompanyId(1)).Returns(new List<SiteDto>());

            var siteGroupDto = new SiteGroupDto();
            siteGroupService.Setup(s => s.GetSiteGroup(1, 1)).Returns(siteGroupDto);

            var target = CreateSiteDetailsViewModelFactory();

            // Act
            var result = target.WithClientId(1).WithSiteId(1).GetViewModel();

            // Assert
            Assert.That(result.ExistingGroups.Skip(1).Take(1).First().label, Is.EqualTo(expectedFirstGroup));
            Assert.That(result.ExistingGroups.Last().label, Is.EqualTo(expectedLastGroup));
        }