Пример #1
0
        public ProductType Create(string name, List <int> categoryIds, List <int> brandIds)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var retVal = new ProductType
                {
                    Name     = name,
                    Brand    = new System.Data.Objects.DataClasses.EntityCollection <Brand>(),
                    Category = new System.Data.Objects.DataClasses.EntityCollection <Category>()
                };

                model.ProductType.AddObject(retVal);
                model.SaveChanges();

                var brands     = model.Brand.Where(b => brandIds.Contains(b.Id)).ToList();
                var categories = model.Category.Where(c => categoryIds.Contains(c.Id)).ToList();

                categories.ForEach(c => retVal.Category.Add(c));
                brands.ForEach(b => retVal.Brand.Add(b));
                model.SaveChanges();

                return(model.ProductType
                       .Include(p => p.Category)
                       .Include(p => p.Brand)
                       .FirstOrDefault(p => p.Id.Equals(retVal.Id)));
            }
        }
Пример #2
0
        public Rating Save(int id, string name, decimal amount)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                Rating retVal;

                if (id == 0)
                {
                    retVal = model.Rating.FirstOrDefault(r => r.Name.Equals(name));

                    if (retVal == null)
                    {
                        retVal = new Rating {
                            Name = name, Amount = amount
                        };
                        model.Rating.AddObject(retVal);
                        model.SaveChanges();
                    }
                }
                else
                {
                    retVal        = model.Rating.FirstOrDefault(b => b.Id.Equals(id));
                    retVal.Name   = name;
                    retVal.Amount = amount;
                    model.SaveChanges();
                }

                return(retVal);
            }
        }
Пример #3
0
        public Brand Save(int id, string name, string[] modelNames, string[] productTypeNames)
        {
            using (DataModelEntities dataModel = new DataModelEntities())
            {
                var retVal = dataModel.Brand.FirstOrDefault(b => b.Id.Equals(id));

                if (retVal != null)
                {
                    retVal.Name = name;
                    retVal.Model.Clear();
                    retVal.ProductType.Clear();
                    dataModel.SaveChanges();

                    foreach (var modelName in modelNames)
                    {
                        var model = dataModel.Model.FirstOrDefault(m => m.Name.Equals(modelName));
                        retVal.Model.Add(model);
                    }

                    foreach (var typeName in productTypeNames)
                    {
                        var type = dataModel.ProductType.FirstOrDefault(t => t.Name.Equals(typeName));
                        retVal.ProductType.Add(type);
                    }

                    dataModel.SaveChanges();
                }

                return(dataModel.Brand
                       .Include(b => b.Model)
                       .FirstOrDefault(b => b.Id.Equals(retVal.Id)));
            }
        }
Пример #4
0
        /// <summary>
        /// Saves the city.
        /// </summary>
        /// <param name="cityId">The city identifier.</param>
        /// <param name="countryId">The country identifier.</param>
        /// <param name="name">The City name.</param>
        /// <returns></returns>
        public bool SaveCity(int cityId, int countryId, string name)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                bool retVal = false;
                City city;

                if (cityId > 0)
                {
                    city = model.City.FirstOrDefault(c => c.Id.Equals(cityId));

                    if (city != null)
                    {
                        city.Name      = name;
                        city.CountryId = countryId;
                        model.SaveChanges();
                        retVal = true;
                    }
                }
                else
                {
                    city = new City {
                        Name = name, CountryId = countryId
                    };
                    model.City.AddObject(city);
                    model.SaveChanges();
                    retVal = true;
                }

                return(retVal);
            }
        }
Пример #5
0
        public Model Save(int modelId, int[] subModelIds, int[] brandIds, string name)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var retVal = model.Model.FirstOrDefault(m => m.Id.Equals(modelId));
                retVal.Brand.Clear();
                retVal.SubModel.Clear();
                model.SaveChanges();

                retVal.Brand    = new System.Data.Objects.DataClasses.EntityCollection <Brand>();
                retVal.SubModel = new System.Data.Objects.DataClasses.EntityCollection <SubModel>();

                foreach (var brandId in brandIds)
                {
                    retVal.Brand.Add(new Brand {
                        Id = brandId
                    });
                }

                foreach (var subModelId in subModelIds)
                {
                    retVal.SubModel.Add(new SubModel {
                        Id = subModelId
                    });
                }

                model.SaveChanges();
                return(model.Model
                       .Include(m => m.Brand)
                       .Include(m => m.SubModel)
                       .FirstOrDefault(m => m.Id.Equals(retVal.Id)));
            }
        }
