Exemple #1
0
        /// <summary>
        /// If Tags are in Database it will load them, if one does not exists, it will create it
        /// This one boii can modify Database, but doesn't save changes
        /// </summary>
        private async Task <List <Tag> > CreateTagListFromStringTagsAsync(DragonContext db, List <string> stringTags)
        {
            var lowerStringTags = stringTags.ConvertAll(st => st.ToLower());
            // Here we will have tags that exists in database
            List <Tag> tagsFound = await
                                   (
                from t in db.Tags
                where lowerStringTags.Contains(t.TagText)
                select t
                                   ).ToListAsync();

            if (tagsFound.Count == stringTags.Count)
            {
                return(tagsFound); // If we found all tags, we don't need to create missing
            }
            // Okey, we need to add some tags, let's go with this shit
            List <string> tagsTextFound = tagsFound.ConvertAll(t => t.TagText);
            List <string> missingTags   =
                (
                    from st in lowerStringTags
                    where tagsTextFound.Contains(st) == false
                    select st
                ).ToList();

            List <Tag> tagsToAdd = missingTags.ConvertAll(mt => new Tag(mt));
            await db.AddRangeAsync(tagsToAdd);

            return(tagsFound.Concat(tagsToAdd).ToList());
        }