예제 #1
0
        public async Task <IActionResult> Add(ArticleAddViewModel articleAddViewModel)
        {
            if (ModelState.IsValid)
            {
                var articleAddDto = Mapper.Map <ArticleAddDto>(articleAddViewModel);
                var imageResult   = await ImageHelper.Upload(articleAddViewModel.Title,
                                                             articleAddViewModel.ThumbnailFile, PictureType.Post);

                articleAddDto.Thumbnail = imageResult.Data.FullName;
                var result = await _articleService.AddAsync(articleAddDto, LoggedInUser.UserName, LoggedInUser.Id);

                if (result.ResultStatus == ResultStatus.Success)
                {
                    _toastNotification.AddSuccessToastMessage(result.Message, new ToastrOptions
                    {
                        Title = "Başarılı İşlem"
                    });
                    return(RedirectToAction("Index", "Article"));
                }
                else
                {
                    ModelState.AddModelError("", result.Message);
                }
            }
            var categories = await _categoryService.GetAllByNoneDeletedAndActiveAsync();

            articleAddViewModel.Categories = categories.Data.Categories;

            return(View(articleAddViewModel));
        }
        public async Task <ResultMessage> Post([FromBody] Article item)
        {
            //没有添加[FromBody],无法获取到user内容,user值为默认值 as user = new User();
            try
            {
                item.Id      = Guid.NewGuid().ToString();
                item.AddTime = DateTime.Now;
                var result = await _service.AddAsync(item);

                return(new ResultMessage
                {
                    Success = result,
                    Status = result ? EnumStatus.Success : EnumStatus.Failure,
                });
            }
            catch (Exception exception)
            {
                return(new ResultMessage
                {
                    Success = false,
                    Status = EnumStatus.Failure,
                    Message = exception.Message
                });
            }
        }
        public async Task <IActionResult> Add(AddArticleViewModel model)
        {
            if (!string.IsNullOrWhiteSpace(model.Title) && await articleService.ExistsAsync(model.Title))
            {
                ModelState.AddModelError(nameof(model.Title),
                                         "Article with the same title already exists.");
            }

            if (!await destinationService.ExistsAsync(model.DestinationId))
            {
                ModelState.AddModelError(nameof(model.DestinationId), "Selected destination does not exist.");
            }

            if (!ModelState.IsValid)
            {
                model.Destinations = await GetDestinations();

                return(View(model));
            }

            var authorId = userManager.GetUserId(User);

            await articleService.AddAsync(
                model.Title,
                model.Content,
                model.DestinationId,
                authorId,
                DateTime.UtcNow);

            return(RedirectToAction(nameof(Add)));
        }
        /// <summary>
        /// POST /Edit/{id?}
        /// Handler for POST requests from the /Edit form. Checks for valid model
        /// state, updates the LastUpdated field, creates/edits the article, and
        /// then redirects to the Article page to view the new/edited article.
        /// </summary>
        /// <returns>Asynchronous Task instance for an IActionResult. If the view
        /// model's state is valid then this should be a RedirectToPageResult to
        /// /Article for the new/edited article; otherwise, the page is rendered
        /// again to allow the user to correct the model state.</returns>
        public async Task <IActionResult> OnPostAsync()
        {
            if (ModelState.IsValid)
            {
                // Use UTC time to maintain consistency when load balancing occurs
                Article.LastUpdated = DateTime.UtcNow;
                // Signifies whether the database modifications were successful
                bool success;

                if (IsNewArticle)
                {
                    success = await _articleService.AddAsync(Article);
                }
                else
                {
                    success = await _articleService.UpdateAsync(Article);
                }

                // If the database was modified successfully, redirect to the
                // new article.
                if (success)
                {
                    return(RedirectToPage("Article", new { id = Article.Id }));
                }
            }

            // A validation error has occurred. Re-render the page with the
            // existing view model to allow the user a chance to correct
            // their input for validation.
            return(Page());
        }
