public MainViewModel()
        {
            _authenticationVM = new AuthenticationViewModel();
            _authenticationVM.Authentication += GoToProductVM;
            _filter = new FilterViewModel();

            BuyOfferVMCommand        = new AsyncRelayCommand(execute => GoToBuyOfferVM(), canExecute => { return(true); });
            SellOfferVMCommand       = new AsyncRelayCommand(execute => GoToSellOfferVM(), canExecute => { return(true); });
            ProductVMCommand         = new AsyncRelayCommand(execute => GoToProductVM(null, null), canExecute => { return(true); });
            SearchCommand            = new AsyncRelayCommand(execute => Search(), canExecute => { return(true); });
            BuyTransactionVMCommand  = new AsyncRelayCommand(execute => GoToBuyTransactionVM(), canExecute => { return(true); });
            SellTransactionVMCommand = new AsyncRelayCommand(execute => GoToSellTransactionVM(), canExecute => { return(true); });
            ManageAccountVMCommand   = new AsyncRelayCommand(execute => GoToManageAccountVM(), canExecute => { return(true); });

            LogoutCommand = new RelayCommand(execute => Logout(), canExecute => { return(true); });

            IsUserAuthenticated = false;
            DisplayedView       = _authenticationVM;
        }
Example #2
0
        public async Task AddOffer()
        {
            if (CreatedOffer.Name == null || CreatedOffer.Product == null ||
                CreatedOffer.Amount <= 0 || CreatedOffer?.Price <= 0)
            {
                CreatedOffer = SellOfferWrapper.CreateSellOffer(_user);
                ErrorString  = (string)Application.Current.FindResource("InvalidSellOfferError");
                return;
            }
            using (var client = new HttpClient())
            {
                CreatedOffer.ProductId = CreatedOffer.Product.Id;
                FilterViewModel filter = new FilterViewModel(_filter);
                filter.Name = _createdOffer.Product.Name;
                URLBuilder url = new URLBuilder(filter, controler);
                url.URL += "&ShowMyOffers=true";
                var request = new HttpRequestMessage()
                {
                    RequestUri = new Uri(url.URL),
                    Method     = HttpMethod.Get
                };
                request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", _authenticationUser.UserId.ToString(), _authenticationUser.Password))));
                var response = await client.SendAsync(request);

                var contents = await response.Content.ReadAsStringAsync();

                List <SellOfferDto> result = JsonConvert.DeserializeObject <List <SellOfferDto> >(contents);
                var totalAmount            = result.Sum(offer => offer.Amount);
                if (CreatedOffer.Amount + totalAmount > CreatedOffer.Product.Stock)
                {
                    CreatedOffer = SellOfferWrapper.CreateSellOffer(_user);
                    ErrorString  = (string)Application.Current.FindResource("StockError");
                    return;
                }
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                SellOfferDto content = createOffer(_createdOffer);

                var json = Newtonsoft.Json.JsonConvert.SerializeObject(content);
                url = new URLBuilder(controler);
                var request2 = new HttpRequestMessage()
                {
                    RequestUri = new Uri(url.URL),
                    Method     = HttpMethod.Post,
                    Content    = new StringContent(json,
                                                   Encoding.UTF8,
                                                   "application/json")
                };
                request2.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", _authenticationUser.UserId.ToString(), _authenticationUser.Password))));
                var response1 = await client.SendAsync(request2);

                if (response.IsSuccessStatusCode)
                {
                    await Load();

                    _createdOffer = SellOfferWrapper.CreateSellOffer(_user);
                }
                else
                {
                    ErrorString = (string)Application.Current.FindResource("InsertSellOfferError");
                    return;
                }
            }
            ErrorString = null;
        }