Example #1
0
        private ICollection <Tag> GetTags(ArticleDataModel model)
        {
            var titletgas = model.Title.Split(' ');
            var allTags   = new HashSet <string>(titletgas);

            foreach (var modelTag in model.Tags)
            {
                allTags.Add(modelTag.Name);
            }

            var articleTags = new HashSet <Tag>();

            foreach (var tagName in allTags)
            {
                var tag = this.data.Tags.All()
                          .FirstOrDefault(t => t.Name == tagName);
                if (tag == null)
                {
                    tag = new Tag {
                        Name = tagName
                    };
                    this.data.Tags.Add(tag);
                }

                articleTags.Add(tag);
            }

            return(articleTags);
        }
Example #2
0
        public IHttpActionResult Create(ArticleDataModel article)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userId = this.User.Identity.GetUserId();

            var category = this.GetCategory(article);
            var tags     = this.GetTags(article);

            var articleToAdd = new Article
            {
                Title       = article.Title,
                Content     = article.Content,
                DateCreated = DateTime.Now,
                AuthorId    = userId,
                CategoryId  = category.Id,
                Tags        = tags
            };

            this.Data.Articles.Add(articleToAdd);
            this.Data.SaveChanges();

            article.Id          = articleToAdd.Id;
            article.DateCreated = articleToAdd.DateCreated;
            article.Tags        = articleToAdd.Tags.Select(t => t.Name);

            return(Ok(article));
        }
Example #3
0
        private HashSet <Tag> GetTags(ArticleDataModel article)
        {
            HashSet <Tag> tags          = new HashSet <Tag>();
            var           newTags       = article.Tags.ToList();
            var           tagsFromTitle = article.Title.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            newTags.AddRange(tagsFromTitle);

            foreach (var tagName in newTags)
            {
                var tag = this.Data.Tags
                          .All()
                          .FirstOrDefault(t => t.Name == tagName);

                if (tag == null)
                {
                    tag = new Tag {
                        Name = tagName
                    };
                    this.Data.Tags.Add(tag);
                }

                tags.Add(tag);
            }

            return(tags);
        }
Example #4
0
        public IHttpActionResult Create(ArticleDataModel model)
        {
            var userID   = this.User.Identity.GetUserId();
            var tags     = GetTags(model);
            var category = GetCategory(model.Category);

            var article = new Article
            {
                Title       = model.Title,
                Content     = model.Content,
                DateCreated = DateTime.Now,
                CategoryID  = category.ID,
                AuthorID    = userID,
                Tags        = tags,
            };

            this.data.Articles.Add(article);
            this.data.SaveChanges();

            model.ID          = article.ID;
            model.DateCreated = article.DateCreated;
            model.Tags        = article.Tags.AsQueryable()
                                .Select(TagDataModel.FromTag).ToArray();

            return(Ok(model));
        }
        private ICollection<Tag> GetTags(ArticleDataModel model)
        {
            var titletgas = model.Title.Split(' ');
            var allTags = new HashSet<string>(titletgas);

            foreach (var modelTag in model.Tags)
            {
                allTags.Add(modelTag.Name);
            }

            var articleTags = new HashSet<Tag>();
            foreach (var tagName in allTags)
            {
                var tag = this.data.Tags.All()
                .FirstOrDefault(t => t.Name == tagName);
                if (tag == null)
                {
                    tag = new Tag { Name = tagName };
                    this.data.Tags.Add(tag);
                }

                articleTags.Add(tag);
            }

            return articleTags;
        }
        public IHttpActionResult Create(ArticleDataModel model)
        {
            var userID = this.User.Identity.GetUserId();
            var tags = GetTags(model);
            var category = GetCategory(model.Category);

            var article = new Article
            {
                Title = model.Title,
                Content = model.Content,
                DateCreated = DateTime.Now,
                CategoryID = category.ID,
                AuthorID = userID,
                Tags = tags,
            };
            this.data.Articles.Add(article);
            this.data.SaveChanges();

            model.ID = article.ID;
            model.DateCreated = article.DateCreated;
            model.Tags = article.Tags.AsQueryable()
                .Select(TagDataModel.FromTag).ToArray();

            return Ok(model);
        }
Example #7
0
        private Category GetCategory(ArticleDataModel article)
        {
            var category = this.Data.Categories
                           .All()
                           .FirstOrDefault(c => c.Name == article.Category);

            if (category == null)
            {
                category = new Category {
                    Name = article.Category
                };
                this.Data.Categories.Add(category);
            }

            return(category);
        }
Example #8
0
        private ArticleViewModel MapTo_ArticleViewModel(ArticleDataModel article)
        {
            try
            {
                var base64 = Convert.ToBase64String(article.Image);
                var imgsrc = string.Format("data:file/gif;base64,{0}", base64);

                var model = Mapper.Map <ArticleViewModel>(article);
                model.Image = imgsrc;
                return(model);
            }
            catch
            {
                return(null);
            }
        }
Example #9
0
        private EditBindingModel MapTo_EditBindingModel(ArticleDataModel article)
        {
            try
            {
                var base64 = Convert.ToBase64String(article.Image);
                var imgsrc = string.Format("data:file/gif;base64,{0}", base64);

                var model = Mapper.Map <EditBindingModel>(article);
                model.Image      = article.Image;
                model.Image64    = imgsrc;
                model.Categories = CategoriesInformation.Categories;

                return(model);
            }
            catch (Exception e)
            {
                return(null);
            }
        }