Skip to content

Instantly share code, notes, and snippets.

@indyone
Last active October 13, 2017 18:32
Show Gist options
  • Save indyone/af32cd5c0a41f6a395b1b167897c7c21 to your computer and use it in GitHub Desktop.
Save indyone/af32cd5c0a41f6a395b1b167897c7c21 to your computer and use it in GitHub Desktop.
Coding School - Todo API

Coding School - Todo API

Quickstart guide

  1. Create a new Web API Project
  2. Add a new model file in the path Models\Todo.cs and copy & paste the content from the Models\Todo.cs gist file
  3. Add a new controller file in the path Controllers\TodoController.cs and copy & paste the content from the Controllers\TodoController.cs gist file
  4. Hit Ctrl+F5 to start app without debugging
  5. Navigate your web browser to http://localhost:52548/api/todo - Note: You may need to change the port

For more experience

  1. Go to https://www.getpostman.com/ and install Postman
  2. Click Import -> Import From Link and paste the link https://gist.githubusercontent.com/indyone/af32cd5c0a41f6a395b1b167897c7c21/raw/CodingSchool_TodoAPI_PostmanCollection.json
  3. Fix the port in the URL of each request
  4. Experiment with some Todos!

Want to learn more?

{
"info": {
"name": "Coding School - Todo API",
"_postman_id": "d540718e-0a74-2fb3-b499-62aa4614ec12",
"description": "Contains sample requests for testing the Todo API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Get All Todos",
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:52548/api/todo",
"protocol": "http",
"host": [
"localhost"
],
"port": "52548",
"path": [
"api",
"todo"
]
}
},
"response": []
},
{
"name": "Get One Todo",
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:52548/api/todo/1",
"protocol": "http",
"host": [
"localhost"
],
"port": "52548",
"path": [
"api",
"todo",
"1"
]
}
},
"response": []
},
{
"name": "Create New Todo",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"name\": \"Buy milk from local store\"\n}"
},
"url": {
"raw": "http://localhost:52548/api/todo",
"protocol": "http",
"host": [
"localhost"
],
"port": "52548",
"path": [
"api",
"todo"
]
}
},
"response": []
},
{
"name": "Update Todo",
"request": {
"method": "PUT",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"name\": \"Buy milk & corn flakes from local store\",\n\t\"completed\" : false\n}"
},
"url": {
"raw": "http://localhost:52548/api/todo/5",
"protocol": "http",
"host": [
"localhost"
],
"port": "52548",
"path": [
"api",
"todo",
"5"
]
}
},
"response": []
},
{
"name": "Update Todo (Completed)",
"request": {
"method": "PUT",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"name\": \"Buy milk & corn flakes from local store\",\n\t\"completed\" : true\n}"
},
"url": {
"raw": "http://localhost:52548/api/todo/5",
"protocol": "http",
"host": [
"localhost"
],
"port": "52548",
"path": [
"api",
"todo",
"5"
]
}
},
"response": []
},
{
"name": "Delete Todo",
"request": {
"method": "DELETE",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"disabled": true
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:52548/api/todo/5",
"protocol": "http",
"host": [
"localhost"
],
"port": "52548",
"path": [
"api",
"todo",
"5"
]
}
},
"response": []
},
{
"name": "Get all Todos (XML)",
"request": {
"method": "GET",
"header": [
{
"key": "Accept",
"value": "application/xml"
}
],
"body": {},
"url": {
"raw": "http://localhost:52548/api/todo",
"protocol": "http",
"host": [
"localhost"
],
"port": "52548",
"path": [
"api",
"todo"
]
},
"description": ""
},
"response": []
}
]
}
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using CodingSchool.TodoAPI.Models;
namespace CodingSchool.TodoAPI.Controllers
{
[Route("api/[controller]")]
public class TodoController : Controller
{
#region Todo List
private readonly static List<Todo> TodoList = new List<Todo>();
static TodoController()
{
TodoList.Add(new Todo { Id = 1, Name = "Catch all 648 Pokemons", Completed = false });
TodoList.Add(new Todo { Id = 2, Name = "Destroy the one ring in Mordor", Completed = false });
TodoList.Add(new Todo { Id = 3, Name = "Save Princess Peach", Completed = true });
TodoList.Add(new Todo { Id = 4, Name = "Go back to 1985", Completed = true });
}
#endregion
// GET api/todo
[HttpGet]
public IActionResult Get()
{
return Ok(TodoList);
}
[HttpGet]
[Route("/api/something/random")]
public IActionResult GetRandom()
{
if (TodoList.Count == 0)
{
return NotFound();
}
var index = new Random().Next(0, TodoList.Count);
var todo = TodoList[index];
return Ok(todo);
}
// GET api/todo/5
[HttpGet("{id}", Name = "GetById")]
public IActionResult Get(int id)
{
var todo = TodoList.FirstOrDefault(t => t.Id == id);
if (todo == null)
{
return NotFound();
}
return Ok(todo);
}
// POST api/todo
[HttpPost]
public IActionResult Post([FromBody] TodoCreate model)
{
if (model == null || string.IsNullOrEmpty(model.Name))
{
return BadRequest();
}
var todo = new Todo
{
Id = TodoList.Count + 1,
Name = model.Name,
Completed = false,
};
TodoList.Add(todo);
return CreatedAtRoute("GetById", new { id = todo.Id }, todo);
}
// PUT api/todo/5
[HttpPut("{id}")]
public IActionResult Put(int id, [FromBody] TodoUpdate model)
{
if (model == null || string.IsNullOrEmpty(model.Name))
{
return BadRequest();
}
var todo = TodoList.FirstOrDefault(t => t.Id == id);
if (todo == null)
{
return NotFound();
}
todo.Name = model.Name;
todo.Completed = model.Completed;
return Ok(todo);
}
// DELETE api/todo/5
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
var todo = TodoList.FirstOrDefault(t => t.Id == id);
if (todo == null)
{
return NotFound();
}
TodoList.Remove(todo);
return NoContent();
}
}
}
namespace CodingSchool.TodoAPI.Models
{
public class Todo
{
public int Id { get; set; }
public string Name { get; set; }
public bool Completed { get; set; }
}
public class TodoCreate
{
public string Name { get; set; }
}
public class TodoUpdate
{
public string Name { get; set; }
public bool Completed { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment