Browse Source

flexiblere Umsetzung des LendControllers durch Depedency-Injection

ControllerJonas
Jonas Schönbach 2 years ago
parent
commit
1c8ec4b517
  1. 11
      Controllers/LendController.cs
  2. 6
      Startup.cs

11
Controllers/LendController.cs

@ -13,13 +13,18 @@ namespace BuecherwurmAPI.Controllers
[ApiController]
public class LendController : ControllerBase
{
private readonly MockLendRepo _mockLendRepo = new MockLendRepo();
private ILendRepo _repository;
public LendController(ILendRepo repository)
{
_repository = repository;
}
//GET api/leihvorgang/
[HttpGet]
public ActionResult<IEnumerable<Lend>> GetAllLends()
{
var lends = _mockLendRepo.GetAllLends();
var lends = _repository.GetAllLends();
return Ok(lends);
}
@ -27,7 +32,7 @@ namespace BuecherwurmAPI.Controllers
[HttpGet("{id}")]
public ActionResult<Lend> GetLend(int id)
{
var lend = _mockLendRepo.GetLendById(id);
var lend = _repository.GetLendById(id);
return Ok(lend);
}
}

6
Startup.cs

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BuecherwurmAPI.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
@ -26,6 +27,11 @@ namespace BuecherwurmAPI
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Adds a service that is created once per connection.
// It takes an interface and a specific implementation.
// That allows to swap the implementation easily.
services.AddScoped<ILendRepo, MockLendRepo>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

Loading…
Cancel
Save