Ejemplo n.º 1
0
        private async void CommitOrdersButton_Click(object sender, RoutedEventArgs e)
        {
            CommitOrdersButton.IsEnabled = false;
            var orders = new Dictionary <int, int>();

            foreach (var kvp in Orders)
            {
                orders.Add((int)kvp.Key.Id, kvp.Value);
            }

            try
            {
                var result = await _client.SendOrders(orders);

                foreach (var kvp in result)
                {
                    if (kvp.Value > 0)
                    {
                        var local = Beers.Find(b => b.Id == kvp.Key);
                        local.InStore += (int)kvp.Value;
                        local.Amount  -= (int)kvp.Value;
                    }
                }

                BeersList.Items.Refresh();
                BeerOrderList.Items.Refresh();
            }
            catch
            {
            }

            Orders.Clear();

            CommitOrdersButton.IsEnabled = true;
        }
Ejemplo n.º 2
0
        public Beer GetById(string id)
        {
            var beerItem = Beers.Find(b => b.id == id).FirstOrDefault();

            beerItem = AttachEntities(beerItem);
            return(beerItem);
        }
Ejemplo n.º 3
0
        public IEnumerable <Beer> GetAll()
        {
            var beerList = Beers.Find(b => true).ToList();

            beerList = AttachEntities(beerList).ToList();
            return(beerList);
        }
 internal Keg AttachEntities(Keg kegItem)
 {
     if (kegItem != null && kegItem.kegSizeId != null)
     {
         var kegSize = KegSizes.Find(ks => ks.id == kegItem.kegSizeId).FirstOrDefault();
         kegItem.kegSize = kegSize;
     }
     if (kegItem != null && kegItem.beerId != null)
     {
         var beer = Beers.Find(b => b.id == kegItem.beerId).FirstOrDefault();
         beer         = AttachEntities(beer);
         kegItem.beer = beer;
     }
     return(kegItem);
 }
Ejemplo n.º 5
0
        private async Task GetData()
        {
            BeersList.IsEnabled  = false;
            SalesList.IsEnabled  = false;
            SellButton.IsEnabled = false;

            Beers = (await _client.GetBeers()).ToList();
            Sales = (await _client.GetSales()).ToList();

            foreach (var sale in Sales)
            {
                sale.Beer = Beers.Find(b => b.Id == sale.BeerId);
            }

            BeersList.ItemsSource     = Beers;
            BeerOrderList.ItemsSource = Beers;
            BeerStatsList.ItemsSource = Beers;
            SalesList.ItemsSource     = Sales;

            BeersList.IsEnabled  = true;
            SalesList.IsEnabled  = true;
            SellButton.IsEnabled = true;
        }
        internal IEnumerable <Keg> AttachEntities(IEnumerable <Keg> kegList)
        {
            //Load KegSizes
            var kegSizeIdsToLoad = kegList.Select(k => k.kegSizeId).Distinct().ToList();
            var kegSizes         = KegSizes.Find(ks => kegSizeIdsToLoad.Contains(ks.id)).ToList();
            //Load Beers
            var beerIdsToLoad = kegList.Select(k => k.beerId).Distinct().ToList();
            var beers         = Beers.Find(b => beerIdsToLoad.Contains(b.id)).ToList();

            beers = AttachEntities(beers).ToList();

            kegList.ToList().ForEach(k => {
                if (k.beerId != null)
                {
                    var beer = beers.FirstOrDefault(b => b.id == k.beerId);
                    k.beer   = beer;
                }
                if (k.kegSizeId != null)
                {
                    k.kegSize = kegSizes.FirstOrDefault(ks => ks.id == k.kegSizeId);
                }
            });
            return(kegList);
        }
Ejemplo n.º 7
0
        public static void Start()
        {
            TcpListener Beerserver = null;
            try
            {
                // Set the TcpListener on port 13000.
                Int32 port = 4646;
                IPAddress localAddr = IPAddress.Parse("127.0.0.1");

                // TcpListener server = new TcpListener(port);
                Beerserver = new TcpListener(localAddr, port);

                Beerserver.Start();
                while (true)
                {
                    Console.WriteLine("Waiting for a connection...");

                    TcpClient client = Beerserver.AcceptTcpClient();
                    Console.WriteLine("Connected!");



                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream();

                    Stream ns = client.GetStream();

                    StreamReader sr = new StreamReader(ns);
                    StreamWriter sw = new StreamWriter(ns);
                    sw.AutoFlush = true;

                    string message = sr.ReadLine();
                    

                    Console.WriteLine(value: "Client: " + message);

                    if (message.Equals("Getall"))
                    {
                        sw.WriteLine("Getall");
                        sw.WriteLine(JsonConvert.SerializeObject(Beers).ToString());
                    }
                    else if (message.Equals("Getbyid"))
                    {
                        sw.WriteLine("Getbyid");
                        sw.WriteLine("Write you Prefered ID");
                        String lineid = sr.ReadLine();
                        int number = Int32.Parse(lineid);
                        sw.WriteLine(JsonConvert.SerializeObject(Beers.Find(Beers => Beers.ID==number)));
                    }
                    else if (message.Equals("Save"))
                    {
                        sw.WriteLine("Save");
                        sw.WriteLine("Write a new Beer");

                        string Beer = sr.ReadLine();

                        Beers.Add(JsonConvert.DeserializeObject<Beer>(Beer));
                    }
                    

                    else
                    {
                        sw.WriteLine("Function not recognized");
                    }
                    
                    client.Close();
                    Console.WriteLine("it has stopped");


                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }