1
+ using System . Linq . Expressions ;
2
+ using System . Collections . Generic ;
3
+ using Devon4Net . Infrastructure . Logger . Logging ;
1
4
using Devon4Net . Infrastructure . LiteDb . Repository ;
2
5
using Devon4Net . Application . WebAPI . Implementation . Domain . Entities ;
3
6
using Devon4Net . Application . WebAPI . Implementation . Business . SessionManagement . Exceptions ;
4
7
using Devon4Net . Application . WebAPI . Implementation . Business . SessionManagement . Dtos ;
8
+ using System . Security . Cryptography ;
9
+ using LiteDB ;
5
10
6
11
namespace Devon4Net . Application . WebAPI . Implementation . Business . SessionManagement . Service
7
12
{
8
- public class SessionService : ISessionService
13
+ /// <summary>
14
+ /// Session service implementation
15
+ /// </summary>
16
+ public class SessionService : ISessionService
9
17
{
10
18
private readonly ILiteDbRepository < Session > _sessionRepository ;
11
19
12
- public SessionService ( ILiteDbRepository < Session > SessionRepository )
20
+ /// <summary>
21
+ /// Constructor
22
+ /// </summary>
23
+ /// <param name="SessionRepository"></param>
24
+ public SessionService ( ILiteDbRepository < Session > SessionRepository )
13
25
{
14
26
_sessionRepository = SessionRepository ;
15
27
}
16
-
28
+ /// <summary>
29
+ /// Creates the Session
30
+ /// </summary>
31
+ /// <param name="sessionDto"></param>
32
+ /// <returns></returns>
33
+ public async Task < BsonValue > CreateSession ( SessionDto sessionDto )
34
+ {
35
+ if ( sessionDto . ExpiresAt <= DateTime . Now || sessionDto . ExpiresAt == null )
36
+ {
37
+ throw new InvalidExpiryDateException ( ) ;
38
+ }
39
+ return _sessionRepository . Create ( new Session {
40
+ InviteToken = generateInviteToken ( ) ,
41
+ ExpiresAt = sessionDto . ExpiresAt ,
42
+ Tasks = new List < Domain . Entities . Task > ( ) ,
43
+ Users = new List < Domain . Entities . User > ( )
44
+ } ) ;
45
+ }
46
+
17
47
public async Task < Session > GetSession ( long id )
18
48
{
19
49
var expression = LiteDB . Query . EQ ( "_id" , id ) ;
20
50
21
51
// FIXME: LiteDb also returs null values, when a matching entity does not exist!
22
52
return _sessionRepository . GetFirstOrDefault ( expression ) ;
23
53
}
24
-
54
+
25
55
public async Task < bool > InvalidateSession ( long sessionId )
26
56
{
27
57
Session sessionResult = await GetSession ( sessionId ) ;
@@ -40,7 +70,7 @@ public async Task<bool> InvalidateSession(long sessionId)
40
70
41
71
return _sessionRepository . Update ( sessionResult ) ;
42
72
}
43
-
73
+
44
74
public async Task < ( bool , Domain . Entities . Task ? ) > GetStatus ( long sessionId )
45
75
{
46
76
var sessionResult = await GetSession ( sessionId ) ;
@@ -94,7 +124,6 @@ public async Task<bool> InvalidateSession(long sessionId)
94
124
95
125
return ( sessionIsValid , null ) ;
96
126
}
97
-
98
127
public async Task < Estimation > AddNewEstimation ( long sessionId , string VoteBy , int Complexity )
99
128
{
100
129
var ( isvalid , currentTask ) = await GetStatus ( sessionId ) ;
@@ -123,13 +152,6 @@ public async Task<Estimation> AddNewEstimation(long sessionId, string VoteBy, in
123
152
124
153
return newEstimation ;
125
154
}
126
-
127
- /// <summary>
128
- /// ARemove a specified user from a given session
129
- /// </summary>
130
- /// <param name="sessionId"></param>
131
- /// <param name="userId"></param>
132
- /// <returns></returns>
133
155
public async Task < bool > RemoveUserFromSession ( long sessionId , string userId )
134
156
{
135
157
var expression = LiteDB . Query . EQ ( "_id" , sessionId ) ;
@@ -147,7 +169,6 @@ public async Task<bool> RemoveUserFromSession(long sessionId, string userId)
147
169
148
170
return false ;
149
171
}
150
-
151
172
/// <summary>
152
173
/// Add an user to a given session
153
174
/// </summary>
@@ -175,7 +196,6 @@ public async Task<bool> AddUserToSession(long sessionId, string userId, Role rol
175
196
}
176
197
return false ;
177
198
}
178
-
179
199
public async Task < bool > AddTaskToSession ( long sessionId , TaskDto task )
180
200
{
181
201
/* var newSession = new Session
@@ -212,5 +232,13 @@ public async Task<bool> AddTaskToSession(long sessionId, TaskDto task)
212
232
}
213
233
return false ;
214
234
}
235
+
236
+ private string generateInviteToken ( )
237
+ { //generates 8 random bytes and returns them as a token string
238
+ byte [ ] randomNumber = new byte [ 8 ] ;
239
+ RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider ( ) ;
240
+ rngCsp . GetNonZeroBytes ( randomNumber ) ;
241
+ return BitConverter . ToString ( randomNumber ) ;
242
+ }
215
243
}
216
244
}
0 commit comments