-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathTeamsController.cs
More file actions
63 lines (52 loc) · 2.08 KB
/
TeamsController.cs
File metadata and controls
63 lines (52 loc) · 2.08 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
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using WebApi.OutputCache.V2.TimeAttributes;
namespace WebApi.OutputCache.V2.Demo
{
public class TeamsController : ApiController
{
private static readonly List<Team> Teams = new List<Team>
{
new Team {Id = 1, League = "NHL", Name = "Leafs"},
new Team {Id = 2, League = "NHL", Name = "Habs"},
};
[CacheOutput(ClientTimeSpan = 50, ServerTimeSpan = 50)]
public IEnumerable<Team> Get()
{
return Teams;
}
[CacheOutputUntil(2016, 7, 20)]
public Team GetById(int id)
{
var team = Teams.FirstOrDefault(i => i.Id == id);
if (team == null) throw new HttpResponseException(HttpStatusCode.NotFound);
return team;
}
[InvalidateCacheOutput("Get")]
public void Post(Team value)
{
if (!ModelState.IsValid) throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
Teams.Add(value);
}
public async Task Put(int id, Team value)
{
if (!ModelState.IsValid) throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
var team = Teams.FirstOrDefault(i => i.Id == id);
if (team == null) throw new HttpResponseException(HttpStatusCode.NotFound);
team.League = value.League;
team.Name = value.Name;
var cache = Configuration.CacheOutputConfiguration().GetCacheOutputProvider(Request);
await cache.RemoveStartsWithAsync(Configuration.CacheOutputConfiguration().MakeBaseCachekey((TeamsController t) => t.GetById(0)));
}
public void Delete(int id)
{
var team = Teams.FirstOrDefault(i => i.Id == id);
if (team == null) throw new HttpResponseException(HttpStatusCode.NotFound);
Teams.Remove(team);
}
}
}