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