forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppHost.cs
More file actions
156 lines (136 loc) · 4.41 KB
/
Copy pathAppHost.cs
File metadata and controls
156 lines (136 loc) · 4.41 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
using System.Collections.Generic;
using System.Runtime.Serialization;
using Funq;
using ServiceStack.Data;
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.Sqlite;
using ServiceStack.Razor;
using ServiceStack.Text;
namespace ServiceStack.RazorHostTests
{
[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 : Service
{
public IDbConnectionFactory DbFactory { get; set; }
public object Get(Rockstars request)
{
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.Scalar<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 object Post(Rockstars request)
{
using (var db = DbFactory.OpenDbConnection())
{
db.Insert(request.ConvertTo<Rockstar>());
return Get(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 : Service
{
public object Any(ViewThatUsesLayoutAndModel request)
{
return new ViewThatUsesLayoutAndModelResponse
{
Name = request.Id ?? "Foo",
Results = new List<string> { "Tom", "Dick", "Harry" }
};
}
}
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 AppHost : AppHostBase
{
public AppHost()
: base("Razor Test", typeof(AppHost).Assembly) { }
public override void Configure(Container container)
{
Plugins.Add(new RazorFormat {
//TemplateProvider = {CompileInParallelWithNoOfThreads = 0}
});
container.Register(new DataSource());
container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider));
using (var db = container.Resolve<IDbConnectionFactory>().OpenDbConnection())
{
db.CreateTable<Rockstar>(overwrite: false);
db.Insert(Rockstar.SeedData);
}
}
}
}