diff --git a/Controllers/GerbilsController.cs b/Controllers/GerbilsController.cs index 2417675..a659119 100644 --- a/Controllers/GerbilsController.cs +++ b/Controllers/GerbilsController.cs @@ -8,11 +8,11 @@ namespace GerbilManager.Controllers [Route("gerbils")] public class GerbilsController : ControllerBase { - private readonly InMemGerbilRepository repository; + private readonly IGerbilRepository repository; - public GerbilsController() + public GerbilsController(IGerbilRepository repo) { - repository = new InMemGerbilRepository(); + this.repository = repo; } //GET /gerbils @@ -22,5 +22,18 @@ namespace GerbilManager.Controllers var items = repository.GetGerbils(); return items; } + + //GET /gerbils + [HttpGet("{id}")] + public ActionResult GetGerbil(Guid id) + { + var item = repository.GetGerbil(id); + + if(item is null) + { + return NotFound(); + } + return item; + } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 15eacee..8ad3f5f 100644 --- a/Program.cs +++ b/Program.cs @@ -1,3 +1,5 @@ +using GerbilManager.Repositories; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -7,6 +9,8 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddSingleton(); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/Repositories/IGerbilRepository.cs b/Repositories/IGerbilRepository.cs new file mode 100644 index 0000000..3ede4c7 --- /dev/null +++ b/Repositories/IGerbilRepository.cs @@ -0,0 +1,11 @@ +using GerbilManager.Models; + +namespace GerbilManager.Repositories +{ + public interface IGerbilRepository + { + IEnumerable GetGerbils(); + + Gerbil GetGerbil(Guid id); + } +} \ No newline at end of file diff --git a/Repositories/InMemGerbilRepository.cs b/Repositories/InMemGerbilRepository.cs index 58bbaff..fd2ebbf 100644 --- a/Repositories/InMemGerbilRepository.cs +++ b/Repositories/InMemGerbilRepository.cs @@ -2,8 +2,7 @@ using GerbilManager.Models; namespace GerbilManager.Repositories { - - public class InMemGerbilRepository + public class InMemGerbilRepository : IGerbilRepository { private readonly List _gerbilList = new() { @@ -19,5 +18,5 @@ namespace GerbilManager.Repositories public Gerbil GetGerbil(Guid id) => _gerbilList.SingleOrDefault(x => x.Id == id); } - + }