//24 public async Task <AdvertResponse> Create(CreateAdvertModel model) { //var advertApiModel = new AdvertModel(); // mapper var advertApiModel = _mapper.Map <AdvertModel>(model); // 26 var jsonModel = JsonConvert.SerializeObject(advertApiModel); // shoul use mapper (model) //_client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); ////_client.setRequestHeader("accept-charset", "UTF-8"); //_client.DefaultRequestHeaders.Add("accept-charset", "UTF-8"); //string _ContentType = "application/json"; //_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_ContentType)); // var response = await _client.PostAsync(_client.BaseAddress, new StringContent(jsonModel)).ConfigureAwait(false); var response = await _client.PostAsync(new Uri($"{_baseAddress}/create"), new StringContent(jsonModel, Encoding.UTF8, "application/json")).ConfigureAwait(false); var responseJson = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var createAdvertResponse = JsonConvert.DeserializeObject <CreateAdvertResponse>(responseJson); //var advertResponse = new AdvertResponse(); // Automapper var advertResponse = _mapper.Map <AdvertResponse>(createAdvertResponse); // 26 return(advertResponse); }
public async Task <AdvertResponse> Create(CreateAdvertModel model) { var advertApiModel = _mapper.Map <AdvertModel>(model); var jsonModel = JsonConvert.SerializeObject(advertApiModel); var response = await _client.PostAsync(new Uri($"{_client.BaseAddress}/create"), new StringContent(jsonModel, Encoding.UTF8, "application/json")); var responseJson = await response.Content.ReadAsStringAsync(); var createAdvertResponse = JsonConvert.DeserializeObject <CreateAdvertResponse>(responseJson); return(_mapper.Map <AdvertResponse>(createAdvertResponse)); }
public async Task <AdvertResponse> CreateAsync(CreateAdvertModel model) { var advertModel = _mapper.Map <AdvertModel>(model); var jsonModel = JsonConvert.SerializeObject(advertModel); var response = await _httpClient.PostAsync($"{_httpClient.BaseAddress}/create", new StringContent(jsonModel)); var responseJson = await response.Content.ReadAsStringAsync(); var createAdvertResponse = JsonConvert.DeserializeObject <CreateAdvertResponse>(responseJson); var advertResponse = _mapper.Map <AdvertResponse>(createAdvertResponse); return(advertResponse); }
public async Task <CreateAdvertResponse> CreateAsync(CreateAdvertModel model) { var advertApiModel = _mapper.Map <AdvertModel>(model); var jsonModel = JsonConvert.SerializeObject(advertApiModel); var response = await _client.PostAsync(new Uri($"{_baseAddress}/create"), new StringContent(jsonModel, Encoding.UTF8, "application/json")).ConfigureAwait(false); var createAdvertResponse = await response.Content.ReadAsAsync <CreateAdvertResponse>().ConfigureAwait(false); var advertResponse = _mapper.Map <CreateAdvertResponse>(createAdvertResponse); return(advertResponse); }
public async Task <AdvertResponse> Create(CreateAdvertModel model) { var advertApiModel = mapper.Map <AdvertModel>(model);//new AdvertModel(); //Automapper var jsonModel = JsonConvert.SerializeObject(advertApiModel); var response = await client.PostAsync(new Uri($"{client.BaseAddress}/Create"), new StringContent(jsonModel, encoding : Encoding.UTF8, mediaType : "application/json")).ConfigureAwait(false); var responseJson = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var createAdvertResponse = JsonConvert.DeserializeObject <CreateAdvertResponse>(responseJson); var advertResponse = mapper.Map <CreateAdvertResponse, AdvertResponse>(createAdvertResponse); return(advertResponse); }
public async Task<AdvertResponse> Create(CreateAdvertModel model) { AdvertModel advertApiModel = _mapper.Map<AdvertModel>(model); string jsonModel = JsonConvert.SerializeObject(advertApiModel); HttpResponseMessage response = await _client.PostAsync( new Uri($"{_baseAddress}/create"), new StringContent(jsonModel, Encoding.UTF8, "application/json") ); string createAdvertResponse = await response.Content.ReadAsStringAsync(); AdvertResponse jsonResponse = JsonConvert.DeserializeObject<AdvertResponse>(createAdvertResponse); AdvertResponse advertResponse = _mapper.Map<AdvertResponse>(jsonResponse); return advertResponse; }
public async Task <AdvertResponse> Create(CreateAdvertModel model) { // first we need to map the model to model that is understood by AdvertApi our service var advertApiModel = this._mapper.Map <AdvertModel>(model); // automapper // need toserialize the model var jsonModel = JsonConvert.SerializeObject(advertApiModel); var response = await this._client .PostAsync(new Uri($"{this._client.BaseAddress}/create"), new StringContent(jsonModel)) .ConfigureAwait(false); var responseJson = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var createAdvertResponse = JsonConvert.DeserializeObject <CreateAdvertResponse>(responseJson); var advertReponse = this._mapper.Map <AdvertResponse>(createAdvertResponse); // automapper return(advertReponse); }
public async Task <AdvertResponse> Create(CreateAdvertModel model) { try { var advertApiModel = _mapper.Map <AdvertModel>(model); var jsonmodel = JsonConvert.SerializeObject(advertApiModel); var response = await _client.PostAsync(new Uri($"{_client.BaseAddress}/Create"), new StringContent(jsonmodel, Encoding.UTF8, "application/json")).ConfigureAwait(false); var responseJson = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var createAdvertResponse = JsonConvert.DeserializeObject <CreateAdvertResponse>(responseJson); var advertResponse = _mapper.Map <AdvertResponse>(createAdvertResponse); return(advertResponse); } catch (Exception e) { Console.WriteLine(e); throw; } }
public async Task <AdvertResponse> CreateAsync(CreateAdvertModel model, string userName) { // var advertApiModel = _mapper.Map<AdvertModel>(model); var advertApiModel = new AdvertModel2 { Title = model.Title, Description = model.Description, Price = model.Price }; var jsonModel = JsonConvert.SerializeObject(advertApiModel); var response = await _client.PostAsync(new Uri($"{_baseAddress}/Create"), new StringContent(jsonModel, Encoding.UTF8, "application/json")).ConfigureAwait(false); var createAdvertResponse = await response.Content.ReadAsAsync <CreateAdvertResponse>().ConfigureAwait(false); var advertResponse = _mapper.Map <AdvertResponse>(createAdvertResponse); return(advertResponse); }