public SellOfferViewModel(FilterViewModel filter, UserWrapper user, UserAuthenticationWrapper authenticationUser) { _authenticationUser = authenticationUser; _user = user; _filter = filter; _products = new BindableCollection <ProductWrapper>(); SellOffers = new BindableCollection <SellOfferWrapper>(); CreatedOffer = SellOfferWrapper.CreateSellOffer(_user); UpdateCommand = new AsyncRelayCommand(execute => UpdateOffer(), canExecute => CanModifyOffer()); DeleteCommand = new AsyncRelayCommand(execute => DeleteOffer(), canExecute => CanModifyOffer()); }
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; }