示例#1
0
        public async Task <IActionResult> Post(ArticleCreateInputModel inputModel)
        {
            var userId         = this.User.GetId();
            var createdArticle = await this.articleService.CreateAsync <ArticleCreateViewModel>(inputModel, userId);

            return(this.Created($"articles/{createdArticle.Id}", createdArticle));
        }
示例#2
0
        public async Task <IActionResult> Create(ArticleCreateInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                this.ViewData["Error"] = ArticleCreateError;
                return(this.View(inputModel));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            var serviceModel = AutoMapperConfig.MapperInstance.Map <ArticleServiceModel>(inputModel);

            serviceModel.UserId = user.Id;

            if (inputModel.Image != null)
            {
                var imageUrl = await this.cloudinaryService.UploadPicture(inputModel.Image, Guid.NewGuid().ToString());

                serviceModel.ImageUrl = imageUrl;
            }

            var postId = await this.articleService.CreateAsync(serviceModel);

            return(this.RedirectToAction("Single", "Article", new { area = string.Empty, id = postId }));
        }
        public async Task TestAddingAlreadyExistingArticle()
        {
            await this.SeedDatabase();

            var path = "Test.jpg";
            ArticleCreateInputModel model;

            using (var img = File.OpenRead(path))
            {
                var testImage = new FormFile(img, 0, img.Length, "Test.jpg", img.Name)
                {
                    Headers     = new HeaderDictionary(),
                    ContentType = "image/jpeg",
                };

                model = new ArticleCreateInputModel
                {
                    Title       = this.firstArticle.Title,
                    Description = this.firstArticle.Description,
                    Image       = testImage,
                    CategoryId  = 1,
                };
            }

            var exception = await Assert
                            .ThrowsAsync <ArgumentException>(async() => await this.articlesService.CreateAsync(model, this.cookingHubUser.Id));

            Assert.Equal(string.Format(ExceptionMessages.ArticleAlreadyExists, model.Title), exception.Message);
        }
示例#4
0
        public async Task <IActionResult> Create(ArticleCreateInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            var userId       = this.userManager.GetUserId(this.User);
            var serviceModel = new ArticleServiceModel
            {
                PublicationTitle = model.PublicationTitle,
                Description      = model.Description,
                CategoryId       = model.CategoryId,
                UserId           = userId,
            };

            bool isCreted = await this.articlesService.CreateArticle(serviceModel);

            if (!isCreted)
            {
                return(this.View(model));
            }

            return(this.Redirect("/Articles/All"));
            // return this.Json(model);
        }
示例#5
0
        public async Task <int> CreateAsync(ArticleCreateInputModel model, string userId)
        {
            var mainImgId = await this.imagesService.CreateAsync(model.MainImage);

            var article = new Article
            {
                AuthorId      = userId,
                Title         = model.Title,
                Content       = model.Content,
                CategoryId    = model.CategoryId,
                SubCategoryId = model.SubCategoryId,
                PictureId     = mainImgId.Id,
                Region        = model.Region,
                VideoUrl      = model.VideoUrl,
            };

            if (model.GalleryContent != null)
            {
                int galleryId = await this.galleryService.CreateAsync(model.GalleryContent, mainImgId);

                article.GalleryId = galleryId;
            }

            await this.articleRepository.AddAsync(article);

            await this.articleRepository.SaveChangesAsync();

            return(article.Id);
        }
        public async Task TestCreatingArticle()
        {
            await this.SeedUsers();

            await this.SeedCategories();

            var path = "Test.jpg";
            ArticleDetailsViewModel articleDetailsViewModel;

            using (var img = File.OpenRead(path))
            {
                var testImage = new FormFile(img, 0, img.Length, "Test.jpg", img.Name)
                {
                    Headers     = new HeaderDictionary(),
                    ContentType = "image/jpeg",
                };

                var model = new ArticleCreateInputModel
                {
                    Title       = this.firstArticle.Title,
                    Description = this.firstArticle.Description,
                    Image       = testImage,
                    CategoryId  = 1,
                };
                articleDetailsViewModel = await this.articlesService.CreateAsync(model, "1");
            }

            await this.cloudinaryService.DeleteImage(this.cloudinary, articleDetailsViewModel.Title + Suffixes.RecipeSuffix);

            var count = await this.categoriesRepository.All().CountAsync();

            Assert.Equal(1, count);
        }
        public async Task TestAddingRecipeWithMissingCategory()
        {
            await this.SeedUsers();

            await this.SeedCategories();

            var path = "Test.jpg";
            ArticleCreateInputModel model;

            using (var img = File.OpenRead(path))
            {
                var testImage = new FormFile(img, 0, img.Length, "Test.jpg", img.Name)
                {
                    Headers     = new HeaderDictionary(),
                    ContentType = "image/jpeg",
                };

                model = new ArticleCreateInputModel
                {
                    Title       = this.firstArticle.Title,
                    Description = this.firstArticle.Description,
                    Image       = testImage,
                    CategoryId  = 2,
                };
            }

            var exception = await Assert
                            .ThrowsAsync <NullReferenceException>(async() => await this.articlesService.CreateAsync(model, this.cookingHubUser.Id));

            Assert.Equal(string.Format(ExceptionMessages.CategoryNotFound, model.CategoryId), exception.Message);
        }
示例#8
0
        public IActionResult Create()
        {
            var categories = this.categoriesService.GetAllCategoriesWithAnySubCategories <CategoriesDropDownViewModel>();
            var viewModel  = new ArticleCreateInputModel()
            {
                CategoriesDropDown = categories,
            };

            return(this.View(viewModel));
        }
        public async Task <IActionResult> Create(ArticleCreateInputModel articleCreateInputModel)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(articleCreateInputModel));
            }

            var userId  = this.userManager.GetUserId(this.User);
            var article = await this.articleService.CreateAsync(articleCreateInputModel, userId);

            return(RedirectToAction("Details", "Articles", new { article.Id }));
        }
示例#10
0
        public async Task <IActionResult> Create()
        {
            var categories = await this.categoriesService
                             .GetAllCategories <AllCategoriesViewModel>();

            var articleCreateInputModel = new ArticleCreateInputModel
            {
                AllCategories = categories,
            };

            return(this.View(articleCreateInputModel));
        }
示例#11
0
        public IActionResult Create()
        {
            var categories    = this.categoriesService.GetAll <CategoryDropdownViewModel>();
            var subcategories = this.subcategoriesService.GetAll <SubcategoriesDropdownViewModel>();
            var viewModel     = new ArticleCreateInputModel
            {
                Categories    = categories,
                Subcategories = subcategories,
            };

            return(this.View(viewModel));
        }
示例#12
0
        public async Task <IActionResult> Edit(ArticleCreateInputModel input, int id)
        {
            this.ModelState.Remove("MainImage");
            if (!this.ModelState.IsValid)
            {
                input.CategoriesDropDown = this.categoriesService.GetAllCategoriesWithAnySubCategories <CategoriesDropDownViewModel>();
                return(this.View(input));
            }

            await this.articleService.Update(input, id);

            return(this.RedirectToAction(nameof(this.Active)));
        }
示例#13
0
        public async Task <int> Update(ArticleCreateInputModel model, int articleId)
        {
            var article = await this.articleRepository.GetByIdWithDeletedAsync(articleId);

            article.Title         = model.Title;
            article.Content       = model.Content;
            article.CategoryId    = model.CategoryId;
            article.SubCategoryId = model.SubCategoryId;
            article.VideoUrl      = model.VideoUrl;

            this.articleRepository.Update(article);
            return(await this.articleRepository.SaveChangesAsync());
        }
示例#14
0
        public async Task <IActionResult> Create(ArticleCreateInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var articleId = await this.articleService
                            .CreateArticle(input.Title, input.Subtitle, input.Author,
                                           input.ImageUrl, input.Content, input.CategoryId, input.SubcategoryId);

            // TO DO...Show created article
            return(this.RedirectToAction(string.Empty, new { id = articleId }));
        }
