Commit c37a2887 authored by Andres Käver's avatar Andres Käver

api animalscontroller and repo

parent 0dd28f68
......@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using Contracts.DAL.Base.Repositories;
using Domain;
using PublicApi.DTO.v1;
namespace Contracts.DAL.App.Repositories
{
......@@ -13,5 +14,9 @@ namespace Contracts.DAL.App.Repositories
Task<bool> ExistsAsync(Guid id, Guid? userId = null);
Task DeleteAsync(Guid id, Guid? userId = null);
// DTO methods
Task<IEnumerable<AnimalDTO>> DTOAllAsync(Guid? userId = null);
Task<AnimalDTO> DTOFirstOrDefaultAsync(Guid id, Guid? userId = null);
}
}
\ No newline at end of file
......@@ -6,6 +6,7 @@ using Contracts.DAL.App.Repositories;
using DAL.Base.EF.Repositories;
using Domain;
using Microsoft.EntityFrameworkCore;
using PublicApi.DTO.v1;
namespace DAL.App.EF.Repositories
{
......@@ -50,5 +51,44 @@ namespace DAL.App.EF.Repositories
var owner = await FirstOrDefaultAsync(id, userId);
base.Remove(owner);
}
#region DTO methods
public async Task<IEnumerable<AnimalDTO>> DTOAllAsync(Guid? userId = null)
{
var query = RepoDbSet.AsQueryable();
if (userId != null)
{
query = query.Where(o => o.AppUserId == userId);
}
return await query
.Select(o => new AnimalDTO()
{
Id = o.Id,
AnimalName = o.AnimalName,
BirthYear = o.BirthYear,
OwnerCount = o.Owners!.Count,
})
.ToListAsync();
}
public async Task<AnimalDTO> DTOFirstOrDefaultAsync(Guid id, Guid? userId = null)
{
var query = RepoDbSet.Where(a => a.Id == id).AsQueryable();
if (userId != null)
{
query = query.Where(a => a.AppUserId == userId);
}
var animalDTO = await query.Select(o => new AnimalDTO()
{
Id = o.Id,
AnimalName = o.AnimalName,
BirthYear = o.BirthYear,
OwnerCount = o.Owners!.Count
}).FirstOrDefaultAsync();
return animalDTO;
}
#endregion
}
}
\ No newline at end of file
using System;
using System.ComponentModel.DataAnnotations;
namespace PublicApi.DTO.v1
{
public class AnimalCreateDTO
{
public Guid Id { get; set; }
[MinLength(1)] [MaxLength(64)]
public string AnimalName { get; set; } = default!;
public int? BirthYear { get; set; }
}
}
\ No newline at end of file
using System;
using System.ComponentModel.DataAnnotations;
namespace PublicApi.DTO.v1
{
public class AnimalEditDTO
{
public Guid Id { get; set; }
[MinLength(1)] [MaxLength(64)]
public string AnimalName { get; set; } = default!;
public int? BirthYear { get; set; }
}
}
\ No newline at end of file
......@@ -2,11 +2,14 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Contracts.DAL.App;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using DAL.App.EF;
using Domain;
using Extensions;
using PublicApi.DTO.v1;
namespace WebApp.ApiControllers
{
......@@ -14,54 +17,60 @@ namespace WebApp.ApiControllers
[ApiController]
public class AnimalsController : ControllerBase
{
private readonly AppDbContext _context;
private readonly IAppUnitOfWork _uow;
public AnimalsController(AppDbContext context)
public AnimalsController(IAppUnitOfWork uow)
{
_context = context;
_uow = uow;
}
// GET: api/Animals
[HttpGet]
public async Task<ActionResult<IEnumerable<Animal>>> GetAnimals()
{
return await _context.Animals.ToListAsync();
return Ok(await _uow.Animals.DTOAllAsync(User.UserGuidId()));
}
// GET: api/Animals/5
[HttpGet("{id}")]
public async Task<ActionResult<Animal>> GetAnimal(Guid id)
{
var animal = await _context.Animals.FindAsync(id);
var animalDTO = await _uow.Animals.DTOFirstOrDefaultAsync(id, User.UserGuidId());
if (animal == null)
if (animalDTO == null)
{
return NotFound();
}
return animal;
return Ok(animalDTO);
}
// PUT: api/Animals/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPut("{id}")]
public async Task<IActionResult> PutAnimal(Guid id, Animal animal)
public async Task<IActionResult> PutAnimal(Guid id, AnimalEditDTO animalEditDTO)
{
if (id != animal.Id)
if (id != animalEditDTO.Id)
{
return BadRequest();
}
_context.Entry(animal).State = EntityState.Modified;
var animal = await _uow.Animals.FirstOrDefaultAsync(animalEditDTO.Id, User.UserGuidId());
if (animal == null)
{
return BadRequest();
}
animal.AnimalName = animalEditDTO.AnimalName;
animal.BirthYear = animalEditDTO.BirthYear;
_uow.Animals.Update(animal);
try
{
await _context.SaveChangesAsync();
await _uow.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!AnimalExists(id))
if (!await _uow.Animals.ExistsAsync(id, User.UserGuidId()))
{
return NotFound();
}
......@@ -78,10 +87,15 @@ namespace WebApp.ApiControllers
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[HttpPost]
public async Task<ActionResult<Animal>> PostAnimal(Animal animal)
public async Task<ActionResult<Animal>> PostAnimal(AnimalCreateDTO animalCreateDTO)
{
_context.Animals.Add(animal);
await _context.SaveChangesAsync();
var animal = new Animal();
animal.AnimalName = animalCreateDTO.AnimalName;
animal.BirthYear = animalCreateDTO.BirthYear;
await _uow.SaveChangesAsync();
return CreatedAtAction("GetAnimal", new { id = animal.Id }, animal);
}
......@@ -90,21 +104,16 @@ namespace WebApp.ApiControllers
[HttpDelete("{id}")]
public async Task<ActionResult<Animal>> DeleteAnimal(Guid id)
{
var animal = await _context.Animals.FindAsync(id);
var animal = await _uow.Animals.FirstOrDefaultAsync(id, User.UserGuidId());
if (animal == null)
{
return NotFound();
}
_context.Animals.Remove(animal);
await _context.SaveChangesAsync();
return animal;
_uow.Animals.Remove(animal);
await _uow.SaveChangesAsync();
return Ok(animal);
}
private bool AnimalExists(Guid id)
{
return _context.Animals.Any(e => e.Id == id);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment