forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRazorAppHost.cs
More file actions
177 lines (155 loc) · 5.12 KB
/
RazorAppHost.cs
File metadata and controls
177 lines (155 loc) · 5.12 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Threading;
using Funq;
using NUnit.Framework;
using ServiceStack.Common;
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.Sqlite;
using ServiceStack.Razor;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface;
using ServiceStack.WebHost.Endpoints;
namespace ServiceStack.Auth.Tests
{
public class DataSource
{
public string[] Items = new[] { "Eeny", "meeny", "miny", "moe" };
}
public class Rockstar
{
public static Rockstar[] SeedData = new[] {
new Rockstar(1, "Jimi", "Hendrix", 27),
new Rockstar(2, "Janis", "Joplin", 27),
new Rockstar(3, "Jim", "Morrisson", 27),
new Rockstar(4, "Kurt", "Cobain", 27),
new Rockstar(5, "Elvis", "Presley", 42),
new Rockstar(6, "Michael", "Jackson", 50),
};
[AutoIncrement]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Age { get; set; }
public Rockstar() { }
public Rockstar(int id, string firstName, string lastName, int age)
{
Id = id;
FirstName = firstName;
LastName = lastName;
Age = age;
}
}
public class RazorAppHost : AppHostHttpListenerBase
{
public RazorAppHost()
: base("Test Razor", typeof(RazorAppHost).Assembly) {}
public override void Configure(Container container)
{
Plugins.Add(new RazorFormat());
container.Register(new DataSource());
container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory(":memory:", false, SqliteOrmLiteDialectProvider.Instance));
using (var db = container.Resolve<IDbConnectionFactory>().OpenDbConnection())
{
db.CreateTable<Rockstar>(overwrite: false);
db.Insert(Rockstar.SeedData);
}
}
}
[Route("/rockstars")]
[Route("/rockstars/aged/{Age}")]
[Route("/rockstars/delete/{Delete}")]
[Route("/rockstars/{Id}")]
public class Rockstars
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Age { get; set; }
public string Delete { get; set; }
}
[DataContract]
public class RockstarsResponse
{
[DataMember]
public int Total { get; set; }
[DataMember]
public int? Aged { get; set; }
[DataMember]
public List<Rockstar> Results { get; set; }
}
public class RockstarsService : RestServiceBase<Rockstars>
{
public IDbConnectionFactory DbFactory { get; set; }
public override object OnGet(Rockstars request)
{
using (var db = DbFactory.OpenDbConnection())
{
if (request.Delete == "reset")
{
db.DeleteAll<Rockstar>();
db.Insert(Rockstar.SeedData);
}
else if (request.Delete.IsInt())
{
db.DeleteById<Rockstar>(request.Delete.ToInt());
}
return new RockstarsResponse {
Aged = request.Age,
Total = db.GetScalar<int>("select count(*) from Rockstar"),
Results = request.Id != default(int) ?
db.Select<Rockstar>(q => q.Id == request.Id)
: request.Age.HasValue ?
db.Select<Rockstar>(q => q.Age == request.Age.Value)
: db.Select<Rockstar>()
};
}
}
public override object OnPost(Rockstars request)
{
using (var db = DbFactory.OpenDbConnection())
{
db.Insert(request.TranslateTo<Rockstar>());
return OnGet(new Rockstars());
}
}
}
[Route("/viewmodel/{Id}")]
public class ViewThatUsesLayoutAndModel
{
public string Id { get; set; }
}
public class ViewThatUsesLayoutAndModelResponse
{
public string Name { get; set; }
public List<string> Results { get; set; }
}
public class ViewService : ServiceBase<ViewThatUsesLayoutAndModel>
{
protected override object Run(ViewThatUsesLayoutAndModel request)
{
return new ViewThatUsesLayoutAndModelResponse {
Name = request.Id ?? "Foo",
Results = new List<string> { "Tom", "Dick", "Harry" }
};
}
}
[NUnit.Framework.Ignore]
[TestFixture]
public class RazorAppHostTests
{
[Test]
public void Hold_open_for_10Mins()
{
using (var appHost = new RazorAppHost())
{
appHost.Init();
appHost.Start("http://localhost:11000/");
Thread.Sleep(TimeSpan.FromMinutes(10));
}
}
}
}