diff --git a/csharp/App/Backend/Controllers/Controller.cs b/csharp/App/Backend/Controllers/Controller.cs index 321e1b8bc..99d622456 100644 --- a/csharp/App/Backend/Controllers/Controller.cs +++ b/csharp/App/Backend/Controllers/Controller.cs @@ -2,95 +2,80 @@ using InnovEnergy.App.Backend.Database; using InnovEnergy.App.Backend.DataTypes; using InnovEnergy.App.Backend.DataTypes.Methods; using InnovEnergy.App.Backend.Relations; +using InnovEnergy.Lib.Utils; using Microsoft.AspNetCore.Mvc; -using static System.Net.HttpStatusCode; namespace InnovEnergy.App.Backend.Controllers; [ApiController] [Route("api/")] -public class Controller +public class Controller : ControllerBase { - private static readonly HttpResponseMessage _Unauthorized = new HttpResponseMessage(Unauthorized); - private static readonly HttpResponseMessage _Ok = new HttpResponseMessage(OK); - private static readonly HttpResponseMessage _BadRequest = new HttpResponseMessage(BadRequest); - - [Returns] - [Returns(Unauthorized)] - [Returns(BadRequest)] - [HttpPost($"{nameof(Login)}")] - public Object Login(Credentials credentials) + [HttpPost(nameof(Login))] + public ActionResult Login(Credentials credentials) { var session = credentials.Login(); return session is null - ? _Unauthorized - : session; + ? Unauthorized() + : session; } - [Returns(OK)] - [Returns(Unauthorized)] - [HttpPost($"{nameof(Logout)}")] - public Object Logout() + [HttpPost(nameof(Logout))] + public ActionResult Logout() { var session = GetSession(); return session.Logout() - ? _Ok - : _Unauthorized; + ? Ok() + : Unauthorized(); } - [Returns] - [Returns(Unauthorized)] - [HttpGet($"{nameof(GetUserById)}")] - public Object GetUserById(Int64 id) + [HttpGet(nameof(GetUserById))] + public ActionResult GetUserById(Int64 id) { var caller = GetSession()?.User; if (caller == null) - return _Unauthorized; + return Unauthorized(); var user = Db.GetUserById(id); if (user is null || !caller.HasAccessTo(user)) - return _Unauthorized; + return Unauthorized(); user.Password = ""; return user; } - [Returns] - [Returns(Unauthorized)] - [HttpGet($"{nameof(GetInstallationById)}")] - public Object GetInstallationById(Int64 id) + [HttpGet(nameof(GetInstallationById))] + public ActionResult GetInstallationById(Int64 id) { var user = GetSession()?.User; if (user == null) - return _Unauthorized; + return Unauthorized(); var installation = Db.GetInstallationById(id); if (installation is null || !user.HasAccessTo(installation)) - return _Unauthorized; + return Unauthorized(); return installation; } - [Returns] - [Returns(Unauthorized)] - [HttpGet($"{nameof(GetUsersWithAccessToInstallation)}")] - public Object GetUsersWithAccessToInstallation(Int64 id) + [HttpGet(nameof(GetUsersWithAccessToInstallation))] + public ActionResult> GetUsersWithAccessToInstallation(Int64 id) { var user = GetSession()?.User; if (user == null) - return _Unauthorized; + return Unauthorized(); var installation = Db.GetInstallationById(id); if (installation is null || !user.HasAccessTo(installation)) - return _Unauthorized; + return Unauthorized(); var directAccess = installation .UsersWithDirectAccess() @@ -103,128 +88,114 @@ public class Controller .Select(u => new { folderId = f.Id, user = u })); return directAccess - .Concat(inheritedAccess); + .Concat(inheritedAccess) + .Apply(Ok); // TODO: typing } - [Returns(Unauthorized)] - [HttpGet($"{nameof(GetUsersWithAccessToFolder)}")] - public Object GetUsersWithAccessToFolder(Int64 id) + [HttpGet(nameof(GetUsersWithAccessToFolder))] + public ActionResult> GetUsersWithAccessToFolder(Int64 id) { var user = GetSession()?.User; if (user == null) - return _Unauthorized; + return Unauthorized(); var folder = Db.GetFolderById(id); if (folder is null || !user.HasAccessTo(folder)) - return _Unauthorized; + return Unauthorized(); return folder .Ancestors() .Prepend(folder) .SelectMany(f => f.UsersWithDirectAccess() .Where(u => u.IsDescendantOf(user)) - .Select(u => new { folderId = f.Id, user = u })); + .Select(u => new { folderId = f.Id, user = u })) + .ToList(); } - [Returns] - [Returns(Unauthorized)] - [HttpGet($"{nameof(GetFolderById)}")] - public Object GetFolderById(Int64 id) + [HttpGet(nameof(GetFolderById))] + public ActionResult GetFolderById(Int64 id) { var user = GetSession()?.User; if (user == null) - return _Unauthorized; + return Unauthorized(); var folder = Db.GetFolderById(id); if (folder is null || !user.HasAccessTo(folder)) - return _Unauthorized; + return Unauthorized(); return folder; } - [Returns] // assuming swagger knows about arrays but not lists (JSON) - [Returns(Unauthorized)] - [HttpGet($"{nameof(GetAllInstallations)}/")] - public Object GetAllInstallations() + [HttpGet(nameof(GetAllInstallations))] + public ActionResult> GetAllInstallations() { var user = GetSession()?.User; + + if (user is null) + return Unauthorized(); - return user is null - ? _Unauthorized - : user.AccessibleInstallations(); + return user.AccessibleInstallations().ToList(); } - [Returns] // assuming swagger knows about arrays but not lists (JSON) - [Returns(Unauthorized)] - [HttpGet($"{nameof(GetAllFolders)}/")] - public Object GetAllFolders() + + [HttpGet(nameof(GetAllFolders))] + public ActionResult> GetAllFolders() { var user = GetSession()?.User; + + if (user is null) + return Unauthorized(); - return user is null - ? _Unauthorized - : user.AccessibleFolders(); + return new(user.AccessibleFolders()); } - [Returns] // assuming swagger knows about arrays but not lists (JSON) - [Returns(Unauthorized)] - [HttpGet($"{nameof(GetAllFoldersAndInstallations)}/")] - public Object GetAllFoldersAndInstallations() + + [HttpGet(nameof(GetAllFoldersAndInstallations))] + public ActionResult> GetAllFoldersAndInstallations() { var user = GetSession()?.User; - - return user is null - ? _Unauthorized - : user.AccessibleFoldersAndInstallations(); - } + if (user is null) + return Unauthorized(); + + return new (user.AccessibleFoldersAndInstallations()); + } - [Returns(OK)] - [Returns(Unauthorized)] - [HttpPost($"{nameof(CreateUser)}/")] - public Object CreateUser(User newUser) + [HttpPost(nameof(CreateUser))] + public ActionResult CreateUser(User newUser) { - var session = GetSession(); - - return session.Create(newUser) + return GetSession().Create(newUser) ? newUser - : _Unauthorized ; + : Unauthorized() ; } - [Returns(OK)] - [Returns(Unauthorized)] - [HttpPost($"{nameof(CreateInstallation)}/")] - public Object CreateInstallation(Installation installation) + [HttpPost(nameof(CreateInstallation))] + public async Task> CreateInstallation(Installation installation) + { + if (!await GetSession().Create(installation)) + return Unauthorized(); + + return installation; + } + + [HttpPost(nameof(CreateFolder))] + public ActionResult CreateFolder(Folder folder) { var session = GetSession(); - return session.Create(installation) - ? installation - : _Unauthorized; + if (!session.Create(folder)) + return Unauthorized(); + + return folder; } - [Returns(OK)] - [Returns(Unauthorized)] - [Returns(InternalServerError)] - [HttpPost($"{nameof(CreateFolder)}/")] - public Object CreateFolder(Folder folder) - { - var session = GetSession(); - - return session.Create(folder) - ? folder - : _Unauthorized; - } - - [Returns(OK)] - [Returns(Unauthorized)] - [HttpPost($"{nameof(GrantUserAccessToFolder)}/")] - public Object GrantUserAccessToFolder(FolderAccess folderAccess) + [HttpPost(nameof(GrantUserAccessToFolder))] + public ActionResult GrantUserAccessToFolder(FolderAccess folderAccess) { var session = GetSession(); @@ -233,15 +204,13 @@ public class Controller var user = Db.GetUserById(folderAccess.UserId); return session.GrantUserAccessTo(user, folder) - ? _Ok - : _Unauthorized; + ? Ok() + : Unauthorized(); } - [Returns(OK)] - [Returns(Unauthorized)] - [HttpPost($"{nameof(RevokeUserAccessToFolder)}/")] - public Object RevokeUserAccessToFolder(FolderAccess folderAccess) + [HttpPost(nameof(RevokeUserAccessToFolder))] + public ActionResult RevokeUserAccessToFolder(FolderAccess folderAccess) { var session = GetSession(); @@ -250,15 +219,13 @@ public class Controller var user = Db.GetUserById(folderAccess.UserId); return session.RevokeUserAccessTo(user, folder) - ? _Ok - : _Unauthorized; + ? Ok() + : Unauthorized(); } - [Returns(OK)] - [Returns(Unauthorized)] - [HttpPost($"{nameof(GrantUserAccessToInstallation)}/")] - public Object GrantUserAccessToInstallation(InstallationAccess installationAccess) + [HttpPost(nameof(GrantUserAccessToInstallation))] + public ActionResult GrantUserAccessToInstallation(InstallationAccess installationAccess) { var session = GetSession(); @@ -267,14 +234,12 @@ public class Controller var user = Db.GetUserById(installationAccess.UserId); return session.GrantUserAccessTo(user, installation) - ? _Ok - : _Unauthorized; + ? Ok() + : Unauthorized(); } - [Returns(OK)] - [Returns(Unauthorized)] - [HttpPost($"{nameof(RevokeUserAccessToInstallation)}/")] - public Object RevokeUserAccessToInstallation(InstallationAccess installationAccess) + [HttpPost(nameof(RevokeUserAccessToInstallation))] + public ActionResult RevokeUserAccessToInstallation(InstallationAccess installationAccess) { var session = GetSession(); @@ -283,88 +248,81 @@ public class Controller var user = Db.GetUserById(installationAccess.UserId); return session.RevokeUserAccessTo(user, installation) - ? _Ok - : _Unauthorized; + ? Ok() + : Unauthorized(); } - [Returns(OK)] - [Returns(Unauthorized)] - [HttpPut($"{nameof(UpdateUser)}/")] - public Object UpdateUser(User updatedUser) + [HttpPut(nameof(UpdateUser))] + public ActionResult UpdateUser(User updatedUser) { var session = GetSession(); - if (!session.Update(updatedUser)) return _Unauthorized; - updatedUser.Password = ""; + if (!session.Update(updatedUser)) + return Unauthorized(); + + updatedUser.Password = ""; // TODO: generic sanitize return values + return updatedUser; } - [Returns(OK)] - [Returns(Unauthorized)] - [HttpPut($"{nameof(UpdateInstallation)}/")] - public Object UpdateInstallation(Installation installation) + [HttpPut(nameof(UpdateInstallation))] + public ActionResult UpdateInstallation(Installation installation) { var session = GetSession(); + + if (!session.Update(installation)) + return Unauthorized(); - return session.Update(installation) - ? installation - : _Unauthorized; + return installation; } - [Returns(OK)] - [Returns(Unauthorized)] - [HttpPut($"{nameof(UpdateFolder)}/")] - public Object UpdateFolder(Folder folder) + [HttpPut(nameof(UpdateFolder))] + public ActionResult UpdateFolder(Folder folder) { var session = GetSession(); + + if (!session.Update(folder)) + return Unauthorized(); - return session.Update(folder) - ? folder - : _Unauthorized; + return folder; } - [Returns(OK)] - [Returns(Unauthorized)] - [HttpDelete($"{nameof(DeleteUser)}/")] - public Object DeleteUser(Int64 userId) + [HttpDelete(nameof(DeleteUser))] + public ActionResult DeleteUser(Int64 userId) { var session = GetSession(); var user = Db.GetUserById(userId); return session.Delete(user) - ? _Ok - : _Unauthorized; + ? Ok() + : Unauthorized(); } - [Returns(OK)] - [Returns(Unauthorized)] - [HttpDelete($"{nameof(DeleteInstallation)}/")] - public Object DeleteInstallation(Int64 installationId) + [HttpDelete(nameof(DeleteInstallation))] + public ActionResult DeleteInstallation(Int64 installationId) { var session = GetSession(); var installation = Db.GetInstallationById(installationId); return session.Delete(installation) - ? _Ok - : _Unauthorized; + ? Ok() + : Unauthorized(); } - - [ProducesResponseType(200)] - [ProducesResponseType(401)] - [HttpDelete($"{nameof(DeleteFolder)}/")] - public Object DeleteFolder(Int64 folderId) + + [HttpDelete(nameof(DeleteFolder))] + public ActionResult DeleteFolder(Int64 folderId) { var session = GetSession(); var folder = Db.GetFolderById(folderId); return session.Delete(folder) - ? _Ok - : _Unauthorized; + ? Ok() + : Unauthorized(); } diff --git a/csharp/App/Backend/Controllers/ReturnsAttribute.cs b/csharp/App/Backend/Controllers/ReturnsAttribute.cs deleted file mode 100644 index 2ef924055..000000000 --- a/csharp/App/Backend/Controllers/ReturnsAttribute.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Net; -using Microsoft.AspNetCore.Mvc; - -namespace InnovEnergy.App.Backend.Controllers; - -public class ReturnsAttribute : ProducesResponseTypeAttribute -{ - public ReturnsAttribute(HttpStatusCode statusCode) : base((Int32)statusCode) - { - } -} - -public class ReturnsAttribute : ProducesResponseTypeAttribute -{ - public ReturnsAttribute(HttpStatusCode statusCode) : base(typeof(T), (Int32)statusCode) - { - } - - public ReturnsAttribute() : base(typeof(T), (Int32)HttpStatusCode.OK) - { - } -} \ No newline at end of file diff --git a/csharp/App/Backend/DataTypes/Methods/Folder.cs b/csharp/App/Backend/DataTypes/Methods/Folder.cs index b8ac4c496..68c12c68b 100644 --- a/csharp/App/Backend/DataTypes/Methods/Folder.cs +++ b/csharp/App/Backend/DataTypes/Methods/Folder.cs @@ -80,7 +80,7 @@ public static class FolderMethods public static Boolean IsRelativeRoot(this Folder folder) { - return folder.ParentId < 0; + return folder.ParentId < 0; // TODO } public static Boolean WasMoved(this Folder folder) diff --git a/csharp/App/Backend/DataTypes/Methods/Session.cs b/csharp/App/Backend/DataTypes/Methods/Session.cs index 9bcdd2295..a1ecf900c 100644 --- a/csharp/App/Backend/DataTypes/Methods/Session.cs +++ b/csharp/App/Backend/DataTypes/Methods/Session.cs @@ -41,7 +41,7 @@ public static class SessionMethods } - public static Boolean Create(this Session? session, Installation? installation) + public static async Task Create(this Session? session, Installation? installation) { var user = session?.User; @@ -51,8 +51,8 @@ public static class SessionMethods && user.HasAccessTo(installation.Parent()) && Db.Create(installation) && Db.Create(new InstallationAccess { UserId = user.Id, InstallationId = installation.Id }) - && installation.CreateBucket().Result // TODO: await? - && installation.RenewS3BucketUrl().Result; // generation of access _after_ generation of + && await installation.CreateBucket() + && await installation.RenewS3BucketUrl(); // generation of access _after_ generation of // bucket to prevent "zombie" access-rights. } diff --git a/csharp/App/Backend/Program.cs b/csharp/App/Backend/Program.cs index e7500ff1a..fbf86d381 100644 --- a/csharp/App/Backend/Program.cs +++ b/csharp/App/Backend/Program.cs @@ -7,13 +7,11 @@ public static class Program { public static void Main(String[] args) { - Db.CreateFakeRelations(); + //Db.CreateFakeRelations(); var builder = WebApplication.CreateBuilder(args); - builder.Services.AddControllers(); // TODO: remove magic, specify controllers explicitly - // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle - + builder.Services.AddControllers(); builder.Services.AddHttpContextAccessor(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddCors(o => o.AddDefaultPolicy(p => p.WithOrigins("*").AllowAnyHeader().AllowAnyMethod())); @@ -22,6 +20,7 @@ public static class Program c.SwaggerDoc("v1", new OpenApiInfo { Title = "InnovEnergy Backend API", Version = "v1" }); c.UseAllOfToExtendReferenceSchemas(); c.OperationFilter(); //Todo testing throw me out + c.SupportNonNullableReferenceTypes(); }); diff --git a/csharp/App/Backend/db.sqlite b/csharp/App/Backend/db.sqlite index ea45e7dbd..8cddf6109 100644 Binary files a/csharp/App/Backend/db.sqlite and b/csharp/App/Backend/db.sqlite differ