Commit 9fa4c5da authored by Andres Käver's avatar Andres Käver

initial owner rest controller, no dtos

parent 49e19610
......@@ -2,6 +2,7 @@ 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;
......@@ -18,38 +19,36 @@ namespace WebApp.ApiControllers
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class OwnersController : ControllerBase
{
private readonly AppDbContext _context;
private readonly IAppUnitOfWork _uow;
public OwnersController(AppDbContext context)
public OwnersController(IAppUnitOfWork uow)
{
_context = context;
_uow = uow;
}
// GET: api/Owners
[HttpGet]
public async Task<ActionResult<IEnumerable<Owner>>> GetOwners()
{
return await _context.Owners.Where(o => o.AppUserId == User.UserGuidId()).ToListAsync();
var owners = await _uow.Owners.AllAsync(User.UserGuidId());
return Ok(owners);
}
// GET: api/Owners/5
[HttpGet("{id}")]
public async Task<ActionResult<Owner>> GetOwner(Guid id)
{
var owner = await _context.Owners
.FirstOrDefaultAsync(o => o.Id == id && o.AppUserId == User.UserGuidId());
var owner = await _uow.Owners.FirstOrDefaultAsync(id, User.UserGuidId());
if (owner == null)
{
return NotFound();
}
return owner;
return Ok(owner);
}
// PUT: api/Owners/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> PutOwner(Guid id, Owner owner)
{
......@@ -58,58 +57,52 @@ namespace WebApp.ApiControllers
return BadRequest();
}
_context.Entry(owner).State = EntityState.Modified;
owner.AppUserId = User.UserGuidId();
_uow.Owners.Update(owner);
try
{
await _context.SaveChangesAsync();
await _uow.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!OwnerExists(id))
if (!await _uow.Owners.ExistsAsync(id, User.UserGuidId()))
{
return NotFound();
}
else
{
throw;
}
throw;
}
return NoContent();
}
// POST: api/Owners
// 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<Owner>> PostOwner(Owner owner)
{
_context.Owners.Add(owner);
await _context.SaveChangesAsync();
owner.AppUserId = User.UserGuidId();
_uow.Owners.Add(owner);
await _uow.SaveChangesAsync();
return CreatedAtAction("GetOwner", new { id = owner.Id }, owner);
return CreatedAtAction("GetOwner", new {id = owner.Id}, owner);
}
// DELETE: api/Owners/5
[HttpDelete("{id}")]
public async Task<ActionResult<Owner>> DeleteOwner(Guid id)
{
var owner = await _context.Owners.FindAsync(id);
var owner = await _uow.Owners.FirstOrDefaultAsync(id, User.UserGuidId());
if (owner == null)
{
return NotFound();
}
_context.Owners.Remove(owner);
await _context.SaveChangesAsync();
_uow.Owners.Remove(owner);
await _uow.SaveChangesAsync();
return owner;
}
private bool OwnerExists(Guid id)
{
return _context.Owners.Any(e => e.Id == id);
return Ok(owner);
}
}
}
}
\ No newline at end of file
......@@ -13,7 +13,7 @@ using Microsoft.AspNetCore.Authorization;
namespace WebApp.Controllers
{
[Authorize(Roles = "User")]
[Authorize]
public class OwnersController : Controller
{
private readonly IAppUnitOfWork _uow;
......
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