Rework so employee dont use repos but servies

This commit is contained in:
Alex Pedersen
2026-07-30 10:23:27 +02:00
parent 6df185d519
commit 9497afe262
26 changed files with 630 additions and 125 deletions

View File

@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using Database.Models;
using Database.Repository;
namespace Pos.Service
{
public class EmployeeService
{
private readonly EmployeeRepository _employeeRepository =
new EmployeeRepository();
public List<EmployeeEntity> GetEmployees()
{
return _employeeRepository.GetAll();
}
public void AddEmployee(string name)
{
_employeeRepository.Add(ValidateName(name));
CacheService.Invalidate();
}
public void RenameEmployee(int employeeId, string name)
{
ValidateId(employeeId);
_employeeRepository.Edit(employeeId, ValidateName(name));
CacheService.Invalidate();
}
public void ArchiveEmployee(int employeeId)
{
ValidateId(employeeId);
_employeeRepository.Delete(employeeId);
CacheService.Invalidate();
}
private static string ValidateName(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(
"Medarbejderens navn skal være udfyldt.",
nameof(name));
return name.Trim();
}
private static void ValidateId(int employeeId)
{
if (employeeId <= 0)
throw new ArgumentOutOfRangeException(nameof(employeeId));
}
}
}

View 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));
}
}
}

View File

@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Database.Models;
using Database.Repository;
namespace Pos.Service
{
public class ProductService
{
private readonly ProductRepository _productRepository =
new ProductRepository();
public List<ProductEntity> GetProducts(int productGroupId)
{
ValidateId(productGroupId, nameof(productGroupId));
return _productRepository.GetByProductGroup(productGroupId);
}
public ProductEntity GetProduct(int productId)
{
ValidateId(productId, nameof(productId));
return _productRepository.GetById(productId);
}
public void AddProduct(string name, int productGroupId)
{
ValidateId(productGroupId, nameof(productGroupId));
_productRepository.Add(ValidateName(name), productGroupId);
CacheService.Invalidate();
}
public void UpdateProduct(
int productId,
int productGroupId,
string name)
{
ValidateId(productId, nameof(productId));
ValidateId(productGroupId, nameof(productGroupId));
_productRepository.Update(
productId,
productGroupId,
ValidateName(name));
CacheService.Invalidate();
}
public void ArchiveProduct(int productId)
{
ValidateId(productId, nameof(productId));
_productRepository.Archive(productId);
CacheService.Invalidate();
}
public void UpdateOrder(
int productGroupId,
IReadOnlyList<int> orderedProductIds)
{
ValidateId(productGroupId, nameof(productGroupId));
if (orderedProductIds == null)
throw new ArgumentNullException(nameof(orderedProductIds));
if (orderedProductIds.Any(id => id <= 0))
throw new ArgumentException(
"Rækkefølgen indeholder et ugyldigt id.",
nameof(orderedProductIds));
if (orderedProductIds.Distinct().Count() != orderedProductIds.Count)
throw new ArgumentException(
"Rækkefølgen indeholder dubletter.",
nameof(orderedProductIds));
_productRepository.SetOrder(
productGroupId,
orderedProductIds);
CacheService.Invalidate();
}
private static string ValidateName(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(
"Produktet skal have et navn.",
nameof(name));
return name.Trim();
}
private static void ValidateId(int id, string parameterName)
{
if (id <= 0)
throw new ArgumentOutOfRangeException(parameterName);
}
}
}

View File

@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Database.Models;
using Database.Repository;
using Pos.Models;
namespace Pos.Service
{
public class SaleHistoryService
{
private readonly SaleRepository _saleRepository =
new SaleRepository();
public List<ReceiptSummaryModel> Search(
DateTime startDate,
DateTime endDate)
{
if (endDate.Date < startDate.Date)
throw new ArgumentException(
"Slutdatoen må ikke ligge før startdatoen.");
List<SaleEntity> sales =
_saleRepository.GetByDateRange(startDate, endDate);
List<ReceiptSummaryModel> receipts =
new List<ReceiptSummaryModel>();
foreach (SaleEntity sale in sales)
{
decimal total = _saleRepository
.GetSaleLineBySaleId(sale.Id)
.Sum(line => line.Total);
receipts.Add(new ReceiptSummaryModel
{
SaleId = sale.Id,
SaleTime = sale.Time,
Total = total
});
}
return receipts;
}
public ReceiptDetailModel GetReceipt(int saleId)
{
if (saleId <= 0)
throw new ArgumentOutOfRangeException(nameof(saleId));
List<SaleLineEntity> saleLines =
_saleRepository.GetSaleLineBySaleId(saleId);
ReceiptDetailModel receipt = new ReceiptDetailModel
{
SaleId = saleId
};
foreach (SaleLineEntity saleLine in saleLines)
{
receipt.Lines.Add(new ReceiptLineModel
{
Product = saleLine.Product,
Pieces = saleLine.Pieces,
Price = saleLine.Price,
Total = saleLine.Total
});
}
receipt.Total = receipt.Lines.Sum(line => line.Total);
return receipt;
}
}
}