Пример #6
0
        /// <summary>
        /// Saves the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="name">The name.</param>
        /// <param name="modelNames">The model names.</param>
        /// <returns></returns>
        public SubModel Save(int id, string name, string[] modelNames)
        {
            using (DataModelEntities dataModel = new DataModelEntities())
            {
                var      retVal = new SubModel();
                var      models = dataModel.Model.Where(m => modelNames.Contains(m.Name)).ToList();
                SubModel item;

                if (id > 0)
                {
                    item = dataModel.SubModel.FirstOrDefault(s => s.Id.Equals(id));

                    if (item != null)
                    {
                        item.Name = name;
                        item.Model.Clear();
                        dataModel.SaveChanges();
                    }
                }
                else
                {
                    item = new SubModel {
                        Name = name
                    };
                    dataModel.SubModel.AddObject(item);
                    dataModel.SaveChanges();
                }

                models.ForEach(m => item.Model.Add(m));
                dataModel.SaveChanges();
                retVal = dataModel.SubModel.Include(m => m.Model).FirstOrDefault(s => s.Id.Equals(item.Id));
                return(retVal);
            }
        }
Пример #7
0
        public ProductType Save(int id, string name, List <int> categoryIds, List <int> brandIds)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                ProductType retVal;

                if (id > 0)
                {
                    retVal = model.ProductType.FirstOrDefault(p => p.Id.Equals(id));

                    if (retVal != null)
                    {
                        retVal.Brand.Clear();
                        retVal.Category.Clear();
                        model.SaveChanges();
                    }
                }
                else
                {
                    retVal = new ProductType {
                        Brand = new System.Data.Objects.DataClasses.EntityCollection <Brand>(), Category = new System.Data.Objects.DataClasses.EntityCollection <Category>()
                    };
                }

                var brands     = model.Brand.Where(b => brandIds.Contains(b.Id)).ToList();
                var categories = model.Category.Where(c => categoryIds.Contains(c.Id)).ToList();

                retVal.Name = name;

                foreach (var category in categories)
                {
                    retVal.Category.Add(category);
                }

                foreach (var brand in brands)
                {
                    retVal.Brand.Add(brand);
                }
                //categories.ForEach(c => retVal.Category.Add(c));
                //brands.ForEach(b => retVal.Brand.Add(b));

                if (id == 0)
                {
                    model.ProductType.AddObject(retVal);
                }

                model.SaveChanges();

                return(model.ProductType
                       .Include(p => p.Category)
                       .Include(p => p.Brand)
                       .FirstOrDefault(p => p.Id.Equals(retVal.Id)));
            }
        }
Пример #8
0
        /// <summary>
        /// Creates the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public Brand Create(string name, int[] modelIds)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var retVal = new Brand
                {
                    Name  = name,
                    Model = new System.Data.Objects.DataClasses.EntityCollection <Model>()
                };

                foreach (var modelId in modelIds)
                {
                    retVal.Model.Add(new Model {
                        Id = modelId
                    });
                }

                model.Brand.AddObject(retVal);
                model.SaveChanges();

                return(model.Brand
                       .Include(b => b.Model)
                       .FirstOrDefault(b => b.Id.Equals(retVal.Id)));
            }
        }
Пример #9
0
        public Category Save(int id, string name, int?parentId, int[] productTypeIds)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var retVal = model.Category.FirstOrDefault(b => b.Id.Equals(id));

                if (retVal != null)
                {
                    retVal.Name     = name;
                    retVal.ParentId = parentId;
                    //retVal.ProductType = new System.Data.Objects.DataClasses.EntityCollection<ProductType>();

                    foreach (var productTypeId in productTypeIds)
                    {
                        retVal.ProductType.Add(new ProductType {
                            Id = productTypeId
                        });
                    }

                    model.SaveChanges();
                }

                return(retVal);
            }
        }
