Ejemplo n.º 1
0
        /// <summary>
        /// Gets the entity keyword corresponding to the specified keyword.
        /// </summary>
        /// <param name="keyword">The keyword or null.</param>
        /// <param name="context">The context.</param>
        /// <returns>The entity or null.</returns>
        /// <exception cref="ArgumentNullException">context</exception>
        public static EfKeyword GetEfKeyword(Keyword keyword, BiblioDbContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (keyword == null)
            {
                return(null);
            }
            EfKeyword ef = context.Keywords.FirstOrDefault(
                k => k.Language == keyword.Language &&
                k.Value == keyword.Value);

            if (ef == null)
            {
                ef = new EfKeyword
                {
                    Language = keyword.Language,
                    Value    = keyword.Value,
                    Valuex   = StandardFilter.Apply(keyword.Value, true)
                };
                context.Keywords.Add(ef);
            }

            return(ef);
        }
Ejemplo n.º 2
0
 /// <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);
     }
 }
Ejemplo n.º 3
0
        private static void AddKeywords(IList <Keyword> keywords, EfWork work,
                                        BiblioDbContext context)
        {
            // collect the keywords to be assigned, adding the missing ones
            List <EfKeywordWork> requested = new List <EfKeywordWork>();

            foreach (Keyword keyword in keywords)
            {
                // find the keyword by its content, as we have no ID
                EfKeyword efk = context.Keywords.FirstOrDefault(k =>
                                                                k.Value == keyword.Value && k.Language == keyword.Language);

                // if not found, add it
                if (efk == null)
                {
                    efk = new EfKeyword
                    {
                        Language = keyword.Language,
                        Value    = keyword.Value,
                        Valuex   = StandardFilter.Apply(keyword.Value, true)
                    };
                    context.Keywords.Add(efk);
                }

                requested.Add(new EfKeywordWork
                {
                    Keyword = efk,
                    Work    = work
                });
            }

            // remove all the keywords which are no more requested
            if (work.KeywordWorks != null)
            {
                foreach (EfKeywordWork kw in work.KeywordWorks)
                {
                    if (requested.All(r => r.KeywordId != kw.KeywordId))
                    {
                        context.KeywordWorks.Remove(kw);
                    }
                }
            }
            else
            {
                work.KeywordWorks = new List <EfKeywordWork>();
            }

            // add all those which are not yet present
            foreach (EfKeywordWork kw in requested)
            {
                if (work.KeywordWorks.All(
                        r => r.KeywordId != kw.KeywordId))
                {
                    work.KeywordWorks.Add(kw);
                }
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Gets the keyword corresponding to the specified keyword entity.
 /// </summary>
 /// <param name="ef">The keyword or null.</param>
 /// <returns>The keyword or null.</returns>
 public static Keyword GetKeyword(EfKeyword ef)
 {
     if (ef == null)
     {
         return(null);
     }
     return(new Keyword
     {
         Language = ef.Language,
         Value = ef.Value
     });
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Deletes the keyword with the specified ID.
 /// </summary>
 /// <param name="id">The identifier.</param>
 public void DeleteKeyword(int id)
 {
     using (var db = GetContext())
     {
         EfKeyword ef = db.Keywords.Find(id);
         if (ef != null)
         {
             db.Keywords.Remove(ef);
             db.SaveChanges();
         }
     }
 }
Ejemplo n.º 6
0
        /// <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);
            }
        }