Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
74 lines (65 sloc) 1.71 KB
using System;
using System.Collections.Generic;
using ConductOfCode.Models;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
namespace ConductOfCode.Controllers
{
[Route("api/[controller]")]
public class StackController : Controller
{
private Stack<Item> Stack { get; set; }
public StackController(Stack<Item> stack)
{
Stack = stack;
}
[HttpPost("[action]")]
[SwaggerResponse(typeof(void))]
public IActionResult Clear()
{
Stack.Clear();
return NoContent();
}
[HttpGet("[action]")]
[SwaggerResponse("200", typeof(Item))]
[SwaggerResponse("400", typeof(Error))]
public IActionResult Peek()
{
try
{
return Ok(Stack.Peek());
}
catch (InvalidOperationException ex)
{
return BadRequest(new Error(ex));
}
}
[HttpGet("[action]")]
[SwaggerResponse("200", typeof(Item))]
[SwaggerResponse("400", typeof(Error))]
public IActionResult Pop()
{
try
{
return Ok(Stack.Pop());
}
catch (InvalidOperationException ex)
{
return BadRequest(new Error(ex));
}
}
[HttpPost("[action]")]
[SwaggerResponse(typeof(void))]
public IActionResult Push([FromBody] Item item)
{
Stack.Push(item);
return NoContent();
}
[HttpGet("[action]")]
[SwaggerResponse(typeof(Item[]))]
public Item[] ToArray()
{
return Stack.ToArray();
}
}
}