Controllers
This commit is contained in:
101
KTUSAPS/Controllers/IssueTypesController.cs
Normal file
101
KTUSAPS/Controllers/IssueTypesController.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using KTUSAPS.Data;
|
||||
using KTUSAPS.Data.Model;
|
||||
using KTUSAPS.Extensions;
|
||||
|
||||
namespace KTUSAPS.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class IssueTypesController : ControllerBase
|
||||
{
|
||||
private readonly SAPSDataContext _context;
|
||||
|
||||
public IssueTypesController(SAPSDataContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<IEnumerable<IssueType>>> GetIssueTypes()
|
||||
{
|
||||
return await _context.IssueTypes.ToListAsync();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<ActionResult<IssueType>> CreateIssueType([FromBody] IssueType issueType)
|
||||
{
|
||||
if (issueType == null)
|
||||
return BadRequest("No data provided for object to be created.");
|
||||
if (issueType.Id != default)
|
||||
return BadRequest("Id has been set on create request, please do not do that, set id to 0 or ommit it.");
|
||||
if (issueType.Issues != null)
|
||||
return BadRequest("Do not privide navigation property values.");
|
||||
|
||||
_context.IssueTypes.Add(issueType);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return CreatedAtAction(nameof(GetIssueType), new { id = issueType.Id }, issueType);
|
||||
}
|
||||
|
||||
// GET: api/IssueTypes/5
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<IssueType>> GetIssueType(int id)
|
||||
{
|
||||
var issueType = await _context.IssueTypes.FindAsync(id);
|
||||
|
||||
if (issueType == null)
|
||||
return NotFound();
|
||||
|
||||
return issueType;
|
||||
}
|
||||
|
||||
[HttpPatch("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> UpdateIssueType(int id, IssueType issueType)
|
||||
{
|
||||
var databaseIssueType = await _context.IssueTypes.FindAsync(id);
|
||||
if (databaseIssueType == default)
|
||||
return NotFound();
|
||||
|
||||
var eIssueType = _context.Attach(databaseIssueType);
|
||||
|
||||
eIssueType.MovePropertyDataWhiteList(issueType, new string[] {
|
||||
nameof(databaseIssueType.Name),
|
||||
nameof(databaseIssueType.NameEn),
|
||||
});
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> DeleteIssueType(int id)
|
||||
{
|
||||
var issueType = await _context.IssueTypes.FindAsync(id);
|
||||
if (issueType == default)
|
||||
return NotFound();
|
||||
|
||||
_context.IssueTypes.Remove(issueType);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
@@ -11,11 +11,11 @@ namespace KTUSAPS.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class IssueController : ControllerBase
|
||||
public class IssuesController : ControllerBase
|
||||
{
|
||||
private readonly Data.SAPSDataContext dataContext;
|
||||
|
||||
public IssueController(Data.SAPSDataContext dataContext)
|
||||
public IssuesController(Data.SAPSDataContext dataContext)
|
||||
{
|
||||
this.dataContext = dataContext;
|
||||
}
|
||||
@@ -23,9 +23,9 @@ namespace KTUSAPS.Controllers
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public IEnumerable<Issue> GetIssues()
|
||||
public async Task<ActionResult<IEnumerable<Issue>>> GetIssues()
|
||||
{
|
||||
return dataContext.Issues;
|
||||
return await dataContext.Issues.ToListAsync();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
@@ -47,8 +47,8 @@ namespace KTUSAPS.Controllers
|
||||
|
||||
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);
|
||||
|
||||
return CreatedAtAction(nameof(GetIssue), new { Id = createdValue.Entity.Id }, createdValue.Entity);
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
@@ -77,7 +77,7 @@ namespace KTUSAPS.Controllers
|
||||
nameof(databaseIssue.Publishable)
|
||||
});
|
||||
await dataContext.SaveChangesAsync();
|
||||
return Ok(eIssue.Entity);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
103
KTUSAPS/Controllers/PublishedFeedbacksController.cs
Normal file
103
KTUSAPS/Controllers/PublishedFeedbacksController.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using KTUSAPS.Data;
|
||||
using KTUSAPS.Data.Model;
|
||||
using KTUSAPS.Extensions;
|
||||
|
||||
namespace KTUSAPS.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class PublishedFeedbacksController : ControllerBase
|
||||
{
|
||||
private readonly SAPSDataContext _context;
|
||||
|
||||
public PublishedFeedbacksController(SAPSDataContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<IEnumerable<PublishedFeedback>>> GetPublishedFeedbacks()
|
||||
{
|
||||
return await _context.PublishedFeedbacks.ToListAsync();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<ActionResult<PublishedFeedback>> PostPublishedFeedback(PublishedFeedback publishedFeedback)
|
||||
{
|
||||
if (publishedFeedback == null)
|
||||
return BadRequest("No data provided for object to be created.");
|
||||
if (publishedFeedback.Id != default)
|
||||
return BadRequest("Id has been set on create request, please do not do that, set id to 0 or ommit it.");
|
||||
if (publishedFeedback.Issue != null)
|
||||
return BadRequest("Do not privide navigation property values.");
|
||||
|
||||
_context.PublishedFeedbacks.Add(publishedFeedback);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return CreatedAtAction(nameof(GetPublishedFeedback), new { id = publishedFeedback.Id }, publishedFeedback);
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<PublishedFeedback>> GetPublishedFeedback(int id)
|
||||
{
|
||||
var publishedFeedback = await _context.PublishedFeedbacks.FindAsync(id);
|
||||
|
||||
if (publishedFeedback == null)
|
||||
return NotFound();
|
||||
|
||||
return publishedFeedback;
|
||||
}
|
||||
|
||||
[HttpPatch("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> UpdatePublishedFeedback(int id, PublishedFeedback publishedFeedback)
|
||||
{
|
||||
var databasePublishedFeedback = await _context.PublishedFeedbacks.FindAsync(id);
|
||||
if (databasePublishedFeedback == default)
|
||||
return NotFound();
|
||||
|
||||
var ePublishedFeedback = _context.Attach(databasePublishedFeedback);
|
||||
|
||||
ePublishedFeedback.MovePropertyDataWhiteList(publishedFeedback, new string[]
|
||||
{
|
||||
nameof(databasePublishedFeedback.FeedbackLt),
|
||||
nameof(databasePublishedFeedback.FeedbackEn),
|
||||
nameof(databasePublishedFeedback.Created),
|
||||
nameof(databasePublishedFeedback.IssueId),
|
||||
});
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> DeletePublishedFeedback(int id)
|
||||
{
|
||||
var publishedFeedback = await _context.PublishedFeedbacks.FindAsync(id);
|
||||
if (publishedFeedback == null)
|
||||
return NotFound();
|
||||
|
||||
_context.PublishedFeedbacks.Remove(publishedFeedback);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
163
KTUSAPS/Controllers/PublishedProblemsController.cs
Normal file
163
KTUSAPS/Controllers/PublishedProblemsController.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using KTUSAPS.Data;
|
||||
using KTUSAPS.Data.Model;
|
||||
using KTUSAPS.Extensions;
|
||||
|
||||
namespace KTUSAPS.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class PublishedProblemsController : ControllerBase
|
||||
{
|
||||
private readonly SAPSDataContext _context;
|
||||
|
||||
public PublishedProblemsController(SAPSDataContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<IEnumerable<PublishedProblem>>> GetPublishedProblems()
|
||||
{
|
||||
return await _context.PublishedProblems.ToListAsync();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<ActionResult<PublishedProblem>> CreatePublishedProblem([FromBody] PublishedProblem publishedProblem)
|
||||
{
|
||||
if (publishedProblem == null)
|
||||
return BadRequest("No data provided for object to be created.");
|
||||
if (publishedProblem.Id != default)
|
||||
return BadRequest("Id has been set on create request, please do not do that, set id to 0 or ommit it.");
|
||||
if (publishedProblem.Issue != null || publishedProblem.Solution != null || publishedProblem.Votes != null)
|
||||
return BadRequest("Do not privide navigation property values.");
|
||||
|
||||
_context.PublishedProblems.Add(publishedProblem);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return CreatedAtAction(nameof(GetPublishedProblem), new { id = publishedProblem.Id }, publishedProblem);
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<PublishedProblem>> GetPublishedProblem(int id)
|
||||
{
|
||||
var publishedProblem = await _context.PublishedProblems.FindAsync(id);
|
||||
|
||||
if (publishedProblem == null)
|
||||
return NotFound();
|
||||
|
||||
return publishedProblem;
|
||||
}
|
||||
|
||||
[HttpPatch("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> UpdatePublishedProblem(int id, PublishedProblem publishedProblem)
|
||||
{
|
||||
var databasePublishedProblem = await _context.PublishedProblems.FindAsync(id);
|
||||
if (databasePublishedProblem == default)
|
||||
return NotFound();
|
||||
|
||||
var ePublishedProblem = _context.Attach(databasePublishedProblem);
|
||||
|
||||
ePublishedProblem.MovePropertyDataWhiteList(publishedProblem, new string[]
|
||||
{
|
||||
nameof(databasePublishedProblem.ProblemLt),
|
||||
nameof(databasePublishedProblem.ProblemEn),
|
||||
nameof(databasePublishedProblem.Created),
|
||||
nameof(databasePublishedProblem.IssueId),
|
||||
nameof(databasePublishedProblem.SolutionId),
|
||||
});
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> DeletePublishedProblem(int id)
|
||||
{
|
||||
var publishedProblem = await _context.PublishedProblems.FindAsync(id);
|
||||
if (publishedProblem == null)
|
||||
return NotFound();
|
||||
|
||||
_context.PublishedProblems.Remove(publishedProblem);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("{id}/Votes")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<int>> GetVoteCount(int id)
|
||||
{
|
||||
var publishedProblem = await _context.PublishedProblems.FindAsync(id);
|
||||
if (publishedProblem == null)
|
||||
return NotFound();
|
||||
|
||||
return (await _context.Votes
|
||||
.Where(v => v.ProblemId == publishedProblem.Id)
|
||||
.ToListAsync())
|
||||
.Count();
|
||||
}
|
||||
|
||||
[HttpPost("{id}/Votes")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<Vote>> Vote(int id, Vote vote)
|
||||
{
|
||||
var publishedProblem = await _context.PublishedProblems.FindAsync(id);
|
||||
if (publishedProblem == null)
|
||||
return NotFound();
|
||||
|
||||
// TODO: Get user id from auth claims
|
||||
if (vote.UserId == default)
|
||||
return BadRequest("Please provide user id");
|
||||
|
||||
vote.Problem = publishedProblem;
|
||||
|
||||
_context.Votes.Add(vote);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return CreatedAtAction(nameof(GetVote), new { id = id, userId = vote.UserId }, vote);
|
||||
}
|
||||
|
||||
[HttpPost("{id}/Votes/{userId}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<Vote>> GetVote(int id, string userId)
|
||||
{
|
||||
var publishedProblem = await _context.PublishedProblems.FindAsync(id);
|
||||
if (publishedProblem == null)
|
||||
return NotFound();
|
||||
|
||||
var vote = await _context.Votes
|
||||
.Where(v => v.ProblemId == publishedProblem.Id && v.UserId == userId)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (vote == default)
|
||||
return NotFound();
|
||||
|
||||
return vote;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user