diff --git a/Controllers/BreedersController.cs b/Controllers/BreedersController.cs new file mode 100644 index 0000000..a81b07f --- /dev/null +++ b/Controllers/BreedersController.cs @@ -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 GetBreeders() + { + var items = repository.GetBreeders().Select( breeder => breeder.AsDto()); + return items; + } + + //GET /gerbils + [HttpGet("{id}")] + public ActionResult GetBreeder(Guid id) + { + var item = repository.GetBreeder(id).AsDto(); + + if(item is null) + { + return NotFound(); + } + return item; + } + + [HttpPost()] + public ActionResult 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(); + } + } +} \ No newline at end of file diff --git a/Controllers/GerbilsController.cs b/Controllers/GerbilsController.cs index 505e82d..fad236b 100644 --- a/Controllers/GerbilsController.cs +++ b/Controllers/GerbilsController.cs @@ -78,7 +78,7 @@ namespace GerbilManager.Controllers } [HttpDelete("{id}")] - public ActionResult DeleteItem(Guid id) + public ActionResult DeleteGerbil(Guid id) { var existingGerbil = repository.GetGerbil(id); diff --git a/Controllers/LittersController.cs b/Controllers/LittersController.cs new file mode 100644 index 0000000..79d410e --- /dev/null +++ b/Controllers/LittersController.cs @@ -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 GetLitters() + { + var items = repository.GetLitters().Select( litter => litter.AsDto()); + return items; + } + + //GET /gerbils + [HttpGet("{id}")] + public ActionResult GetLitter(Guid id) + { + var item = repository.GetLitter(id).AsDto(); + + if(item is null) + { + return NotFound(); + } + return item; + } + + [HttpPost()] + public ActionResult 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(); + } + } +} \ No newline at end of file diff --git a/Dtos/UpdateBreederDto.cs b/Dtos/UpdateBreederDto.cs new file mode 100644 index 0000000..bc98059 --- /dev/null +++ b/Dtos/UpdateBreederDto.cs @@ -0,0 +1,8 @@ + +namespace GerbilManager.Dtos +{ + public record UpdateBreederDto + { + public string Name { get; init; } + } +} \ No newline at end of file diff --git a/Dtos/UpdateLitterDto.cs b/Dtos/UpdateLitterDto.cs new file mode 100644 index 0000000..b5e584e --- /dev/null +++ b/Dtos/UpdateLitterDto.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Repositories/IBreederRepository.cs b/Repositories/IBreederRepository.cs index c3d068d..64a1163 100644 --- a/Repositories/IBreederRepository.cs +++ b/Repositories/IBreederRepository.cs @@ -9,5 +9,9 @@ namespace GerbilManager.Repositories Breeder GetBreeder(Guid id); void CreateBreeder(Breeder breeder); + + void UpdateBreeder(Breeder breeder); + + void DeleteBreeder(Breeder breeder); } } \ No newline at end of file diff --git a/Repositories/ILitterRepostitory.cs b/Repositories/ILitterRepostitory.cs index 6f30331..d330098 100644 --- a/Repositories/ILitterRepostitory.cs +++ b/Repositories/ILitterRepostitory.cs @@ -8,6 +8,8 @@ namespace GerbilManager.Repositories Litter GetLitter(Guid id); - void CreateLitter(Litter breeder); + void CreateLitter(Litter litter); + void UpdateLitter(Litter litter); + void DeleteLitter(Litter litter); } } \ No newline at end of file