forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServices.cs
More file actions
203 lines (174 loc) · 5.58 KB
/
Copy pathServices.cs
File metadata and controls
203 lines (174 loc) · 5.58 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//#define HTTP_LISTENER
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using ServiceStack.Auth;
using ServiceStack.Data;
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;
using ServiceStack.Text;
#if HTTP_LISTENER
namespace ServiceStack.Auth.Tests
#else
namespace ServiceStack.AuthWeb.Tests
#endif
{
[Route("/profile")]
public class GetUserProfile { }
public class UserProfile
{
public int Id { get; set; }
public UserAuth UserAuth { get; set; }
public AuthUserSession Session { get; set; }
public List<UserAuthDetails> UserAuthDetails { get; set; }
}
public class UserProfileResponse
{
public UserProfile Result { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
[Route("/lockallusers")]
public class LockAllUsers {}
public class LockServices : Service
{
public object Any(LockAllUsers request)
{
Db.UpdateOnly(new UserAuth { LockedDate = DateTime.UtcNow },
onlyFields: x => new { x.LockedDate },
where: x => x.LockedDate == null);
return HttpResult.Redirect("/");
}
}
[Authenticate]
public class UserProfileService : Service
{
public UserProfile Get(GetUserProfile request)
{
var session = base.SessionAs<CustomUserSession>();
var userAuthId = session.UserAuthId.ToInt();
var userProfile = new UserProfile
{
Id = userAuthId,
Session = session,
UserAuth = Db.SingleById<UserAuth>(userAuthId),
UserAuthDetails = Db.Select<UserAuthDetails>(x => x.UserAuthId == userAuthId),
};
return userProfile;
}
}
[Route("/reset-userauth")]
public class ResetUserAuth {}
public class ResetUserAuthService : Service
{
public object Get(ResetUserAuth request)
{
this.Cache.Remove(SessionFeature.GetSessionKey(Request));
Db.DeleteAll<UserAuth>();
Db.DeleteAll<UserAuthDetails>();
var referrer = Request.UrlReferrer != null
? Request.UrlReferrer.AbsoluteUri
: HttpHandlerFactory.GetBaseUrl();
return HttpResult.Redirect(referrer);
}
}
[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 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 bool Alive { get; set; }
public Rockstar() { }
public Rockstar(int id, string firstName, string lastName, int age)
{
Id = id;
FirstName = firstName;
LastName = lastName;
Age = age;
}
}
[DefaultRequest(typeof(Rockstars))]
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)
{
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" }
};
}
}
}