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,11 @@
using System.Collections.Generic;
namespace Pos.Models
{
public class ReceiptDetailModel
{
public int SaleId { get; set; }
public List<ReceiptLineModel> Lines { get; set; } = new();
public decimal Total { get; set; }
}
}

View File

@@ -0,0 +1,10 @@
namespace Pos.Models
{
public class ReceiptLineModel
{
public string Product { get; set; } = string.Empty;
public int Pieces { get; set; }
public decimal Price { get; set; }
public decimal Total { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
using System;
namespace Pos.Models
{
public class ReceiptSummaryModel
{
public int SaleId { get; set; }
public DateTime SaleTime { get; set; }
public decimal Total { get; set; }
}
}

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

View File

@@ -69,10 +69,30 @@ namespace Database.Repository
context.SaveChanges();
}
public void SetOrder(IReadOnlyList<int> orderedIds)
{
using PosDbContext context = new PosDbContext();
List<ProductGroupEntity> productGroups = context.ProductGroups
.Where(productGroup => orderedIds.Contains(productGroup.Id))
.ToList();
if (productGroups.Count != orderedIds.Count)
throw new InvalidOperationException(
"En eller flere produktgrupper findes ikke.");
Dictionary<int, ProductGroupEntity> productGroupsById =
productGroups.ToDictionary(productGroup => productGroup.Id);
for (int index = 0; index < orderedIds.Count; index++)
productGroupsById[orderedIds[index]].Index = index;
context.SaveChanges();
}
public bool Any()
{
using PosDbContext context = new PosDbContext();
bool any = context.ProductGroups.Any();
bool any = context.ProductGroups.Any(c => !c.IsArchived);
return any;
}
}

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System;
using System.Linq;
using Database.Models;
@@ -38,6 +39,33 @@ namespace Database.Repository
context.SaveChanges();
}
public void SetOrder(
int productGroupId,
IReadOnlyList<int> orderedIds)
{
using PosDbContext context = new PosDbContext();
List<ProductEntity> products = context.Products
.Where(product => product.ProductGroupId == productGroupId)
.Where(product => orderedIds.Contains(product.Id))
.ToList();
if (products.Count != orderedIds.Count)
throw new InvalidOperationException(
"Et eller flere produkter findes ikke i produktgruppen.");
Dictionary<int, ProductEntity> productsById =
products.ToDictionary(product => product.Id);
for (int index = 0; index < orderedIds.Count; index++)
{
ProductEntity product = productsById[orderedIds[index]];
product.Index = index;
product.IsModified = true;
}
context.SaveChanges();
}
public ProductEntity GetById(int id)
{
using PosDbContext context = new PosDbContext();
@@ -72,7 +100,11 @@ namespace Database.Repository
product.ProductGroupId = productGroupId;
using PosDbContext context = new PosDbContext();
//Get the highest index
ProductEntity highest = context.Products.OrderByDescending(c => c.Index).Take(1).FirstOrDefault();
ProductEntity highest = context.Products
.Where(c => c.ProductGroupId == productGroupId)
.OrderByDescending(c => c.Index)
.Take(1)
.FirstOrDefault();
if (highest == null)
{
highest = new ProductEntity();

View File

@@ -11,8 +11,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Models;
using Database.Repository;
using Pos.Models;
using Pos.Service;
namespace Pos
@@ -25,6 +24,9 @@ namespace Pos
private List<ListReceiptModel> _receiptList = new List<ListReceiptModel>();
private bool _init = false;
private int _saleId = 0;
private readonly SaleHistoryService _saleHistoryService =
new SaleHistoryService();
private readonly SaleService _saleService = new SaleService();
public PrintReceiptWindow()
{
@@ -38,28 +40,39 @@ namespace Pos
private void Search_OnClick(object sender, RoutedEventArgs e)
{
DateTime startDate = (DateTime)StartDatePicker.SelectedDate;
DateTime endDate = (DateTime)EndDatePicker.SelectedDate;
SaleRepository saleRepository = new SaleRepository();
List<SaleEntity> saleEntities = saleRepository.GetByDateRange(startDate, endDate);
foreach (SaleEntity entity in saleEntities)
if (StartDatePicker.SelectedDate is not DateTime startDate ||
EndDatePicker.SelectedDate is not DateTime endDate)
return;
List<ReceiptSummaryModel> receipts;
try
{
List<SaleLineEntity> saleLineBySaleId = saleRepository.GetSaleLineBySaleId(entity.Id);
decimal total = 0;
foreach (SaleLineEntity lineEntity in saleLineBySaleId)
receipts = _saleHistoryService.Search(startDate, endDate);
}
catch (ArgumentException exception)
{
total += lineEntity.Total;
MessageBox.Show(
exception.Message,
"Ugyldigt datointerval",
MessageBoxButton.OK,
MessageBoxImage.Warning);
return;
}
_receiptList.Clear();
foreach (ReceiptSummaryModel receipt in receipts)
{
ListReceiptModel receiptModel = new ListReceiptModel
{
Id = entity.Id,
Content = $"{entity.Time:dd-MM-yyyy HH:mm} - Pris: {total:0.00}"
Id = receipt.SaleId,
Content =
$"{receipt.SaleTime:dd-MM-yyyy HH:mm} - Pris: {receipt.Total:0.00}"
};
_receiptList.Add(receiptModel);
}
_init = true;
ListReciept.ItemsSource = null;
ListReciept.ItemsSource = _receiptList;
_init = false;
}
@@ -78,23 +91,21 @@ namespace Pos
private void ListReciept_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_init)
if (_init || e.AddedItems.Count == 0)
return;
ListReceiptModel data = (ListReceiptModel) e.AddedItems[0];
_saleId = data.Id;
SaleRepository saleRepository = new SaleRepository();
List<SaleLineEntity> saleLineBySaleId = saleRepository.GetSaleLineBySaleId(data.Id);
ReceiptDetailModel receipt =
_saleHistoryService.GetReceipt(data.Id);
StringBuilder sb = new StringBuilder();
decimal total = 0;
foreach (SaleLineEntity saleLine in saleLineBySaleId)
foreach (ReceiptLineModel saleLine in receipt.Lines)
{
total += saleLine.Total;
sb.AppendLine(
$"{saleLine.Product} - {saleLine.Pieces} stk. a' {saleLine.Price.ToString("0.00")} = {saleLine.Total.ToString("0.00")}");
$"{saleLine.Product} - {saleLine.Pieces} stk. a' {saleLine.Price:0.00} = {saleLine.Total:0.00}");
}
sb.AppendLine($"Total: {total.ToString("0.00")} kr.");
sb.AppendLine($"Total: {receipt.Total:0.00} kr.");
PrintReceipt.Visibility = Visibility.Visible;
SaleLines.Clear();
SaleLines.Text = sb.ToString();
@@ -102,8 +113,7 @@ namespace Pos
private void PrintReceipt_OnClick(object sender, RoutedEventArgs e)
{
SaleService saleService = new SaleService();
saleService.PrintReceipt(_saleId);
_saleService.PrintReceipt(_saleId);
}
}
}