Пример #10
0
        /// <summary>
        /// Saves the ordering.
        /// </summary>
        /// <param name="productId">The product identifier.</param>
        /// <param name="hasOrder">if set to <c>true</c> [has order].</param>
        /// <param name="orderingOperatorId">The ordering operator identifier.</param>
        /// <param name="orderingCustomerId">The ordering customer identifier.</param>
        /// <param name="orderingDate">The ordering date.</param>
        /// <param name="orderingSellingPrice">The ordering selling price.</param>
        /// <param name="advancedAmount">The advanced amount.</param>
        /// <param name="balancedAmount">The balanced amount.</param>
        /// <param name="estimatedDeliveryDate">The estimated delivery date.</param>
        /// <param name="deliveryDate">The delivery date.</param>
        /// <returns></returns>
        public Product SaveOrdering(int productId, bool hasOrder, int orderingOperatorId, int orderingCustomerId, DateTime orderingDate, decimal orderingSellingPrice,
                                    decimal advancedAmount, decimal balancedAmount, DateTime estimatedDeliveryDate, DateTime deliveryDate)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var product = model.Product.FirstOrDefault(p => p.Id.Equals(productId));

                if (product != null)
                {
                    product.HasOrder              = hasOrder;
                    product.OrderCustomerId       = orderingCustomerId;
                    product.OrderOperatorId       = orderingOperatorId;
                    product.OrderDate             = orderingDate;
                    product.OrderSellingPrice     = orderingSellingPrice;
                    product.AdvancePaymentAmount  = advancedAmount;
                    product.BalanceAmount         = balancedAmount;
                    product.EstimatedDeliveryDate = estimatedDeliveryDate;
                    product.FinalDate             = deliveryDate;
                    product.StatusId              = (int)Enums.ProductStatus.Ordered;

                    model.SaveChanges();
                }

                return(this.GetById(product.Id));
            }
        }
Пример #11
0
        /// <summary>
        /// Saves the selling.
        /// </summary>
        /// <param name="productId">The product identifier.</param>
        /// <param name="buyerId">The buyer identifier.</param>
        /// <param name="buyDate">The buy date.</param>
        /// <param name="buyingPrice">The buying price.</param>
        /// <param name="sellingPrice">The selling price.</param>
        /// <param name="sellingOperatorId">The selling operator identifier.</param>
        /// <param name="cardIdentificationNumber">The card identification number.</param>
        /// <param name="mecanic">if set to <c>true</c> [mecanic].</param>
        /// <returns></returns>
        public Product SaveSelling(int productId, int buyerId, DateTime buyDate, decimal sellingPrice, int sellingOperatorId, string cardIdentificationNumber, bool mecanic)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var product = model.Product.FirstOrDefault(p => p.Id.Equals(productId));
                int?buyer   = null;

                if (buyerId == 0)
                {
                    buyer = null;
                }

                if (product != null)
                {
                    product.BuyerId           = buyer;
                    product.DepositBuyDate    = buyDate;
                    product.OrderSellingPrice = sellingPrice;
                    product.Sold = true;
                    product.SellingOperatorId       = sellingOperatorId;
                    product.Mecanic                 = mecanic;
                    product.CarIdentificationNumber = cardIdentificationNumber;
                    product.StatusId                = (int)Enums.ProductStatus.Sold;

                    model.SaveChanges();
                }

                return(this.GetById(product.Id));
            }
        }
