﻿using System.Collections.Generic;
using System.Web.Http;
using Serilog;

namespace SerilogPoC.Controllers
{
    [RoutePrefix("api/test")]
    public class TestController : ApiController
    {
        private static readonly ILogger Logger = Log.ForContext<TestController>();
        private static List<string> _items = new List<string>();

        [Route("")]
        // GET: api/test
        public IEnumerable<string> Get()
        {
            Logger.Information("Returning a list of {@Count} items.", _items.Count);
            return _items;
        }

        [Route("{id:int}")]
        // GET: api/test/5
        public string Get(int id)
        {
            if (id >= 0 && id < _items.Count)
            {
                var value = _items[id];
                Logger.Information("Returning item {@id}: {@value}.", id, value);
                return value;
            }

            Logger.Error("No item with id {@id} found!", id);
            throw new KeyNotFoundException();
        }

        [Route("")]
        // POST: api/test
        public void Post([FromBody]string value)
        {
            _items.Add(value);
            var id = _items.Count - 1;
            Logger.Information("Added {@value} as id {@id}.", id, value);
        }

        [Route("{id:int}")]
        // PUT: api/test/5
        public void Put(int id, [FromBody]string value)
        {
            if (id >= 0 && id < _items.Count)
            {
                _items[id] = value;
                Logger.Information("Replaced id {@id} with {@value}.", id, value);
            }

            Logger.Error("No item with id {@id} found!", id);
            throw new KeyNotFoundException();
        }

        [Route("{id:int}")]
        // DELETE: api/test/5
        public void Delete(int id)
        {
        }
    }
}
