/// <summary>Determines whether this instance [can borrow more than d books from same domain] the specified domain.</summary> /// <param name="editions">The editions.</param> /// <returns> /// <c>true</c> if this instance [can borrow more than d books from same domain] the specified domain; otherwise, <c>false</c>.</returns> public bool MoreThanDBooksFromSameDomain(List <Edition> editions) { Log.Info("Checking if the books the reader tries to borrow are from the same domain."); IDomainDataService dds = new SQLDomainDataService(); IConfigurationDataService cds = new SQLConfigurationDataService(); IDictionary <int, int> domainApparitions = new Dictionary <int, int>(); using (var context = new LibraryContext()) { foreach (Edition ed in editions) { Edition currentEdition = context.Editions.Where(e => e.ID == ed.ID).Single(); foreach (Domain dom in currentEdition.Book.Domains) { var id = dds.GetRootDomain(dom).ID; if (!domainApparitions.ContainsKey(id)) { domainApparitions.Add(id, 1); } else { domainApparitions[id]++; } } } foreach (var counter in domainApparitions.Values) { if (counter > cds.GetConfiguration("MaxBooksFromSameDomain").Value) { Log.Info("The books the reader tries to borrow are not from the same domain."); return(false); } } } Log.Info("The books the reader tries to borrow are from the same domain."); return(true); }
/// <summary>Gets the books from domain.</summary> /// <param name="domain">The domain.</param> /// <returns>a a</returns> public ICollection <Book> GetBooksFromDomain(Domain domain) { Log.Info("Getting books from domain "); if (domain == null) { return(new List <Book>()); } IDomainDataService domainServices = new SQLDomainDataService(); using (var context = new LibraryContext()) { List <Domain> domains = new List <Domain> { domain }; List <Book> books = new List <Book>(); while (domains.Count > 0) { var currentDomain = domains[0]; currentDomain = context.Domains.Where(d => d.ID == currentDomain.ID).Single(); domains.RemoveAt(0); if (currentDomain.Books != null) { books.AddRange(currentDomain.Books); } domains.AddRange(domainServices.GetChildDomains(currentDomain)); } Log.Info("Returning books from domain " + domain.Name); return(books); } }