Skip to content

Commit

Permalink
29.06.2022
Browse files Browse the repository at this point in the history
  • Loading branch information
ElxanQuliyev committed Jun 29, 2022
1 parent cb549df commit 6facefb
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 4 deletions.
1 change: 1 addition & 0 deletions Business/Abstract/ICategoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Business.Abstract
public interface ICategoryManager
{
List<CategoryWithChildernDTO> GetAll();
Task<List<Category>> GetCategoryWithParent();
List<CategoryListDTO> GetChildrenByParentId(int parentId);
void Add(CategoryDTO categoryDTO);
void Update(Category category);
Expand Down
13 changes: 11 additions & 2 deletions Business/Concrete/CategoryManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Business.Abstract;
using AutoMapper;
using Business.Abstract;
using DataAccess.Abstract;
using Entites;
using Entites.DTOs;
Expand All @@ -13,10 +14,13 @@ namespace Business.Concrete
public class CategoryManager : ICategoryManager
{
private readonly ICategoryDal _dal;
private readonly IMapper _mapper;

public CategoryManager(ICategoryDal dal)

public CategoryManager(ICategoryDal dal, IMapper mapper)
{
_dal = dal;
_mapper = mapper;
}

public void Add(CategoryDTO category)
Expand All @@ -36,6 +40,11 @@ public List<CategoryWithChildernDTO> GetAll()
return _dal.GetCategoryWithChildrens();
}

public async Task<List<Category>> GetCategoryWithParent()
{
return await _dal.GetCategoriesWithParent();
}

public List<CategoryListDTO> GetChildrenByParentId(int parentId)
{
return _dal.GetCategoryChildrenByID(parentId);
Expand Down
1 change: 1 addition & 0 deletions DataAccess/Abstract/ICategoryDal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace DataAccess.Abstract
public interface ICategoryDal : IEntityRepository <Category>
{
List<CategoryListDTO> GetDTOCategories();
Task<List<Category>> GetCategoriesWithParent();
List<CategoryWithChildernDTO> GetCategoryWithChildrens();
List<CategoryListDTO> GetCategoryChildrenByID(int parentId);
}
Expand Down
7 changes: 7 additions & 0 deletions DataAccess/Concrete/EntityFrameWork/EfCategoryDal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using DataAccess.Abstract;
using Entites;
using Entites.DTOs;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -41,6 +42,12 @@ public List<CategoryWithChildernDTO> GetCategoryWithChildrens()

}

public async Task<List<Category>> GetCategoriesWithParent()
{
using UdMyDbContext context = new();
return await context.Categories
.Where(c=>!c.IsDeleted).Include(c => c.ParentCategory).ToListAsync();
}
public List<CategoryListDTO> GetDTOCategories()
{
using UdMyDbContext context = new();
Expand Down
18 changes: 18 additions & 0 deletions Entites/DTOs/CategoryWithParentDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entites.DTOs
{
public class CategoryWithParentDTO
{
public int CategoryId { get; set; }
public string Name { get; set; } = null!;
public string Description { get; set; } = null!;
public int ParentId { get; set; }
public string? ParentName { get; set; }
public bool IsFeatured { get; set; }
}
}
31 changes: 31 additions & 0 deletions Entites/MyProfilers/CategoryParentProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using AutoMapper;
using Entites.DTOs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entites.MyProfilers
{
public class CategoryParentProfile : Profile
{

public CategoryParentProfile()
{
CreateMap<Category, CategoryWithParentDTO>()
.ForMember(
dest => dest.CategoryId,
opt => opt.MapFrom(src => src.Id)
)
.ForMember(
dest => dest.ParentId,
opt => opt.MapFrom(src => src.ParentCategoryId)
)
.ForMember(
dest => dest.ParentName,
opt => opt.MapFrom(src => src.ParentCategory.Name)
);
}
}
}
15 changes: 13 additions & 2 deletions UmdyApi/Controllers/CategoryController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Business.Abstract;
using AutoMapper;
using Business.Abstract;
using Entites;
using Entites.DTOs;
using Microsoft.AspNetCore.Http;
Expand All @@ -11,17 +12,27 @@ namespace UdmyApi.Controllers
public class CategoryController : ControllerBase
{
private readonly ICategoryManager _categoryManager;
private readonly IMapper _mapper;

public CategoryController(ICategoryManager categoryManager)
public CategoryController(ICategoryManager categoryManager, IMapper mapper)
{
_categoryManager = categoryManager;
_mapper = mapper;
}

[HttpGet("getall")]
public List<CategoryWithChildernDTO> GetCategories()
{
return _categoryManager.GetAll();
}

[HttpGet("with-parent")]
public async Task<List<CategoryWithParentDTO>> GetWithParent()
{
var categoryList = await _categoryManager.GetCategoryWithParent();
var categoryMapper = _mapper.Map<List<CategoryWithParentDTO>>(categoryList);
return categoryMapper;
}
[HttpGet("getchildrens/{parentId}")]
public List<CategoryListDTO>? GetChildrens(int? parentId)
{
Expand Down

0 comments on commit 6facefb

Please sign in to comment.