Skip to content
Snippets Groups Projects

New: Get TOS Version for user

Merged Petar Hristov requested to merge Fix/xxxx-migrateLogin into dev

Files

@@ -5,6 +5,8 @@ using Coscine.Database.Models;
@@ -5,6 +5,8 @@ using Coscine.Database.Models;
using Coscine.Database.DataModel;
using Coscine.Database.DataModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Linq;
using Coscine.Api.User.ReturnObjects;
using Coscine.Api.User.ReturnObjects;
 
using System.Collections.Generic;
 
using System.Linq;
namespace Coscine.Api.User.Controllers
namespace Coscine.Api.User.Controllers
{
{
@@ -30,13 +32,34 @@ namespace Coscine.Api.User.Controllers
@@ -30,13 +32,34 @@ namespace Coscine.Api.User.Controllers
/// Returns the current TOS Version.
/// Returns the current TOS Version.
/// </summary>
/// </summary>
/// <returns>Current TOS Version as Json</returns>
/// <returns>Current TOS Version as Json</returns>
 
[AllowAnonymous]
 
[HttpGet("[controller]/version")]
 
public ActionResult<string> GetCurrentTOSVersion()
 
{
 
return Ok(Program.Configuration.GetStringAndWait("coscine/global/tos/version"));
 
}
 
 
/// <summary>
 
/// Returns the TOS Versions accepted by the current user.
 
/// </summary>
 
/// <returns>StatusCode</returns>
[HttpGet("[controller]")]
[HttpGet("[controller]")]
public ActionResult<Tos> GetCurrentTOSVersion()
public ActionResult<Tos> AcceptedTOSVersion()
{
{
return Json(new Tos
var user = _authenticator.GetUser();
 
var tosAccepted = _tosModel.GetAllWhere(tos => tos.UserId.Equals(user.Id));
 
 
if (tosAccepted is not null)
{
{
Version = Program.Configuration.GetStringAndWait("coscine/global/tos/version")
return Ok(new Tos
});
{
 
Version = tosAccepted.Select(tos => tos.Version).Distinct().ToList()
 
});;
 
}
 
else
 
{
 
return NotFound();
 
}
}
}
/// <summary>
/// <summary>
@@ -44,7 +67,7 @@ namespace Coscine.Api.User.Controllers
@@ -44,7 +67,7 @@ namespace Coscine.Api.User.Controllers
/// </summary>
/// </summary>
/// <returns>StatusCode</returns>
/// <returns>StatusCode</returns>
[HttpPost("[controller]")]
[HttpPost("[controller]")]
public ActionResult<int> AcceptCurrentTOSVersion()
public ActionResult<string> AcceptCurrentTOSVersion()
{
{
var user = _authenticator.GetUser();
var user = _authenticator.GetUser();
var tosAccepted = new Tosaccepted
var tosAccepted = new Tosaccepted
@@ -52,8 +75,14 @@ namespace Coscine.Api.User.Controllers
@@ -52,8 +75,14 @@ namespace Coscine.Api.User.Controllers
UserId = user.Id,
UserId = user.Id,
Version = Program.Configuration.GetStringAndWait("coscine/global/tos/version")
Version = Program.Configuration.GetStringAndWait("coscine/global/tos/version")
};
};
return Ok(_tosModel.Insert(tosAccepted));
}
 
var isAlreadyAccepted = _tosModel.GetAllWhere(t => t.UserId.Equals(user.Id) && t.Version.Equals(tosAccepted.Version)).Any();
 
if (isAlreadyAccepted)
 
{
 
return BadRequest($"TOS Version \"{tosAccepted.Version}\" is already accepted by the user.");
 
}
 
_tosModel.Insert(tosAccepted);
 
return Ok(tosAccepted.Version);
 
}
}
}
}
}
Loading