コード例 #1
0
        public OrderModel GetOrder([FromUri] int id)
        {
            var ctx           = new DAL.Grupptenta4Entities();
            var order         = ctx.Orders.Find(id);
            var orderBouquets = order.Boquetts.Where(b => b.OrderId == order.Id);

            ICollection <BoquetteModel> list = orderBouquets.Select(ob => new BoquetteModel {
                Id = ob.Id, Quantity = ob.Quantity, Sum = ob.Sum, Products = ctx.Products.Where(p => p.ProductBoquetts.Any(pb => pb.BoquettId == ob.Id)).ToList().Select(p => new ProductModel {
                    Id = p.Id, Name = p.Name, ColorId = p.ColorId, CategoryId = p.CategoryId, Description = p.Description, Price = p.Price, Active = p.Active, Quantity = p.ProductBoquetts.FirstOrDefault(pbb => pbb.BoquettId == ob.Id && pbb.ProductId == p.Id).Quantity
                }).ToList()
            }).ToList();

            return(new OrderModel()
            {
                Id = order.Id,
                Buquettes = list.ToList(),
                Address = ctx.Addresses.Select(a => new AddressModel {
                    Id = a.Id, PostalCode = a.PostalCode, Street = a.Street
                }).FirstOrDefault(a => a.Id == order.Address.Id),
                Comment = order.Comment,
                TotalPrice = order.TotalPrice,
                Delivery = ctx.DeliveryMethods.Select(dm => new DeliveryModel {
                    Id = dm.Id, Method = dm.Method
                }).FirstOrDefault(dm => dm.Id == order.DeliveryId),
                Payment = ctx.PaymentMethods.Select(pm => new PaymentModel {
                    Id = pm.Id, Method = pm.Method
                }).FirstOrDefault(pm => pm.Id == order.PaymentId),
                Store = ctx.Stores.Select(s => new StoreModel {
                    Id = s.Id, Name = s.Name
                }).FirstOrDefault(s => s.Id == order.StoreId),
                Email = ctx.Emails.Select(e => new EmailModel {
                    Id = e.Id, Email = e.Email1, Subscribe = e.Subscribe
                }).FirstOrDefault(e => e.Id == order.EmailId),
            });
        }
コード例 #2
0
        public IEnumerable <OrderModel> GetOrders()
        {
            var ctx = new DAL.Grupptenta4Entities();

            return(ctx.Orders.Select(o => new OrderModel
            {
                Id = o.Id,
                Comment = o.Comment,
                TotalPrice = o.TotalPrice,
                IsDelivered = o.IsDelivered,
                Email = new EmailModel
                {
                    Email = o.Email.Email1
                },
                Address = new AddressModel
                {
                    Id = o.Address.Id,
                    PostalCode = o.Address.PostalCode,
                    Street = o.Address.Street
                },
                Delivery = ctx.DeliveryMethods.Select(dm => new DeliveryModel {
                    Id = dm.Id, Method = dm.Method
                }).FirstOrDefault(dm => dm.Id == o.DeliveryId),
                Payment = ctx.PaymentMethods.Select(pm => new PaymentModel {
                    Id = pm.Id, Method = pm.Method
                }).FirstOrDefault(pm => pm.Id == o.PaymentId),
                Store = ctx.Stores.Select(s => new StoreModel {
                    Id = s.Id, Name = s.Name
                }).FirstOrDefault(s => s.Id == o.StoreId),
            }).OrderByDescending(o => o.Id));
        }
コード例 #3
0
        public void DeleteOrder([FromUri] int id)
        {
            var ctx = new DAL.Grupptenta4Entities();

            var orderToBeRemoved = ctx.Orders.Find(id);
            var orderBouqet      = orderToBeRemoved.Boquetts.Where(pb => pb.OrderId == orderToBeRemoved.Id);

            var transaction = ctx.Database.BeginTransaction();

            try
            {
                var listOfJunc = new List <DAL.ProductBoquett>();

                foreach (var item in orderBouqet)
                {
                    foreach (var prod in item.ProductBoquetts)
                    {
                        var temp = ctx.ProductBoquetts.Where(pb => pb.Boquett.Id == item.Id).First();
                        listOfJunc.Add(temp);
                    }
                }

                ctx.ProductBoquetts.RemoveRange(listOfJunc);
                ctx.Boquetts.RemoveRange(orderToBeRemoved.Boquetts);
                ctx.Orders.Remove(orderToBeRemoved);
                ctx.SaveChanges();

                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }
        }
