Skip to content

Commit 6730c79

Browse files
authoredNov 16, 2022
Merge pull request #11 from OthmanMoukhli/main
Added Service and Route to create a Session
2 parents c289329 + 2e01d86 commit 6730c79

File tree

5 files changed

+106
-20
lines changed

5 files changed

+106
-20
lines changed
 

‎dotnet-estimation/dotnet-estimation/Templates/WebAPI/Devon4Net.Application.WebAPI.Implementation/Business/SessionManagement/Controllers/SessionController.cs

+26-4
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,50 @@
1010
using Microsoft.AspNetCore.Http;
1111
using Microsoft.AspNetCore.Mvc;
1212
using Microsoft.AspNetCore.Authorization;
13-
1413
using Devon4Net.Infrastructure.JWT.Common.Const;
15-
1614
using System.Net;
1715
using Task = System.Threading.Tasks.Task;
1816
using System.Net.WebSockets;
17+
using LiteDB;
1918

2019
namespace Devon4Net.Application.WebAPI.Implementation.Business.SessionManagement.Controllers
2120
{
21+
/// <summary>
22+
/// Session controller
23+
/// </summary>
2224
[ApiController]
2325
[Route("[controller]")]
2426
[EnableCors("CorsPolicy")]
25-
public class SessionController : ControllerBase
27+
28+
public class SessionController: ControllerBase
2629
{
2730
private readonly ISessionService _sessionService;
2831
private readonly IWebSocketHandler _webSocketHandler;
29-
32+
3033
public SessionController(ISessionService SessionService, IWebSocketHandler webSocketHandler)
3134
{
3235
_sessionService = SessionService;
3336
_webSocketHandler = webSocketHandler;
3437
}
38+
39+
3540

41+
/// <summary>
42+
/// Creates a session
43+
/// </summary>
44+
/// <returns></returns>
45+
[HttpPost]
46+
[Route("/estimation/v1/session/newSession")]
47+
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
48+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
49+
[ProducesResponseType(StatusCodes.Status404NotFound)]
50+
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
51+
public async Task<ActionResult> CreateSession(SessionDto sessionDto)
52+
{
53+
Devon4NetLogger.Debug($"Create session that will expire at {sessionDto.ExpiresAt}");
54+
var result = await _sessionService.CreateSession(sessionDto);
55+
return StatusCode(StatusCodes.Status200OK, LiteDB.JsonSerializer.Serialize(result));
56+
}
3657
[HttpPut]
3758
[AllowAnonymous]
3859
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
@@ -137,4 +158,5 @@ public async Task<ActionResult> AddTask(long sessionId, [FromBody]TaskDto task)
137158
return BadRequest();
138159
}
139160
}
161+
140162
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using Devon4Net.Application.WebAPI.Implementation.Domain.Entities;
3+
using Devon4Net.Application.WebAPI.Implementation;
4+
namespace Devon4Net.Application.WebAPI.Implementation.Business.SessionManagement.Dtos
5+
{
6+
/// <summary>
7+
/// Session definition
8+
/// </summary>
9+
public class SessionDto
10+
{
11+
/// <summary>
12+
/// the Expiry Date
13+
/// </summary>
14+
[Required]
15+
public DateTime ExpiresAt { get; set; }
16+
}
17+
}
18+
19+
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
namespace Devon4Net.Application.WebAPI.Implementation.Business.SessionManagement.Exceptions
3+
{
4+
public class InvalidExpiryDateException : Exception
5+
{
6+
public InvalidExpiryDateException() : base($"ExpiryDate is Invalid") {}
7+
}
8+
}

‎dotnet-estimation/dotnet-estimation/Templates/WebAPI/Devon4Net.Application.WebAPI.Implementation/Business/SessionManagement/Service/ISessionService.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
using Devon4Net.Application.WebAPI.Implementation.Domain.Entities;
22
using Devon4Net.Application.WebAPI.Implementation.Business.SessionManagement.Dtos;
33

