Пример #1
0
        /// <summary>
        /// Gets the author entity corresponding to the specified author.
        /// </summary>
        /// <param name="author">The author or null.</param>
        /// <param name="context">The context.</param>
        /// <returns>The entity or null.</returns>
        /// <exception cref="ArgumentNullException">context</exception>
        public static EfAuthor GetEfAuthor(Author author, BiblioDbContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (author == null)
            {
                return(null);
            }

            EfAuthor ef = author.Id != Guid.Empty
                ? context.Authors.Find(author.Id) : null;

            if (ef == null)
            {
                if (author.Last == null)
                {
                    return(null);
                }
                ef = new EfAuthor();
                context.Authors.Add(ef);
            }

            if (author.Last != null)
            {
                ef.First  = author.First;
                ef.Last   = author.Last;
                ef.Lastx  = StandardFilter.Apply(author.Last, true);
                ef.Suffix = author.Suffix;
            }

            return(ef);
        }
Пример #2
0
 /// <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));
     }
 }
Пример #3
0
 /// <summary>
 /// Deletes the author with the specified ID.
 /// </summary>
 /// <param name="id">The identifier.</param>
 public void DeleteAuthor(Guid id)
 {
     using (var db = GetContext())
     {
         EfAuthor ef = db.Authors.Find(id);
         if (ef != null)
         {
             db.Authors.Remove(ef);
             db.SaveChanges();
         }
     }
 }
Пример #4
0
        /// <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;
            }
        }
Пример #5
0
        private static EfAuthor GetEfAuthorFor(WorkAuthor author,
                                               BiblioDbContext context)
        {
            // find the author
            EfAuthor efa = author.Id != Guid.Empty
                ? context.Authors.Find(author.Id) : null;

            // if not found, add a new author
            if (efa == null)
            {
                // if an existing author was required but was not found,
                // just ignore him (defensive)
                if (author.Last == null)
                {
                    return(null);
                }

                // else we have a new author, add it
                efa = new EfAuthor
                {
                    First  = author.First,
                    Last   = author.Last,
                    Suffix = author.Suffix
                };
                author.Id = efa.Id;         // update the received ID
                context.Authors.Add(efa);
            }
            else
            {
                // if found, supply data in the source author if empty
                if (author.Last == null)
                {
                    author.First  = efa.First;
                    author.Last   = efa.Last;
                    author.Suffix = efa.Suffix;
                }
                // else update the target author
                else
                {
                    efa.First  = author.First;
                    efa.Last   = author.Last;
                    efa.Suffix = author.Suffix;
                }
            }
            // update indexed last
            efa.Lastx = StandardFilter.Apply(efa.Last, true);

            return(efa);
        }
Пример #6
0
        /// <summary>
        /// Gets the author corresponding to the specified EF author.
        /// </summary>
        /// <param name="ef">The author or null.</param>
        /// <returns>The author or null.</returns>
        public static Author GetAuthor(EfAuthor ef)
        {
            if (ef == null)
            {
                return(null);
            }

            return(new Author
            {
                Id = ef.Id,
                First = ef.First,
                Last = ef.Last,
                Suffix = ef.Suffix
            });
        }
Пример #7
0
        private static void AddAuthors(IList <WorkAuthor> authors,
                                       EfContainer container,
                                       BiblioDbContext context)
        {
            // collect the authors to be assigned, adding the missing ones
            List <EfAuthorContainer> requested = new List <EfAuthorContainer>();

            foreach (WorkAuthor author in authors)
            {
                EfAuthor efa = GetEfAuthorFor(author, context);
                if (efa == null)
                {
                    continue;
                }
                requested.Add(new EfAuthorContainer
                {
                    Author    = efa,
                    Container = container
                });
            } // for

            // remove all the no more requested authors
            if (container.AuthorContainers != null)
            {
                foreach (EfAuthorContainer ac in container.AuthorContainers)
                {
                    if (requested.All(r => r.AuthorId != ac.AuthorId))
                    {
                        context.AuthorContainers.Remove(ac);
                    }
                }
            }
            else
            {
                container.AuthorContainers = new List <EfAuthorContainer>();
            }

            // add all those which are not yet present
            foreach (EfAuthorContainer ac in requested)
            {
                if (container.AuthorContainers.All(
                        r => r.AuthorId != ac.AuthorId))
                {
                    container.AuthorContainers.Add(ac);
                }
            }
        }
Пример #8
0
        private static void AddAuthors(IList <WorkAuthor> authors, EfWork work,
                                       BiblioDbContext context)
        {
            // collect the authors to be assigned, adding the missing ones
            List <EfAuthorWork> requested = new List <EfAuthorWork>();

            foreach (WorkAuthor author in authors)
            {
                EfAuthor efa = GetEfAuthorFor(author, context);
                if (efa == null)
                {
                    continue;
                }
                requested.Add(new EfAuthorWork
                {
                    Author = efa,
                    Work   = work
                });
            } // for

            // remove all the no more requested authors
            if (work.AuthorWorks != null)
            {
                foreach (EfAuthorWork aw in work.AuthorWorks)
                {
                    if (requested.All(r => r.AuthorId != aw.AuthorId))
                    {
                        context.AuthorWorks.Remove(aw);
                    }
                }
            }
            else
            {
                work.AuthorWorks = new List <EfAuthorWork>();
            }

            // add all those which are not yet present
            foreach (EfAuthorWork aw in requested)
            {
                if (work.AuthorWorks.All(
                        r => r.AuthorId != aw.AuthorId))
                {
                    work.AuthorWorks.Add(aw);
                }
            }
        }