forked from Code-Sharp/uHttpSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringsRestController.cs
More file actions
52 lines (37 loc) · 1.52 KB
/
Copy pathStringsRestController.cs
File metadata and controls
52 lines (37 loc) · 1.52 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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using uhttpsharp.Handlers;
namespace uhttpsharp.Demo {
internal 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();
}
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 string GetId(IHttpRequest request) {
var id = request.RequestParameters[1];
return id;
}
private static Exception GetNotFoundException() {
return new HttpException(HttpResponseCode.NotFound, "The resource you've looked for is not found");
}
}
}