Rework so employee dont use repos but servies
This commit is contained in:
85
PointOfSale/Pos.Service/ProductGroupService.cs
Normal file
85
PointOfSale/Pos.Service/ProductGroupService.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Database.Models;
|
||||
using Database.Repository;
|
||||
|
||||
namespace Pos.Service
|
||||
{
|
||||
public class ProductGroupService
|
||||
{
|
||||
private readonly ProductGroupRepository _productGroupRepository =
|
||||
new ProductGroupRepository();
|
||||
|
||||
public List<ProductGroupEntity> GetProductGroups()
|
||||
{
|
||||
return _productGroupRepository.GetAll();
|
||||
}
|
||||
|
||||
public bool HasProductGroups()
|
||||
{
|
||||
return _productGroupRepository.Any();
|
||||
}
|
||||
|
||||
public void AddProductGroup(string name)
|
||||
{
|
||||
_productGroupRepository.Add(ValidateName(name));
|
||||
CacheService.Invalidate();
|
||||
}
|
||||
|
||||
public void RenameProductGroup(int productGroupId, string name)
|
||||
{
|
||||
ValidateId(productGroupId);
|
||||
_productGroupRepository.Edit(
|
||||
ValidateName(name),
|
||||
productGroupId);
|
||||
CacheService.Invalidate();
|
||||
}
|
||||
|
||||
public void ArchiveProductGroup(int productGroupId)
|
||||
{
|
||||
ValidateId(productGroupId);
|
||||
_productGroupRepository.Archive(productGroupId);
|
||||
CacheService.Invalidate();
|
||||
}
|
||||
|
||||
public void UpdateOrder(IReadOnlyList<int> orderedProductGroupIds)
|
||||
{
|
||||
ValidateOrder(orderedProductGroupIds);
|
||||
_productGroupRepository.SetOrder(orderedProductGroupIds);
|
||||
CacheService.Invalidate();
|
||||
}
|
||||
|
||||
private static string ValidateName(string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new ArgumentException(
|
||||
"Produktgruppen skal have et navn.",
|
||||
nameof(name));
|
||||
|
||||
return name.Trim();
|
||||
}
|
||||
|
||||
private static void ValidateId(int productGroupId)
|
||||
{
|
||||
if (productGroupId <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(productGroupId));
|
||||
}
|
||||
|
||||
private static void ValidateOrder(IReadOnlyList<int> orderedIds)
|
||||
{
|
||||
if (orderedIds == null)
|
||||
throw new ArgumentNullException(nameof(orderedIds));
|
||||
|
||||
if (orderedIds.Any(id => id <= 0))
|
||||
throw new ArgumentException(
|
||||
"Rækkefølgen indeholder et ugyldigt id.",
|
||||
nameof(orderedIds));
|
||||
|
||||
if (orderedIds.Distinct().Count() != orderedIds.Count)
|
||||
throw new ArgumentException(
|
||||
"Rækkefølgen indeholder dubletter.",
|
||||
nameof(orderedIds));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user