public static void AddTown(string name, string zipcode, string countryName, string continent)
        {
            using (AvalonContext context = new AvalonContext())
            {
                Country country = context.Countries.FirstOrDefault(c => c.Name.ToLower() == countryName.ToLower());

                if (country == null)
                {
                    country = new Country
                    {
                        Name      = name,
                        Continent = continent
                    };
                    context.Countries.Add(country);
                    context.SaveChanges();
                }
                Town town = new Town
                {
                    Name    = name,
                    ZipCode = zipcode,
                    Country = country
                };
                context.Towns.Add(town);
                context.SaveChanges();
            }
        }
        public static void UpdateBeer(Beer beerToUpdate, string newstyle, string newdistributor, string newbrewery)
        {
            using (AvalonContext context = new AvalonContext())
            {
                context.Beers.Attach(beerToUpdate);

                context.Entry(beerToUpdate).Reference(o => o.Style).Load();
                context.Entry(beerToUpdate).Reference(o => o.Brewery).Load();
                context.Entry(beerToUpdate).Reference(o => o.Distributor).Load();


                if (newstyle != String.Empty)
                {
                    Style newStyle = context.Styles.Where(s => s.Name == newstyle).Include("Beers").FirstOrDefault();
                    beerToUpdate.Style   = newStyle;
                    beerToUpdate.StyleId = newStyle.Id;
                }
                if (newdistributor != String.Empty)
                {
                    Distributor newDistributor = context.Distributors.Where(s => s.Name == newdistributor).Include("Breweries").Include("Beers").FirstOrDefault();
                    beerToUpdate.Distributor   = newDistributor;
                    beerToUpdate.DistributorId = newDistributor.Id;
                }
                if (newbrewery != String.Empty)
                {
                    Brewery newBrewery = context.Breweries.Where(s => s.Name == newbrewery).Include("Beers").Include("Distributors").FirstOrDefault();
                    beerToUpdate.Brewery   = newBrewery;
                    beerToUpdate.BreweryId = newBrewery.Id;
                }

                context.Entry(beerToUpdate).State = EntityState.Modified;
                context.SaveChanges();
            }
        }
        public static void AddBrewery(Brewery brewery, string chosenTown, List <string> chosenBeers, List <string> chosenDistributors)
        {
            using (var context = new AvalonContext())
            {
                var town = context.Towns.Where(t => t.Name == chosenTown).FirstOrDefault();

                brewery.Town = town;

                foreach (var chosenBeer in chosenBeers)
                {
                    var beer = context.Beers.Where(b => b.Name == chosenBeer).FirstOrDefault();
                    brewery.Beers.Add(beer);
                }

                foreach (var chosenDistributor in chosenDistributors)
                {
                    var distro = context.Distributors.Where(b => b.Name == chosenDistributor).FirstOrDefault();
                    brewery.Distributors.Add(distro);
                }


                context.Breweries.Add(brewery);
                context.SaveChanges();
            }
        }
 public static int BeerCount(string beerName)
 {
     using (var db = new AvalonContext())
     {
         return(db.Beers.Where(x => x.Name == beerName).Count());
     }
 }
 public static List <string> GetAllCustomers()
 {
     using (AvalonContext context = new AvalonContext())
     {
         return(context.Customers.Select(x => x.Name).ToList());
     }
 }
        public static void AddSale(Dictionary <string, int> beers, string customerName)
        {
            using (AvalonContext context = new AvalonContext())
            {
                var customer = context.Customers
                               .Where(c => c.Name.ToLower() == customerName.ToLower()).First();

                Sale sale = new Sale
                {
                    Date     = DateTime.Now,
                    Customer = customer,
                    SellerId = SecurityService.GetLoggedUser().Id
                };
                context.Sales.Add(sale);

                foreach (var b in beers)
                {
                    string beerName = b.Key;
                    int    quantity = b.Value;
                    var    beer     = context.Beers.Where(be => be.Name.ToLower() == beerName.ToLower()).First();
                    beer.Quantity -= quantity;

                    BeerSale beerSale = new BeerSale
                    {
                        Beer     = beer,
                        Quantity = quantity,
                        Sale     = sale
                    };
                    sale.Beers.Add(beerSale);
                }
                context.SaveChanges();
            }
        }
 public static void UpdateClient(Customer client)
 {
     using (var db = new AvalonContext())
     {
         db.Entry(client).State = EntityState.Modified;
         db.SaveChanges();
     }
 }
 public static void DeleteBeer(string beerName)
 {
     using (AvalonContext context = new AvalonContext())
     {
         var beer = context.Beers.Where(b => b.Name == beerName).FirstOrDefault();
         context.Beers.Remove(beer);
         context.SaveChanges();
     }
 }
        public static ObservableCollection <SalesGrid> ShowAllSales()
        {
            using (AvalonContext context = new AvalonContext())
            {
                var sales = context.Sales.Select(s => new
                {
                    Id               = s.Id,
                    Date             = s.Date,
                    Customer         = s.Customer.Name,
                    BeersCount       = s.Beers.Select(b => b.Quantity).ToList(),
                    TotalSalePrice   = s.Beers.Select(b => new { BeerPrice = b.Beer.SalePrice, Quantity = b.Quantity }).ToList(),
                    TotalBoughtPrice = s.Beers.Select(b => new { BeerPrice = b.Beer.DistributorPrice, Quantity = b.Quantity }).ToList(),
                    Seller           = s.Seller.Username
                });
                ObservableCollection <SalesGrid> result = new ObservableCollection <SalesGrid>();

                foreach (var sale in sales)
                {
                    SalesGrid saleGrid = new SalesGrid
                    {
                        SaleId   = sale.Id,
                        Date     = sale.Date,
                        Customer = sale.Customer,
                        Seller   = sale.Seller
                    };

                    int     beerCount   = 0;
                    decimal salePrice   = 0;
                    decimal boughtPrice = 0;

                    foreach (var q in sale.BeersCount)
                    {
                        beerCount += q;
                    }

                    foreach (var b in sale.TotalSalePrice)
                    {
                        salePrice += (decimal)(b.BeerPrice * b.Quantity);
                    }

                    foreach (var b in sale.TotalBoughtPrice)
                    {
                        boughtPrice += (decimal)(b.BeerPrice * b.Quantity);
                    }
                    var profit = salePrice - boughtPrice;

                    saleGrid.TotalBoughtPrice = boughtPrice;
                    saleGrid.Profit           = profit;
                    saleGrid.TotalSalePrice   = salePrice;
                    saleGrid.BeersCount       = beerCount;
                    result.Add(saleGrid);
                }
                return(result);
            }
        }
 public static bool IsTownExisiting(string townName, string countryName)
 {
     using (AvalonContext context = new AvalonContext())
     {
         if (context.Towns.Any(t => t.Name.ToLower() == townName.ToLower() && t.Country.Name.ToLower() == countryName.ToLower()))
         {
             return(true);
         }
         return(false);
     }
 }