View File

@@ -13,11 +13,11 @@ namespace Pos
{
private bool _initialLoad = true;
private TotalSaleDetail totalSaleDetail;
private readonly SaleService _saleService = new SaleService();
public SaleOfDay()
{
InitializeComponent();
SaleService saleService = new SaleService();
totalSaleDetail = saleService.TotalSale(DateTime.Now);
totalSaleDetail = _saleService.TotalSale(DateTime.Now);
TotalSale.Content = $"Dagens salg: kr. {totalSaleDetail.TotalSale:0.00}";
StartDatePicker.SelectedDate = DateTime.Now;
GridSaleDetail.ItemsSource = totalSaleDetail.TotalSaleCategories;
@@ -29,18 +29,20 @@ namespace Pos
if (_initialLoad)
return;
_initialLoad = false;
DateTime selectedDate = DateTime.Parse(sender.ToString());
SaleService saleService = new SaleService();
totalSaleDetail = saleService.TotalSale(selectedDate);
if (StartDatePicker.SelectedDate is not DateTime selectedDate)
return;
totalSaleDetail = _saleService.TotalSale(selectedDate);
TotalSale.Content = $"Dagens salg: kr. {totalSaleDetail.TotalSale:0.00}";
GridSaleDetail.ItemsSource = totalSaleDetail.TotalSaleCategories;
}
private void PrintSale_OnClick(object sender, RoutedEventArgs e)
{
SaleService saleService = new SaleService();
saleService.PrintSaleOfDay(StartDatePicker.SelectedDate.Value,totalSaleDetail);
if (StartDatePicker.SelectedDate is not DateTime selectedDate)
return;
_saleService.PrintSaleOfDay(selectedDate, totalSaleDetail);
}
}
}

View File

