/// <summary> /// Gets the specified page of authors matching <paramref name="filter" />. /// </summary> /// <param name="filter">The filter.</param> /// <returns> /// Page of authors. /// </returns> /// <exception cref="ArgumentNullException">filter</exception> public DataPage <Author> GetAuthors(AuthorFilter filter) { if (filter == null) { throw new ArgumentNullException(nameof(filter)); } PrepareAuthorFilter(filter); using (var db = GetContext()) { var authors = db.Authors.AsQueryable(); if (!string.IsNullOrEmpty(filter.Last)) { authors = authors.Where(a => a.Lastx.Contains(filter.Last)); } int tot = authors.Count(); // sort and page authors = authors.OrderBy(a => a.Last) .ThenBy(a => a.First) .ThenBy(a => a.Suffix) .ThenBy(a => a.Id); var pgAuthors = authors.Skip(filter.GetSkipCount()) .Take(filter.PageSize) .ToList(); return(new DataPage <Author>( filter.PageNumber, filter.PageSize, tot, (from a in pgAuthors select EfHelper.GetAuthor(a)).ToList())); } }
/// <summary> /// Gets the keyword with the specified ID. /// </summary> /// <param name="id">The identifier.</param> /// <returns>The keyword or null if not found.</returns> public Keyword GetKeyword(int id) { using (var db = GetContext()) { EfKeyword ef = db.Keywords.Find(id); return(ef != null?EfHelper.GetKeyword(ef) : null); } }
/// <summary> /// Gets the author with the specified ID. /// </summary> /// <param name="id">The identifier.</param> /// <returns> /// The author, or null if not found. /// </returns> public Author GetAuthor(Guid id) { using (var db = GetContext()) { EfAuthor ef = db.Authors.Find(id); return(EfHelper.GetAuthor(ef)); } }
/// <summary> /// Gets the type with the specified ID. /// </summary> /// <param name="id">The identifier.</param> /// <returns>The type, or null if not found.</returns> /// <exception cref="ArgumentNullException">id</exception> public WorkType GetWorkType(string id) { if (id == null) { throw new ArgumentNullException(nameof(id)); } using (var db = GetContext()) { EfWorkType ef = db.WorkTypes.Find(id); return(EfHelper.GetWorkType(ef)); } }
/// <summary> /// Adds or updates the type with the specified ID and name. /// </summary> /// <param name="type">The type.</param> /// <exception cref="ArgumentNullException">type</exception> public void AddWorkType(WorkType type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } using (var db = GetContext()) { EfWorkType ef = EfHelper.GetEfWorkType(type, db); db.SaveChanges(); } }
/// <summary> /// Adds or updates the specified keyword. /// </summary> /// <param name="keyword">The keyword.</param> /// <returns>The keyword's ID.</returns> /// <exception cref="ArgumentNullException">keyword</exception> public int AddKeyword(Keyword keyword) { if (keyword == null) { throw new ArgumentNullException(nameof(keyword)); } using (var db = GetContext()) { EfKeyword ef = EfHelper.GetEfKeyword(keyword, db); db.SaveChanges(); return(ef.Id); } }
/// <summary> /// Adds or updates the specified author. /// If the ID is not specified, a new author will be created and /// the <paramref name="author"/>'s ID will be updated. /// </summary> /// <param name="author">The author.</param> /// <exception cref="ArgumentNullException">author</exception> public void AddAuthor(Author author) { if (author == null) { throw new ArgumentNullException(nameof(author)); } using (var db = GetContext()) { EfAuthor ef = EfHelper.GetEfAuthor(author, db); db.SaveChanges(); author.Id = ef.Id; } }
/// <summary> /// Adds or updates the specified container. /// Container type, authors, and keywords are stored too. /// As for authors, you can specify only the author's ID to assign /// the work to an existing author; otherwise, the author will be /// either added or updated as required. Keywords are added or /// updated as required. Type is added if not found, even this should /// not happen, as types are a predefined set. /// If new, <paramref name="container"/> ID gets updated; the key is /// always updated. /// </summary> /// <param name="container">The container.</param> /// <exception cref="ArgumentNullException">container</exception> public void AddContainer(Container container) { if (container == null) { throw new ArgumentNullException(nameof(container)); } using (var db = GetContext()) { EfContainer ef = EfHelper.GetEfContainer(container, db); db.SaveChanges(); container.Id = ef.Id; container.Key = ef.Key; } }
/// <summary> /// Gets the container by its ID. /// </summary> /// <param name="id">The container's identifier.</param> /// <returns> /// Container, or null if not found /// </returns> public Container GetContainer(Guid id) { using (var db = GetContext()) { EfContainer container = db.Containers .AsNoTracking() .Include(w => w.Type) .Include(w => w.AuthorContainers) .ThenInclude(aw => aw.Author) .Include(w => w.KeywordContainers) .ThenInclude(kw => kw.Keyword) .FirstOrDefault(w => w.Id == id); return(EfHelper.GetContainer(container)); } }
/// <summary> /// Adds or updates the specified work. /// Work type, container, authors, and keywords are stored too. /// As for authors, you can specify only the author's ID to assign /// the work to an existing author; otherwise, the author will be /// either added or updated as required. Keywords are added or /// updated as required. Type is added if not found, even this should /// not happen, as types are a predefined set. As for the container, /// you can specify only its ID to assign the work to it; otherwise, /// the container will be added or updated as required. /// If new, <paramref name="work"/> ID gets updated; the key is /// always updated. /// </summary> /// <param name="work">The work.</param> /// <exception cref="ArgumentNullException">work</exception> public void AddWork(Work work) { if (work == null) { throw new ArgumentNullException(nameof(work)); } using (var db = GetContext()) { EfWork ef = EfHelper.GetEfWork(work, db); db.SaveChanges(); work.Id = ef.Id; work.Key = ef.Key; } }
/// <summary> /// Gets the specified page of keywords. /// </summary> /// <param name="filter">The filter.</param> /// <returns>The page.</returns> public DataPage <Keyword> GetKeywords(KeywordFilter filter) { PrepareKeywordFilter(filter); using (var db = GetContext()) { var keywords = db.Keywords.AsQueryable(); if (!string.IsNullOrEmpty(filter.Language)) { keywords = keywords.Where(k => k.Language == filter.Language); } if (!string.IsNullOrEmpty(filter.Value)) { // filter value for keyword can be language:value int i = filter.Value.IndexOf(':'); if (i == 3) { string l = filter.Value.Substring(0, 3); keywords = keywords.Where(k => k.Language == l); if (filter.Value.Length > 4) { string v = filter.Value.Substring(4); keywords = keywords.Where(k => k.Valuex.Contains(v)); } } else { keywords = keywords.Where(k => k.Valuex.Contains(filter.Value)); } } int tot = keywords.Count(); // sort and page keywords = keywords.OrderBy(k => k.Language) .ThenBy(k => k.Value) .ThenBy(k => k.Id); keywords = keywords.Skip(filter.GetSkipCount()).Take(filter.PageSize); return(new DataPage <Keyword>( filter.PageNumber, filter.PageSize, tot, (from k in keywords select EfHelper.GetKeyword(k)).ToList())); } }
/// <summary> /// Gets the work by its ID. /// </summary> /// <param name="id">The work's identifier.</param> /// <returns> /// Work, or null if not found /// </returns> public Work GetWork(Guid id) { using (var db = GetContext()) { EfWork work = db.Works .AsNoTracking() .Include(w => w.Type) .Include(w => w.Container) .Include(w => w.AuthorWorks) .ThenInclude(aw => aw.Author) .Include(w => w.KeywordWorks) .ThenInclude(kw => kw.Keyword) .FirstOrDefault(w => w.Id == id); return(EfHelper.GetWork(work)); } }
/// <summary> /// Gets the page of types matching the specified filter, /// or all of them when page size is 0. /// </summary> /// <param name="filter">The filter.</param> /// <returns>The types page.</returns> /// <exception cref="ArgumentNullException">filter</exception> public DataPage <WorkType> GetWorkTypes(WorkTypeFilter filter) { if (filter == null) { throw new ArgumentNullException(nameof(filter)); } using (var db = GetContext()) { IQueryable <EfWorkType> types = db.WorkTypes.AsQueryable(); if (!string.IsNullOrEmpty(filter.Name)) { types = types.Where(t => t.Name.Contains(filter.Name)); } int tot = types.Count(); // sort and page types = types.OrderBy(t => t.Name).ThenBy(t => t.Id); List <EfWorkType> pgTypes; if (filter.PageSize > 0) { pgTypes = types.Skip(filter.GetSkipCount()) .Take(filter.PageSize) .ToList(); } else { pgTypes = types.Skip(filter.GetSkipCount()).ToList(); } return(new DataPage <WorkType>( filter.PageNumber, filter.PageSize, tot, (from t in pgTypes select EfHelper.GetWorkType(t)).ToList())); } }
/// <summary> /// Gets the specified page of filtered works. /// </summary> /// <param name="filter">The filter.</param> /// <returns>The works page.</returns> /// <exception cref="ArgumentNullException">filter</exception> public DataPage <WorkInfo> GetWorks(WorkFilter filter) { if (filter == null) { throw new ArgumentNullException(nameof(filter)); } PrepareWorkFilter(filter); using (var db = GetContext()) { IQueryable <EfWork> works = db.Works .AsNoTracking() .Include(w => w.Type) .Include(w => w.Container) .Include(w => w.AuthorWorks) .ThenInclude(aw => aw.Author) .Include(w => w.KeywordWorks) .ThenInclude(kw => kw.Keyword); if (filter.IsMatchAnyEnabled) { // we need a predicate builder to chain clauses with OR // (note: this requires package LinqKit.Core) // http://www.albahari.com/nutshell/predicatebuilder.aspx var predicate = PredicateBuilder.New <EfWork>(); if (!string.IsNullOrEmpty(filter.Key)) { predicate.Or(w => w.Key.Equals(filter.Key)); } if (!string.IsNullOrEmpty(filter.Type)) { predicate.Or(w => w.Type.Equals(filter.Type)); } if (filter.AuthorId != Guid.Empty) { predicate.Or(w => w.AuthorWorks.Any( aw => aw.AuthorId == filter.AuthorId)); } if (!string.IsNullOrEmpty(filter.LastName)) { predicate.Or(w => w.AuthorWorks.Any(aw => aw.Author.Lastx.Contains(filter.LastName))); } if (!string.IsNullOrEmpty(filter.Language)) { predicate.Or(w => w.Language.Equals(filter.Language)); } if (!string.IsNullOrEmpty(filter.Title)) { predicate.Or(w => w.Titlex.Contains(filter.Title)); } if (filter.ContainerId != Guid.Empty) { predicate.Or(w => w.Container.Id.Equals(filter.ContainerId)); } if (!string.IsNullOrEmpty(filter.Keyword)) { predicate.Or(w => w.KeywordWorks.Any( kw => kw.Keyword.Valuex.Equals(filter.Keyword))); } if (filter.YearPubMin > 0) { predicate.Or(w => w.YearPub >= filter.YearPubMin); } if (filter.YearPubMax > 0) { predicate.Or(w => w.YearPub <= filter.YearPubMax); } works = works.AsExpandable().Where(predicate); } else { // key if (!string.IsNullOrEmpty(filter.Key)) { works = works.Where(w => w.Key == filter.Key); } // type if (!string.IsNullOrEmpty(filter.Type)) { works = works.Where(w => w.Type.Name == filter.Type); } // author ID if (filter.AuthorId != Guid.Empty) { works = works.Where(w => w.AuthorWorks.Any( aw => aw.AuthorId == filter.AuthorId)); } // last if (!string.IsNullOrEmpty(filter.LastName)) { works = works.Where(w => w.AuthorWorks.Any( aw => aw.Author.Lastx.Contains(filter.LastName))); } // language if (!string.IsNullOrEmpty(filter.Language)) { works = works.Where(w => w.Language == filter.Language); } // title if (!string.IsNullOrEmpty(filter.Title)) { works = works.Where(w => w.Titlex.Contains(filter.Title)); } // container ID if (filter.ContainerId != Guid.Empty) { works = works.Where(w => w.ContainerId.Equals(filter.ContainerId)); } // keyword if (!string.IsNullOrEmpty(filter.Keyword)) { works = works.Where(w => w.KeywordWorks.Any( kw => kw.Keyword.Valuex.Equals(filter.Keyword))); } // yearpubmin if (filter.YearPubMin > 0) { works = works.Where(w => w.YearPub >= filter.YearPubMin); } // yearpubmax if (filter.YearPubMax > 0) { works = works.Where(w => w.YearPub <= filter.YearPubMax); } } int tot = works.Count(); // sort and page works = works .OrderBy(w => w.AuthorWorks.Select(aw => aw.Author) .First().Lastx) .ThenBy(w => w.AuthorWorks.Select(aw => aw.Author) .First().First) .ThenBy(w => w.Titlex) .ThenBy(w => w.Key) .ThenBy(w => w.Id); var pgWorks = works.Skip(filter.GetSkipCount()) .Take(filter.PageSize) .ToList(); return(new DataPage <WorkInfo>( filter.PageNumber, filter.PageSize, tot, (from w in pgWorks select EfHelper.GetWorkInfo(w, db)).ToList())); } }