예제 #5
0
        public async Task <IActionResult> Add(ArticleAddViewModel articleAddViewModel)
        {
            if (ModelState.IsValid)
            {
                var articleAddDto = Mapper.Map <ArticleAddDto>(articleAddViewModel);
                var imageResult   = await ImageHelper.Upload(articleAddViewModel.Title, articleAddViewModel.ThumbnailFile, PictureType.Post);

                articleAddDto.Thumbnail = imageResult.Data.PictureName;
                //var userName = User.Identity.Name;
                var result = await _articleService.AddAsync(articleAddDto, LoggedInUser.UserName, LoggedInUser.Id);

                if (result.ResultStatus == ResultStatus.Success)
                {
                    //TempData.Add("SuccessMessage", result.Message);  Makale eklediğimizde success mesajını TempData ile gösteriyorduk. Bunu NToastNotify ile değiştirdik.
                    _toastNotification.AddSuccessToastMessage(result.Message, new ToastrOptions
                    {
                        Title = "Başarılı İşlem!"
                    });
                    return(RedirectToAction("Index", "Article"));
                }
                else
                {
                    ModelState.AddModelError("", result.Message);
                }
            }
            var categories = await _categoryService.GetAllByNonDeletedAndActiveAsync();

            articleAddViewModel.Categories = categories.Data.Categories;
            return(View(articleAddViewModel));
        }
예제 #6
0
        public async Task <IActionResult> Add(ArticleAddDto model, IFormFile pic)
        {
            if (pic != null)
            {
                var picName = Guid.NewGuid() + Path.GetExtension(pic.FileName);
                var path    = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img/article-cover/" + picName);
                using var stream = new FileStream(path, FileMode.Create);

                await pic.CopyToAsync(stream);

                model.ImagePath = picName;
            }
            if (ModelState.IsValid)
            {
                var article = new Article()
                {
                    Title      = model.Title,
                    Content    = model.Content,
                    CategoryId = model.CategoryId,
                    ImagePath  = model.ImagePath,
                    Url        = Guid.NewGuid().ToString()
                };
                await _articleService.AddAsync(article);

                return(RedirectToAction("Index", "Article", new { area = "Admin" }));
            }
            return(View());
        }
예제 #7
0
        public async Task <IActionResult> AddArticle(ArticleAddDto model, IFormFile pic)
        {
            if (ModelState.IsValid)
            {
                await _articleService.AddAsync(_mapper.Map <Article>(model), pic);

                return(RedirectToAction("Index", "Article", new { area = "Admin" }));
            }
            return(View(model));
        }
        public async Task <IActionResult> AddArticle([FromBody] ArticleDto article)
        {
            if (article == null)
            {
                return(BadRequest("Invalid data/datas. Please check your informations!"));
            }
            var entity = _mapper.Map <Infrastructure.Entities.Article>(article);
            var result = await _articleService.AddAsync(entity);

            return(HttpEntity(result));
        }
        public async Task <IActionResult> Create([FromForm] ArticleAddModel articleAddModel)
        {
            var uploadModel = await UploadFileAsync(articleAddModel.Image, "image/jpeg");

            if (uploadModel.UploadState == Enums.UploadState.Success)
            {
                articleAddModel.ImagePath = uploadModel.NewName;
                await _articleService.AddAsync(_mapper.Map <Article>(articleAddModel));

                return(Created("", articleAddModel));
            }
            else if (uploadModel.UploadState == Enums.UploadState.NotExist)
            {
                await _articleService.AddAsync(_mapper.Map <Article>(articleAddModel));

                return(Created("", articleAddModel));
            }
            else
            {
                return(BadRequest(uploadModel.ErrorMessage));
            }
        }
예제 #10
0
        public async Task <ActionResult> Create(ArticleViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    model.CreateBy = User.Identity.Name;
                    await _articleService.AddAsync(model);

                    return(RedirectToAction("Index"));
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }

            return(View(model));
        }
