-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathNewApiTodos.cs
145 lines (121 loc) · 3.54 KB
/
NewApiTodos.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System.Collections.Generic;
using System.Linq;
using Funq;
using NUnit.Framework;
using ServiceStack;
namespace NewApi.Todos
{
public class AppHost : AppHostHttpListenerBase
{
public AppHost() : base("TODOs Tests", typeof(Todo).Assembly) {}
public override void Configure(Container container)
{
container.Register(new TodoRepository());
}
}
//REST Resource DTO
[Route("/todos")]
[Route("/todos/{Ids}")]
public class Todos : IReturn<List<Todo>>
{
public long[] Ids { get; set; }
public Todos() {}
public Todos(params long[] ids)
{
this.Ids = ids;
}
}
[Route("/todos", "POST")]
[Route("/todos/{Id}", "PUT")]
public class Todo : IReturn<Todo>
{
public long Id { get; set; }
public string Content { get; set; }
public int Order { get; set; }
public bool Done { get; set; }
}
public class TodosService : Service
{
public TodoRepository Repository { get; set; } //Injected by IOC
public object Get(Todos request)
{
return request.Ids.IsEmpty()
? Repository.GetAll()
: Repository.GetByIds(request.Ids);
}
public object Post(Todo todo)
{
return Repository.Store(todo);
}
public object Put(Todo todo)
{
return Repository.Store(todo);
}
public void Delete(Todos request)
{
Repository.DeleteByIds(request.Ids);
}
}
public class TodoRepository
{
List<Todo> todos = new List<Todo>();
public List<Todo> GetByIds(long[] ids)
{
return todos.Where(x => ids.Contains(x.Id)).ToList();
}
public List<Todo> GetAll()
{
return todos;
}
public Todo Store(Todo todo)
{
var existing = todos.FirstOrDefault(x => x.Id == todo.Id);
if (existing == null)
{
var newId = todos.Count > 0 ? todos.Max(x => x.Id) + 1 : 1;
todo.Id = newId;
}
todos.Add(todo);
return todo;
}
public void DeleteByIds(params long[] ids)
{
todos.RemoveAll(x => ids.Contains(x.Id));
}
}
[TestFixture]
public class NewApiTodosTests
{
const string BaseUri = "http://localhost:1337/";
AppHost appHost;
[OneTimeSetUp]
public void TestFixtureSetUp()
{
appHost = new AppHost();
appHost.Init();
appHost.Start(BaseUri);
}
[OneTimeTearDown]
public void TestFixtureTearDown()
{
appHost.Dispose();
}
[Test]
public void Run()
{
var restClient = new JsonServiceClient(BaseUri);
List<Todo> all = restClient.Get(new Todos());
Assert.That(all.Count, Is.EqualTo(0));
var todo = restClient.Post(new Todo { Content = "New TODO", Order = 1 });
Assert.That(todo.Id, Is.EqualTo(1));
all = restClient.Get(new Todos());
Assert.That(all.Count, Is.EqualTo(1));
todo.Content = "Updated TODO";
todo = restClient.Put(todo);
Assert.That(todo.Content, Is.EqualTo("Updated TODO"));
restClient.Delete(new Todos(todo.Id));
all = restClient.Get(new Todos());
Assert.That(all.Count, Is.EqualTo(0));
}
}
}