ticket_manager/Controllers/NotesController.cs

110 lines
3 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/[controller]")]
[ApiController]
public class NotesController : ControllerBase
{
private readonly AppDbContext _context;
public NotesController(AppDbContext context)
{
_context = context;
}
// GET: api/Notes
[HttpGet]
public async Task<ActionResult<IEnumerable<Note>>> GetNotes()
{
return await _context.Notes.ToListAsync();
}
// GET: api/Notes/5
[HttpGet("{id}")]
public async Task<ActionResult<Note>> GetNote(int id)
{
var note = await _context.Notes.FindAsync(id);
if (note == null)
{
return NotFound();
}
return note;
}
// PUT: api/Notes/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> PutNote(int id, Note note)
{
if (id != note.Id)
{
return BadRequest();
}
_context.Entry(note).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!NoteExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Notes
// 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<Note>> PostNote(Note note)
{
_context.Notes.Add(note);
await _context.SaveChangesAsync();
return CreatedAtAction("GetNote", new { id = note.Id }, note);
}
// DELETE: api/Notes/5
[HttpDelete("{id}")]
public async Task<ActionResult<Note>> DeleteNote(int id)
{
var note = await _context.Notes.FindAsync(id);
if (note == null)
{
return NotFound();
}
_context.Notes.Remove(note);
await _context.SaveChangesAsync();
return note;
}
private bool NoteExists(int id)
{
return _context.Notes.Any(e => e.Id == id);
}
}
}