forked from bonesoul/uhttpsharp
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathStringsRestController.cs
More file actions
64 lines (55 loc) · 1.65 KB
/
StringsRestController.cs
File metadata and controls
64 lines (55 loc) · 1.65 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using uhttpsharp;
using uhttpsharp.Handlers;
namespace uhttpsharpdemo
{
class StringsRestController : IRestController<string>
{
private readonly ICollection<string> _collection = new HashSet<string>();
public Task<IEnumerable<string>> Get(IHttpRequest request)
{
return Task.FromResult<IEnumerable<string>>(_collection);
}
public Task<string> GetItem(IHttpRequest request)
{
var id = GetId(request);
if (_collection.Contains(id))
{
return Task.FromResult(id);
}
throw GetNotFoundException();
}
private static string GetId(IHttpRequest request)
{
var id = request.RequestParameters[1];
return id;
}
public Task<string> Create(IHttpRequest request)
{
var id = GetId(request);
_collection.Add(id);
return Task.FromResult(id);
}
public Task<string> Upsert(IHttpRequest request)
{
return Create(request);
}
public Task<string> Delete(IHttpRequest request)
{
var id = GetId(request);
if (_collection.Remove(id))
{
return Task.FromResult(id);
}
throw GetNotFoundException();
}
private static Exception GetNotFoundException()
{
return new HttpException(HttpResponseCode.NotFound, "The resource you've looked for is not found");
}
}
}