diff --git a/ToDo.Server/Controllers/TodoController.cs b/ToDo.Server/Controllers/TodoController.cs index 8a8333c..64e51c8 100755 --- a/ToDo.Server/Controllers/TodoController.cs +++ b/ToDo.Server/Controllers/TodoController.cs @@ -44,29 +44,30 @@ public IHttpActionResult Create([FromBody] TodoItem item) return BadRequest(); } TodoItems.Add(item); - return CreatedAtRoute("GetTodo", new { id = item.Key }, item); - } + return CreatedAtRoute("DefaultApi", new { id = item.Key }, item); + } + /// + /// Update the given item using the default PUT route + /// + /// the item to update with the modified data + /// 200 OK if success [HttpPut] - [Route("{id}")] - public IHttpActionResult Update(string id, [FromBody] TodoItem item) - { - if (item == null || item.Key != id) - { - return BadRequest(); - } + [Route("{id}")] + public IHttpActionResult Update([FromBody] TodoItem item) + { + if (item == null) + return BadRequest(); - var todo = TodoItems.Find(id); - if (todo == null) - { - return NotFound(); - } + var todo = TodoItems.Find(item.Key); + if (todo == null) + return NotFound(); - TodoItems.Update(item); - return Ok(); - } + TodoItems.Update(item); + return Ok(); + } - [HttpPut] + [HttpPut] [Route("{id}")] public IHttpActionResult Update([FromBody] TodoItem item, string id) { diff --git a/ToDo.Server/Program.cs b/ToDo.Server/Program.cs index 1ecbd82..6341cc2 100755 --- a/ToDo.Server/Program.cs +++ b/ToDo.Server/Program.cs @@ -1,20 +1,54 @@ using System; using Microsoft.Owin.Hosting; +using System.Net.NetworkInformation; +using System.Net; namespace TodoApi { - public class Program - { - protected static void Main(string[] args) - { - var port = 8080; - var url = $"http://localhost:{port}"; - using (WebApp.Start(url)) - { - Console.WriteLine($"Web Server is running at {url}."); - Console.WriteLine("Press any key to quit."); - Console.ReadLine(); - } - } - } + public class Program + { + protected static void Main(string[] args) + { + var port = 8080; + var url = $"http://localhost:{port}"; + + if (IsPortAvailable(port)) + { + using (WebApp.Start(url)) + { + Console.WriteLine($"Web Server is running at {url}."); + Console.WriteLine("Press any key to quit."); + Console.ReadLine(); + } + } + else + { + Console.WriteLine($"The port {port} is being used by your system. Try with another port"); + Console.WriteLine("Press any key to quit."); + Console.ReadLine(); + } + + + } + + /// + /// Check if the port is being used by the system + /// + /// TCP port + /// + private static bool IsPortAvailable(int port) + { + IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); + IPEndPoint[] tcpListenersEndPoints = ipGlobalProperties.GetActiveTcpListeners(); + + foreach (var tcpEp in tcpListenersEndPoints) + { + if (tcpEp.Port == port) + return false; + } + + return true; + } + } } +