示例#11
0
 public static bool IsUserExist(string username)
 {
     using (AvalonContext context = new AvalonContext())
     {
         if (context.Users.Any(u => u.Username.ToLower() == username))
         {
             return(true);
         }
         return(false);
     }
 }
 public static bool IsZipCodeValid(string zipcode)
 {
     using (AvalonContext context = new AvalonContext())
     {
         int zipCodeInt;
         if (!int.TryParse(zipcode, out zipCodeInt))
         {
             return(false);
         }
     }
     return(true);
 }
 public static bool Login(string username, string password)
 {
     using (AvalonContext context = new AvalonContext())
     {
         if (context.Users.Any(u => u.Username == username && u.Password == password))
         {
             loggedUser = context.Users.SingleOrDefault(u => u.Username.ToLower() == username.ToLower());
             return(true);
         }
         return(false);
     }
 }
 public static bool BeerExist(string beerName)
 {
     using (var db = new AvalonContext())
     {
         var isBeerExist = db.Beers.Any(b => b.Name == beerName);
         if (isBeerExist)
         {
             return(true);
         }
         return(false);
     }
 }
 public static void DeleteClient(string clientName)
 {
     using (var db = new AvalonContext())
     {
         var client = db.Customers.FirstOrDefault(x => x.Name == clientName);
         if (client != null)
         {
             db.Customers.Remove(client);
             db.SaveChanges();
         }
     }
 }
        public static ObservableCollection <Beer> GetBeersByName(string name)
        {
            using (AvalonContext context = new AvalonContext())
            {
                var beers = context.Beers.Where(b => b.Name.Contains(name)).OrderBy(b => b.Name).ToList();

                var result = new ObservableCollection <Beer>();
                foreach (var beer in beers)
                {
                    result.Add(beer);
                }
                return(result);
            }
        }
