Exemple #1
0
        public async Task <ActionResult> CreateTool(ToolAddDto tool)
        {
            var entity = _mapper.Map <Tool>(tool);

            _toolRepository.AddTool(entity);
            await _toolRepository.SaveAsync();

            return(Ok());
        }
        private void AddTool()
        {
            var purchaseDate = DatePickerPurchaseDate.SelectedDate?.Date ?? DateTime.Now;

            decimal.TryParse(TextBoxPurchaseValue.Text, out decimal purchaseValue);
            decimal.TryParse(TextBoxPrice.Text, out decimal rentalPrice);
            int.TryParse(TextBoxWarranty.Text, out int warranty);

            var tool = new Tool()
            {
                ToolId            = Guid.NewGuid(),
                Name              = TextBoxName.Text,
                Sn                = TextBoxSn.Text,
                PurchaseDate      = purchaseDate.Add(DateTime.Now.TimeOfDay),
                PurchasesValue    = purchaseValue,
                DocumentNumber    = TextBoxDocumentNumber.Text,
                Warranty          = warranty,
                RentalPrice       = rentalPrice,
                Destroyed         = Destroyed.IsChecked != null && (bool)Destroyed.IsChecked,
                DestroyedDate     = DestroyedDate.SelectedDate?.Date.Add(DateTime.Now.TimeOfDay),
                DestroyedCustomer = (User)SelectDestroyed.SelectedItem,
                Lost              = Lost.IsChecked != null && (bool)Lost.IsChecked,
                LostDate          = DestroyedDate.SelectedDate?.Date.Add(DateTime.Now.TimeOfDay),
                LostCustomer      = (User)SelectLost.SelectedItem,
                Description       = new TextRange(TextBoxDescription.Document.ContentStart, TextBoxDescription.Document.ContentEnd).Text,
                Producer          = (Company)SelectProducer.SelectedItem
            };

            _db.AddTool(tool);
            try
            {
                _db.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                DbExceptionValidate("Błąd zapisu narzędzi", e);
            }
            catch (Exception e)
            {
                MessageBox.Show("Błąd zapisu narzędzi: " + e.Message, "Uwaga", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            SetEdited(false);
            this.Close();
        }