Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create services #58

Merged
merged 11 commits into from
Apr 2, 2024
5 changes: 5 additions & 0 deletions Inflow.Domain/ResourceParameters/ProductResourceParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ namespace Inflow.ResourceParameters
public class ProductResourceParameters : ResourceParametersBase
{
public override string OrderBy { get; set; } = "name";
public int? CategoryId { get; set; }
public decimal? Price { get; set; }
public decimal? PriceLessThan { get; set; }
public decimal? PriceGreaterThan { get; set; }
public DateTime? ExpireDate { get; set; }
}
}
4 changes: 1 addition & 3 deletions Inflow.Domain/ResourceParameters/ResourceParametersBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
{
public abstract class ResourceParametersBase
{
protected virtual int MaxPageSize { get; set; } = 500;

protected virtual int MaxPageSize { get; set; } = 25;
public virtual string? SearchString { get; set; }
public abstract string OrderBy { get; set; }

public int PageNumber { get; set; } = 1;

private int _pageSize = 15;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
public class SaleItemResourceParameters : ResourceParametersBase
{
public override string OrderBy { get; set; } = "id";
public int? ProductId { get; set; }
public int? SaleId { get; set; }
public decimal? UnitPrice { get; set; }
public decimal? UnitPriceLessThan { get; set; }
public decimal? UnitPriceGreaterThan { get; set; }
}
}
2 changes: 2 additions & 0 deletions Inflow.Domain/ResourceParameters/SaleResourceParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{
public class SaleResourceParameters : ResourceParametersBase
{
public int? CustomerId { get; set; }
public DateTime? SaleDate { get; set; }
public override string OrderBy { get; set; } = "id";
}
}
13 changes: 6 additions & 7 deletions Inflow.Domain/ResourceParameters/SupplyItemResourceParameters.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inflow.Domain.ResourceParameters
namespace Inflow.Domain.ResourceParameters
{
public class SupplyItemResourceParameters : ResourceParametersBase
{
public int? ProductId { get; set; }
public int? SupplyId { get; set; }
public decimal? UnitPrice { get; set; }
public decimal? UnitPriceLessThan { get; set; }
public decimal? UnitPriceGreaterThan { get; set; }
public override string OrderBy { get; set; } = "id";
}
}
2 changes: 2 additions & 0 deletions Inflow.Domain/ResourceParameters/SupplyResourceParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{
public class SupplyResourceParameters : ResourceParametersBase
{
public int? SupplierId { get; set; }
public DateTime? SupplyDate { get; set; }
public override string OrderBy { get; set; } = "id";
}
}
113 changes: 113 additions & 0 deletions Inflow.Service/CategoryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using AutoMapper;
using Inflow.Domain.DTOs.Category;
using Inflow.Domain.Entities;
using Inflow.Domain.Exeptions;
using Inflow.Domain.Interfaces.Services;
using Inflow.Domain.Pagniation;
using Inflow.Domain.ResourceParameters;
using Inflow.Domain.Responses;
using Inflow.Infrastructure;
using Microsoft.EntityFrameworkCore;

namespace DiyorMarket.Services
{
public class CategoryService : ICategoryService
{
private readonly IMapper _mapper;
private readonly InflowDbContext _context;

public CategoryService(IMapper mapper,
InflowDbContext context)
{
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
_context = context ?? throw new ArgumentNullException(nameof(context));
}

public GetBaseResponse<CategoryDto> GetCategories(CategoryResourceParameters categoryResourceParameters)
{
var query = _context.Categories.Include(s => s.Products).IgnoreAutoIncludes().AsQueryable();

if (!string.IsNullOrWhiteSpace(categoryResourceParameters.SearchString))
{
query = query.Where(x => x.Name.Contains(categoryResourceParameters.SearchString));
}

if (!string.IsNullOrEmpty(categoryResourceParameters.OrderBy))
{
query = categoryResourceParameters.OrderBy.ToLowerInvariant() switch
{
"name" => query.OrderBy(x => x.Name),
"namedesc" => query.OrderByDescending(x => x.Name),
_ => query.OrderBy(x => x.Name),
};
}

var categories = query.ToPaginatedList(categoryResourceParameters.PageSize, categoryResourceParameters.PageNumber);
var categoryDtos = _mapper.Map<List<CategoryDto>>(categories);
var paginatedResult = new PaginatedList<CategoryDto>(categoryDtos, categories.TotalCount, categories.CurrentPage, categories.PageSize);

return paginatedResult.ToResponse();
}

public IEnumerable<CategoryDto> GetAllCategories()
{
var categories = _context.Categories.Include(s => s.Products).ToList();

return _mapper.Map<IEnumerable<CategoryDto>>(categories) ?? Enumerable.Empty<CategoryDto>();
}

public CategoryDto? GetCategoryById(int id)
{
var category = _context.Categories
.Include(c => c.Products)
.IgnoreAutoIncludes()
.FirstOrDefault(x => x.Id == id);

if (category is null)
{
throw new EntityNotFoundException($"Category with id: {id} not found");
}

var categoryDto = _mapper.Map<CategoryDto>(category);

return categoryDto;
}

public CategoryDto CreateCategory(CategoryForCreateDto categoryToCreate)
{
var categoryEntity = _mapper.Map<Category>(categoryToCreate);

_context.Categories.Add(categoryEntity);

_context.SaveChanges();

var categoryDto = _mapper.Map<CategoryDto>(categoryEntity);

return categoryDto;
}

public CategoryDto UpdateCategory(CategoryForUpdateDto categoryToUpdate)
{
var categoryEntity = _mapper.Map<Category>(categoryToUpdate);

_context.Categories.Update(categoryEntity);
_context.SaveChanges();

var updatedCategoryDto = _mapper.Map<CategoryDto>(categoryEntity);

return updatedCategoryDto;
}

public void DeleteCategory(int id)
{
var category = _context.Categories.FirstOrDefault(x => x.Id == id);

if (category is not null)
{
_context.Categories.Remove(category);
}

_context.SaveChanges();
}
}
}
107 changes: 107 additions & 0 deletions Inflow.Service/CustomerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using AutoMapper;
using Inflow.Domain.DTOs.Customer;
using Inflow.Domain.Entities;
using Inflow.Domain.Interfaces.Services;
using Inflow.Domain.Pagniation;
using Inflow.Domain.ResourceParameters;
using Inflow.Domain.Responses;
using Inflow.Infrastructure;

namespace DiyorMarket.Services
{
public class CustomerService : ICustomerService
{
private readonly IMapper _mapper;
private readonly InflowDbContext _context;

public CustomerService(IMapper mapper, InflowDbContext context)
{
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
_context = context ?? throw new ArgumentNullException(nameof(context));
}

public GetBaseResponse<CustomerDto> GetCustomers(CustomerResourceParameters customerResourceParameters)
{
var query = _context.Customers.AsQueryable();

if (!string.IsNullOrWhiteSpace(customerResourceParameters.SearchString))
{
query = query.Where(x => x.FirstName.Contains(customerResourceParameters.SearchString)
|| x.LastName.Contains(customerResourceParameters.SearchString)
|| x.PhoneNumber.Contains(customerResourceParameters.SearchString));
}

if (!string.IsNullOrEmpty(customerResourceParameters.OrderBy))
{
query = customerResourceParameters.OrderBy.ToLowerInvariant() switch
{
"firstname" => query.OrderBy(x => x.FirstName),
"firstnamedesc" => query.OrderByDescending(x => x.FirstName),
"lastname" => query.OrderBy(x => x.LastName),
"lastnamedesc" => query.OrderByDescending(x => x.LastName),
"phonenumber" => query.OrderBy(x => x.PhoneNumber),
"phonenumberdesc" => query.OrderByDescending(x => x.PhoneNumber),
_ => query.OrderBy(x => x.FirstName),
};
}

var customers = query.ToPaginatedList(customerResourceParameters.PageSize, customerResourceParameters.PageNumber);

var customerDtos = _mapper.Map<List<CustomerDto>>(customers);

var paginatedResult = new PaginatedList<CustomerDto>(customerDtos, customers.TotalCount, customers.CurrentPage, customers.PageSize);

return paginatedResult.ToResponse();
}

public IEnumerable<CustomerDto> GetCustomers()
{
var customers = _context.Customers.ToList();

return _mapper.Map<IEnumerable<CustomerDto>>(customers) ?? Enumerable.Empty<CustomerDto>();
}

public CustomerDto? GetCustomerById(int id)
{
var customer = _context.Customers.FirstOrDefault(x => x.Id == id);

var customerDto = _mapper.Map<CustomerDto>(customer);

return customerDto;
}

public CustomerDto CreateCustomer(CustomerForCreateDto customerToCreate)
{
var customerEntity = _mapper.Map<Customer>(customerToCreate);

_context.Customers.Add(customerEntity);
_context.SaveChanges();

var customerDto = _mapper.Map<CustomerDto>(customerEntity);

return customerDto;
}

public CustomerDto UpdateCustomer(CustomerForUpdateDto customerToUpdate)
{
var customerEntity = _mapper.Map<Customer>(customerToUpdate);

_context.Customers.Update(customerEntity);
_context.SaveChanges();

var customerDto = _mapper.Map<CustomerDto>(customerEntity);

return customerDto;
}

public void DeleteCustomer(int id)
{
var customer = _context.Customers.FirstOrDefault(x => x.Id == id);
if (customer is not null)
{
_context.Customers.Remove(customer);
}
_context.SaveChanges();
}
}
}
Loading
Loading