lmplement all actions
This commit is contained in:
@@ -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<GerbilDto> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user