-
Notifications
You must be signed in to change notification settings - Fork 6k
Expand file tree
/
Copy pathUserModule.cs
More file actions
233 lines (189 loc) · 8.43 KB
/
Copy pathUserModule.cs
File metadata and controls
233 lines (189 loc) · 8.43 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using Nancy;
using Nancy.ModelBinding;
using System.Collections.Generic;
using Sharpility.Base;
using IO.Swagger.v2.Models;
using IO.Swagger.v2.Utils;
using NodaTime;
namespace IO.Swagger.v2.Modules
{
/// <summary>
/// Module processing requests of User domain.
/// </summary>
public sealed class UserModule : NancyModule
{
/// <summary>
/// Sets up HTTP methods mappings.
/// </summary>
/// <param name="service">Service handling requests</param>
public UserModule(UserService service) : base("/v2")
{
Post["/user"] = parameters =>
{
var body = this.Bind<User>();
Preconditions.IsNotNull(body, "Required parameter: 'body' is missing at 'CreateUser'");
service.CreateUser(Context, body);
return new Response { ContentType = "application/xml"};
};
Post["/user/createWithArray"] = parameters =>
{
var body = this.Bind<List<User>>();
Preconditions.IsNotNull(body, "Required parameter: 'body' is missing at 'CreateUsersWithArrayInput'");
service.CreateUsersWithArrayInput(Context, body);
return new Response { ContentType = "application/xml"};
};
Post["/user/createWithList"] = parameters =>
{
var body = this.Bind<List<User>>();
Preconditions.IsNotNull(body, "Required parameter: 'body' is missing at 'CreateUsersWithListInput'");
service.CreateUsersWithListInput(Context, body);
return new Response { ContentType = "application/xml"};
};
Delete["/user/{username}"] = parameters =>
{
var username = Parameters.ValueOf<string>(parameters, Context.Request, "username", ParameterType.Path);
Preconditions.IsNotNull(username, "Required parameter: 'username' is missing at 'DeleteUser'");
service.DeleteUser(Context, username);
return new Response { ContentType = "application/xml"};
};
Get["/user/{username}"] = parameters =>
{
var username = Parameters.ValueOf<string>(parameters, Context.Request, "username", ParameterType.Path);
Preconditions.IsNotNull(username, "Required parameter: 'username' is missing at 'GetUserByName'");
return service.GetUserByName(Context, username);
};
Get["/user/login"] = parameters =>
{
var username = Parameters.ValueOf<string>(parameters, Context.Request, "username", ParameterType.Query);
var password = Parameters.ValueOf<string>(parameters, Context.Request, "password", ParameterType.Query);
Preconditions.IsNotNull(username, "Required parameter: 'username' is missing at 'LoginUser'");
Preconditions.IsNotNull(password, "Required parameter: 'password' is missing at 'LoginUser'");
return service.LoginUser(Context, username, password);
};
Get["/user/logout"] = parameters =>
{
service.LogoutUser(Context);
return new Response { ContentType = "application/xml"};
};
Put["/user/{username}"] = parameters =>
{
var username = Parameters.ValueOf<string>(parameters, Context.Request, "username", ParameterType.Path);
var body = this.Bind<User>();
Preconditions.IsNotNull(username, "Required parameter: 'username' is missing at 'UpdateUser'");
Preconditions.IsNotNull(body, "Required parameter: 'body' is missing at 'UpdateUser'");
service.UpdateUser(Context, username, body);
return new Response { ContentType = "application/xml"};
};
}
}
/// <summary>
/// Service handling User requests.
/// </summary>
public interface UserService
{
/// <summary>
/// This can only be done by the logged in user.
/// </summary>
/// <param name="context">Context of request</param>
/// <param name="body">Created user object</param>
/// <returns></returns>
void CreateUser(NancyContext context, User body);
/// <summary>
///
/// </summary>
/// <param name="context">Context of request</param>
/// <param name="body">List of user object</param>
/// <returns></returns>
void CreateUsersWithArrayInput(NancyContext context, List<User> body);
/// <summary>
///
/// </summary>
/// <param name="context">Context of request</param>
/// <param name="body">List of user object</param>
/// <returns></returns>
void CreateUsersWithListInput(NancyContext context, List<User> body);
/// <summary>
/// This can only be done by the logged in user.
/// </summary>
/// <param name="context">Context of request</param>
/// <param name="username">The name that needs to be deleted</param>
/// <returns></returns>
void DeleteUser(NancyContext context, string username);
/// <summary>
///
/// </summary>
/// <param name="context">Context of request</param>
/// <param name="username">The name that needs to be fetched. Use user1 for testing.</param>
/// <returns>User</returns>
User GetUserByName(NancyContext context, string username);
/// <summary>
///
/// </summary>
/// <param name="context">Context of request</param>
/// <param name="username">The user name for login</param>
/// <param name="password">The password for login in clear text</param>
/// <returns>string</returns>
string LoginUser(NancyContext context, string username, string password);
/// <summary>
///
/// </summary>
/// <param name="context">Context of request</param>
/// <returns></returns>
void LogoutUser(NancyContext context);
/// <summary>
/// This can only be done by the logged in user.
/// </summary>
/// <param name="context">Context of request</param>
/// <param name="username">name that need to be deleted</param>
/// <param name="body">Updated user object</param>
/// <returns></returns>
void UpdateUser(NancyContext context, string username, User body);
}
/// <summary>
/// Abstraction of UserService.
/// </summary>
public abstract class AbstractUserService: UserService
{
public virtual void CreateUser(NancyContext context, User body)
{
CreateUser(body);
}
public virtual void CreateUsersWithArrayInput(NancyContext context, List<User> body)
{
CreateUsersWithArrayInput(body);
}
public virtual void CreateUsersWithListInput(NancyContext context, List<User> body)
{
CreateUsersWithListInput(body);
}
public virtual void DeleteUser(NancyContext context, string username)
{
DeleteUser(username);
}
public virtual User GetUserByName(NancyContext context, string username)
{
return GetUserByName(username);
}
public virtual string LoginUser(NancyContext context, string username, string password)
{
return LoginUser(username, password);
}
public virtual void LogoutUser(NancyContext context)
{
LogoutUser();
}
public virtual void UpdateUser(NancyContext context, string username, User body)
{
UpdateUser(username, body);
}
protected abstract void CreateUser(User body);
protected abstract void CreateUsersWithArrayInput(List<User> body);
protected abstract void CreateUsersWithListInput(List<User> body);
protected abstract void DeleteUser(string username);
protected abstract User GetUserByName(string username);
protected abstract string LoginUser(string username, string password);
protected abstract void LogoutUser();
protected abstract void UpdateUser(string username, User body);
}
}