.NET MVC REST API Calling 2nd API w/Repository Pattern Issues

  Kiến thức lập trình

So, I am trying to create an API that can call another API and store some data in a SQL DB, i.e:

CLIENT -> HTTPGET/POST -> API -> CALLS 2ND API -> STORES DATA IN SQL

I have achieved this with a console app:

using System.Net;
using System.Net.Http.Headers;
using System.Text.Json;
using WebApiClient.Data;
using HttpClient client = new();
  
client.DefaultRequestHeaders.Accept.Clear();



var breweries = await ProcessBreweriesApiAsync(client);
foreach (var brew in breweries)
{
    Console.WriteLine($"id: {brew.Id}");
    Console.WriteLine($"name: {brew.Name}");
    Console.WriteLine($"brewery_type: {brew.BreweryType}");
    // Console.WriteLine($"address_1: {brew.Address1}");
    // Console.WriteLine($"address_2: {brew.Address2}");
    // Console.WriteLine($"address_3: {brew.Address3}");
    // Console.WriteLine($"city: {brew.City}");
    // Console.WriteLine($"state_province: {brew.StateProvince}");
    // Console.WriteLine($"postal_code: {brew.PostalCode}");
    // Console.WriteLine($"longitude: {brew.Longitude}");
    // Console.WriteLine($"latitude: {brew.Latitude}");
    // Console.WriteLine($"phone: {brew.Phone}");
    // Console.WriteLine($"state: {brew.State}");
    // Console.WriteLine($"street: {brew.Street}");
    Console.WriteLine("Saving data into SQL");
    
    var db = new AppDbContext();
    db.Breweries?.AddRange(breweries);
    db.SaveChanges();

    Console.WriteLine("Finished Saving");
    
    
    Console.WriteLine();
}


static async Task<List<BreweryModel>> ProcessBreweriesApiAsync(HttpClient client)
{
    await using Stream stream =
        await client.GetStreamAsync("https://api.openbrewerydb.org/breweries");
    var breweries =
        await JsonSerializer.DeserializeAsync<List<BreweryModel>>(stream);
    return breweries ?? new();
}

However, I’m now trying to implement the same with an MVC API, using a couple of concepts I’d learnt in previous tutorials, such as repository pattern to get accustomed with the concepts, I cannot seem to figure out how to get this built.

I have created a method sig in the interface, then an async implementation of the method, however, this errors that I haven’t implemented the interface, but when I change the interface to ASYNC I am receiving:

The ‘async’ modifier can only be used in methods that have a body.CS1994

**Controller **

[ApiController]
[Route("api/[controller]")]
public class BreweryController : ControllerBase
{
    private readonly IBreweryRepo _repository;
    public BreweryController(
        IBreweryRepo repository
    )
    {
        _repository = repository;
    }



    [HttpGet(Name = "UpdateBreweries")]
      public ActionResult<IEnumerable<BreweryModel>> UpdateBreweries ()
        {
            Console.WriteLine("Updating Breweries Now...");
            var breweries = _repository.ImportBreweries();
            _repository.SaveChanges();
            return Ok (breweries);
        
        }

}

Interface

using XXX.Models;
namespace XXX.Data
{
    public interface IBreweryRepo
    {
        Task ImportBreweries ();
         bool SaveChanges();


    }
}

Implementation

namespace XXX.Data
{
public class BreweryRepo : IBreweryRepo
{
//DI
private readonly AppDbContext _context;
public BreweryRepo(AppDbContext context)
{
_context = context;
}

    //public async IActionResult UpdateBreweries <BreweryModel<List>> (HttpClient client)
    public async Task UpdateBreweries()
    {
        HttpClient client = new HttpClient();
        await using Stream stream =
        await client.GetStreamAsync("https://api.openbrewerydb.org/breweries");
        var breweries =
        await JsonSerializer.DeserializeAsync<List<BreweryModel>>(stream);
        _context.Breweries.AddRange(breweries);
        
    }
    public bool SaveChanges()
    {
        return (_context.SaveChanges() >= 0);
    }

}

}

Cannot seem to get it to load async.

LEAVE A COMMENT