コード例 #4
0
        public CategoryModel GetCategory(int id)
        {
            var ctx = new DAL.Grupptenta4Entities();

            return(ctx.Categories.Where(c => c.Id == id).Select(q => new CategoryModel {
                Id = q.Id, Name = q.Name, ParentId = q.ParentId
            }).FirstOrDefault());
        }
コード例 #5
0
        public ColorModel GetColor(int id)
        {
            var ctx = new DAL.Grupptenta4Entities();

            return(ctx.Colors.Where(c => c.Id == id).Select(q => new ColorModel {
                Id = q.Id, Name = q.Name
            }).FirstOrDefault());
        }
コード例 #6
0
        public void Put([FromUri] int id)
        {
            var ctx         = new DAL.Grupptenta4Entities();
            var orderToEdit = ctx.Orders.Find(id);

            orderToEdit.IsDelivered = true;
            ctx.SaveChanges();
        }
コード例 #7
0
        public void Put([FromBody] ColorModel color, [FromUri] int id) // Måste ses över mer
        {
            var ctx         = new DAL.Grupptenta4Entities();
            var colorToEdit = ctx.Colors.Find(id);

            colorToEdit.Name = color.Name;

            ctx.SaveChanges();
        }
コード例 #8
0
        public void Put([FromBody] CategoryModel category, [FromUri] int id) // Måste ses över mer
        {
            var ctx            = new DAL.Grupptenta4Entities();
            var categoryToEdit = ctx.Categories.Find(id);

            categoryToEdit.Name     = category.Name;
            categoryToEdit.ParentId = categoryToEdit.ParentId;

            ctx.SaveChanges();
        }
コード例 #9
0
        public IEnumerable <ColorModel> GetColors()
        {
            var ctx = new DAL.Grupptenta4Entities();

            return(ctx.Colors.Where(q => q.Active == true).Select(c => new ColorModel
            {
                Id = c.Id,
                Name = c.Name,
            }).ToList());
        }
コード例 #10
0
        public void DeleteProduct([FromUri] int id)
        {
            var ctx = new DAL.Grupptenta4Entities();

            var productToBeRemoved = ctx.Products.Find(id);

            productToBeRemoved.Active = false;

            ctx.SaveChanges();
        }
コード例 #11
0
        public void DeleteColor([FromUri] int id)
        {
            var ctx = new DAL.Grupptenta4Entities();

            var colorToBeRemoved = ctx.Colors.Find(id);

            colorToBeRemoved.Active = false;

            ctx.SaveChanges();
        }
コード例 #12
0
        public void DeleteCategory([FromUri] int id)
        {
            var ctx = new DAL.Grupptenta4Entities();

            var categoryToBeRemoved = ctx.Categories.Find(id);

            categoryToBeRemoved.Active = false;

            ctx.SaveChanges();
        }
コード例 #13
0
        public IEnumerable <CategoryModel> GetCategories()
        {
            var ctx = new DAL.Grupptenta4Entities();

            return(ctx.Categories.Where(q => q.Active == true).Select(c => new CategoryModel
            {
                Id = c.Id,
                Name = c.Name,
                ParentId = c.ParentId,
            }).ToList());
        }
コード例 #14
0
        public void PostColor([FromBody] ColorModel color)
        {
            var ctx = new DAL.Grupptenta4Entities();

            ctx.Colors.Add(new DAL.Color
            {
                Name   = color.Name,
                Active = true,
            });

            ctx.SaveChanges();
        }
コード例 #15
0
        public void Put([FromBody] CreateProductModel product, [FromUri] int id) // Måste ses över mer
        {
            var ctx           = new DAL.Grupptenta4Entities();
            var productToEdit = ctx.Products.Find(id);

            productToEdit.Name        = product.Name;
            productToEdit.Description = product.Description;
            productToEdit.ColorId     = product.ColorId;
            productToEdit.CategoryId  = product.CategoryId;
            productToEdit.Price       = product.Price;

            ctx.SaveChanges();
        }
コード例 #16
0
        public void PostCategory([FromBody] CategoryModel category)
        {
            var ctx = new DAL.Grupptenta4Entities();

            ctx.Categories.Add(new DAL.Category
            {
                Name     = category.Name,
                ParentId = category.ParentId,
                Active   = true,
            });

            ctx.SaveChanges();
        }
