예제 #1
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"));
            }
        }
예제 #2
0
        public async Task <IActionResult> AddOrUpdate(int?blogId)
        {
            if (blogId != null)
            {
                var result = await _blogService.GetByIdWithImages((int)blogId);

                var model = new AddOrUpdateBlogViewModel
                {
                    CategoryList       = await GetCategories(result.Data.CategoryId),
                    AddOrUpdateBlogDto = result.Data
                };

                return(View(model));
            }
            else
            {
                return(View(new AddOrUpdateBlogViewModel
                {
                    CategoryList = await GetCategories(),
                    AddOrUpdateBlogDto = new AddOrUpdateBlogDto()
                }));
            }
        }