示例#1
0
        public async Task <Article> Add(Article newsArticle)
        {
            if (await _context.Authors.FindAsync(newsArticle.Author.Id) == null)
            {
                _context.Add(newsArticle.Author);
            }

            _context.Add(newsArticle);

            await _context.SaveChangesAsync();

            return(await GetArticleAsync(newsArticle.Id));
        }
示例#2
0
        public bool CreateArticle(Article article)
        {
            try
            {
                _context.Add(article);
                _context.SaveChanges();
            }
            catch (DbUpdateException)
            {
                return(false);
            }

            return(true);
        }
示例#3
0
        public async Task <IActionResult> Create([Bind("Id,Title,DateOfCreation,DateOfPublication,IsPublish,PhotoName,Content,AdminId,SubTopicId")] Article article)
        {
            if (ModelState.IsValid)
            {
                article.Id = Guid.NewGuid();
                _context.Add(article);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AdminId"]    = new SelectList(_context.Admins, "Id", "Id", article.AdminId);
            ViewData["SubTopicId"] = new SelectList(_context.SubTopics, "Id", "Id", article.SubTopicId);
            return(View(article));
        }
示例#4
0
        public async Task <string> Post(TblNews tblNews)
        {
            try
            {
                tblNews.UrlFriendly     = CommonUtil.UrlFriendly(tblNews.TitleNews);
                tblNews.DescriptionNews = CommonUtil.StripHTML(tblNews.Content);
                if (tblNews.DescriptionNews != null && tblNews.DescriptionNews.Length > 150)
                {
                    tblNews.DescriptionNews = tblNews.DescriptionNews.Substring(0, 150) + ComConst.READ_MORE;
                }

                var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot/images", tblNews.UrlFriendly + ".jpg");
                await LoadImageFromUrl(filePath, tblNews.UrlImg);

                tblNews.UrlImg   = ComConst.URL + "/images/" + tblNews.UrlFriendly + ".jpg";
                tblNews.DateNews = DateTime.Now;
                _context.Add(tblNews);
                _context.SaveChanges();

                LIST_CATEGORY = _context.TblCategory.ToList();
                LIST_NEWS     = _context.TblNews.ToList();
                return("thành công");
            } catch (Exception ex)
            {
                return(ex.Message);
            }
        }
示例#5
0
        public async Task <IActionResult> Create(Guid id, [Bind("Id,Title,Summary,PostTime,Photo")] NewsViewModel new_news)
        {
            if (ModelState.IsValid)
            {
                News news = new News {
                    Id = Guid.NewGuid(), Title = new_news.Title, Summary = new_news.Summary, PostTime = DateTime.Now
                };

                if (new_news.Photo != null)
                {
                    byte[] imageData = null;
                    // считываем переданный файл в массив байтов
                    using (var binaryReader = new BinaryReader(new_news.Photo.OpenReadStream()))
                    {
                        imageData = binaryReader.ReadBytes((int)new_news.Photo.Length);
                    }
                    // установка массива байтов
                    news.Photo = imageData;
                    Console.WriteLine(imageData);
                }

                _context.Add(news);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(RedirectToAction("Index"));
        }
示例#6
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
            }));
        }
示例#7
0
        public async Task <IActionResult> Create([Bind("Id,Name,DisplayName,Priority")] Langs langs)
        {
            if (ModelState.IsValid)
            {
                _context.Add(langs);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(langs));
        }
示例#8
0
        public async Task <IActionResult> Create([Bind("AuthorId,AuthorName,SurName,UserName,PassWord,Email")] Author author)
        {
            if (ModelState.IsValid)
            {
                _context.Add(author);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(author));
        }
