Exemple #1
0
        public void DeleteOrder(OrderViewModel vm)
        {
            using (var context = new KebapBobEntities())
            {
                var thisorder = context.Order.FirstOrDefault(x => x.Id == vm.Id);

                if (thisorder == null)
                {
                    throw new Exception("Record could not found!");
                }
                else
                {
                    thisorder.Id             = vm.Id;
                    thisorder.TrackingNumber = vm.TrackingNumber;
                    thisorder.Address        = new Address
                    {
                        Id            = vm.AddressId,
                        StreetAddress = vm.StreetName,
                        City          = vm.City,
                        State         = vm.State,
                        ZipCode       = vm.ZipCode,
                        RecipientName = vm.RecipientName
                    };
                    context.Order.Attach(thisorder);
                    context.Order.Remove(thisorder);
                    context.SaveChanges();
                }
            }
        }
Exemple #2
0
        public void CreateOrder(OrderViewModel vm)
        {
            using (var context = new KebapBobEntities())
            {
                var newOrder = new OrderItems
                {
                    Order = new Order
                    {
                        TrackingNumber = Guid.NewGuid().ToString(),

                        Address = new Address
                        {
                            StreetAddress = vm.StreetName,
                            City          = vm.City,
                            State         = vm.State,
                            ZipCode       = vm.ZipCode,
                            RecipientName = vm.RecipientName
                        },
                        UserID = vm.UserId
                    }
                };

                context.OrderItems.Add(newOrder);
                context.SaveChanges();
            }
        }
Exemple #3
0
        public void CreateProduct(ProductViewModel vm)
        {
            using (var context = new KebapBobEntities())
            {
                var newProduct = new Product
                {
                    Name        = vm.ItemName,
                    Description = vm.itemDescription,
                    UserID      = vm.UserId
                };

                context.Product.Add(newProduct);
                context.SaveChanges();
            }
        }
Exemple #4
0
        public void UpdateProduct(ProductViewModel vm)
        {
            using (var context = new KebapBobEntities())
            {
                var product = context.Product.FirstOrDefault(x => x.Id == vm.Id);
                if (product == null)
                {
                    throw new Exception("Product couldn't found");
                }
                product.Name        = vm.ItemName;
                product.Description = vm.itemDescription;

                context.SaveChanges();
            }
        }
Exemple #5
0
        public void UpdateOrder(OrderViewModel vvm)
        {
            using (var context = new KebapBobEntities())
            {
                var thisorder = context.Order
                                .Include(d => d.Address)
                                .FirstOrDefault(x => x.Id == vvm.Id);
                if (thisorder == null)
                {
                    throw new Exception("Record could not found!");
                }

                thisorder.Address.RecipientName = vvm.RecipientName;
                thisorder.Address.StreetAddress = vvm.StreetName;
                thisorder.Address.City          = vvm.City;
                thisorder.Address.State         = vvm.State;
                thisorder.Address.ZipCode       = vvm.ZipCode;

                context.SaveChanges();
            }
        }
Exemple #6
0
        public void Register(UserViewModel user)
        {
            using (var context = new KebapBobEntities())
            {
                var existingUser = context.User.FirstOrDefault(x => x.UserName == user.UserName);

                if (existingUser != default(User)) // null

                {
                    throw new ArgumentException("User already exists!");
                }
                var newUser = new User
                {
                    UserName  = user.UserName,
                    FirstName = user.FirstName,
                    LastName  = user.LastName,
                    Password  = user.Password
                };
                context.User.Add(newUser);
                context.SaveChanges();
            }
        }
Exemple #7
0
        /// <summary>
        /// Save method.
        /// </summary>
        public void Save()
        {
            try
            {
                _context.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                var outputLines = new List <string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:", DateTime.Now,
                                        eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage));
                    }
                }
                System.IO.File.AppendAllLines(@"C:\errors.txt", outputLines);

                throw e;
            }
        }