forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
106 lines (88 loc) · 2.4 KB
/
Copy pathProgram.cs
File metadata and controls
106 lines (88 loc) · 2.4 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using Funq;
using PclTest.ServiceModel;
using ServiceStack;
using ServiceStack.Text;
namespace PclTest
{
public class AppHost : AppHostHttpListenerBase
{
public AppHost()
: base("Pcl Test", typeof(WebServices).Assembly) { }
public override void Configure(Container container)
{
Plugins.Add(new CorsFeature());
Routes.AddFromAssembly(typeof(WebServices).Assembly);
container.Register(new TodoRepository());
}
}
public class WebServices : Service
{
public object Any(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name };
}
}
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
{
readonly 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));
}
}
class Program
{
static void Main(string[] args)
{
new AppHost()
.Init()
.Start("http://*:81/");
"http://localhost:81/".Print();
Console.ReadLine();
}
}
}