示例#9
0
        public async Task <IActionResult> Create([Bind("UserId,Email")] User user)
        {
            if (ModelState.IsValid)
            {
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
示例#10
0
        public async Task <IActionResult> Create([Bind("Id,Name,Description")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
示例#11
0
        //Action to insert new news
        public IActionResult AddNews(Models.News model)
        {
            model.Date = DateTime.Now;
            var category = _dataBaseContext.Categories.Where(c => c.Name == model.Category.Name).ToList();

            model.CategoryId = category[0].Id;
            model.Category   = null;
            _dataBaseContext.Add(model);
            _dataBaseContext.SaveChanges();
            return(RedirectToAction("Admin"));
        }
        public async Task <IActionResult> Create([Bind("QuestionID,Email,QuestionText")] Question question)
        {
            if (ModelState.IsValid)
            {
                _context.Add(question);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(question));
        }
示例#13
0
        public async Task <ActionResult <int> > Add([FromBody] NewsDto newsDto)
        {
            var news = _mapper.Map <News>(newsDto);

            _context.Add(news);

            await _context.SaveChangesAsync();

            _eventBus.Publish(new NewsAddEvent(news));

            return(CreatedAtAction(nameof(Get), new { id = news.Id }, news.Id));
        }
示例#14
0
 public async Task SaveNews(News news)
 {
     try
     {
         _context.Add(news);
         await _context.SaveChangesAsync();
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
示例#15
0
 public bool InsertPage(PagesTB page)
 {
     try
     {
         _db.Add(page);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
        public async Task <IActionResult> Create(Teammember teammember)
        {
            if (ModelState.IsValid)
            {
                uploadphote(teammember);
                _context.Add(teammember);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(teammember));
        }
示例#17
0
        public async Task <IActionResult> Create([Bind("MediaId,Path,NewsId")] Media media)
        {
            if (ModelState.IsValid)
            {
                _context.Add(media);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["NewsId"] = new SelectList(_context.News, "NewsId", "LitleTitle", media.NewsId);
            return(View(media));
        }
示例#18
0
        public async Task <IActionResult> Create([Bind("Id,Value")] Topic topic)
        {
            if (ModelState.IsValid)
            {
                topic.Id = Guid.NewGuid();
                _context.Add(topic);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(topic));
        }
示例#19
0
 public bool InsertGroup(PageGroups groups)
 {
     try
     {
         _db.Add(groups);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
        public async Task <IActionResult> Create(News news)
        {
            if (ModelState.IsValid)
            {
                uploadphote(news);
                _context.Add(news);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", news.CategoryId);
            return(View(news));
        }
示例#21
0
        public async Task <IActionResult> Create([Bind("Id,Value,TopicId")] SubTopic subTopic)
        {
            if (ModelState.IsValid)
            {
                subTopic.Id = Guid.NewGuid();
                _context.Add(subTopic);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["TopicId"] = new SelectList(_context.Topics, "Id", "Id", subTopic.TopicId);
            return(View(subTopic));
        }
示例#22
0
        public async Task <IActionResult> Create([Bind("NewsId,NewsTitle,LitleTitle,NewsSummary,NewsText,Source,ReadCount,NewsReleasetime,AuthorId,CategoryId")] News news)
        {
            if (ModelState.IsValid)
            {
                _context.Add(news);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AuthorId"]   = new SelectList(_context.Authors, "AuthorId", "AuthorId", news.AuthorId);
            ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "CategoryId", news.CategoryId);
            return(View(news));
        }
示例#23
0
        public async Task <IActionResult> Create(NewsLang newsLang)
        {
            if (ModelState.IsValid)
            {
                newsLang.CreateDate = DateTime.Now;
                newsLang.ViewCount  = 0;
                _context.Add(newsLang);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["NewsId"]   = new SelectList(_context.News, "Id", "NewsTitle", newsLang.NewsId);
            ViewData["LangName"] = new SelectList(_context.Langs, "Id", "Name", newsLang.LangId);
            return(View(newsLang));
        }
示例#24
0
        public void Post([FromBody] string value)
        {
            News news = new News()
            {
                NewsId      = 800,
                Title       = "Overskrift",
                Author      = "Simon",
                Content     = "Indhold",
                CreatedDate = DateTime.Now.AddDays(-1),
                UpdatedDate = DateTime.Now,
                HashTags    = "#SimonErGud;#FedOpgave"
            };

            _context.Add(news);
            _context.SaveChanges();
        }
示例#25
0
        public async Task <IActionResult> Add([Bind("id,title,content,author,category")] Post post)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _context.Add(post);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            catch (DbUpdateException /* ex */)
            {
                //Log the error (uncomment ex variable name and write a log.
                ModelState.AddModelError("", "Unable to save changes. " +
                                         "Try again, and if the problem persists " +
                                         "see your system administrator.");
            }
            return(View(post));
        }
示例#26
0
        public async Task <IActionResult> Create([Bind("Id,Title,PublisherId,NewsDate,Content,Image")] Models.News news)
        {
            if (ModelState.IsValid)
            {
                var files = HttpContext.Request.Form.Files;
                foreach (var Image in files)
                {
                    if (Image != null && Image.Length > 0)
                    {
                        var file    = Image;
                        var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "uploads\\img\\News");

                        if (file.Length > 0)
                        {
                            var fileName = ContentDispositionHeaderValue.Parse
                                               (file.ContentDisposition).FileName.Trim('"');

                            using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName), FileMode.Create))
                            {
                                await file.CopyToAsync(fileStream);

                                news.Image = file.FileName;
                            }
                        }
                    }
                }

                news.NewsDate = Convert.ToDateTime(DateTime.Now.ToString("MMMM dd yyyy"));
                _context.Add(news);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors);
            }
            return(View(news));
        }