Y3S2-Aya/Aya-Backend/Aya-Backend/Controllers/WorkbookController.cs
2020-06-09 21:28:47 +01:00

59 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Aya_Backend.Data.Repositories.UserRepositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Aya_Backend.Controllers
{
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class WorkbookController : ControllerBase
{
private readonly IRepository _context;
public WorkbookController(IRepository context)
{
_context = context;
}
//Gets a workbook if the logged in user is owner of the workbook.
[Route("get")]
[HttpGet]
public async Task<IActionResult> GetWorkbook(int id)
{
string userId = User.FindFirst(ClaimTypes.Name)?.Value;
var workbook = _context.GetWorkbook(id);
if (workbook.OwnerID != Int32.Parse(userId))
return BadRequest();
return Ok(workbook);
}
//Adds a workbook to the user currently logged in.
[Route("add")]
[HttpPost]
public async Task<IActionResult> AddWorkbook(string name)
{
string userId = User.FindFirst(ClaimTypes.Name)?.Value;
return Ok(_context.AddWorkbook(name, Int32.Parse(userId)));
}
//Gets a list of all of the workbooks that belong to that user.
[Route("userworkbooks")]
[HttpGet]
public async Task<IActionResult> GetUserWorkbooks()
{
Console.WriteLine("workbook");
string userId = User.FindFirst(ClaimTypes.Name)?.Value;
return Ok(_context.GetUserWorkbooks(Int32.Parse(userId)));
}
}
}