Пример #12
0
        /// <summary>
        /// Creates the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="productTypeId">The category identifier.</param>
        /// <param name="itemId">The item identifier.</param>
        /// <param name="paperReferenceId">The paper reference identifier.</param>
        /// <param name="locationId">The location identifier.</param>
        /// <param name="customerId">The customer identifier.</param>
        /// <param name="operatorId">The operator identifier.</param>
        /// <param name="ratingId">The rating identifier.</param>
        /// <param name="yearIds">The year ids.</param>
        /// <param name="depositDate">The deposit date.</param>
        /// <param name="buyerId">The buyer identifier.</param>
        /// <param name="buyingPrice">The buying price.</param>
        /// <param name="sellingPrice">The selling price.</param>
        /// <param name="sellingOperatorId">The selling operator identifier.</param>
        /// <param name="sellingDate">The selling date.</param>
        /// <param name="cardIdentificationNumber">The card identification number.</param>
        /// <param name="mecanic">if set to <c>true</c> [mecanic].</param>
        /// <returns></returns>
        public Product Create(string name, int productTypeId, int itemId, int paperReferenceId, int locationId, int customerId, int operatorId, int ratingId,
                              int[] yearIds, DateTime depositDate, int buyerId, decimal buyingPrice,
                              decimal sellingPrice, int sellingOperatorId, DateTime sellingDate, string cardIdentificationNumber, bool mecanic)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var product = new Product
                {
                    Name                    = name,
                    ProductTypeId           = productTypeId,
                    ItemId                  = itemId,
                    PaperReferenceId        = paperReferenceId,
                    LocationId              = locationId,
                    CustomerId              = customerId,
                    OperatorId              = operatorId,
                    RatingId                = ratingId,
                    BuyerId                 = buyerId,
                    DepositBuyDate          = depositDate,
                    BuyingPrice             = buyingPrice,
                    OrderSellingPrice       = sellingPrice,
                    SellingOperatorId       = sellingOperatorId,
                    SellingDate             = sellingDate,
                    CarIdentificationNumber = cardIdentificationNumber,
                    Mecanic                 = mecanic
                };

                model.Product.AddObject(product);
                model.SaveChanges();

                this.SetProductYears(product, yearIds, model);

                return(this.GetById(product.Id));
            }
        }
 public static void addWeek(Week contextWeek)
 {
     contextWeek.status = "pending";
     try {
         using (DataModelEntities db = new DataModelEntities())
         {
             db.Weeks.Add(contextWeek);
             Debug.WriteLine(db.SaveChanges() + " changes have been made to the database");
         }
     } catch (Exception e) {
         Debug.WriteLine(e);
         using (DataModelEntities db = new DataModelEntities())
         {
             Week obj = Shared.GetWeek.ReturnObject(contextWeek.UserId, contextWeek.WeekId);
             db.Entry(obj).State = System.Data.Entity.EntityState.Modified;
             obj.SunHours        = contextWeek.SunHours;
             obj.MonHours        = contextWeek.MonHours;
             obj.TuesHours       = contextWeek.TuesHours;
             obj.WedsHours       = contextWeek.WedsHours;
             obj.ThursHours      = contextWeek.ThursHours;
             obj.FriHours        = contextWeek.FriHours;
             obj.SatHours        = contextWeek.SatHours;
             obj.status          = contextWeek.status;
             obj.TotalHours      = contextWeek.SunHours +
                                   contextWeek.MonHours +
                                   contextWeek.TuesHours +
                                   contextWeek.WedsHours +
                                   contextWeek.ThursHours +
                                   contextWeek.FriHours +
                                   contextWeek.SatHours;
             Debug.WriteLine(db.SaveChanges() + " changes have been made to the database");
         }
     }
 }
Пример #14
0
        /// <summary>
        /// Creates the city.
        /// </summary>
        /// <param name="countryId">The country identifier.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public City CreateCity(int countryId, string name)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var country = model.Country.FirstOrDefault(c => c.Id.Equals(countryId));

                if (country == null)
                {
                    return(null);
                }

                var retVal = model.City.Where(c => c.Name.Equals(name) && c.Country.Equals(country)).FirstOrDefault();

                if (retVal == null)
                {
                    retVal = new City
                    {
                        Name    = name,
                        Country = country
                    };

                    model.City.AddObject(retVal);
                    model.SaveChanges();
                }

                return(retVal);
            }
        }
Пример #15
0
        public bool Delete(int id)
        {
            using (DataModelEntities dataModel = new DataModelEntities())
            {
                bool retval       = false;
                var  itemToDelete = dataModel.SubModel.FirstOrDefault(s => s.Id.Equals(id));

                if (itemToDelete != null)
                {
                    //Deletes links with models
                    itemToDelete.Model.Clear();
                    dataModel.SaveChanges();
                    dataModel.SubModel.DeleteObject(itemToDelete);
                    dataModel.SaveChanges();
                    retval = true;
                }

                return(retval);
            }
        }
Пример #16
0
        public void SetProductYears(Product product, int[] yearIds, DataModelEntities model)
        {
            foreach (var yearId in yearIds)
            {
                if (!product.ProductYear.Any(y => y.ProductId.Equals(product.Id) && y.Year.Id.Equals(yearId)))
                {
                    product.ProductYear.Add(new ProductYear {
                        ProductId = product.Id, YearId = yearId
                    });
                }
            }

            model.SaveChanges();
        }
Пример #17
0
        public Year Save(int id, short year)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var retVal = model.Year.FirstOrDefault(b => b.Id.Equals(id));

                if (retVal != null)
                {
                    retVal.Year1 = year;
                    model.SaveChanges();
                }

                return(retVal);
            }
        }
Пример #18
0
        public PaperReference Create(string name)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var retVal = new PaperReference
                {
                    Name = name
                };

                model.PaperReference.AddObject(retVal);
                model.SaveChanges();

                return(retVal);
            }
        }
Пример #19
0
        public Year Create(short year)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var retVal = new Year
                {
                    Year1 = year
                };

                model.Year.AddObject(retVal);
                model.SaveChanges();

                return(retVal);
            }
        }
