예제 #1
0
        public ActionResult Create(BlogViewModel model)
        {
            if (ModelState.IsValid)
            {
                blogService.AddOrUpdate(model);

                var action = RedirectToAction("Index");
                return(action.WithSuccess(string.Format("The blog \"{0}\" has been added".TA(), model.Title)));
            }
            return(View(model));
        }
예제 #2
0
        public async Task <IActionResult> AddOrUpdate(AddOrUpdateBlogViewModel model, IFormFile mainImage)
        {
            if (mainImage != null)
            {
                var dir      = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\img\uploads");
                var filePath = Path.Combine(dir, mainImage.FileName);
                var imageUrl = Path.Combine(Path.DirectorySeparatorChar.ToString(), "img", "uploads", mainImage.FileName);

                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await mainImage.CopyToAsync(stream);
                }

                model.AddOrUpdateBlogDto.MainImage = imageUrl;
            }

            //apply writer
            var userIdResult = _accountService.GetUserId(HttpContext.User);

            model.AddOrUpdateBlogDto.WriterId = userIdResult.Data;

            var result = await _blogService.AddOrUpdate(model.AddOrUpdateBlogDto);

            if (result.Success)
            {
                if (model.AddOrUpdateBlogDto.BlogId == 0)
                {
                    model.AddOrUpdateBlogDto.BlogId = result.Data;
                    model.CategoryList = await GetCategories(model.AddOrUpdateBlogDto.CategoryId);

                    return(View(model));
                }

                TempData.Put("message", new ResultMessageViewModel {
                    Message = result.Message, CssColor = CssColor.success
                });
                return(RedirectToAction("BlogList"));
            }
            else
            {
                return(View("Error"));
            }
        }