@@ -11,7 +11,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Repository;
using Pos.Service;
namespace Pos.Setting
{
@@ -20,6 +20,9 @@ namespace Pos.Setting
/// </summary>
public partial class AddEmployeeWindow : Window
{
private readonly EmployeeService _employeeService =
new EmployeeService();
public AddEmployeeWindow()
{
InitializeComponent();
@@ -27,9 +30,19 @@ namespace Pos.Setting
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
EmployeeRepository employeeRepository = new EmployeeRepository();
employeeRepository.Add(txtStaff.Text);
this.Close();
try
{
_employeeService.AddEmployee(txtStaff.Text);
Close();
}
catch (ArgumentException exception)
{
MessageBox.Show(
exception.Message,
"Ugyldig medarbejder",
MessageBoxButton.OK,
MessageBoxImage.Warning);
}
}
}
}

View File

@@ -11,7 +11,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Repository;
using Pos.Service;
namespace Pos.Setting
{
@@ -21,6 +21,8 @@ namespace Pos.Setting
public partial class DeleteEmployeeWindow : Window
{
private int _employeeId;
private readonly EmployeeService _employeeService =
new EmployeeService();
public DeleteEmployeeWindow(int employeeId, string staffName)
{
@@ -31,9 +33,8 @@ namespace Pos.Setting
private void btnYes_Click(object sender, RoutedEventArgs e)
{
EmployeeRepository employeeRepository = new EmployeeRepository();
employeeRepository.Delete(_employeeId);
this.Close();
_employeeService.ArchiveEmployee(_employeeId);
Close();
}
private void btnNo_Click(object sender, RoutedEventArgs e)

View File

@@ -11,7 +11,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Repository;
using Pos.Service;
namespace Pos.Setting
{
@@ -21,6 +21,8 @@ namespace Pos.Setting
public partial class EditEmployeeWindow : Window
{
private int _employeeId;
private readonly EmployeeService _employeeService =
new EmployeeService();
public EditEmployeeWindow(int employeeId, string staffName)
{
@@ -31,9 +33,21 @@ namespace Pos.Setting
private void btnEdit_Click(object sender, RoutedEventArgs e)
{
EmployeeRepository employeeRepository = new EmployeeRepository();
employeeRepository.Edit(_employeeId,txtEmployee.Text);
this.Close();
try
{
_employeeService.RenameEmployee(
_employeeId,
txtEmployee.Text);
Close();
}
catch (ArgumentException exception)
{
MessageBox.Show(
exception.Message,
"Ugyldig medarbejder",
MessageBoxButton.OK,
MessageBoxImage.Warning);
}
}
}
}

View File

@@ -13,7 +13,7 @@ using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Models;
using Database.Repository;
using Pos.Service;
namespace Pos.Setting
{
@@ -24,6 +24,8 @@ namespace Pos.Setting
{
private int _selectedEmployee = -1;
private string _employeeName = string.Empty;
private readonly EmployeeService _employeeService =
new EmployeeService();
public EmployeeWindow()
{
InitializeComponent();
@@ -62,15 +64,14 @@ namespace Pos.Setting
deleteEmployeeWindow.Show();
}
private void WindowClosed(object? sender, EventArgs e)
private void WindowClosed(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
using EmployeeRepository employeeRepository = new EmployeeRepository();
List<EmployeeEntity> allStaff = employeeRepository.GetAll();
List<EmployeeEntity> allStaff = _employeeService.GetEmployees();
LstStaff.Items.Clear();
foreach (EmployeeEntity staff in allStaff)
{

View File

@@ -1,8 +1,8 @@
using Database.Repository;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Pos.Service;
namespace Pos.Setting.ProductGroup
{
@@ -11,6 +11,11 @@ namespace Pos.Setting.ProductGroup
/// </summary>
public partial class AddProductWindow : Window
{
private readonly ProductGroupService _productGroupService =
new ProductGroupService();
private readonly ProductService _productService =
new ProductService();
public AddProductWindow()
{
InitializeComponent();
@@ -19,13 +24,16 @@ namespace Pos.Setting.ProductGroup
private void LoadData(int selectedIndex)
{
ProductGroupRepository productGroupRepository = new ProductGroupRepository();
List<Database.Models.ProductGroupEntity> products = productGroupRepository.GetAll();
foreach (Database.Models.ProductGroupEntity productGroup in products)
List<Database.Models.ProductGroupEntity> productGroups =
_productGroupService.GetProductGroups();
foreach (Database.Models.ProductGroupEntity productGroup in productGroups)
{
ComboBoxItem comboBoxItem = new ComboBoxItem();
comboBoxItem.Tag = productGroup.Id;
comboBoxItem.Content = productGroup.Name;
ComboBoxItem comboBoxItem = new ComboBoxItem
{
Tag = productGroup.Id,
Content = productGroup.Name
};
cmbProductGroup.Items.Add(comboBoxItem);
}
@@ -34,12 +42,23 @@ namespace Pos.Setting.ProductGroup
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
ComboBoxItem item = (ComboBoxItem) cmbProductGroup.SelectedItem;
string name = txtProduct.Text;
int id = Convert.ToInt32(item.Tag);
ProductRepository productRepository = new ProductRepository();
productRepository.Add(name,id);
this.Close();
ComboBoxItem item = (ComboBoxItem)cmbProductGroup.SelectedItem;
try
{
_productService.AddProduct(
txtProduct.Text,
Convert.ToInt32(item.Tag));
Close();
}
catch (ArgumentException exception)
{
MessageBox.Show(
exception.Message,
"Ugyldigt produkt",
MessageBoxButton.OK,
MessageBoxImage.Warning);
}
}
}
}

View File

@@ -11,7 +11,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Repository;
using Pos.Service;
namespace Pos.Setting.Product
{
@@ -21,20 +21,21 @@ namespace Pos.Setting.Product
public partial class ArchiveProductWindow : Window
{
private int _productId;
private readonly ProductService _productService =
new ProductService();
public ArchiveProductWindow(int productId)
{
InitializeComponent();
_productId = productId;
ProductRepository productRepository = new ProductRepository();
Database.Models.ProductEntity product = productRepository.GetById(_productId);
Database.Models.ProductEntity product =
_productService.GetProduct(_productId);
txtDelProduct.Text = product.Name;
}
private void btnYes_Click(object sender, RoutedEventArgs e)
{
ProductRepository productRepository = new ProductRepository();
productRepository.Archive(_productId);
this.Close();
_productService.ArchiveProduct(_productId);
Close();
}
private void btnNo_Click(object sender, RoutedEventArgs e)

View File

@@ -12,7 +12,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Repository;
using Pos.Service;
namespace Pos.Setting.Product
{
@@ -23,6 +23,8 @@ namespace Pos.Setting.Product
{
ObservableCollection<Database.Models.ProductEntity> _productList = new ObservableCollection<Database.Models.ProductEntity>();
private int _productGroupId;
private readonly ProductService _productService =
new ProductService();
public ChangeProductOrderWindow(int productGroupId)
{
InitializeComponent();
@@ -32,8 +34,8 @@ namespace Pos.Setting.Product
private void LoadData()
{
ProductRepository productRepository = new ProductRepository();
List<Database.Models.ProductEntity> products = productRepository.GetByProductGroup(_productGroupId);
List<Database.Models.ProductEntity> products =
_productService.GetProducts(_productGroupId);
foreach (Database.Models.ProductEntity product in products)
{
_productList.Add(product);
@@ -86,13 +88,10 @@ namespace Pos.Setting.Product
private void btnSave_Click(object sender, RoutedEventArgs e)
{
ProductRepository productRepository = new ProductRepository();
for (int i = 0; i < _productList.Count; i++)
{
Database.Models.ProductEntity product = _productList[i];
productRepository.SetIndex(product.Id, i);
}
this.Close();
_productService.UpdateOrder(
_productGroupId,
_productList.Select(product => product.Id).ToList());
Close();
}
}

View File

@@ -11,7 +11,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Repository;
using Pos.Service;
namespace Pos.Setting.Product
{
@@ -23,6 +23,10 @@ namespace Pos.Setting.Product
private int _productId;
private int _selectedProductGroupId;
private Database.Models.ProductEntity _product;
private readonly ProductGroupService _productGroupService =
new ProductGroupService();
private readonly ProductService _productService =
new ProductService();
public EditProductWindow(int productId, int selectedProductGroupId)
{
@@ -34,8 +38,8 @@ namespace Pos.Setting.Product
private void Init()
{
ProductGroupRepository categoryRepository = new ProductGroupRepository();
List<Database.Models.ProductGroupEntity> productGroups = categoryRepository.GetAll();
List<Database.Models.ProductGroupEntity> productGroups =
_productGroupService.GetProductGroups();
foreach (Database.Models.ProductGroupEntity productGroup in productGroups)
{
@@ -46,17 +50,29 @@ namespace Pos.Setting.Product
}
cmbProductGroup.SelectedIndex = _selectedProductGroupId;
ProductRepository productRepository = new ProductRepository();
_product = productRepository.GetById(_productId);
_product = _productService.GetProduct(_productId);
txtProduct.Text = _product.Name;
}
private void btnEdit_Click(object sender, RoutedEventArgs e)
{
ComboBoxItem item = (ComboBoxItem)cmbProductGroup.SelectedItem;
ProductRepository productRepository = new ProductRepository();
productRepository.Update(_product.Id,(int)item.Tag,txtProduct.Text);
this.Close();
try
{
_productService.UpdateProduct(
_product.Id,
(int)item.Tag,
txtProduct.Text);
Close();
}
catch (ArgumentException exception)
{
MessageBox.Show(
exception.Message,
"Ugyldigt produkt",
MessageBoxButton.OK,
MessageBoxImage.Warning);
}
}
}
}

View File

@@ -3,7 +3,7 @@ using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Database.Repository;
using Pos.Service;
using Pos.Setting.Product;
namespace Pos.Setting.ProductGroup
@@ -14,7 +14,10 @@ namespace Pos.Setting.ProductGroup
public partial class ProductWindow : Window
{
private List<Database.Models.ProductGroupEntity> _productGroups;
private int _productId;
private readonly ProductGroupService _productGroupService =
new ProductGroupService();
private readonly ProductService _productService =
new ProductService();
public ProductWindow()
{
@@ -26,8 +29,7 @@ namespace Pos.Setting.ProductGroup
private void Init()
{
ProductGroupRepository categoryRepository = new ProductGroupRepository();
_productGroups = categoryRepository.GetAll();
_productGroups = _productGroupService.GetProductGroups();
cmbCat.Items.Clear();
foreach (Database.Models.ProductGroupEntity productGroup in _productGroups)
{
@@ -38,14 +40,19 @@ namespace Pos.Setting.ProductGroup
cmbCat.Items.Add(comboBoxItem);
}
if (_productGroups.Count > 0)
cmbCat.SelectedIndex = 0;
}
private void LoadData()
{
ProductRepository productRepository = new ProductRepository();
List<Database.Models.ProductEntity> products = productRepository.GetByProductGroup(_productGroups[cmbCat.SelectedIndex].Id);
if (cmbCat.SelectedIndex < 0)
return;
List<Database.Models.ProductEntity> products =
_productService.GetProducts(
_productGroups[cmbCat.SelectedIndex].Id);
lstProductGroup.Items.Clear();
foreach (Database.Models.ProductEntity product in products)
{

View File

@@ -11,7 +11,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Repository;
using Pos.Service;
namespace Pos.Setting.Category
{
@@ -20,6 +20,9 @@ namespace Pos.Setting.Category
/// </summary>
public partial class AddProductGroupWindow : Window
{
private readonly ProductGroupService _productGroupService =
new ProductGroupService();
public AddProductGroupWindow()
{
InitializeComponent();
@@ -27,9 +30,19 @@ namespace Pos.Setting.Category
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
ProductGroupRepository categoryRepository = new ProductGroupRepository();
categoryRepository.Add(txtProductGroup.Text);
this.Close();
try
{
_productGroupService.AddProductGroup(txtProductGroup.Text);
Close();
}
catch (ArgumentException exception)
{
MessageBox.Show(
exception.Message,
"Ugyldig produktgruppe",
MessageBoxButton.OK,
MessageBoxImage.Warning);
}
}
}
}

View File

@@ -11,7 +11,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Repository;
using Pos.Service;
namespace Pos.Setting.Category
{
@@ -21,6 +21,8 @@ namespace Pos.Setting.Category
public partial class ArchiveProductGroupWindow : Window
{
private readonly int _id;
private readonly ProductGroupService _productGroupService =
new ProductGroupService();
public ArchiveProductGroupWindow(string name, int id)
{
InitializeComponent();
@@ -36,9 +38,8 @@ namespace Pos.Setting.Category
private void btnYes_Click(object sender, RoutedEventArgs e)
{
ProductGroupRepository categoryRepository = new ProductGroupRepository();
categoryRepository.Archive(_id);
this.Close();
_productGroupService.ArchiveProductGroup(_id);
Close();
}
}
}

View File

@@ -12,7 +12,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Repository;
using Pos.Service;
namespace Pos.Setting.Category
{
@@ -22,6 +22,8 @@ namespace Pos.Setting.Category
public partial class ChangeProductGroupOrderWindow : Window
{
ObservableCollection<Database.Models.ProductGroupEntity> _prodGroupList = new ObservableCollection<Database.Models.ProductGroupEntity>();
private readonly ProductGroupService _productGroupService =
new ProductGroupService();
public ChangeProductGroupOrderWindow()
{
InitializeComponent();
@@ -30,8 +32,8 @@ namespace Pos.Setting.Category
private void LoadData()
{
ProductGroupRepository categoryRepository = new ProductGroupRepository();
List<Database.Models.ProductGroupEntity> categories = categoryRepository.GetAll();
List<Database.Models.ProductGroupEntity> categories =
_productGroupService.GetProductGroups();
foreach (Database.Models.ProductGroupEntity category in categories)
{
_prodGroupList.Add(category);
@@ -84,13 +86,9 @@ namespace Pos.Setting.Category
private void btnSave_Click(object sender, RoutedEventArgs e)
{
ProductGroupRepository categoryRepository = new ProductGroupRepository();
for (int i = 0; i < _prodGroupList.Count; i++)
{
Database.Models.ProductGroupEntity productGroup = _prodGroupList[i];
categoryRepository.SetIndex(productGroup.Id,i);
}
this.Close();
_productGroupService.UpdateOrder(
_prodGroupList.Select(productGroup => productGroup.Id).ToList());
Close();
}
}
}

View File

@@ -11,7 +11,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Repository;
using Pos.Service;
namespace Pos.Setting.Category
{
@@ -21,6 +21,8 @@ namespace Pos.Setting.Category
public partial class EditProductGroupWindow : Window
{
private readonly int _id;
private readonly ProductGroupService _productGroupService =
new ProductGroupService();
public EditProductGroupWindow(string name, int id)
{
@@ -31,9 +33,21 @@ namespace Pos.Setting.Category
private void btnEdit_Click(object sender, RoutedEventArgs e)
{
ProductGroupRepository categoryRepository = new ProductGroupRepository();
categoryRepository.Edit(txtProductGroup.Text,_id);
this.Close();
try
{
_productGroupService.RenameProductGroup(
_id,
txtProductGroup.Text);
Close();
}
catch (ArgumentException exception)
{
MessageBox.Show(
exception.Message,
"Ugyldig produktgruppe",
MessageBoxButton.OK,
MessageBoxImage.Warning);
}
}
}
}

View File

@@ -11,7 +11,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Repository;
using Pos.Service;
using Pos.Setting.Category;
namespace Pos.Category
@@ -23,6 +23,8 @@ namespace Pos.Category
{
private int _id = -1;
private string _name = string.Empty;
private readonly ProductGroupService _productGroupService =
new ProductGroupService();
public ProductGroupWindow()
{
@@ -53,8 +55,8 @@ namespace Pos.Category
private void LoadData()
{
ProductGroupRepository categoryRepository = new ProductGroupRepository();
List<Database.Models.ProductGroupEntity> categories = categoryRepository.GetAll();
List<Database.Models.ProductGroupEntity> categories =
_productGroupService.GetProductGroups();
LstCategory.Items.Clear();
foreach (Database.Models.ProductGroupEntity category in categories)
{

View File

@@ -11,8 +11,8 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Database.Repository;
using Pos.Category;
using Pos.Service;
using Pos.Setting.ProductGroup;
namespace Pos.Setting
@@ -22,6 +22,9 @@ namespace Pos.Setting
/// </summary>
public partial class SettingWindow : Window
{
private readonly ProductGroupService _productGroupService =
new ProductGroupService();
public SettingWindow()
{
InitializeComponent();
@@ -42,10 +45,8 @@ namespace Pos.Setting
private void btnProductGroups_Click(object sender, RoutedEventArgs e)
{
ProductWindow productWindow = new ProductWindow();
//Check if there is any categories, if not close this window with a message.
ProductGroupRepository categoryRepository = new ProductGroupRepository();
bool anyExist = categoryRepository.Any();
bool anyExist = _productGroupService.HasProductGroups();
if (!anyExist)
{
MessageBox.Show("Der er ingen kategorier, opret dem først", "Kategorier", MessageBoxButton.OK,
@@ -53,6 +54,7 @@ namespace Pos.Setting
}
else
{
ProductWindow productWindow = new ProductWindow();
productWindow.Show();
}
}