108 lines
3.9 KiB
C#
108 lines
3.9 KiB
C#
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 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)]
|
|
[Authorize("admin")]
|
|
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.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[Authorize("admin")]
|
|
public async Task<ActionResult<PublishedFeedback>> 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 Ok(ePublishedFeedback.Entity);
|
|
}
|
|
|
|
|
|
[HttpDelete("{id}")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[Authorize("admin")]
|
|
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();
|
|
}
|
|
}
|
|
}
|