コード例 #17
0
        public void PostProduct([FromBody] CreateProductModel product)
        {
            var ctx = new DAL.Grupptenta4Entities();

            product.Active = true;

            ctx.Products.Add(new DAL.Product
            {
                Name        = product.Name,
                Description = product.Description,
                ColorId     = product.ColorId,
                CategoryId  = product.CategoryId,
                Price       = product.Price,
                Picture     = product.Image,
                Active      = product.Active,
            });

            ctx.SaveChanges();
        }
コード例 #18
0
        public ProductModel GetProduct([FromUri] int id)
        {
            var ctx = new DAL.Grupptenta4Entities();

            return(ctx.Products.Where(p => p.Id == id).Select(p => new ProductModel
            {
                Id = p.Id,
                Name = p.Name,
                Description = p.Description,
                Color = ctx.Colors.Where(c => c.Products.Any(co => co.ColorId == p.ColorId)).Select(c => new ColorModel {
                    Id = c.Id, Name = c.Name
                }).FirstOrDefault(),
                Category = ctx.Categories.Where(c => c.Products.Any(pr => pr.Id == p.Id)).Select(c => new CategoryModel {
                    Id = c.Id, Name = c.Name, ParentId = c.ParentId
                }).FirstOrDefault(),
                Price = p.Price,
                Image = p.Picture,
            }).FirstOrDefault());
        }
コード例 #19
0
        public void Put([FromBody] OrderModel model, [FromUri] int id) // Måste ses över mer
        {
            var ctx         = new DAL.Grupptenta4Entities();
            var orderToEdit = ctx.Orders.Find(id);

            orderToEdit.Comment    = model.Comment;
            orderToEdit.TotalPrice = model.TotalPrice;

            // Editera databasens buketter till modellens uppdaterade buketter
            foreach (var dbBouquet in orderToEdit.Boquetts)
            {
                dbBouquet.Quantity = model.Buquettes.First(b => b.Id == dbBouquet.Id).Quantity;

                foreach (var dbProduct in dbBouquet.ProductBoquetts)
                {
                    dbProduct.Quantity = model.Buquettes.First(b => b.Id == dbBouquet.Id).Products.First(p => p.Id == dbProduct.ProductId).Quantity;
                }
            }

            // Räkna ut nya bukett priset
            decimal bouquetPrice = 0;

            foreach (var dbBouquet in orderToEdit.Boquetts)
            {
                foreach (var dbProduct in dbBouquet.ProductBoquetts)
                {
                    var productTemp = ctx.Products.FirstOrDefault(p => p.Id == dbProduct.ProductId);
                    bouquetPrice += productTemp.Price * dbProduct.Quantity;
                }
                dbBouquet.Sum = bouquetPrice;
                bouquetPrice  = 0;
            }

            orderToEdit.Email   = ctx.Emails.FirstOrDefault(e => e.Email1 == model.Email.Email) ?? (new DAL.Email {
                Email1 = model.Email.Email, Subscribe = model.Email.Subscribe
            });
            orderToEdit.Address = ctx.Addresses.FirstOrDefault(a => a.Street == model.Address.Street && a.PostalCode == model.Address.PostalCode) ?? (new DAL.Address {
                Street = model.Address.Street, PostalCode = model.Address.PostalCode
            });

            ctx.SaveChanges();
        }
コード例 #20
0
        public IEnumerable <ProductModel> GetAllProducts()
        {
            var ctx = new DAL.Grupptenta4Entities();

            return(ctx.Products.Select(p => new ProductModel
            {
                Id = p.Id,
                Name = p.Name,
                Description = p.Description,
                Color = ctx.Colors.Where(c => c.Products.Any(co => co.ColorId == p.ColorId)).Select(c => new ColorModel {
                    Id = c.Id, Name = c.Name
                }).FirstOrDefault(),
                Category = ctx.Categories.Where(c => c.Products.Any(co => co.CategoryId == p.CategoryId)).Select(c => new CategoryModel {
                    Id = c.Id, Name = c.Name, ParentId = c.ParentId
                }).FirstOrDefault(),
                Price = p.Price,
                Image = p.Picture,
                Active = p.Active,
            }).Where(pr => pr.Active == true).ToList());
        }
コード例 #21
0
 public OrderController()
 {
     ctx = new DAL.Grupptenta4Entities();
 }