Move webapi to sub folder
This commit is contained in:
91
GerbilManagerWebAPI/Controllers/BreedersController.cs
Normal file
91
GerbilManagerWebAPI/Controllers/BreedersController.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using GerbilManager.Models;
|
||||
using GerbilManager.Repositories;
|
||||
using GerbilManager.Dtos;
|
||||
|
||||
using GerbilManager.Converter;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Xml;
|
||||
|
||||
namespace GerbilManager.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("breeders")]
|
||||
public class BreederController : ControllerBase
|
||||
{
|
||||
private readonly IGerbilRepository gerbilrepository;
|
||||
private readonly IBreederRepository repository;
|
||||
private readonly ILitterRepository litterRepository;
|
||||
|
||||
public BreederController(IBreederRepository repo)
|
||||
{
|
||||
this.repository = repo;
|
||||
}
|
||||
|
||||
//GET /gerbils
|
||||
[HttpGet]
|
||||
public IEnumerable<BreederDto> GetBreeders()
|
||||
{
|
||||
var items = repository.GetBreeders().Select( breeder => breeder.AsDto());
|
||||
return items;
|
||||
}
|
||||
|
||||
//GET /gerbils
|
||||
[HttpGet("{id}")]
|
||||
public ActionResult<BreederDto> GetBreeder(Guid id)
|
||||
{
|
||||
var item = repository.GetBreeder(id).AsDto();
|
||||
|
||||
if(item is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
[HttpPost()]
|
||||
public ActionResult<BreederDto> CreateBreeder(CreateBreederDto dto)
|
||||
{
|
||||
Breeder breeder = new Breeder{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = dto.Name,
|
||||
};
|
||||
|
||||
this.repository.CreateBreeder(breeder);
|
||||
|
||||
return CreatedAtAction(nameof(GetBreeder), new { id = breeder.Id}, breeder.AsDto());
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
public ActionResult UpdateBreeder(Guid id, UpdateBreederDto breederDto)
|
||||
{
|
||||
var existingItem = repository.GetBreeder(id);
|
||||
if(existingItem is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
Breeder updatedBreeder = existingItem with{
|
||||
Name = breederDto.Name
|
||||
};
|
||||
|
||||
repository.UpdateBreeder(updatedBreeder);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public ActionResult DeleteBreeder(Guid id)
|
||||
{
|
||||
var existingLitter = repository.GetBreeder(id);
|
||||
|
||||
if(existingLitter is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
repository.DeleteBreeder(existingLitter);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user