Implement generic repo pattern and unitofwork
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using GerbilManagerWebAPI.Models;
|
||||
using GerbilManagerWebAPI.Repositories;
|
||||
using GerbilManagerWebAPI.DAL;
|
||||
using GerbilManagerWebAPI.Dtos;
|
||||
|
||||
using GerbilManagerWebAPI.Converter;
|
||||
@@ -13,20 +13,18 @@ namespace GerbilManagerWebAPI.Controllers
|
||||
[Route("litters")]
|
||||
public class LittersController : ControllerBase
|
||||
{
|
||||
private readonly IGerbilRepository gerbilrepository;
|
||||
private readonly IBreederRepository breederRepository;
|
||||
private readonly ILitterRepository repository;
|
||||
private readonly UnitOfWork unitOfWork;
|
||||
|
||||
public LittersController(ILitterRepository repo)
|
||||
public LittersController(UnitOfWork unitOfWork)
|
||||
{
|
||||
this.repository = repo;
|
||||
this.unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
//GET /gerbils
|
||||
[HttpGet]
|
||||
public IEnumerable<LitterDto> GetLitters()
|
||||
{
|
||||
var items = repository.GetLitters().Select( litter => litter.AsDto());
|
||||
var items = unitOfWork.LitterRepository.Get().Select( litter => litter.AsDto());
|
||||
return items;
|
||||
}
|
||||
|
||||
@@ -34,7 +32,7 @@ namespace GerbilManagerWebAPI.Controllers
|
||||
[HttpGet("{id}")]
|
||||
public ActionResult<LitterDto> GetLitter(Guid id)
|
||||
{
|
||||
var item = repository.GetLitter(id).AsDto();
|
||||
var item = unitOfWork.LitterRepository.GetByID(id).AsDto();
|
||||
|
||||
if(item is null)
|
||||
{
|
||||
@@ -49,13 +47,13 @@ namespace GerbilManagerWebAPI.Controllers
|
||||
Litter litter = new Litter{
|
||||
Id = Guid.NewGuid(),
|
||||
Date = dto.Date,
|
||||
Father = gerbilrepository.GetGerbil(dto.Father ?? Guid.Empty),
|
||||
Mother = gerbilrepository.GetGerbil(dto.Mother ?? Guid.Empty),
|
||||
Father = this.unitOfWork.GerbilRepository.GetByID(dto.Father ?? Guid.Empty),
|
||||
Mother = this.unitOfWork.GerbilRepository.GetByID(dto.Mother ?? Guid.Empty),
|
||||
Name = dto.Name,
|
||||
Strength = dto.Strength
|
||||
};
|
||||
|
||||
this.repository.CreateLitter(litter);
|
||||
this.unitOfWork.LitterRepository.Insert(litter);
|
||||
|
||||
return CreatedAtAction(nameof(GetLitter), new { id = litter.Id}, litter.AsDto());
|
||||
}
|
||||
@@ -63,7 +61,7 @@ namespace GerbilManagerWebAPI.Controllers
|
||||
[HttpPut("{id}")]
|
||||
public ActionResult UpdateLitter(Guid id, UpdateLitterDto litterDto)
|
||||
{
|
||||
var existingItem = repository.GetLitter(id);
|
||||
var existingItem = unitOfWork.LitterRepository.GetByID(id);
|
||||
if(existingItem is null)
|
||||
{
|
||||
return NotFound();
|
||||
@@ -72,26 +70,26 @@ namespace GerbilManagerWebAPI.Controllers
|
||||
Litter updatedLitter = existingItem with{
|
||||
Date = litterDto.Date ?? DateTime.Now,
|
||||
Strength = litterDto.Strength,
|
||||
Father = gerbilrepository.GetGerbil(litterDto.Father ?? Guid.Empty),
|
||||
Mother = gerbilrepository.GetGerbil(litterDto.Mother ?? Guid.Empty),
|
||||
Father = this.unitOfWork.GerbilRepository.GetByID(litterDto.Father ?? Guid.Empty),
|
||||
Mother = this.unitOfWork.GerbilRepository.GetByID(litterDto.Mother ?? Guid.Empty),
|
||||
Name = litterDto.Name
|
||||
};
|
||||
|
||||
repository.UpdateLitter(updatedLitter);
|
||||
unitOfWork.LitterRepository.Update(updatedLitter);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public ActionResult DeleteLitter(Guid id)
|
||||
{
|
||||
var existingLitter = repository.GetLitter(id);
|
||||
var existingLitter = unitOfWork.LitterRepository.GetByID(id);
|
||||
|
||||
if(existingLitter is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
repository.DeleteLitter(existingLitter);
|
||||
unitOfWork.LitterRepository.Delete(existingLitter);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user