diff --git a/Controllers/GerbilsController.cs b/Controllers/GerbilsController.cs index 33813e6..505e82d 100644 --- a/Controllers/GerbilsController.cs +++ b/Controllers/GerbilsController.cs @@ -12,6 +12,8 @@ namespace GerbilManager.Controllers public class GerbilsController : ControllerBase { private readonly IGerbilRepository repository; + private readonly IBreederRepository breederRepository; + private readonly ILitterRepository litterRepository; public GerbilsController(IGerbilRepository repo) { @@ -38,5 +40,56 @@ namespace GerbilManager.Controllers } return item; } + + [HttpPost()] + public ActionResult CreateGerbil(CreateGerbilDto dto) + { + Gerbil gerbil = new Gerbil{ + Id = Guid.NewGuid(), + Name = dto.Name, + Breeder = breederRepository.GetBreeder(dto.Breeder), + Litter = litterRepository.GetLitter(dto.Litter), + Gender = (Gender) (int) dto.Gender + }; + + this.repository.CreateGerbil(gerbil); + + return CreatedAtAction(nameof(GetGerbil), new { id = gerbil.Id}, gerbil.AsDto()); + } + + [HttpPut("{id}")] + public ActionResult UpdateGerbil(Guid id, UpdateGerbilDto gerbilDto) + { + var existingItem = repository.GetGerbil(id); + if(existingItem is null) + { + return NotFound(); + } + + Gerbil updatedGerbil = existingItem with{ + Breeder = breederRepository.GetBreeder(gerbilDto.Breeder.Id), + Litter = litterRepository.GetLitter(gerbilDto.Litter.Id), + Gender = (Gender) (int) gerbilDto.Gender, + Name = gerbilDto.Name + }; + + repository.UpdateGerbil(updatedGerbil); + return NoContent(); + } + + [HttpDelete("{id}")] + public ActionResult DeleteItem(Guid id) + { + var existingGerbil = repository.GetGerbil(id); + + if(existingGerbil is null) + { + return NotFound(); + } + + repository.DeleteGerbil(existingGerbil); + + return NoContent(); + } } } \ No newline at end of file diff --git a/Converter/BreederConveter.cs b/Converter/BreederConveter.cs index 5b8f936..25aea18 100644 --- a/Converter/BreederConveter.cs +++ b/Converter/BreederConveter.cs @@ -11,7 +11,8 @@ namespace GerbilManager.Converter Id = breeder.Id, Name = breeder.Name }; - } + } + } } diff --git a/Dtos/CreateBreederDto.cs b/Dtos/CreateBreederDto.cs new file mode 100644 index 0000000..a4616ae --- /dev/null +++ b/Dtos/CreateBreederDto.cs @@ -0,0 +1,7 @@ +namespace GerbilManager.Dtos +{ + public record CreateBreederDto + { + public string Name { get; init; } + } +} \ No newline at end of file diff --git a/Dtos/CreateGerbilDto.cs b/Dtos/CreateGerbilDto.cs new file mode 100644 index 0000000..8654756 --- /dev/null +++ b/Dtos/CreateGerbilDto.cs @@ -0,0 +1,11 @@ +namespace GerbilManager.Dtos +{ + public record CreateGerbilDto + { + public string Name { get; init; } + public GenderDto Gender { get; init; } + public Guid Litter { get; init; } + public Guid Breeder { get; init; } + } + +} \ No newline at end of file diff --git a/Dtos/CreateLitterDto.cs b/Dtos/CreateLitterDto.cs new file mode 100644 index 0000000..4465aa7 --- /dev/null +++ b/Dtos/CreateLitterDto.cs @@ -0,0 +1,11 @@ +namespace GerbilManager.Dtos +{ + public record CreateLitterDto + { + 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/Dtos/UpdateGerbilDto.cs b/Dtos/UpdateGerbilDto.cs new file mode 100644 index 0000000..8d68301 --- /dev/null +++ b/Dtos/UpdateGerbilDto.cs @@ -0,0 +1,10 @@ +namespace GerbilManager.Dtos +{ + public record UpdateGerbilDto + { + public string Name { get; init; } + public GenderDto Gender { get; init; } + public LitterDto Litter { get; init; } + public BreederDto Breeder { get; init; } + } +} \ No newline at end of file diff --git a/Repositories/IBreederRepository.cs b/Repositories/IBreederRepository.cs new file mode 100644 index 0000000..c3d068d --- /dev/null +++ b/Repositories/IBreederRepository.cs @@ -0,0 +1,13 @@ +using GerbilManager.Models; + +namespace GerbilManager.Repositories +{ + public interface IBreederRepository + { + IEnumerable GetBreeders(); + + Breeder GetBreeder(Guid id); + + void CreateBreeder(Breeder breeder); + } +} \ No newline at end of file diff --git a/Repositories/IGerbilRepository.cs b/Repositories/IGerbilRepository.cs index 3ede4c7..320d09c 100644 --- a/Repositories/IGerbilRepository.cs +++ b/Repositories/IGerbilRepository.cs @@ -7,5 +7,11 @@ namespace GerbilManager.Repositories IEnumerable GetGerbils(); Gerbil GetGerbil(Guid id); + + void CreateGerbil(Gerbil gerbil); + + void UpdateGerbil(Gerbil gerbil); + + void DeleteGerbil(Gerbil gerilb); } } \ No newline at end of file diff --git a/Repositories/ILitterRepostitory.cs b/Repositories/ILitterRepostitory.cs new file mode 100644 index 0000000..6f30331 --- /dev/null +++ b/Repositories/ILitterRepostitory.cs @@ -0,0 +1,13 @@ +using GerbilManager.Models; + +namespace GerbilManager.Repositories +{ + public interface ILitterRepository + { + IEnumerable GetLitters(); + + Litter GetLitter(Guid id); + + void CreateLitter(Litter breeder); + } +} \ No newline at end of file diff --git a/Repositories/InMemGerbilRepository.cs b/Repositories/InMemGerbilRepository.cs index fd2ebbf..d55bc5c 100644 --- a/Repositories/InMemGerbilRepository.cs +++ b/Repositories/InMemGerbilRepository.cs @@ -17,6 +17,23 @@ namespace GerbilManager.Repositories } public Gerbil GetGerbil(Guid id) => _gerbilList.SingleOrDefault(x => x.Id == id); + + public void CreateGerbil(Gerbil gerbil) + { + _gerbilList.Add(gerbil); + } + + public void UpdateGerbil(Gerbil gerbil) + { + var index = this._gerbilList.FindIndex(existingGerbil => existingGerbil.Id == gerbil.Id); + _gerbilList[index] = gerbil; + } + + public void DeleteGerbil(Gerbil gerbil) + { + var index = this._gerbilList.FindIndex(existingGerbil => existingGerbil.Id == gerbil.Id); + _gerbilList.RemoveAt(index); + } } }