コード例 #1
0
        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);
        }
コード例 #2
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("/"));
        }
コード例 #3
0
        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}"));
        }
コード例 #4
0
        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);
        }
コード例 #5
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);
        }