4+
using LiteDB;
5+
46
namespace Devon4Net.Application.WebAPI.Implementation.Business.SessionManagement.Service
57
{
68
/// <summary>
7-
/// TodoService interface
9+
/// ISessionService
810
/// </summary>
911
public interface ISessionService
1012
{
1113
public Task<Session> GetSession(long id);
1214

1315
public Task<(bool, Devon4Net.Application.WebAPI.Implementation.Domain.Entities.Task?)> GetStatus(long sessionId);
1416

17+
/// <summary>
18+
/// CreateSession
19+
/// </summary>
20+
/// <param name="sessionDto"></param>
21+
/// <returns></returns>
22+
public Task<BsonValue> CreateSession(SessionDto sessionDto);
1523
public Task<bool> InvalidateSession(long sessionId);
1624

1725
public Task<Estimation> AddNewEstimation(long sessionId , string VoteBy, int Conplexity);

‎dotnet-estimation/dotnet-estimation/Templates/WebAPI/Devon4Net.Application.WebAPI.Implementation/Business/SessionManagement/Service/SessionService.cs

+43-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,57 @@
1+
using System.Linq.Expressions;
2+
using System.Collections.Generic;
3+
using Devon4Net.Infrastructure.Logger.Logging;
14
using Devon4Net.Infrastructure.LiteDb.Repository;
25
using Devon4Net.Application.WebAPI.Implementation.Domain.Entities;
36
using Devon4Net.Application.WebAPI.Implementation.Business.SessionManagement.Exceptions;
47
using Devon4Net.Application.WebAPI.Implementation.Business.SessionManagement.Dtos;
8+
using System.Security.Cryptography;
9+
using LiteDB;
510

611
namespace Devon4Net.Application.WebAPI.Implementation.Business.SessionManagement.Service
712
{
8-
public class SessionService : ISessionService
13+
/// <summary>
14+
/// Session service implementation
15+
/// </summary>
16+
public class SessionService: ISessionService
917
{
1018
private readonly ILiteDbRepository<Session> _sessionRepository;
1119

12-
public SessionService(ILiteDbRepository<Session> SessionRepository)
20+
/// <summary>
21+
/// Constructor
22+
/// </summary>
23+
/// <param name="SessionRepository"></param>
24+
public SessionService(ILiteDbRepository<Session> SessionRepository)
1325
{
1426
_sessionRepository = SessionRepository;
1527
}
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+
1747
public async Task<Session> GetSession(long id)
1848
{
1949
var expression = LiteDB.Query.EQ("_id", id);
2050

2151
// FIXME: LiteDb also returs null values, when a matching entity does not exist!
2252
return _sessionRepository.GetFirstOrDefault(expression);
2353
}
24-
54+
2555
public async Task<bool> InvalidateSession(long sessionId)
2656
{
2757
Session sessionResult = await GetSession(sessionId);
@@ -40,7 +70,7 @@ public async Task<bool> InvalidateSession(long sessionId)
4070

4171
return _sessionRepository.Update(sessionResult);
4272
}
43-
73+
4474
public async Task<(bool, Domain.Entities.Task?)> GetStatus(long sessionId)
4575
{
4676
var sessionResult = await GetSession(sessionId);
@@ -94,7 +124,6 @@ public async Task<bool> InvalidateSession(long sessionId)
94124

95125
return (sessionIsValid, null);
96126
}
97-
98127
public async Task<Estimation> AddNewEstimation(long sessionId, string VoteBy, int Complexity)
99128
{
100129
var (isvalid, currentTask) = await GetStatus(sessionId);
@@ -123,13 +152,6 @@ public async Task<Estimation> AddNewEstimation(long sessionId, string VoteBy, in
123152

124153
return newEstimation;
125154
}
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>
133155
public async Task<bool> RemoveUserFromSession(long sessionId, string userId)
134156
{
135157
var expression = LiteDB.Query.EQ("_id", sessionId);
@@ -147,7 +169,6 @@ public async Task<bool> RemoveUserFromSession(long sessionId, string userId)
147169

148170
return false;
149171
}
150-
151172
/// <summary>
152173
/// Add an user to a given session
153174
/// </summary>
@@ -175,7 +196,6 @@ public async Task<bool> AddUserToSession(long sessionId, string userId, Role rol
175196
}
176197
return false;
177198
}
178-
179199
public async Task<bool> AddTaskToSession(long sessionId, TaskDto task)
180200
{
181201
/* var newSession = new Session
@@ -212,5 +232,13 @@ public async Task<bool> AddTaskToSession(long sessionId, TaskDto task)
212232
}
213233
return false;
214234
}
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+
}
215243
}
216244
}

0 commit comments

Comments
 (0)
Please sign in to comment.