Skip to content

Commit

Permalink
Day 19 - Blog Processing
Browse files Browse the repository at this point in the history
  • Loading branch information
fdeniz07 committed Oct 6, 2021
1 parent f9c8044 commit ca9285a
Show file tree
Hide file tree
Showing 62 changed files with 900 additions and 212 deletions.
23 changes: 23 additions & 0 deletions BlogWeb/Areas/Admin/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using AutoMapper;
using BlogWeb.Helpers.Abstract;
using EntityLayer.Concrete;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace BlogWeb.Areas.Admin.Controllers
{
public class BaseController : Controller
{
public BaseController(UserManager<User> userManager, IMapper mapper, IImageHelper imageHelper)
{
UserManager = userManager;
Mapper = mapper;
ImageHelper = imageHelper;
}

protected UserManager<User> UserManager { get; }
protected IMapper Mapper { get; }
protected IImageHelper ImageHelper { get; }
protected User LoggedInUser => UserManager.GetUserAsync(HttpContext.User).Result;
}
}
132 changes: 127 additions & 5 deletions BlogWeb/Areas/Admin/Controllers/BlogController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using AutoMapper;
using BlogWeb.Areas.Admin.Models;
using BlogWeb.Helpers.Abstract;
using BusinessLayer.Abstract;
using CoreLayer.Utilities.Results.ComplexTypes;
using EntityLayer.ComplexTypes;
using EntityLayer.Concrete;
using EntityLayer.Dtos;
using Microsoft.AspNetCore.Identity;

