public async Task <bool> Edit(string id, NewsServiceModel newsServiceModel) { NewsCategory newsCategoryFromDb = await this.context.NewsCategories .SingleOrDefaultAsync(newsCategory => newsCategory.Name == newsServiceModel.NewsCategory.Name); if (newsCategoryFromDb == null) { throw new ArgumentNullException(nameof(newsCategoryFromDb)); } News newsFromDb = await this.context.News.SingleOrDefaultAsync(news => news.Id == id); if (newsFromDb == null) { throw new ArgumentNullException(nameof(newsFromDb)); } newsFromDb.Title = newsServiceModel.Title; newsFromDb.Content = newsServiceModel.Content; newsFromDb.Picture = newsServiceModel.Picture; newsFromDb.NewsCategory = newsCategoryFromDb; this.context.Update(newsFromDb); int result = await this.context.SaveChangesAsync(); return(result > 0); }
public async Task <IActionResult> Create(NewsCreateBindingModel newsCreateBindingModel) { if (!this.ModelState.IsValid) { var allNewsCategory = await this.newsService.GetAllNewsCategory().ToListAsync(); this.ViewData["categories"] = allNewsCategory .Select(newsCategory => new NewsCategoryCreateBindingModel { Name = newsCategory.Name, }) .ToList(); return(this.View()); } string pictureUrl = await this.cloudinaryService .UploadPictureAync(newsCreateBindingModel.Picture, newsCreateBindingModel.Title); NewsServiceModel newsServiceModel = newsCreateBindingModel.To <NewsServiceModel>(); newsServiceModel.Picture = pictureUrl; await this.newsService.Create(newsServiceModel); return(this.Redirect("/")); }
public async Task <IActionResult> Edit(string id, NewsEditBindingModel newsEditBindingModel) { if (!this.ModelState.IsValid) { var allNewsCategories = await this.newsService.GetAllNewsCategory().ToListAsync(); this.ViewData["categories"] = allNewsCategories .Select(newsCategories => new NewsCreateNewsCategoryViewModel { Name = newsCategories.Name, }) .ToList(); return(this.View(newsEditBindingModel)); } string pictureUrl = await this.cloudinaryService .UploadPictureAync(newsEditBindingModel.Picture, newsEditBindingModel.Title); NewsServiceModel newsServiceModel = newsEditBindingModel.To <NewsServiceModel>(); newsServiceModel.Picture = pictureUrl; await this.newsService.Edit(id, newsServiceModel); return(this.Redirect($"/News/Details/{id}")); }
public async Task <bool> CreateNews(NewsServiceModel newsServiceModel) { News news = newsServiceModel.To <News>(); this.context.News.Add(news); int resutl = await this.context.SaveChangesAsync(); return(resutl > 0); }
public async Task <bool> Create(NewsServiceModel newsServiceModel) { NewsCategory newsCategoryFromDb = this.context.NewsCategories .SingleOrDefault(newsCategory => newsCategory.Name == newsServiceModel.NewsCategory.Name); if (newsCategoryFromDb == null) { throw new ArgumentNullException(nameof(newsCategoryFromDb)); } News news = newsServiceModel.To <News>(); news.NewsCategory = newsCategoryFromDb; this.context.News.Add(news); int result = await this.context.SaveChangesAsync(); return(result > 0); }