Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance/fix rest server #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions ToDo.Server/Controllers/TodoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
/// Update the given item using the default PUT route
/// </summary>
/// <param name="item">the item to update with the modified data</param>
/// <returns>200 OK if success</returns>
[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)
{
Expand Down
62 changes: 48 additions & 14 deletions ToDo.Server/Program.cs
Original file line number Diff line number Diff line change
@@ -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<Startup>(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<Startup>(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();
}


}

/// <summary>
/// Check if the port is being used by the system
/// </summary>
/// <param name="port">TCP port</param>
/// <returns></returns>
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;
}
}
}