Huge work
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
@@ -10,15 +11,39 @@ namespace KTUSAPS.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[Produces("application/json")]
|
||||
public class AuthMetadataController : ControllerBase
|
||||
{
|
||||
public class AuthMetadata
|
||||
{
|
||||
public string ClientId { get; set; }
|
||||
public string Authority { get; set; }
|
||||
public string Tenant { get; set; }
|
||||
}
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
public AuthMetadataController(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get authethication metadata needed to obtain token.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public object Index() => new { ClientId = _configuration["ClientId"], Authority = _configuration["Authority"], Tenant = _configuration["Tenant"] };
|
||||
public AuthMetadata Index() => new AuthMetadata { ClientId = _configuration["ClientId"], Authority = _configuration["Authority"], Tenant = _configuration["Tenant"] };
|
||||
|
||||
/// <summary>
|
||||
/// Returns true is provided token is valid, else throws exception
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <response code="200">Provided token is correct.</response>
|
||||
/// <response code="401">No valid token provided.</response>
|
||||
[Authorize]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[HttpGet("Authed")]
|
||||
public bool IsAuthed() => true;
|
||||
}
|
||||
}
|
||||
|
96
KTUSAPS/Controllers/IssueController.cs
Normal file
96
KTUSAPS/Controllers/IssueController.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using KTUSAPS.Data.Model;
|
||||
using KTUSAPS.Extensions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KTUSAPS.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class IssueController : ControllerBase
|
||||
{
|
||||
private readonly Data.SAPSDataContext dataContext;
|
||||
|
||||
public IssueController(Data.SAPSDataContext dataContext)
|
||||
{
|
||||
this.dataContext = dataContext;
|
||||
}
|
||||
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public IEnumerable<Issue> GetIssues()
|
||||
{
|
||||
return dataContext.Issues;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<ActionResult<Issue>> CreateIssueAsync([FromBody] Issue issueToCreate)
|
||||
{
|
||||
if (issueToCreate == null)
|
||||
return BadRequest("No data provided for object to be created.");
|
||||
if (issueToCreate.Id != default)
|
||||
return BadRequest("Id has been set on create request, please do not do that, set id to 0 or ommit it.");
|
||||
if (issueToCreate.IssueTypeId == default)
|
||||
return BadRequest("No typeId has been specified");
|
||||
if (issueToCreate.Problem != null && issueToCreate.Feedback != null && issueToCreate.IssueType != null)
|
||||
return BadRequest("Do not privide navigation property values.");
|
||||
// TODO: Enable next line and make thoes two fields come from user identity
|
||||
//if (issueToCreate.UserID != default || issueToCreate.Email != default)
|
||||
// return BadRequest("Do not provide indentity values.");
|
||||
|
||||
var createdValue = await dataContext.AddAsync(issueToCreate);
|
||||
await dataContext.SaveChangesAsync();
|
||||
var url = Url.ActionLink(action: nameof(GetIssue), values: new { Id = createdValue.Entity.Id });
|
||||
return Created(url, createdValue.Entity);
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public ActionResult<Issue> GetIssue(int id)
|
||||
{
|
||||
var issue = dataContext.Issues.AsQueryable().Where(i => i.Id == id).FirstOrDefault();
|
||||
if(issue == default)
|
||||
return NotFound();
|
||||
return Ok(issue);
|
||||
}
|
||||
|
||||
[HttpPatch("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<Issue>> UpdateIssueAsync(int id, [FromBody] Issue issue)
|
||||
{
|
||||
var databaseIssue = dataContext.Issues.AsQueryable().Where(i => i.Id == id).FirstOrDefault();
|
||||
if (databaseIssue == default)
|
||||
return NotFound();
|
||||
var eIssue = dataContext.Attach(databaseIssue);
|
||||
eIssue.MovePropertyDataWhiteList(issue, new string[] {
|
||||
nameof(databaseIssue.Description),
|
||||
nameof(databaseIssue.IssueTypeId),
|
||||
nameof(databaseIssue.Publishable)
|
||||
});
|
||||
await dataContext.SaveChangesAsync();
|
||||
return Ok(eIssue.Entity);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> DeleteIssueAsync(int id)
|
||||
{
|
||||
var issue = dataContext.Issues.AsQueryable().Where(i => i.Id == id).FirstOrDefault();
|
||||
if (issue == default)
|
||||
return NotFound();
|
||||
dataContext.Issues.Remove(issue);
|
||||
await dataContext.SaveChangesAsync();
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KTUSAPS.Controllers
|
||||
{
|
||||
[Route("[controller]")]
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
public class TestController : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public object[] Index()
|
||||
{
|
||||
return HttpContext.User.Claims.Select(x => new { Name = x.Type, Value= x.Value }).ToArray();
|
||||
}
|
||||
|
||||
[HttpGet("authed")]
|
||||
public bool IsAuthed() => true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user