public async Task GetPublicProfileReturnsCachedProfileTest() { var expected = Model.Create <Profile>().Set(x => { x.Status = ProfileStatus.Unavailable; x.BannedAt = null; x.Gender = null; x.Skills.Clear(); x.Languages.Clear(); }); var categories = Model.Create <Collection <Category> >(); var visibleCategories = categories.Where(x => x.Visible); var store = Substitute.For <IProfileStore>(); var cache = Substitute.For <IProfileCache>(); var query = Substitute.For <ICategoryQuery>(); var sut = new ProfileQuery(store, cache, query); using (var tokenSource = new CancellationTokenSource()) { query.GetCategories(ReadType.VisibleOnly, tokenSource.Token).Returns(visibleCategories); cache.GetProfile(expected.Id).Returns(expected); var actual = await sut.GetPublicProfile(expected.Id, tokenSource.Token).ConfigureAwait(false); actual.Should().BeEquivalentTo(expected, opt => opt.ExcludingMissingMembers()); } }
public async Task GetPublicProfileClearAllUnapprovedCategoriesTest() { var expected = Model.Create <Profile>().Set(x => x.Status = ProfileStatus.Unavailable) .Set(x => x.BannedAt = null); var categories = Model.Create <Collection <Category> >(); var store = Substitute.For <IProfileStore>(); var cache = Substitute.For <IProfileCache>(); var query = Substitute.For <ICategoryQuery>(); var sut = new ProfileQuery(store, cache, query); using (var tokenSource = new CancellationTokenSource()) { query.GetCategories(ReadType.VisibleOnly, tokenSource.Token).Returns(categories); store.GetProfile(expected.Id, tokenSource.Token).Returns(expected); var actual = await sut.GetPublicProfile(expected.Id, tokenSource.Token).ConfigureAwait(false); actual.Gender.Should().BeNull(); actual.Languages.Should().BeEmpty(); actual.Skills.Should().BeEmpty(); } }
public void Put(ProfileQuery profileQuery) { try { _dataService.SaveProfileQuery(profileQuery); } catch { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } }
public async Task <IEnumerable <Profile> > GetProfiles(ProfileQuery filter) { try { return(await repository.GetProfiles(filter)); } catch (Exception e) { logger.LogError(e.Message); return(null); } }
public void GetPublicProfileThrowsExceptionWithEmptyIdTest() { var store = Substitute.For <IProfileStore>(); var cache = Substitute.For <IProfileCache>(); var query = Substitute.For <ICategoryQuery>(); var sut = new ProfileQuery(store, cache, query); Func <Task> action = async() => await sut.GetPublicProfile(Guid.Empty, CancellationToken.None) .ConfigureAwait(false); action.Should().Throw <ArgumentException>(); }
public async Task GetPublicProfileReturnsCompleteProfileWhenAllCategoriesApprovedTest() { var expected = Model.Create <Profile>().Set(x => x.Status = ProfileStatus.Unavailable) .Set(x => x.BannedAt = null); var categories = new Collection <Category> { new Category { Group = CategoryGroup.Gender, Name = expected.Gender, Visible = true } }; foreach (var language in expected.Languages) { categories.Add(new Category { Group = CategoryGroup.Language, Name = language, Visible = true }); } foreach (var skill in expected.Skills) { categories.Add(new Category { Group = CategoryGroup.Skill, Name = skill.Name, Visible = true }); } var expectedGender = expected.Gender; var expectedLanguageCount = expected.Languages.Count; var expectedSkillCount = expected.Skills.Count; var visibleCategories = categories.Where(x => x.Visible); var store = Substitute.For <IProfileStore>(); var cache = Substitute.For <IProfileCache>(); var query = Substitute.For <ICategoryQuery>(); var sut = new ProfileQuery(store, cache, query); using (var tokenSource = new CancellationTokenSource()) { store.GetProfile(expected.Id, tokenSource.Token).Returns(expected); query.GetCategories(ReadType.VisibleOnly, tokenSource.Token).Returns(visibleCategories); var actual = await sut.GetPublicProfile(expected.Id, tokenSource.Token).ConfigureAwait(false); actual.Gender.Should().Be(expectedGender); actual.Languages.Should().HaveCount(expectedLanguageCount); actual.Skills.Should().HaveCount(expectedSkillCount); } }
// POST api/profilequeries public ProfileQuery Post(ProfileQuery profileQuery) { try { if (profileQuery.Id == null || profileQuery.Id == "null") { profileQuery.Id = null; } _dataService.SaveProfileQuery(profileQuery); return(profileQuery); } catch { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } }
public void SaveProfileQuery(ProfileQuery profileQuery) { var documentStore = GetDocumentStore(); var session = documentStore.OpenSession(); //if (String.IsNullOrEmpty(profileQuery.Id)) //{ // var existing = session.Load<ProfileQuery>(profileQuery.Id); //} //else //{ session.Store(profileQuery); session.SaveChanges(); //} }
public async Task GetPublicProfileReturnsNullWhenProfileNotFoundTest() { var expected = Model.Create <Profile>().Set(x => x.Status = ProfileStatus.Unavailable); var store = Substitute.For <IProfileStore>(); var cache = Substitute.For <IProfileCache>(); var query = Substitute.For <ICategoryQuery>(); var sut = new ProfileQuery(store, cache, query); using (var tokenSource = new CancellationTokenSource()) { var actual = await sut.GetPublicProfile(expected.Id, tokenSource.Token).ConfigureAwait(false); actual.Should().BeNull(); cache.DidNotReceive().StoreProfile(Arg.Any <Profile>()); } }
public async Task GetProfileReturnsCachedProfileTest() { var expected = Model.Create <Profile>().Set(x => x.Status = ProfileStatus.Unavailable); var store = Substitute.For <IProfileStore>(); var cache = Substitute.For <IProfileCache>(); var query = Substitute.For <ICategoryQuery>(); var sut = new ProfileQuery(store, cache, query); using (var tokenSource = new CancellationTokenSource()) { cache.GetProfile(expected.Id).Returns(expected); var actual = await sut.GetProfile(expected.Id, tokenSource.Token).ConfigureAwait(false); actual.Should().BeEquivalentTo(expected); } }
public async Task GetPublicProfileAppliesCategoryFilteringToSkillsTest(bool isVisible, string profileValue, string categoryValue) { var skill = Model.Create <Skill>().Set(x => x.Name = profileValue); var expected = Model.Create <Profile>().Set(x => x.Status = ProfileStatus.Unavailable) .Set(x => x.BannedAt = null).Set(x => x.Skills.Add(skill)); var categories = Model.Create <Collection <Category> >(); var matchingCategory = new Category { Group = CategoryGroup.Skill, Name = categoryValue, Visible = isVisible }; categories.Add(matchingCategory); var visibleCategories = categories.Where(x => x.Visible); var store = Substitute.For <IProfileStore>(); var cache = Substitute.For <IProfileCache>(); var query = Substitute.For <ICategoryQuery>(); var sut = new ProfileQuery(store, cache, query); using (var tokenSource = new CancellationTokenSource()) { store.GetProfile(expected.Id, tokenSource.Token).Returns(expected); query.GetCategories(ReadType.VisibleOnly, tokenSource.Token).Returns(visibleCategories); var actual = await sut.GetPublicProfile(expected.Id, tokenSource.Token).ConfigureAwait(false); if (isVisible) { actual.Skills.Select(x => x.Name).Should().Contain(profileValue); } else { actual.Skills.Select(x => x.Name).Should().NotContain(profileValue); } } }
public async Task GetPublicProfileReturnsNullWhenStoreProfileIsBannedTest() { var expected = Model.Create <Profile>().Set(x => x.BannedAt = DateTimeOffset.UtcNow); var store = Substitute.For <IProfileStore>(); var cache = Substitute.For <IProfileCache>(); var query = Substitute.For <ICategoryQuery>(); var sut = new ProfileQuery(store, cache, query); using (var tokenSource = new CancellationTokenSource()) { store.GetProfile(expected.Id, tokenSource.Token).Returns(expected); var actual = await sut.GetPublicProfile(expected.Id, tokenSource.Token).ConfigureAwait(false); actual.Should().BeNull(); cache.Received().StoreProfile(expected); } }
public async Task GetPublicProfileReturnsNewInstanceOfProfileFromCacheToAvoidCacheCorruptionTest() { var expected = Model.Create <Profile>().Set(x => x.Status = ProfileStatus.Unavailable) .Set(x => x.BannedAt = null); var store = Substitute.For <IProfileStore>(); var cache = Substitute.For <IProfileCache>(); var query = Substitute.For <ICategoryQuery>(); var sut = new ProfileQuery(store, cache, query); using (var tokenSource = new CancellationTokenSource()) { cache.GetProfile(expected.Id).Returns(expected); var actual = await sut.GetPublicProfile(expected.Id, tokenSource.Token).ConfigureAwait(false); actual.Should().NotBeSameAs(expected); } }
public async Task GetProfileReturnsHiddenProfileFromCachedTest() { var expected = Model.Create <Profile>().Set(x => x.Status = ProfileStatus.Hidden) .Set(x => x.BannedAt = null); var store = Substitute.For <IProfileStore>(); var cache = Substitute.For <IProfileCache>(); var query = Substitute.For <ICategoryQuery>(); var sut = new ProfileQuery(store, cache, query); using (var tokenSource = new CancellationTokenSource()) { cache.GetProfile(expected.Id).Returns(expected); var actual = await sut.GetProfile(expected.Id, tokenSource.Token).ConfigureAwait(false); actual.Should().BeEquivalentTo(expected, opt => opt.ExcludingMissingMembers()); cache.DidNotReceive().StoreProfile(Arg.Any <Profile>()); } }
public async Task GetProfileReturnsBannedProfileFromStoreTest() { var expected = Model.Create <Profile>().Set(x => x.Status = ProfileStatus.Available) .Set(x => x.BannedAt = DateTimeOffset.UtcNow); var store = Substitute.For <IProfileStore>(); var cache = Substitute.For <IProfileCache>(); var query = Substitute.For <ICategoryQuery>(); var sut = new ProfileQuery(store, cache, query); using (var tokenSource = new CancellationTokenSource()) { store.GetProfile(expected.Id, tokenSource.Token).Returns(expected); var actual = await sut.GetProfile(expected.Id, tokenSource.Token).ConfigureAwait(false); actual.Should().BeEquivalentTo(expected, opt => opt.ExcludingMissingMembers()); cache.Received().StoreProfile(expected); } }
public async Task GetPublicProfileHandlesCategoryFilteringWhenSkillsIsNullTest() { var expected = Model.Create <Profile>().Set(x => x.Status = ProfileStatus.Unavailable) .Set(x => x.BannedAt = null).Set(x => x.Skills = null); var categories = Model.Create <Collection <Category> >(); var visibleCategories = categories.Where(x => x.Visible); var store = Substitute.For <IProfileStore>(); var cache = Substitute.For <IProfileCache>(); var query = Substitute.For <ICategoryQuery>(); var sut = new ProfileQuery(store, cache, query); using (var tokenSource = new CancellationTokenSource()) { store.GetProfile(expected.Id, tokenSource.Token).Returns(expected); query.GetCategories(ReadType.VisibleOnly, tokenSource.Token).Returns(visibleCategories); var actual = await sut.GetPublicProfile(expected.Id, tokenSource.Token).ConfigureAwait(false); actual.Skills.Should().BeNullOrEmpty(); } }
public static IQueryable <Profile> ApplyFiltering(this IQueryable <Profile> query, ProfileQuery queryObj) { if (queryObj.Age.HasValue) { query = query.Where(p => p.Age == queryObj.Age); } if (queryObj.Gender != null) { query = query.Where(p => p.Gender == queryObj.Gender); } if (queryObj.Ocupation.HasValue) { query = query.Where(p => p.OcupationId == queryObj.Ocupation); } if (queryObj.Address != null) { query = query.Where(p => p.Address == queryObj.Address); } return(query); }
public async Task <Profile> GetById(Guid id) => await _context.Profiles.Where(ProfileQuery.FindById(id)).AsNoTracking().FirstOrDefaultAsync();
public async Task <Profile> GetByUserId(Guid id) => await _context.Profiles.Where(ProfileQuery.FindByUserId(id)).AsNoTracking().Include(x => x.Address).FirstOrDefaultAsync();
public async Task <IEnumerable <Profile> > GetProfiles(ProfileQuery queryObj) { var query = context.Profiles.Include(p => p.Ocupation) .Where(p => p.Deleted == false); /* * Filter */ query = query.ApplyFiltering(queryObj); /* * Forma Ampliada de Sorting * * if (queryObj.Age.HasValue) * query = query.Where(p => p.Age == queryObj.Age); * if (queryObj.Gender != null) * query = query.Where(p => p.Gender == queryObj.Gender); * if (queryObj.Ocupation.HasValue) * query = query.Where(p => p.OcupationId == queryObj.Ocupation); * if (queryObj.Address != null) * query = query.Where(p => p.Address == queryObj.Address); */ /* * Sorting */ var columnsMap = new Dictionary <string, Expression <Func <Profile, object> > >() { ["age"] = profile => profile.Age, ["gender"] = profile => profile.Gender, ["ocupation"] = profile => profile.Ocupation, ["address"] = profile => profile.Address }; query = query.ApplySorting(queryObj, columnsMap); /* * Forma Ampliada de Sorting * * if (queryObj.SortBy == "age") * query = queryObj.IsSortAsc ? query.OrderBy(p => p.Age) : query.OrderByDescending(p => p.Age); * if (queryObj.SortBy == "gender") * query = queryObj.IsSortAsc ? query.OrderBy(p => p.Gender) : query.OrderByDescending(p => p.Gender); * if (queryObj.SortBy == "ocupation") * query = queryObj.IsSortAsc * ? query.OrderBy(p => p.Ocupation) * : query.OrderByDescending(p => p.Ocupation); * if (queryObj.SortBy == "address") * query = queryObj.IsSortAsc ? query.OrderBy(p => p.Address) : query.OrderByDescending(p => p.Address); */ /* * Paginig */ query = query.ApplyPaging(queryObj); /* * Forma Ampliada de Sorting * * if (queryObj.Page <= 0) * queryObj.Page = 1; * if (queryObj.PageSize <= 0) * queryObj.PageSize = 10; * query = query.Skip((queryObj.Page - 1) * queryObj.PageSize).Take(queryObj.PageSize); */ return(await query.ToListAsync()); }