Skip to content

Instantly share code, notes, and snippets.

@PandiyanCool
Created October 15, 2018 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PandiyanCool/975c83f0ffeb4d61fdcce5e8f0edc0b0 to your computer and use it in GitHub Desktop.
Save PandiyanCool/975c83f0ffeb4d61fdcce5e8f0edc0b0 to your computer and use it in GitHub Desktop.
namespace APITest.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EventsController : ControllerBase
{
private readonly EventContext _context;
public EventsController(EventContext context)
{
_context = context;
}
// GET: api/Events
[HttpGet]
public async Task<ActionResult<IEnumerable<Event>>> GetEvent()
{
return await _context.Event.ToListAsync();
}
// GET: api/Events/5
[HttpGet("{id}")]
public async Task<ActionResult<Event>> GetEvent(int id)
{
var @event = await _context.Event.FindAsync(id);
if (@event == null)
{
return NotFound();
}
return @event;
}
// PUT: api/Events/5
[HttpPut("{id}")]
public async Task<IActionResult> PutEvent(int id, Event @event)
{
if (id != @event.Id)
{
return BadRequest();
}
_context.Entry(@event).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!EventExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Events
[HttpPost]
public async Task<ActionResult<Event>> PostEvent(Event @event)
{
_context.Event.Add(@event);
await _context.SaveChangesAsync();
return CreatedAtAction("GetEvent", new { id = @event.Id }, @event);
}
// DELETE: api/Events/5
[HttpDelete("{id}")]
public async Task<ActionResult<Event>> DeleteEvent(int id)
{
var @event = await _context.Event.FindAsync(id);
if (@event == null)
{
return NotFound();
}
_context.Event.Remove(@event);
await _context.SaveChangesAsync();
return @event;
}
private bool EventExists(int id)
{
return _context.Event.Any(e => e.Id == id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment