lmplement all actions
This commit is contained in:
@@ -12,6 +12,8 @@ namespace GerbilManager.Controllers
|
|||||||
public class GerbilsController : ControllerBase
|
public class GerbilsController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IGerbilRepository repository;
|
private readonly IGerbilRepository repository;
|
||||||
|
private readonly IBreederRepository breederRepository;
|
||||||
|
private readonly ILitterRepository litterRepository;
|
||||||
|
|
||||||
public GerbilsController(IGerbilRepository repo)
|
public GerbilsController(IGerbilRepository repo)
|
||||||
{
|
{
|
||||||
@@ -38,5 +40,56 @@ namespace GerbilManager.Controllers
|
|||||||
}
|
}
|
||||||
return item;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,8 @@ namespace GerbilManager.Converter
|
|||||||
Id = breeder.Id,
|
Id = breeder.Id,
|
||||||
Name = breeder.Name
|
Name = breeder.Name
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
7
Dtos/CreateBreederDto.cs
Normal file
7
Dtos/CreateBreederDto.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace GerbilManager.Dtos
|
||||||
|
{
|
||||||
|
public record CreateBreederDto
|
||||||
|
{
|
||||||
|
public string Name { get; init; }
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Dtos/CreateGerbilDto.cs
Normal file
11
Dtos/CreateGerbilDto.cs
Normal file
@@ -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; }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
11
Dtos/CreateLitterDto.cs
Normal file
11
Dtos/CreateLitterDto.cs
Normal file
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
10
Dtos/UpdateGerbilDto.cs
Normal file
10
Dtos/UpdateGerbilDto.cs
Normal file
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
13
Repositories/IBreederRepository.cs
Normal file
13
Repositories/IBreederRepository.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using GerbilManager.Models;
|
||||||
|
|
||||||
|
namespace GerbilManager.Repositories
|
||||||
|
{
|
||||||
|
public interface IBreederRepository
|
||||||
|
{
|
||||||
|
IEnumerable<Breeder> GetBreeders();
|
||||||
|
|
||||||
|
Breeder GetBreeder(Guid id);
|
||||||
|
|
||||||
|
void CreateBreeder(Breeder breeder);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,5 +7,11 @@ namespace GerbilManager.Repositories
|
|||||||
IEnumerable<Gerbil> GetGerbils();
|
IEnumerable<Gerbil> GetGerbils();
|
||||||
|
|
||||||
Gerbil GetGerbil(Guid id);
|
Gerbil GetGerbil(Guid id);
|
||||||
|
|
||||||
|
void CreateGerbil(Gerbil gerbil);
|
||||||
|
|
||||||
|
void UpdateGerbil(Gerbil gerbil);
|
||||||
|
|
||||||
|
void DeleteGerbil(Gerbil gerilb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
13
Repositories/ILitterRepostitory.cs
Normal file
13
Repositories/ILitterRepostitory.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using GerbilManager.Models;
|
||||||
|
|
||||||
|
namespace GerbilManager.Repositories
|
||||||
|
{
|
||||||
|
public interface ILitterRepository
|
||||||
|
{
|
||||||
|
IEnumerable<Litter> GetLitters();
|
||||||
|
|
||||||
|
Litter GetLitter(Guid id);
|
||||||
|
|
||||||
|
void CreateLitter(Litter breeder);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,6 +17,23 @@ namespace GerbilManager.Repositories
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Gerbil GetGerbil(Guid id) => _gerbilList.SingleOrDefault(x => x.Id == id);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user