namespace BlogWeb.Areas.Admin.Controllers
{
[Area("Admin")]
public class BlogController : Controller
public class BlogController : BaseController
{
private readonly IBlogService _blogService;
private readonly ICategoryService _categoryService;

public BlogController(IBlogService blogService)

public BlogController(IBlogService blogService, ICategoryService categoryService, UserManager<User> userManager,IMapper mapper ,IImageHelper imageHelper):base (userManager, mapper, imageHelper)
{
_blogService = blogService;
_categoryService = categoryService;
}

[HttpGet]
Expand All @@ -24,9 +36,119 @@ public async Task<IActionResult> Index()
}

[HttpGet]
public IActionResult Add()
public async Task<IActionResult> Add()
{
var result = await _categoryService.GetAllByNonDeletedAndActiveAsync(); // silinmemis kategorileri getiriyoruz

if (result.ResultStatus==ResultStatus.Success)
{
return View(new BlogAddViewModel
{
Categories =result.Data.Categories
});
}
return NotFound();
}

[HttpPost]
public async Task<IActionResult> Add(BlogAddViewModel blogAddViewModel)
{
if (ModelState.IsValid)
{
var blogAddDto = Mapper.Map<BlogAddDto>(blogAddViewModel);
var imageResult = await ImageHelper.Upload(blogAddViewModel.Title, blogAddViewModel.ThumbnailFile, ImageType.Post);
blogAddDto.Thumbnail = imageResult.Data.FullName;
var result = await _blogService.AddAsync(blogAddDto, LoggedInUser.UserName,LoggedInUser.Id);
if (result.ResultStatus==ResultStatus.Success)
{
TempData.Add("SuccessMesage",result.Message);
return RedirectToAction("Index", "Blog");
}
else
{
ModelState.AddModelError("",result.Message);
}
}
var categories = await _categoryService.GetAllByNonDeletedAndActiveAsync();
blogAddViewModel.Categories = categories.Data.Categories;
return View(blogAddViewModel);
}

[HttpGet]
public async Task<IActionResult> Update(int blogId)
{
var blogResult = await _blogService.GetBlogUpdateDtoAsync(blogId);
var categoriesResult = await _categoryService.GetAllByNonDeletedAndActiveAsync();
if (blogResult.ResultStatus==ResultStatus.Success && categoriesResult.ResultStatus==ResultStatus.Success)
{
var blogUpdateViewModel = Mapper.Map<BlogUpdateViewModel>(blogResult.Data);
blogUpdateViewModel.Categories = categoriesResult.Data.Categories;
return View(blogUpdateViewModel);
}
else
{
return NotFound();
}
}

[HttpPost]
public async Task<IActionResult> Update(BlogUpdateViewModel blogUpdateViewModel)
{
if (ModelState.IsValid)
{
bool isNewThumbnailUploaded = false;
var oldThumbnail = blogUpdateViewModel.Thumbnail;
if (blogUpdateViewModel.ThumbnailFile!=null)
{
var uploadedImageResult = await ImageHelper.Upload(blogUpdateViewModel.Title,
blogUpdateViewModel.ThumbnailFile, ImageType.Post);
blogUpdateViewModel.Thumbnail = uploadedImageResult.ResultStatus == ResultStatus.Success
? uploadedImageResult.Data.FullName
: "postImages/defaultThumbnailImage.jpg";
if (oldThumbnail!= "postImages/defaultThumbnail.jpg")
{
isNewThumbnailUploaded = true;
}
}
var blogUpdateDto = Mapper.Map<BlogUpdateDto>(blogUpdateViewModel);
var result = await _blogService.UpdateAsync(blogUpdateDto, LoggedInUser.UserName);
if (result.ResultStatus == ResultStatus.Success)
{
if (isNewThumbnailUploaded)
{
ImageHelper.Delete(oldThumbnail);
}
TempData.Add("SuccessMessage", result.Message);
return RedirectToAction("Index", "Blog");
}
else
{
ModelState.AddModelError("", result.Message);
}
}

var categories = await _categoryService.GetAllByNonDeletedAndActiveAsync();
blogUpdateViewModel.Categories = categories.Data.Categories;
return View(blogUpdateViewModel);
}

[HttpPost]
public async Task<JsonResult> Delete(int blogId)
{
var result = await _blogService.DeleteAsync(blogId, LoggedInUser.UserName);
var blogResult = JsonSerializer.Serialize(result);
return Json(blogResult);
}

[HttpGet]
public async Task<JsonResult> GetAllBlogs()
{
return View();
var blogs = await _blogService.GetAllByNonDeletedAndActiveAsync();
var blogResult = JsonSerializer.Serialize(blogs, new JsonSerializerOptions
{
ReferenceHandler = ReferenceHandler.Preserve
});
return Json(blogResult);
}
}
}
14 changes: 9 additions & 5 deletions BlogWeb/Areas/Admin/Controllers/CategoryController.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using AutoMapper;
using BlogWeb.Areas.Admin.Models;
using BlogWeb.Helpers.Abstract;
using BusinessLayer.Abstract;
using CoreLayer.Utilities.Extensions;
using CoreLayer.Utilities.Results.ComplexTypes;
using EntityLayer.Concrete;
using EntityLayer.Dtos;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace BlogWeb.Areas.Admin.Controllers
{
[Area("Admin")]
[Authorize(Roles = "Admin,Editor")]
public class CategoryController : Controller
public class CategoryController : BaseController
{
private readonly ICategoryService _categoryService;

public CategoryController(ICategoryService categoryService)
public CategoryController(ICategoryService categoryService,UserManager<User> userManager, IMapper mapper, IImageHelper imageHelper):base(userManager,mapper,imageHelper)
{
_categoryService = categoryService;
}
Expand All @@ -39,7 +43,7 @@ public async Task<IActionResult> Add(CategoryAddDto categoryAddDto)
{
if (ModelState.IsValid)
{
var result = await _categoryService.AddAsync(categoryAddDto, "Fatih Deniz");
var result = await _categoryService.AddAsync(categoryAddDto, LoggedInUser.UserName);
if (result.ResultStatus==ResultStatus.Success)
{
var categoryAddAjaxModel = JsonSerializer.Serialize(new CategoryAddAjaxViewModel
Expand Down Expand Up @@ -70,7 +74,7 @@ public async Task<JsonResult> GetAllCategories()
[HttpPost]
public async Task<JsonResult> Delete(int categoryId)
{
var result = await _categoryService.DeleteAsync(categoryId, "Fatih Deniz");
var result = await _categoryService.DeleteAsync(categoryId, LoggedInUser.UserName);
var deletedCategory = JsonSerializer.Serialize(result.Data);
return Json(deletedCategory);
}
Expand All @@ -94,7 +98,7 @@ public async Task<IActionResult> Update(CategoryUpdateDto categoryUpdateDto)
{
if (ModelState.IsValid)
{
var result = await _categoryService.UpdateAsync(categoryUpdateDto, "Fatih Deniz");
var result = await _categoryService.UpdateAsync(categoryUpdateDto, LoggedInUser.UserName);
if (result.ResultStatus == ResultStatus.Success)
{
var categoryUpdateAjaxModel = JsonSerializer.Serialize(new CategoryUpdateAjaxViewModel()
Expand Down
7 changes: 4 additions & 3 deletions BlogWeb/Areas/Admin/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using BlogWeb.Helpers.Abstract;
using EntityLayer.ComplexTypes;

namespace BlogWeb.Areas.Admin.Controllers
{
Expand Down Expand Up @@ -137,7 +138,7 @@ public async Task<IActionResult> Add(UserAddDto userAddDto)

if (ModelState.IsValid)
{
var uploadedImageDtoResult = await _imageHelper.UploadUserImage(userAddDto.UserName, userAddDto.ImageFile);
var uploadedImageDtoResult = await _imageHelper.Upload(userAddDto.UserName, userAddDto.ImageFile,ImageType.User);
userAddDto.Image = uploadedImageDtoResult.ResultStatus == ResultStatus.Success
? uploadedImageDtoResult.Data.FullName: "userImages/defaultUser.png";
var user = _mapper.Map<User>(userAddDto);
Expand Down Expand Up @@ -249,7 +250,7 @@ public async Task<IActionResult> Update(UserUpdateDto userUpdateDto)
var oldUserImage = oldUser.Image; //kullanicinin eski resmini bir degiskene atiyoruz
if (userUpdateDto.ImageFile != null) //Eger kullanici yeni bir resim yüklerse
{
var uploadedImageDtoResult = await _imageHelper.UploadUserImage(userUpdateDto.UserName, userUpdateDto.ImageFile);
var uploadedImageDtoResult = await _imageHelper.Upload(userUpdateDto.UserName, userUpdateDto.ImageFile,ImageType.User);
userUpdateDto.Image = uploadedImageDtoResult.ResultStatus == ResultStatus.Success
? uploadedImageDtoResult.Data.FullName : "userImages/defaultUser.png"; //kullanicinin yeni resmini güncelle
if (oldUserImage != "userImages/defaultUser.png") // Diger kullanicilarinda kullandigi ortak resmin kontrolü yapiliyor, ortak resim ise silme isleminin önüne geciyoruz
Expand Down Expand Up @@ -322,7 +323,7 @@ public async Task<ViewResult> ChangeDetails(UserUpdateDto userUpdateDto)
var oldUserImage = oldUser.Image; //kullanicinin eski resmini bir degiskene atiyoruz
if (userUpdateDto.ImageFile != null) //Eger kullanici yeni bir resim yüklerse
{
var uploadedImageDtoResult = await _imageHelper.UploadUserImage(userUpdateDto.UserName, userUpdateDto.ImageFile);
var uploadedImageDtoResult = await _imageHelper.Upload(userUpdateDto.UserName, userUpdateDto.ImageFile,ImageType.User);
userUpdateDto.Image = uploadedImageDtoResult.ResultStatus == ResultStatus.Success
? uploadedImageDtoResult.Data.FullName : "userImages/defaultUser.png"; //kullanicinin yeni resmini güncelle
if (oldUserImage!= "userImages/defaultUser.png") // Diger kullanicilarinda kullandigi ortak resmin kontrolü yapiliyor, ortak resim ise silme isleminin önüne geciyoruz
Expand Down
73 changes: 73 additions & 0 deletions BlogWeb/Areas/Admin/Models/BlogAddViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using EntityLayer.Concrete;
using Microsoft.AspNetCore.Http;

namespace BlogWeb.Areas.Admin.Models
{
public class BlogAddViewModel
{
[DisplayName("Başlık")]
[Required(ErrorMessage = "{0} alanı boş geçilmemelidir")]
[MaxLength(100, ErrorMessage = "{0} alanı {1} karakterden büyük olmamalıdır")]
[MinLength(5, ErrorMessage = "{0} alanı {1} karakterden küçük olmamalıdır")]
public string Title { get; set; }

[DisplayName("İçerik")]
[Required(ErrorMessage = "{0} alanı boş geçilmemelidir")]
[MinLength(20, ErrorMessage = "{0} alanı {1} karakterden küçük olmamalıdır")]
public string Content { get; set; }

[DisplayName("Küçük Resim")]
[Required(ErrorMessage = "{0} alanı boş geçilmemelidir")]
public IFormFile ThumbnailFile { get; set; }

[DisplayName("Resim")]
//[Required(ErrorMessage = "{0} alanı boş geçilmemelidir")]
[MaxLength(100, ErrorMessage = "{0} alanı {1} karakterden büyük olmamalıdır")]
//[MinLength(5, ErrorMessage = "{0} alanı {1} karakterden küçük olmamalıdır")]
public string Image { get; set; }

[DisplayName("Tarih")]
[Required(ErrorMessage = "{0} alanı boş geçilmemelidir")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime Date { get; set; }

[DisplayName("Yazar Adı")]
[Required(ErrorMessage = "{0} alanı boş geçilmemelidir")]
[MaxLength(50, ErrorMessage = "{0} alanı {1} karakterden büyük olmamalıdır")]
[MinLength(0, ErrorMessage = "{0} alanı {1} karakterden küçük olmamalıdır")]
public string SeoAuthor { get; set; }

[DisplayName("Makale Açıklaması")]
[Required(ErrorMessage = "{0} alanı boş geçilmemelidir")]
[MaxLength(150, ErrorMessage = "{0} alanı {1} karakterden büyük olmamalıdır")]
[MinLength(0, ErrorMessage = "{0} alanı {1} karakterden küçük olmamalıdır")]
public string SeoDescription { get; set; }

[DisplayName("Makale Etiketleri")]
[Required(ErrorMessage = "{0} alanı boş geçilmemelidir")]
[MaxLength(100, ErrorMessage = "{0} alanı {1} karakterden büyük olmamalıdır")]
[MinLength(0, ErrorMessage = "{0} alanı {1} karakterden küçük olmamalıdır")]
public string SeoTags { get; set; }

[DisplayName("Kategori")]
[Required(ErrorMessage = "{0} alanı boş geçilmemelidir")]
public int CategoryId { get; set; }

[DisplayName("Aktif Mi?")]
[Required(ErrorMessage = "{0} alanı boş geçilmemelidir")]
public bool IsActive { get; set; }

public IList<Category> Categories { get; set; }

//yukaridaki alanlari yazmak yerine asagidaki yazim sekli de kullanilabilir

//public ArticleAddDto ArticleAddDto { get; set; }
//public CategoryListDto CategoryListDto { get; set; }
}
}
Loading

0 comments on commit ca9285a

Please sign in to comment.