示例#17
0
        public static void DeleteDistributor(string distributorName)
        {
            using (AvalonContext context = new AvalonContext())
            {
                Distributor distributorToDelete = context.Distributors
                                                  .FirstOrDefault(b => b.Name.ToLower() == distributorName.ToLower());
                distributorToDelete.Breweries.Clear();
                distributorToDelete.Town = null;
                distributorToDelete.Beers.Clear();

                context.Distributors.Remove(distributorToDelete);
                context.SaveChanges();
            }
        }
        public static ObservableCollection <Customer> GetCustomersByName(string name)
        {
            using (AvalonContext context = new AvalonContext())
            {
                var clients = context.Customers.Where(b => b.Name.Contains(name)).OrderBy(b => b.Name).ToList();

                var result = new ObservableCollection <Customer>();
                foreach (var client in clients)
                {
                    result.Add(client);
                }
                return(result);
            }
        }
        public static ObservableCollection <Customer> GetAllClients()
        {
            using (AvalonContext context = new AvalonContext())
            {
                var customers = context.Customers.ToList();

                var result = new ObservableCollection <Customer>();
                foreach (var c in customers)
                {
                    result.Add(c);
                }
                return(result);
            }
        }
示例#20
0
        public static ObservableCollection <string> GetAllDistributorsNames()
        {
            using (AvalonContext context = new AvalonContext())
            {
                var distros = context.Distributors.Include("Town").Include("Breweries").OrderBy(b => b.Name).Select(d => d.Name).ToList();

                var result = new ObservableCollection <string>();
                foreach (var d in distros)
                {
                    result.Add(d);
                }
                return(result);
            }
        }
        public static ObservableCollection <Beer> GetAllBeers()
        {
            using (AvalonContext context = new AvalonContext())
            {
                var beers = context.Beers.Include("Style").Include("Brewery").Include("Distributor").OrderBy(b => b.Name).ToList();

                var result = new ObservableCollection <Beer>();
                foreach (var beer in beers)
                {
                    result.Add(beer);
                }
                return(result);
            }
        }
        public static ObservableCollection <string> GetAllBreweriesNames()
        {
            using (AvalonContext context = new AvalonContext())
            {
                var breweries = context.Breweries.OrderBy(b => b.Name).Select(b => b.Name).ToList();

                ObservableCollection <string> result = new ObservableCollection <string>();
                result.Add("Select brewery");
                foreach (var brew in breweries)
                {
                    result.Add(brew);
                }
                return(result);
            }
        }
        public static ObservableCollection <string> GetAllDistributors()
        {
            using (AvalonContext context = new AvalonContext())
            {
                var distributorNames = context.Distributors.OrderBy(d => d.Name).Select(d => d.Name);

                var result = new ObservableCollection <string>();
                result.Add("Select distributor");
                foreach (var distributor in distributorNames)
                {
                    result.Add(distributor);
                }
                return(result);
            }
        }
