public async Task DeleteOffer() { if (SelectedOffer != null) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); SellOfferDto content = createOffer(_selectedOffer); var json = Newtonsoft.Json.JsonConvert.SerializeObject(content); URLBuilder url = new URLBuilder(controler); var request = new HttpRequestMessage() { RequestUri = new Uri(url.URL), Method = HttpMethod.Delete, Content = new StringContent(json, Encoding.UTF8, "application/json") }; 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); if (!response.IsSuccessStatusCode) { ErrorString = (string)Application.Current.FindResource("DeleteProductError"); return; } await Load(); } ErrorString = null; } }
public async Task <IHttpActionResult> Post([FromBody] SellOfferDto dto) { if (ModelState.IsValid == false) { return(BadRequest("Invalid data")); } if (dto.SellerId != (Thread.CurrentPrincipal as UserPrincipal).Id) { return(Unauthorized()); } var offer = _sellAssembler.DtoToEntity(dto); var result = await _service.Add(offer); if (result == ErrorValue.ServerError) { return(BadRequest("Transaction error")); } else if (result == ErrorValue.AmountGreaterThanStock) { return(BadRequest("Amount greater than stock")); } dto = _sellAssembler.EntityToDto(offer); return(Ok(dto)); }
public async Task UpdateOffer() { using (var client = new HttpClient()) { FilterViewModel filter = new FilterViewModel(_filter); filter.clear(); filter.Name = _selectedOffer.Product.Name; filter.SellerId = _authenticationUser.UserId; filter.Stock = "1"; URLBuilder url = new URLBuilder(filter, "/api//Product/"); 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 <ProductDto> result = JsonConvert.DeserializeObject <List <ProductDto> >(contents); if (result.Count() == 1) { if (result.First().Stock < _selectedOffer.Amount) { ErrorString = (string)Application.Current.FindResource("StockError"); return; } } client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); SellOfferDto content = createOffer(_selectedOffer); var json = Newtonsoft.Json.JsonConvert.SerializeObject(content); url = new URLBuilder(controler); var request2 = new HttpRequestMessage() { RequestUri = new Uri(url.URL), Method = HttpMethod.Put, 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 response2 = await client.SendAsync(request2); if (!response2.IsSuccessStatusCode) { ErrorString = (string)Application.Current.FindResource("InvalidBuyOfferError"); return; } } await Load(); ErrorString = null; }
public async Task Accept() { string rating = ""; RateWindow win = new RateWindow(); win.ShowDialog(); rating = win.Rating; BuyOfferDto bOffer = new BuyOfferDto(); bOffer.Id = 0; bOffer.BuyerId = _authenticationUser.UserId; bOffer.Price = (decimal?)SelectedOffer.Price; bOffer.Amount = SelectedOffer.Amount; bOffer.Name = "a"; bOffer.ProductId = SelectedOffer.ProductId; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); SellOfferDto sellOffer = createOffer(SelectedOffer); TransactionDto transaction = new TransactionDto(); transaction.BuyOffer = bOffer; transaction.SellOffer = sellOffer; if (rating == "") { transaction.Rating = null; } else { transaction.Rating = Convert.ToInt32(rating); } var json = Newtonsoft.Json.JsonConvert.SerializeObject(transaction); var url = new URLBuilder("/AcceptSellTransaction/"); 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 response = await client.SendAsync(request2); if (!response.IsSuccessStatusCode) { ErrorString = (string)Application.Current.FindResource("TransactionError"); return; } Offers.Remove(SelectedOffer); } }
SellOfferDto createOffer(SellOfferWrapper offer) { SellOfferDto wrap = new SellOfferDto(); wrap.Id = offer.Id; wrap.SellerId = _authenticationUser.UserId; wrap.Price = offer.Price; wrap.Amount = offer.Amount; wrap.Name = offer.Name; wrap.ProductId = offer.ProductId; return(wrap); }
public async Task <IHttpActionResult> Delete([FromBody] SellOfferDto dto) { if (ModelState.IsValid == false) { return(BadRequest("Invalid data")); } if (dto.SellerId != (Thread.CurrentPrincipal as UserPrincipal).Id) { return(Unauthorized()); } var offer = _sellAssembler.DtoToEntity(dto); var result = await _service.Delete(offer); if (result == ErrorValue.ServerError) { return(BadRequest("Entity not found")); } return(Ok()); }
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; }