Trying to sync data from third party api

ajax c# entity-framework-core sql-server


This question has probably been asked correctly before, and I'll gladly accept an answer pointing me to the right spot. The problem is I don't know how to ask the question correctly to get anything returned in a search.

I'm trying to pull data from a 3rd party api (ADP) and store data in my database using core.

I am wanting to take the users returned from the API and store them in my database, where I have an ADP ancillary table seeded with the majority of the data from the api.

I would then like to update or add any missing or altered records in my database FROM the API.

I'm thinking that about using an ajax call to the api to retrieve the records, then either storing the data to another table and using sql to look for records that are changed between the two tables and making any necessary changes(this would be manually activated via a button), or some kind of scheduled background task to perform this through methods in my c# code instead of ajax.

The question I have is:

  1. Is it a better fit to do this as a stored procedure in sql or rather have a method in my web app perform the data transformation.
  2. I'm looking for any examples of iterating through the returned data and updating/creating records in my database.
  3. I've only seen vague not quite what I'm looking for examples and nothing definitive on the best way to accomplish this. If I can find any reference material or examples, I'll gladly research but I don't even know where to start, or the correct terms to search for. I've looked into model binding, ajax calls, json serialization & deserialization. I'm probably overthinking this.

Any suggestions or tech I should look at would be appreciated. Thanks for you time in advance.

My app is written in core 2.2 using EF Core

* EDIT *

For anyone looking -

This with John Wu's Answer helped me achieve what I was looking for.

12/14/2018 9:04:12 PM

Accepted Answer

If this were my project this is how I would break down the tasks, in this order.

First, start an empty console application.

Next, write a method that gets the list of users from the API. You didn't tell us anything at all about the API, so here is a dummy example that uses an HTTP client.

public async Task<List<User>> GetUsers()
    var client = new HttpClient();
    var response = await client.GetAsync("");
    var users = await ParseResponse(response);
    return users.ToList();

Test the above (e.g. write a little shoestring code to run it and dump the results, or something) to ensure that it works independently. You want to make sure it is solid before moving on.

Next, create a temporary table (or tables) that matches the schema of the data objects that are returned from the API. For now you will just want to store it exactly the way you retrieve it.

Next, write some code to insert records into the table(s). Again, test this independently, and review the data in the table to make sure it all worked correctly. It might look a little like this:

public async Task InsertUser(User user)
    using (var conn = new SqlConnection(Configuration.ConnectionString))
        var cmd = new SqlCommand();
        await cmd.ExecuteNonQueryAsync();

Once you know how to pull the data and store it, you can finish the code to extract the data from the API and insert it. It might look a little like this:

public async Task DoTheMigration()
    var users = await GetUsers();
    var tasks = users.Select
        u => InsertUser(u)
    await Task.WhenAll(tasks.ToArray());

As a final step, write a series of stored procedures or a DTS package to move the data from the temp tables to their final resting place. If you are using MS Access, you can write a series of queries and execute them in order with some VBA. At a high level it would:

  1. Check for any records that exist in the temp table but not in the final table and insert them into the final table.

  2. Check for any records that exist in the final table but not the temp table and remove them or mark them as deleted.

  3. Check for any records in common that have different column values and update the final table.

Each of these development activities raises it own set of questions, of course, which you can post back to StackOverflow with details. As it is your question doesn't have enough specificity for a more in-depth answer.

12/12/2018 12:46:10 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow