ticket_manager/Controllers/HistoriesController.cs

110 lines
3.1 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 TicketManager.Data;
using TicketManager.Models;
namespace TicketManager.Controllers
{
[Route("api/v1/[controller]")]
[ApiController]
public class HistoriesController : ControllerBase
{
private readonly AppDbContext _context;
public HistoriesController(AppDbContext context)
{
_context = context;
}
// GET: api/Histories
[HttpGet]
public async Task<ActionResult<IEnumerable<History>>> GetEdits()
{
return await _context.Edits.ToListAsync();
}
// GET: api/Histories/5
[HttpGet("{id}")]
public async Task<ActionResult<History>> GetHistory(int id)
{
var history = await _context.Edits.FindAsync(id);
if (history == null)
{
return NotFound();
}
return history;
}
// PUT: api/Histories/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> PutHistory(int id, History history)
{
if (id != history.Id)
{
return BadRequest();
}
_context.Entry(history).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!HistoryExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Histories
// 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<History>> PostHistory(History history)
{
_context.Edits.Add(history);
await _context.SaveChangesAsync();
return CreatedAtAction("GetHistory", new { id = history.Id }, history);
}
// DELETE: api/Histories/5
[HttpDelete("{id}")]
public async Task<ActionResult<History>> DeleteHistory(int id)
{
var history = await _context.Edits.FindAsync(id);
if (history == null)
{
return NotFound();
}
_context.Edits.Remove(history);
await _context.SaveChangesAsync();
return history;
}
private bool HistoryExists(int id)
{
return _context.Edits.Any(e => e.Id == id);
}
}
}