예제 #1
0
        /// <summary>
        /// Додає постачальника
        /// </summary>
        /// <param name="supplierDto">Екземпляр постачальника</param>
        public void AddSupplier(SuppliersDtoModel supplierDto)
        {
            var            mapper   = new MapperConfiguration(cfg => cfg.CreateMap <SuppliersDtoModel, SuppliersModel>()).CreateMapper();
            SuppliersModel supplier = mapper.Map <SuppliersModel>(supplierDto);

            var results = suppliersValidator.Validate(supplier);

            if (results.IsValid)
            {
                suppliersRepository.Add(supplier);
            }
            else
            {
                throw new Exception("Помилка валідації постачальника:" + Environment.NewLine +
                                    ValidationResultsHelper.GetValidationErrors(results));
            }
        }
        public static void ChooseOperatioForSuppliersRepository(ISuppliersRepository suppliersRepository, OperationForSuppliersRepository suppliersRepositoryOperation)
        {
            switch (suppliersRepositoryOperation)
            {
            case OperationForSuppliersRepository.ShowAll:
                ShowAllSuppliers(suppliersRepository.Get());
                break;

            case OperationForSuppliersRepository.ShowById:
                Console.WriteLine("Enter supplier Id: ");
                if (int.TryParse(Console.ReadLine(), out var supplierId))
                {
                    Console.WriteLine(suppliersRepository.Get(supplierId));
                }
                break;

            case OperationForSuppliersRepository.Add:
                Supplier supplierToAdd = CreateSupplierToAdd();
                suppliersRepository.Add(supplierToAdd);
                Console.WriteLine("New supplier added");
                break;

            case OperationForSuppliersRepository.Update:

                Supplier supplierToUpdate = null;
                Console.Write("Enter supplier Id to update: ");
                if (int.TryParse(Console.ReadLine(), out var supplierIdToUpdate))
                {
                    supplierToUpdate = suppliersRepository.Get(supplierIdToUpdate);
                }
                Console.Write("Enter new Name: ");
                supplierToUpdate.Name = Console.ReadLine();

                suppliersRepository.Update(supplierToUpdate);
                Console.WriteLine("Supplier updated");
                break;

            case OperationForSuppliersRepository.Delete:
                Console.WriteLine("Enter supplier Id: ");
                if (int.TryParse(Console.ReadLine(), out var supplierIdToDelete))
                {
                    suppliersRepository.Delete(supplierIdToDelete);
                    Console.WriteLine("Supplier deleted");
                }
                break;

            case OperationForSuppliersRepository.GetSupplierInfoWithMaxQuantityOfGoods:
                var maxQuantityOfGoodsSupplier = suppliersRepository.GetSupplierInfoWithMaxQuantityOfGoods();
                Console.WriteLine($"Supplier with max quantity of goods: {maxQuantityOfGoodsSupplier}");
                break;

            case OperationForSuppliersRepository.GetSupplierInfoWithMinQuantityOfGoods:
                var minQuantityOfGoodsSupplier = suppliersRepository.GetSupplierInfoWithMaxQuantityOfGoods();
                Console.WriteLine($"Supplier with max quantity of goods: {minQuantityOfGoodsSupplier}");
                break;

            default:
                Environment.Exit(0);
                break;
            }
        }