mirror of
https://github.com/rjNemo/ticket_manager
synced 2026-06-06 16:56:40 +00:00
110 lines
3 KiB
C#
110 lines
3 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using TicketManager.Data;
|
|
using TicketManager.Models;
|
|
|
|
namespace TicketManager.Controllers
|
|
{
|
|
[Authorize]
|
|
[Route("api/v1/[controller]")]
|
|
[ApiController]
|
|
public class FilesController : ControllerBase
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public FilesController(AppDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/Files
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<File>>> GetFiles()
|
|
{
|
|
return await _context.Files.ToListAsync();
|
|
}
|
|
|
|
// GET: api/Files/5
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<File>> GetFile(int id)
|
|
{
|
|
var file = await _context.Files.FindAsync(id);
|
|
|
|
if (file == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return file;
|
|
}
|
|
|
|
// PUT: api/Files/5
|
|
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
|
// more details see https://aka.ms/RazorPagesCRUD.
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutFile(int id, File file)
|
|
{
|
|
if (id != file.Id)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
_context.Entry(file).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!FileExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/Files
|
|
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
|
// more details see https://aka.ms/RazorPagesCRUD.
|
|
[HttpPost]
|
|
public async Task<ActionResult<File>> PostFile(File file)
|
|
{
|
|
_context.Files.Add(file);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction("GetFile", new { id = file.Id }, file);
|
|
}
|
|
|
|
// DELETE: api/Files/5
|
|
[HttpDelete("{id}")]
|
|
public async Task<ActionResult<File>> DeleteFile(int id)
|
|
{
|
|
var file = await _context.Files.FindAsync(id);
|
|
if (file == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
_context.Files.Remove(file);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return file;
|
|
}
|
|
|
|
private bool FileExists(int id)
|
|
{
|
|
return _context.Files.Any(e => e.Id == id);
|
|
}
|
|
}
|
|
}
|