コード例 #1
0
        //Dodaj możliwość edycji tylko jednego parametru, np. samego Imienia
        public Customer Update(int id, CustomerDTO customerDTO)
        {
            var customer = new Customer
            {
                CustomerID = id,
                FirstName  = customerDTO.FirstName, // ?? "Imie",
                LastName   = customerDTO.LastName
            };

            if (!string.IsNullOrEmpty(customer.FirstName) &&
                !string.IsNullOrEmpty(customer.LastName))
            {
                _context.Attach(customer).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                _context.SaveChanges();
            }

            return(customer);
        }
コード例 #2
0
        public Product Update(int id, ProductDTO productDTO)
        {
            var product = new Product
            {
                ProductID    = id,
                Name         = productDTO.Name,
                Desctription = productDTO.Desctription,
                Price        = productDTO.Price,
                Quantity     = productDTO.Quantity
            };

            if (!string.IsNullOrEmpty(product.Name) &&
                !string.IsNullOrEmpty(product.Desctription))
            {
                _context.Attach(product).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                _context.SaveChanges();
            }

            return(product);
        }
コード例 #3
0
        public Order Update(int id, OrderDTO orderDTO)
        {
            var dateResult    = new DateTime();
            var isCorrectDate = DateTime.TryParse(orderDTO.Date, out dateResult);

            var order = new Order
            {
                OrderID    = id,
                Date       = isCorrectDate ? dateResult : DateTime.Now, // jeżeli nie sparsuje ustawia aktualną - popraw to
                CustomerID = orderDTO.CustomerID,
                ProductID  = orderDTO.ProductID
            };

            if ((order.CustomerID > 0) && (order.ProductID > 0)) // a co jeśli dane Id nie istnieje??
            {
                _context.Attach(order).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                _context.SaveChanges();
            }

            return(order);
        }