forked from martijnboland/VSReact
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connected Redux TodoMVC with Web API todo backend
- Loading branch information
1 parent
a95ff07
commit ebaa587
Showing
18 changed files
with
641 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Net.Http.Formatting; | ||
using System.Net.Http.Headers; | ||
|
||
namespace VSReact.Api.App_Start | ||
{ | ||
public class JsonContentNegotiator : IContentNegotiator | ||
{ | ||
private readonly JsonMediaTypeFormatter _jsonFormatter; | ||
|
||
public JsonContentNegotiator(JsonMediaTypeFormatter formatter) | ||
{ | ||
_jsonFormatter = formatter; | ||
} | ||
|
||
public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters) | ||
{ | ||
var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json")); | ||
return result; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Web.Http; | ||
|
||
namespace TodoMvc.Controllers | ||
{ | ||
public class TodoController : ApiController | ||
{ | ||
private readonly Data.TodoRepository repo; | ||
|
||
public TodoController() | ||
{ | ||
repo = new Data.TodoRepository(); | ||
} | ||
|
||
// GET: Todo | ||
public IEnumerable<Models.Todo> Get() | ||
{ | ||
var items = repo.GetAll(); | ||
|
||
foreach (var t in items) | ||
{ | ||
t.Url = String.Format("{0}/{1}", Request.RequestUri.ToString(), t.Id.ToString()); | ||
} | ||
|
||
return items; | ||
} | ||
|
||
// GET: Todo/5 | ||
public Models.Todo Get(int id) | ||
{ | ||
var t = repo.Get(id); | ||
if (t != null) | ||
{ | ||
t.Url = Request.RequestUri.ToString(); | ||
} | ||
return t; | ||
} | ||
|
||
// POST: Todo | ||
public HttpResponseMessage Post(Models.Todo item) | ||
{ | ||
var t = repo.Save(item); | ||
t.Url = String.Format("{0}/{1}", Request.RequestUri.ToString(), t.Id.ToString()); | ||
|
||
var response = Request.CreateResponse<Models.Todo>(HttpStatusCode.OK, t); | ||
response.Headers.Location = new Uri(Request.RequestUri, "todo/" + t.Id.ToString()); | ||
|
||
return response; | ||
} | ||
|
||
// PATCH: Todo/5 | ||
public HttpResponseMessage Patch(int id, Models.Todo item) | ||
{ | ||
var todo = repo.Get(id); | ||
if (item.Title != null) | ||
{ | ||
todo.Title = item.Title; | ||
} | ||
if (item.Completed.HasValue) | ||
{ | ||
todo.Completed = item.Completed; | ||
} | ||
var t = repo.Save(todo); | ||
t.Url = Request.RequestUri.ToString(); | ||
|
||
var response = Request.CreateResponse<Models.Todo>(HttpStatusCode.OK, t); | ||
response.Headers.Location = new Uri(Request.RequestUri, t.Id.ToString()); | ||
|
||
return response; | ||
} | ||
|
||
// DELETE: Todo | ||
public void Delete() | ||
{ | ||
repo.Delete(); | ||
} | ||
|
||
// DELETE: Todo/5 | ||
public void Delete(int id) | ||
{ | ||
repo.Delete(id); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web; | ||
|
||
namespace TodoMvc.Models | ||
{ | ||
public class Todo | ||
{ | ||
public int Id { get; set; } | ||
public int? Order { get; set; } | ||
public string Title { get; set; } | ||
public string Url { get; set; } | ||
public bool? Completed { get; set; } | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
var todo = obj as Todo; | ||
return (todo != null) && (Id == todo.Id); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return Id.GetHashCode(); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web; | ||
|
||
namespace TodoMvc.Data | ||
{ | ||
public class TodoRepository | ||
{ | ||
public static List<Models.Todo> Todos = new List<Models.Todo>(); | ||
public static int MaxId = 0; | ||
|
||
public IEnumerable<Models.Todo> GetAll() | ||
{ | ||
return Todos.OrderByDescending(t => t.Order); | ||
} | ||
|
||
public Models.Todo Get(int id) | ||
{ | ||
return Todos.Where(t => t.Id == id).FirstOrDefault(); | ||
} | ||
|
||
public Models.Todo Save(Models.Todo item) | ||
{ | ||
if (item.Id == 0) | ||
{ | ||
item.Id = ++MaxId; | ||
if (!item.Order.HasValue) | ||
{ | ||
item.Order = item.Id; | ||
} | ||
} | ||
|
||
int index = Todos.IndexOf(item); | ||
if (index != -1) | ||
{ | ||
Todos[index] = item; | ||
} | ||
else | ||
{ | ||
Todos.Add(item); | ||
} | ||
|
||
return item; | ||
} | ||
|
||
public void Delete() | ||
{ | ||
Todos.Clear(); | ||
} | ||
|
||
public void Delete(int id) | ||
{ | ||
Todos.RemoveAll(t => t.Id == id); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"ecmaFeatures": { | ||
"jsx": true, | ||
"modules": true | ||
}, | ||
"env": { | ||
"browser": true, | ||
"node": true | ||
}, | ||
"parser": "babel-eslint", | ||
"rules": { | ||
"quotes": [2, "single"], | ||
"strict": [2, "never"], | ||
"react/jsx-uses-react": 2, | ||
"react/jsx-uses-vars": 2, | ||
"react/react-in-jsx-scope": 2 | ||
}, | ||
"plugins": [ | ||
"react" | ||
] | ||
} |
This file contains 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
Oops, something went wrong.