예제 #11
0
 public async Task <IActionResult> Create(ArticleCreateDto articleDto)
 {
     return(ActionResultInstance(await _articleService.AddAsync(ObjectMapper.Mapper.Map <ArticleDto>(articleDto))));
 }
예제 #12
0
        public async Task <IActionResult> CreateArticle([FromBody] ArticleCreateDto model)
        {
            var result = await _articleService.AddAsync(model);

            return(CreatedAtAction(nameof(GetArticleById), new { id = result.Id }, null));
        }
예제 #13
0
 public async Task OnPostAddInstruction(string content, string title)
 {
     await articleService.AddAsync(0, title, content, 101, new int[] { }, 0, userId, 1.0, new byte[] { });
 }
예제 #14
0
 public async Task <IActionResult> Add(Article article)
 {
     return(Ok(await _articleService.AddAsync(article)));
 }
예제 #15
0
        public async Task <IActionResult> OnPostAsync(int[] selectedTags, int submit, IFormFile avatar, Article Article = null)
        {
            byte[] image = new byte[] { };
            if (avatar != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    avatar.CopyTo(memoryStream);
                    image = memoryStream.ToArray();
                    Image icon = new Bitmap(memoryStream);
                    if (icon.Width > imageProperties.Width || icon.Height > imageProperties.Height)
                    {
                        throw new Exception("Image dimensions are wrong");
                    }
                }
            }

            if (Article.TextId != 0)
            {
                var editedArticle = await articleService.GetAsync(Article.TextId);

                var master = (await articleService.BrowseAsync(1, null, editedArticle.Id, null)).SingleOrDefault();
                if (master.Master != null)
                {
                    var masterDetails = await articleService.GetAsync(master.Master.Id);

                    Article.Version = masterDetails.Master.Version + 0.1;
                }
                else
                {
                    Article.Version = 1.0;
                }
                Article.Comment   = editedArticle.Master.TextComment;
                Article.ArticleId = editedArticle.Id;
                Article.Category  = new CategoryFilter
                {
                    Id = editedArticle.Category.Id
                };

                if (submit == 0 && editedArticle.Master.Status.Status == "NotSubmitted")
                {
                    await articleService.UpdateVersion(Article.ArticleId, Article.TextId, Article.Title, Article.Content, 41, selectedTags, Article.Category.Id, userId, Article.Version, Article.Comment);
                }
                else if (submit == 0 && editedArticle.Master.Status.Status != "NotSubmitted")
                {
                    await articleService.AddAsync(Article.ArticleId, Article.Title, Article.Content, 41, selectedTags, Article.Category.Id, userId, Article.Version, image);
                }
                else if (submit == 1 && editedArticle.Master.Status.Status == "NotSubmitted")
                {
                    await articleService.UpdateVersion(Article.ArticleId, Article.TextId, Article.Title, Article.Content, 2, selectedTags, Article.Category.Id, userId, Article.Version, Article.Comment);
                }
                else if (submit == 1 && editedArticle.Master.Status.Status != "NotSubmitted")
                {
                    await articleService.AddAsync(Article.ArticleId, Article.Title, Article.Content, 2, selectedTags, Article.Category.Id, userId, Article.Version, image);
                }
            }
            else
            {
                Article.Version = 1.0;
                if (submit == 0)
                {
                    await articleService.AddAsync(Article.ArticleId, Article.Title, Article.Content, 41, selectedTags, Article.Category.Id, userId, Article.Version, image);
                }
                else if (submit == 1)
                {
                    await articleService.AddAsync(Article.ArticleId, Article.Title, Article.Content, 2, selectedTags, Article.Category.Id, userId, Article.Version, image);
                }
            }
            return(RedirectToPage("/Articles/Index"));
        }
예제 #16
0
 public Task <Article> AddAsync(Article article)
 {
     return(_articleService.AddAsync(article, CancellationToken.None));
 }