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/[controller]")] [ApiController] public class TicketsController : ControllerBase { private readonly AppDbContext _context; public TicketsController(AppDbContext context) { _context = context; } // GET: api/Tickets [HttpGet] public async Task>> GetTickets() { return await _context.Tickets .Include(t => t.Creator) .Include(p => p.Notes) .Include(p => p.Edits) .Include(p => p.Files) .AsNoTracking() .ToListAsync(); } // GET: api/Tickets/5 [HttpGet("{id}")] public async Task> GetTicket(int id) { var ticket = await _context.Tickets .Include(t => t.Creator) .Include(p => p.Notes) .Include(p => p.Edits) .Include(p => p.Files) .FirstOrDefaultAsync(t => t.Id == id); if (ticket == null) { return NotFound(); } return ticket; } // PUT: api/Tickets/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 PutTicket(int id, Ticket ticket) { if (id != ticket.Id) { return BadRequest(); } _context.Entry(ticket).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TicketExists(id)) { return NotFound(); } else { throw; } } return NoContent(); } // POST: api/Tickets // 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> PostTicket(Ticket ticket) { _context.Tickets.Add(ticket); await _context.SaveChangesAsync(); return CreatedAtAction("GetTicket", new { id = ticket.Id }, ticket); } // DELETE: api/Tickets/5 [HttpDelete("{id}")] public async Task> DeleteTicket(int id) { var ticket = await _context.Tickets.FindAsync(id); if (ticket == null) { return NotFound(); } _context.Tickets.Remove(ticket); await _context.SaveChangesAsync(); return ticket; } private bool TicketExists(int id) { return _context.Tickets.Any(e => e.Id == id); } } }