I try to update local database in UWP
app by WEB API
service.
I need send list of existing entity ID to Web service and get other entity from service.
my model
public class LibraryCategoryModel
{
public int ID { get; set; }
public string CategoryTitle { get; set; }
public byte[] Picture { get; set; }
}
and my controller
in web api
// GET: api/Category
public IQueryable<LibraryCategoryModel> GetLibraryCategoryModels(HttpRequestMessage request)
{
var string1 = request.Content.ReadAsStringAsync();
List<int> existingIdCategoryList = new List<int>();
existingIdCategoryList = JsonConvert.DeserializeObject<List<int>>(string1.Result);
var entityList = db.LibraryCategoryModels.ToList();
foreach (var Id in existingIdCategoryList)
{
entityList = entityList.Where(item => item.ID != Id).ToList();
}
return entityList as IQueryable<LibraryCategoryModel>;
}
and my request
private async void LoadDataFromNetwork(List<int> CategoryIdList)
{
if (NetworkHelper.IsInternet() || NetworkHelper.HasInternet())
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(serviceUrl);
// Set requast
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "Api/Category"))
{
// Set the Accept header for BSON.
request.Headers.Accept.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/bson"));
string Content = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(CategoryIdList));
request.Content = new StringContent(Content, System.Text.Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
// Use BSON formatter to deserialize the result.
MediaTypeFormatter[] formatters = new MediaTypeFormatter[] { new BsonMediaTypeFormatter() };
try
{
var newCategorys = await response.Content.ReadAsAsync<LibraryCategoryModel[]>(formatters);
foreach (var category in newCategorys)
{
_dataService.InsertCategory(category);
AllCategoryList.Add(new LibraryCategoryViewModel(category));
_newCategoryIdListFromNetWork.Add(category.ID);
}
}
catch (Exception)
{
throw;
}
}
}
}
}
this work for me but i do not Sure this way is proper and best solution. please guide me.
First off, if it works and there are no specific issues you're encountering then it may be good enough.
There are lots of ways this could potentially be improved but definitions of a "proper way" or "best solution" will always be opinion based.
That said, here are some suggestions:
LibraryCategoryModel
is redundant. I'd remove it.string1
is a poor name for a variable as it has no meaning. Use something that describes what it holds, such as requestContent
.List<int>
directly then you can do away with needing to handle any unexpected input.NetworkHelper
method names don't clearly distinguish between what they each do. Rename them to make their meaning and differences clearer.LoadDataFromNetwork
does a lot and would probably be more maintainable if split into multiple methods: for creating the request, making the request and handling the response._newCategoryIdListFromNetWork
variable looks like it should be more closely tied to this method, not the class and any logic relating to it may be overly distributed around the class.