92 lines
2.4 KiB
C#
92 lines
2.4 KiB
C#
using GerbilManagerWebAPI.Models;
|
|
using GerbilManagerWebAPI.DAL;
|
|
using GerbilManagerWebAPI.Dtos;
|
|
|
|
using GerbilManagerWebAPI.Converter;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Xml;
|
|
|
|
namespace GerbilManagerWebAPI.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("breeders")]
|
|
public class BreederController : ControllerBase
|
|
{
|
|
private readonly UnitOfWork unitOfWork;
|
|
|
|
public BreederController(UnitOfWork unitOfWork)
|
|
{
|
|
this.unitOfWork = unitOfWork;
|
|
}
|
|
|
|
//GET /gerbils
|
|
[HttpGet]
|
|
public IEnumerable<BreederDto> GetBreeders()
|
|
{
|
|
var items = unitOfWork.BreederRepository.Get().Select( breeder => breeder.AsDto());
|
|
return items;
|
|
}
|
|
|
|
//GET /gerbils
|
|
[HttpGet("{id}")]
|
|
public ActionResult<BreederDto> GetBreeder(Guid id)
|
|
{
|
|
var item = unitOfWork.BreederRepository.GetByID(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.unitOfWork.BreederRepository.Insert(breeder);
|
|
this.unitOfWork.Save();
|
|
|
|
return CreatedAtAction(nameof(GetBreeder), new { id = breeder.Id}, breeder.AsDto());
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
public ActionResult UpdateBreeder(Guid id, UpdateBreederDto breederDto)
|
|
{
|
|
var existingItem = unitOfWork.BreederRepository.GetByID(id);
|
|
if(existingItem is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
Breeder updatedBreeder = existingItem with{
|
|
Name = breederDto.Name
|
|
};
|
|
|
|
unitOfWork.BreederRepository.Update(updatedBreeder);
|
|
this.unitOfWork.Save();
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
public ActionResult DeleteBreeder(Guid id)
|
|
{
|
|
var existingLitter = unitOfWork.BreederRepository.GetByID(id);
|
|
|
|
if(existingLitter is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
unitOfWork.BreederRepository.Delete(existingLitter);
|
|
this.unitOfWork.Save();
|
|
|
|
return NoContent();
|
|
}
|
|
}
|
|
} |