-
-
Save SerhiiKozachenko/6942934 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 aspapi.Controllers | |
{ | |
public class TaskApiController : ApiController | |
{ | |
private readonly ITaskRepository taskRepository; | |
public TaskApiController() | |
{ | |
taskRepository = new TaskRepository(); | |
} | |
public IEnumerable<Task> Get() | |
{ | |
return taskRepository.All; | |
} | |
public Task Get(int id) | |
{ | |
var task = taskRepository.Find(id); | |
if (task == null) | |
{ | |
throw new HttpResponseException(new HttpResponseMessage | |
{ | |
StatusCode = HttpStatusCode.NotFound, | |
Content = new StringContent("Task not found") | |
}); | |
} | |
return task; | |
} | |
public HttpResponseMessage Post(Task task) | |
{ | |
taskRepository.InsertOrUpdate(task); | |
taskRepository.Save(); | |
var response = Request.CreateResponse<Task>(HttpStatusCode.Created, task); | |
string uri = Url.Route(null, new { id = task.Id }); | |
response.Headers.Location = new Uri(Request.RequestUri, uri); | |
return response; | |
} | |
public Task Put(Task task) | |
{ | |
try | |
{ | |
taskRepository.InsertOrUpdate(task); | |
taskRepository.Save(); | |
} | |
catch (Exception) | |
{ | |
throw new HttpResponseException(new HttpResponseMessage | |
{ | |
StatusCode = HttpStatusCode.NotFound, | |
Content = new StringContent("Task not found") | |
}); | |
} | |
return task; | |
} | |
public HttpResponseMessage Delete(int id) | |
{ | |
taskRepository.Delete(id); | |
taskRepository.Save(); | |
return new HttpResponseMessage | |
{ | |
StatusCode = HttpStatusCode.NoContent | |
}; | |
} | |
} | |
} |
This file contains hidden or 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 nancyapi | |
{ | |
public class ApiModule : Nancy.NancyModule | |
{ | |
private ITaskRepository taskRepository; | |
public ApiModule() { | |
taskRepository = new TaskRepository(); | |
Get["/api"] = x => taskRepository.All; | |
Get["/api/{id}"] = x => taskRepository.Find(x.id); | |
Post["/api"] = pars => | |
{ | |
var task = new Task(); | |
task.Description = Request.Form.description; | |
task.Priority = Request.Form.priority; | |
task.CreatedOn = Request.Form.createdon; | |
var id = taskRepository.InsertOrUpdate(task); | |
taskRepository.Save(); | |
return JsonEncode(id); | |
}; | |
Put["/api/{id}"] = pars => | |
{ | |
var task = new Task(); | |
task.Id = pars.id; | |
task.Description = Request.Form.description; | |
task.Priority = Request.Form.priority; | |
task.CreatedOn = Request.Form.createdon; | |
var id = taskRepository.InsertOrUpdate(task); | |
taskRepository.Save(); | |
return JsonEncode(id); | |
}; | |
Delete["/api/{id}"] = x => taskRepository.Delete(x.id); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment