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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user