Пример #20
0
        public Item Create(string name)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var retVal = new Item
                {
                    Name = name
                };

                model.Item.AddObject(retVal);
                model.SaveChanges();

                return(retVal);
            }
        }
Пример #21
0
        /// <summary>
        /// Creates the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public CustomerType Create(string name)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var retVal = new CustomerType
                {
                    Name = name
                };

                model.CustomerType.AddObject(retVal);
                model.SaveChanges();

                return(retVal);
            }
        }
Пример #22
0
        public Item Save(int id, string name)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var retVal = model.Item.FirstOrDefault(b => b.Id.Equals(id));

                if (retVal != null)
                {
                    retVal.Name = name;
                    model.SaveChanges();
                }

                return(retVal);
            }
        }
Пример #23
0
        /// <summary>
        /// Saves the country.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public bool SaveCountry(int id, string name)
        {
            using (DataModelEntities dataModel = new DataModelEntities())
            {
                bool    retVal = false;
                Country item;

                if (id == 0)
                {
                    item = dataModel.Country.FirstOrDefault(c => c.Name.Equals(name));

                    if (item == null)
                    {
                        item = new Country {
                            Name = name
                        };
                        dataModel.Country.AddObject(item);
                        dataModel.SaveChanges();
                        retVal = true;
                    }
                }
                else
                {
                    item = dataModel.Country.FirstOrDefault(c => c.Id.Equals(id));

                    if (item != null)
                    {
                        item.Name = name;
                        dataModel.SaveChanges();
                        retVal = true;
                    }
                }

                return(retVal);
            }
        }
Пример #24
0
        public SubCategory1 Save(int id, string name, int categoryId)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var retVal = model.SubCategory1.FirstOrDefault(s => s.Id.Equals(id));

                if (retVal != null)
                {
                    retVal.Name       = name;
                    retVal.CategoryId = categoryId;
                    model.SaveChanges();
                }

                return(model.SubCategory1.Include(s => s.Category).FirstOrDefault(s => s.Id.Equals(retVal.Id)));
            }
        }
Пример #25
0
        public Rating Create(string name, decimal amount)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                var retVal = new Rating
                {
                    Name   = name,
                    Amount = amount
                };

                model.Rating.AddObject(retVal);
                model.SaveChanges();

                return(retVal);
            }
        }
Пример #26
0
        public bool Delete(int id)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                bool retVal = false;
                var  item   = model.Category.FirstOrDefault(b => b.Id.Equals(id));

                if (item != null)
                {
                    item.IsActive = false;
                    model.SaveChanges();
                    retVal = true;
                }

                return(retVal);
            }
        }
Пример #27
0
        public bool Delete(int id)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                bool retVal         = false;
                var  PaperReference = model.PaperReference.FirstOrDefault(b => b.Id.Equals(id));

                if (PaperReference != null)
                {
                    model.DeleteObject(PaperReference);
                    model.SaveChanges();
                    retVal = true;
                }

                return(retVal);
            }
        }
Пример #28
0
        public bool Delete(int id)
        {
            using (DataModelEntities model = new DataModelEntities())
            {
                bool retVal = false;
                var  item   = model.Item.FirstOrDefault(b => b.Id.Equals(id));

                if (item != null)
                {
                    model.DeleteObject(item);
                    model.SaveChanges();
                    retVal = true;
                }

                return(retVal);
            }
        }
Пример #29
0
        public bool DeleteCity(int id)
        {
            bool retVal = false;

            using (DataModelEntities dataModel = new DataModelEntities())
            {
                var toDelete = dataModel.City.FirstOrDefault(c => c.Id.Equals(id));

                if (toDelete != null)
                {
                    dataModel.City.DeleteObject(toDelete);
                    dataModel.SaveChanges();
                    retVal = true;
                }
            }

            return(retVal);
        }
Пример #30
0
        /// <summary>
        /// Creates the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public SubModel Create(string name, int[] modelIds)
        {
            using (DataModelEntities dataModel = new DataModelEntities())
            {
                var models = dataModel.Model.Where(m => modelIds.Contains(m.Id)).ToList();

                var retVal = new SubModel
                {
                    Name = name
                };

                models.ForEach(m => retVal.Model.Add(m));
                dataModel.SubModel.AddObject(retVal);
                dataModel.SaveChanges();

                return(dataModel.SubModel.Include(s => s.Model).FirstOrDefault(s => s.Id.Equals(retVal.Id)));
            }
        }