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; using Microsoft.AspNetCore.Authorization; 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>> GetIssueTypes() { return await _context.IssueTypes.ToListAsync(); } [HttpPost] [ProducesResponseType(StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [Authorize("admin")] public async Task> 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> GetIssueType(int id) { var issueType = await _context.IssueTypes.FindAsync(id); if (issueType == null) return NotFound(); return Ok(issueType); } [HttpPatch("{id}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize("admin")] public async Task 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 Ok(eIssueType.Entity); } [HttpDelete("{id}")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize("admin")] public async Task DeleteIssueType(int id) { var issueType = await _context.IssueTypes.FindAsync(id); if (issueType == default) return NotFound(); _context.IssueTypes.Remove(issueType); await _context.SaveChangesAsync(); return NoContent(); } } }