示例#24
0
        public static void AddDistributor(Distributor distributor, string chosenTown, List <string> chosenBreweries)
        {
            using (var context = new AvalonContext())
            {
                var town = context.Towns.Where(t => t.Name == chosenTown).FirstOrDefault();
                distributor.Town = town;

                foreach (var brew in chosenBreweries)
                {
                    var brewery = context.Breweries.Where(b => b.Name == brew).FirstOrDefault();
                    distributor.Breweries.Add(brewery);
                }
                context.Distributors.Add(distributor);
                context.SaveChanges();
            }
        }
        public static ObservableCollection <string> GetAllBeerStyles()
        {
            using (AvalonContext context = new AvalonContext())
            {
                ObservableCollection <string> result = new ObservableCollection <string>();

                var styles = context.Styles.OrderBy(s => s.Name).ToList();

                result.Add("Select styles");
                foreach (var style in styles)
                {
                    result.Add(style.Name);
                }
                return(result);
            }
        }
示例#26
0
        public static void RegisterUser(string username, string password)
        {
            using (AvalonContext context = new AvalonContext())
            {
                User user = new User
                {
                    Username = username,
                    Password = password
                };

                context.Users.Add(user);
                context.SaveChanges();

                SecurityService.Login(username, password);
            }
        }
        public static ObservableCollection <string> GetAllTowns()
        {
            using (AvalonContext context = new AvalonContext())
            {
                ObservableCollection <string> result = new ObservableCollection <string>();

                var towns = context.Towns.OrderBy(t => t.Name).ToList();
                result.Add("Select towns");

                foreach (var town in towns)
                {
                    result.Add(town.Name);
                }
                return(result);
            }
        }
        public static ObservableCollection <BreweryGrid> GetAllBreweries()
        {
            using (AvalonContext context = new AvalonContext())
            {
                var breweries = context.Breweries.OrderBy(b => b.Name).ToList();

                ObservableCollection <BreweryGrid> result = new ObservableCollection <BreweryGrid>();
                foreach (var brew in breweries)
                {
                    BreweryGrid brewGrid = new BreweryGrid()
                    {
                        Name    = brew.Name,
                        Town    = brew.Town.Name,
                        Address = brew.Adress
                    };
                    result.Add(brewGrid);
                }
                return(result);
            }
        }
        public static void AddCustomer(string name, string address, string townName, string email, string phone, string styleName)
        {
            using (AvalonContext context = new AvalonContext())
            {
                Town  town  = context.Towns.FirstOrDefault(t => t.Name == townName);
                Style style = context.Styles.FirstOrDefault(s => s.Name == styleName);

                Customer customer = new Customer
                {
                    Name          = name,
                    Address       = address,
                    Town          = town,
                    FavoriteStyle = style,
                    Email         = email,
                    Phone         = phone
                };
                context.Customers.Add(customer);
                context.SaveChanges();
            }
        }
 public static void AddBeer(string beerName, decimal salePrice, int quantity, float rating, string styleName, string breweryName, string distributorName, decimal distributorPrice)
 {
     using (AvalonContext context = new AvalonContext())
     {
         Style       style      = context.Styles.FirstOrDefault(s => s.Name.ToLower() == styleName.ToLower());
         Brewery     brewery    = context.Breweries.FirstOrDefault(b => b.Name.ToLower() == breweryName.ToLower());
         Distributor distrbutor = context.Distributors.FirstOrDefault(d => d.Name.ToLower() == distributorName.ToLower());
         Beer        beer       = new Beer
         {
             Name             = beerName,
             SalePrice        = salePrice,
             Quantity         = quantity,
             Rating           = rating,
             Style            = style,
             Brewery          = brewery,
             Distributor      = distrbutor,
             DistributorPrice = distributorPrice
         };
         context.Beers.Add(beer);
         context.SaveChanges();
     }
 }