Add all controllers
This commit is contained in:
91
Controllers/BreedersController.cs
Normal file
91
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -78,7 +78,7 @@ namespace GerbilManager.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
public ActionResult DeleteItem(Guid id)
|
public ActionResult DeleteGerbil(Guid id)
|
||||||
{
|
{
|
||||||
var existingGerbil = repository.GetGerbil(id);
|
var existingGerbil = repository.GetGerbil(id);
|
||||||
|
|
||||||
|
|||||||
99
Controllers/LittersController.cs
Normal file
99
Controllers/LittersController.cs
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
using GerbilManager.Models;
|
||||||
|
using GerbilManager.Repositories;
|
||||||
|
using GerbilManager.Dtos;
|
||||||
|
|
||||||
|
using GerbilManager.Converter;
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Xml;
|
||||||
|
|
||||||
|
namespace GerbilManager.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("litters")]
|
||||||
|
public class LittersController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly IGerbilRepository gerbilrepository;
|
||||||
|
private readonly IBreederRepository breederRepository;
|
||||||
|
private readonly ILitterRepository repository;
|
||||||
|
|
||||||
|
public LittersController(ILitterRepository repo)
|
||||||
|
{
|
||||||
|
this.repository = repo;
|
||||||
|
}
|
||||||
|
|
||||||
|
//GET /gerbils
|
||||||
|
[HttpGet]
|
||||||
|
public IEnumerable<LitterDto> GetLitters()
|
||||||
|
{
|
||||||
|
var items = repository.GetLitters().Select( litter => litter.AsDto());
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
//GET /gerbils
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public ActionResult<LitterDto> GetLitter(Guid id)
|
||||||
|
{
|
||||||
|
var item = repository.GetLitter(id).AsDto();
|
||||||
|
|
||||||
|
if(item is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost()]
|
||||||
|
public ActionResult<LitterDto> CreateLitter(CreateLitterDto dto)
|
||||||
|
{
|
||||||
|
Litter litter = new Litter{
|
||||||
|
Id = Guid.NewGuid(),
|
||||||
|
Date = dto.Date,
|
||||||
|
Father = gerbilrepository.GetGerbil(dto.Father),
|
||||||
|
Mother = gerbilrepository.GetGerbil(dto.Mother),
|
||||||
|
Name = dto.Name,
|
||||||
|
Strength = dto.Strength
|
||||||
|
};
|
||||||
|
|
||||||
|
this.repository.CreateLitter(litter);
|
||||||
|
|
||||||
|
return CreatedAtAction(nameof(GetLitter), new { id = litter.Id}, litter.AsDto());
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
public ActionResult UpdateLitter(Guid id, UpdateLitterDto litterDto)
|
||||||
|
{
|
||||||
|
var existingItem = repository.GetLitter(id);
|
||||||
|
if(existingItem is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
Litter updatedLitter = existingItem with{
|
||||||
|
Date = litterDto.Date,
|
||||||
|
Strength = litterDto.Strength,
|
||||||
|
Father = gerbilrepository.GetGerbil(litterDto.Father),
|
||||||
|
Mother = gerbilrepository.GetGerbil(litterDto.Mother),
|
||||||
|
Name = litterDto.Name
|
||||||
|
};
|
||||||
|
|
||||||
|
repository.UpdateLitter(updatedLitter);
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
public ActionResult DeleteLitter(Guid id)
|
||||||
|
{
|
||||||
|
var existingLitter = repository.GetLitter(id);
|
||||||
|
|
||||||
|
if(existingLitter is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
repository.DeleteLitter(existingLitter);
|
||||||
|
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
8
Dtos/UpdateBreederDto.cs
Normal file
8
Dtos/UpdateBreederDto.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
namespace GerbilManager.Dtos
|
||||||
|
{
|
||||||
|
public record UpdateBreederDto
|
||||||
|
{
|
||||||
|
public string Name { get; init; }
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Dtos/UpdateLitterDto.cs
Normal file
12
Dtos/UpdateLitterDto.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
namespace GerbilManager.Dtos
|
||||||
|
{
|
||||||
|
public record UpdateLitterDto
|
||||||
|
{
|
||||||
|
public string Name { get; init; }
|
||||||
|
public DateTime Date { get; init; }
|
||||||
|
public int Strength { get; init; }
|
||||||
|
public Guid Father { get; init; }
|
||||||
|
public Guid Mother { get; init; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,5 +9,9 @@ namespace GerbilManager.Repositories
|
|||||||
Breeder GetBreeder(Guid id);
|
Breeder GetBreeder(Guid id);
|
||||||
|
|
||||||
void CreateBreeder(Breeder breeder);
|
void CreateBreeder(Breeder breeder);
|
||||||
|
|
||||||
|
void UpdateBreeder(Breeder breeder);
|
||||||
|
|
||||||
|
void DeleteBreeder(Breeder breeder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,8 @@ namespace GerbilManager.Repositories
|
|||||||
|
|
||||||
Litter GetLitter(Guid id);
|
Litter GetLitter(Guid id);
|
||||||
|
|
||||||
void CreateLitter(Litter breeder);
|
void CreateLitter(Litter litter);
|
||||||
|
void UpdateLitter(Litter litter);
|
||||||
|
void DeleteLitter(Litter litter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user