Esempio n. 1
0
        public IActionResult AddNews(News news, FormHelper formhelper)
        {
            if (news.Header == null || formhelper.CategoryId == null)
            {
                return(BadRequest(ModelState)); // TODO: Märk upp meddelande för modelstate
                                                // TODO: Validera att rubrik och kategori är unik(?)
            }

            using (var context = new NewsContext())
            {
                news.Created = DateTime.Now;
                news.Updated = DateTime.Now;

                context.Add(news);

                var author = context.Authors.Single(x => x.Id == formhelper.AuthorId);

                var authornews = new AuthorsNews(news, author);

                context.Add(authornews);

                context.SaveChanges();


                for (int i = 0; i < formhelper.CategoryId.Length; i++)
                {
                    var tmpnewscategory = new NewsCategories()
                    {
                        NewsId     = news.Id,
                        CategoryId = formhelper.CategoryId[i]
                    };

                    context.Add(tmpnewscategory);
                    context.SaveChanges();
                }
            }

            var addedMessage = string.Format("Nyheten fick ID {0}", news.Id);

            return(Json(new
            {
                success = true,
                Message = addedMessage
            }));
        }
Esempio n. 2
0
        public void SeedTheNews()
        {
            var context = new NewsContext();

            var news1 = new News();

            news1.Header   = "En fotbollsartikel";
            news1.Intro    = "Lorem ipsum dolor sit amet.";
            news1.Paragraf = "Some more text.";
            news1.Created  = DateTime.Now;
            news1.Updated  = DateTime.Now;

            var news2 = new News();

            news2.Header   = "En lokal artikel";
            news2.Intro    = "Lorem ipsum dolor sit amet.";
            news2.Paragraf = "Some more text.";
            news2.Created  = DateTime.Now;
            news2.Updated  = DateTime.Now;

            var news3 = new News();

            news3.Header   = "En krönika";
            news3.Intro    = "Lorem ipsum dolor sit amet.";
            news3.Paragraf = "Some more text.";
            news3.Created  = DateTime.Now;
            news3.Updated  = DateTime.Now;

            var news4 = new News();

            news4.Header   = "Ett reportage om Kicki Danielsson";
            news4.Intro    = "Lorem ipsum dolor sit amet.";
            news4.Paragraf = "Some more text.";
            news4.Created  = DateTime.Now;
            news4.Updated  = DateTime.Now;

            var news5 = new News();

            news5.Header   = "Ny kungabäbis";
            news5.Intro    = "Lorem ipsum dolor sit amet.";
            news5.Paragraf = "Some more text.";
            news5.Created  = DateTime.Now;
            news5.Updated  = DateTime.Now;

            using (context)
            {
                context.AddRange(news1, news2, news3, news4, news5);

                var result1 = context.Categories.Single(x => x.Id == 3);

                var result2 = context.Categories.Single(x => x.Id == 4);

                var result3 = context.Authors.Single(x => x.Id == 1);


                var newscategory1 = new NewsCategories()
                {
                    NewsId     = news1.Id,
                    CategoryId = result1.Id
                };

                var newscategory2 = new NewsCategories()
                {
                    NewsId     = news1.Id,
                    CategoryId = result2.Id
                };

                var newscategory3 = new NewsCategories()
                {
                    NewsId     = news2.Id,
                    CategoryId = result1.Id
                };

                var newscategory4 = new NewsCategories()
                {
                    NewsId     = news3.Id,
                    CategoryId = result1.Id
                };

                var authornews = new AuthorsNews(news1, result3);

                var authornews1 = new AuthorsNews(news2, result3);

                context.AddRange(newscategory1, newscategory2, newscategory3, newscategory4, authornews, authornews1);

                context.SaveChanges();
            }
        }