示例#15
0
        public async Task <IActionResult> Create(ArticleCreateInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                input.CategoriesDropDown = this.categoriesService.GetAllCategoriesWithAnySubCategories <CategoriesDropDownViewModel>();
                return(this.View(input));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            var articleId = await this.articleService.CreateAsync(input, user.Id);

            return(this.RedirectToAction("Index", "Articles", new { area = string.Empty, id = articleId }));
        }
        public async Task <IActionResult> Create(ArticleCreateInputModel input)
        {
            var user = await this.userManager.GetUserAsync(this.User);

            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var postId = await this.articlesService.CreateAsync(input.Title, input.Content, input.CategoryId, user.Id);

            this.TempData["InfoMessage"] = "Article created!";
            return(this.RedirectToAction(nameof(this.ById), new { id = postId }));
        }
        public async Task <IActionResult> Create(ArticleCreateInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }


            ArticleServiceModel serviceModel = model.To <ArticleServiceModel>();

            serviceModel.AuthorId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            await this.articlesService.CreateArticle(serviceModel);

            return(this.Redirect("/Articles/All"));
        }
        public async Task <IActionResult> Create(ArticleCreateInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            if (await this.articlesService.DoesTitleExistAsync(model.Title))
            {
                this.ViewBag.ErrorMessage = "Title already exists";
                return(this.View(model));
            }

            string thumbnailLink;

            try
            {
                thumbnailLink = model.ThumbnailLink ??
                                (model.ThumbnailImage is null
                                    ? null
                                    : await this.filesService.UploadAsync(
                                     model.ThumbnailImage.OpenReadStream(),
                                     model.ThumbnailImage.FileName,
                                     this.UserId));
            }
            catch (InvalidOperationException)
            {
                this.ViewBag.ErrorMessage = "No more available storage. Please delete some files to upload new ones";
                return(this.View(model));
            }

            await this.articlesService.CreateAsync(
                this.UserId,
                model.Title,
                model.Content,
                model.Categories.Select(x => x.Name).ToArray(),
                thumbnailLink,
                model.Description);

            return(this.RedirectToRoute(new
            {
                action = "GetByTitle",
                title = model.Title,
            }));
        }
示例#19
0
        public async Task <IActionResult> Create(ArticleCreateInputModel articleCreateInputModel)
        {
            var user = await this.userManager.GetUserAsync(this.User);

            if (!this.ModelState.IsValid)
            {
                var categories = await this.categoriesService
                                 .GetAllCategoriesAsync <CategoryDetailsViewModel>();

                articleCreateInputModel.Categories = categories;

                return(this.View(articleCreateInputModel));
            }

            await this.articlesService.CreateAsync(articleCreateInputModel, user.Id);

            return(this.RedirectToAction("GetAll", "Articles", new { area = "Administration" }));
        }
示例#20
0
        public async Task <ArticleDetailsViewModel> CreateAsync(ArticleCreateInputModel articleCreateInputModel, string userId)
        {
            var category = await this.categoriesRepository
                           .All()
                           .FirstOrDefaultAsync(x => x.Id == articleCreateInputModel.CategoryId);

            if (category == null)
            {
                throw new NullReferenceException(
                          string.Format(ExceptionMessages.CategoryNotFound, articleCreateInputModel.CategoryId));
            }

            var article = new Article
            {
                Title       = articleCreateInputModel.Title,
                Description = articleCreateInputModel.Description,
                Category    = category,
                UserId      = userId,
            };

            bool doesArticleExist = await this.articlesRepository
                                    .All()
                                    .AnyAsync(a => a.Title == article.Title);

            if (doesArticleExist)
            {
                throw new ArgumentException(
                          string.Format(ExceptionMessages.ArticleAlreadyExists, article.Title));
            }

            var imageUrl = await this.cloudinaryService
                           .UploadAsync(articleCreateInputModel.Image, articleCreateInputModel.Title + Suffixes.ArticleSuffix);

            article.ImagePath = imageUrl;

            await this.articlesRepository.AddAsync(article);

            await this.articlesRepository.SaveChangesAsync();

            var viewModel = await this.GetViewModelByIdAsync <ArticleDetailsViewModel>(article.Id);

            return(viewModel);
        }
示例#21
0
        public async Task <Article> CreateAsync(ArticleCreateInputModel articleCreateInputModel, string creatorId)
        {
            var article = Mapper.Map <Article>(articleCreateInputModel);

            this.categoryService.ThrowIfAnyNotExist(articleCreateInputModel.CategoriesIds);
            this.ThrowIfAnyNotExist(articleCreateInputModel.ConnectedArticlesIds);

            UpdateCategories(article.Categories, articleCreateInputModel.CategoriesIds?.ToList());
            UpdateConnectedArticles(article.ConnectedTo, articleCreateInputModel.ConnectedArticlesIds?.ToList());

            article.CreatorId   = creatorId;
            article.PublishedOn = DateTime.UtcNow;
            article.IsDeleted   = false;

            await this.db.Articles.AddAsync(article);

            await this.db.SaveChangesAsync();

            return(article);
        }
示例#22
0
        public async Task <T> CreateAsync <T>(ArticleCreateInputModel model, string userId)
        {
            var article = new Article
            {
                Name     = model.Name,
                Content  = model.Content,
                AuthorId = userId,
            };

            this.articleRepository.Add(article);
            await this.articleRepository.SaveChangesAsync();

            var articleViewModel = await this.articleRepository
                                   .AllAsNoTracking()
                                   .Where(x => x.Id == article.Id)
                                   .To <T>()
                                   .SingleAsync();

            return(articleViewModel);
        }