Created
October 15, 2018 18:30
-
-
Save PandiyanCool/975c83f0ffeb4d61fdcce5e8f0edc0b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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