2021-10-24 22:40:15 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-10-26 19:26:41 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2021-10-24 22:40:15 +01:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-10-26 19:26:41 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
2021-10-24 22:40:15 +01:00
|
|
|
using Selector.Model;
|
2021-10-26 19:26:41 +01:00
|
|
|
using Selector.Model.Authorisation;
|
2021-10-24 22:40:15 +01:00
|
|
|
|
|
|
|
namespace Selector.Web.Controller {
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
[Route("api/[controller]")]
|
2021-10-26 19:26:41 +01:00
|
|
|
public class WatchersController : BaseAuthController
|
|
|
|
{
|
|
|
|
public WatchersController(
|
|
|
|
ApplicationDbContext context,
|
|
|
|
IAuthorizationService auth,
|
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
ILogger<WatchersController> logger
|
|
|
|
) : base(context, auth, userManager, logger) { }
|
2021-10-24 22:40:15 +01:00
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
public async Task<ActionResult<IEnumerable<Watcher>>> Get()
|
|
|
|
{
|
2021-10-26 19:26:41 +01:00
|
|
|
var isAuthed = User.IsInRole(Constants.AdminRole);
|
|
|
|
|
|
|
|
if(isAuthed)
|
|
|
|
{
|
|
|
|
return await Context.Watcher.AsNoTracking().ToListAsync();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var userId = UserManager.GetUserId(User);
|
|
|
|
return await Context.Watcher.AsNoTracking().Where(w => w.UserId == userId).ToListAsync();
|
|
|
|
}
|
2021-10-24 22:40:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
[Route("api/[controller]")]
|
2021-10-26 19:26:41 +01:00
|
|
|
public class WatcherController : BaseAuthController
|
|
|
|
{
|
|
|
|
public WatcherController(
|
|
|
|
ApplicationDbContext context,
|
|
|
|
IAuthorizationService auth,
|
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
ILogger<WatcherController> logger
|
|
|
|
) : base(context, auth, userManager, logger) { }
|
2021-10-24 22:40:15 +01:00
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
public async Task<ActionResult<Watcher>> Get(int id)
|
|
|
|
{
|
2021-10-26 19:26:41 +01:00
|
|
|
var watcher = await Context.Watcher.AsNoTracking().FirstOrDefaultAsync(w => w.Id == id);
|
|
|
|
|
|
|
|
if(watcher is null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
var isAuthed = await AuthorizationService.AuthorizeAsync(User, watcher, WatcherOperations.Read);
|
|
|
|
|
|
|
|
if(!isAuthed.Succeeded)
|
|
|
|
{
|
|
|
|
return Unauthorized();
|
|
|
|
}
|
|
|
|
|
|
|
|
return watcher;
|
2021-10-24 22:40:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|