2023-03-08 12:20:33 +00:00
|
|
|
using InnovEnergy.App.Backend.Database;
|
2023-03-15 13:38:06 +00:00
|
|
|
using InnovEnergy.App.Backend.DataTypes;
|
|
|
|
using InnovEnergy.App.Backend.DataTypes.Methods;
|
|
|
|
using InnovEnergy.App.Backend.Relations;
|
2023-02-16 12:57:06 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-03-15 13:38:06 +00:00
|
|
|
using static System.Net.HttpStatusCode;
|
|
|
|
using Folder = InnovEnergy.App.Backend.DataTypes.Folder;
|
|
|
|
using Installation = InnovEnergy.App.Backend.DataTypes.Installation;
|
|
|
|
using Object = System.Object;
|
|
|
|
using User = InnovEnergy.App.Backend.DataTypes.User;
|
2023-02-16 12:57:06 +00:00
|
|
|
|
2023-03-08 12:20:33 +00:00
|
|
|
namespace InnovEnergy.App.Backend.Controllers;
|
2023-02-16 12:57:06 +00:00
|
|
|
|
|
|
|
[ApiController]
|
2023-03-15 13:38:06 +00:00
|
|
|
[Route("api/")]
|
2023-02-16 12:57:06 +00:00
|
|
|
public class Controller
|
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
private static readonly HttpResponseMessage _Unauthorized = new HttpResponseMessage(Unauthorized);
|
|
|
|
private static readonly HttpResponseMessage _Ok = new HttpResponseMessage(OK);
|
|
|
|
private static readonly HttpResponseMessage _BadRequest = new HttpResponseMessage(BadRequest);
|
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
[Returns<String>]
|
2023-03-15 13:38:06 +00:00
|
|
|
[Returns(Unauthorized)]
|
|
|
|
[Returns(BadRequest)]
|
|
|
|
[HttpPost($"{nameof(Login)}")]
|
2023-02-23 10:26:55 +00:00
|
|
|
public Object Login(Credentials credentials)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var session = credentials.Login();
|
2023-03-09 16:21:31 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
return session is null
|
|
|
|
? _Unauthorized
|
|
|
|
: session;
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
2023-02-23 10:26:55 +00:00
|
|
|
|
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
[Returns(OK)]
|
|
|
|
[Returns(Unauthorized)]
|
|
|
|
[HttpPost($"{nameof(Logout)}")]
|
2023-02-21 08:58:21 +00:00
|
|
|
public Object Logout()
|
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var session = GetSession();
|
2023-02-21 08:58:21 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
return session.Logout()
|
|
|
|
? _Ok
|
|
|
|
: _Unauthorized;
|
2023-02-21 08:58:21 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 11:50:21 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
// [Returns<User>]
|
|
|
|
// [Returns(HttpStatusCode.Unauthorized)]
|
|
|
|
// [HttpGet($"{nameof(GetUserById)}")]
|
|
|
|
// public Object GetUserById(Int64 id)
|
|
|
|
// {
|
|
|
|
// var caller = GetCaller();
|
|
|
|
// if (caller is null)
|
|
|
|
// return new HttpResponseMessage(HttpStatusCode.Unauthorized);
|
|
|
|
//
|
|
|
|
// var user = Db.GetUserById(id);
|
|
|
|
//
|
|
|
|
// if (user is null || !caller.HasAccessTo(user))
|
|
|
|
// return new HttpResponseMessage(HttpStatusCode.Unauthorized);
|
|
|
|
//
|
|
|
|
// return user;
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
// [Returns<Installation>]
|
|
|
|
// [Returns(HttpStatusCode.Unauthorized)]
|
|
|
|
// [HttpGet($"{nameof(GetInstallationById)}")]
|
|
|
|
// public Object GetInstallationById(Int64 id)
|
|
|
|
// {
|
|
|
|
// var caller = GetCaller();
|
|
|
|
// if (caller == null)
|
|
|
|
// return new HttpResponseMessage(HttpStatusCode.Unauthorized);
|
|
|
|
//
|
|
|
|
// var installation = Db.GetInstallationById(id);
|
|
|
|
//
|
|
|
|
// if (installation is null || !caller.HasAccessTo(installation))
|
|
|
|
// return new HttpResponseMessage(HttpStatusCode.Unauthorized);
|
|
|
|
//
|
|
|
|
// return installation;
|
|
|
|
// }
|
2023-03-09 11:50:21 +00:00
|
|
|
|
2023-02-22 13:46:36 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
// [Returns<Folder>]
|
|
|
|
// [Returns(HttpStatusCode.Unauthorized)]
|
|
|
|
// [HttpGet($"{nameof(GetFolderById)}")]
|
|
|
|
// public Object GetFolderById(Int64 id)
|
|
|
|
// {
|
|
|
|
// var caller = GetCaller();
|
|
|
|
// if (caller == null)
|
|
|
|
// return new HttpResponseMessage(HttpStatusCode.Unauthorized);
|
|
|
|
//
|
|
|
|
// var folder = Db.GetFolderById(id);
|
|
|
|
//
|
|
|
|
// if (folder is null || !caller.HasAccessTo(folder))
|
|
|
|
// return new HttpResponseMessage(HttpStatusCode.Unauthorized);
|
|
|
|
//
|
|
|
|
// return folder;
|
|
|
|
// }
|
2023-02-16 14:08:50 +00:00
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
|
|
|
|
[Returns<Installation[]>] // assuming swagger knows about arrays but not lists (JSON)
|
2023-03-15 13:38:06 +00:00
|
|
|
[Returns(Unauthorized)]
|
|
|
|
[HttpGet($"{nameof(GetAllInstallations)}/")]
|
2023-02-16 12:57:06 +00:00
|
|
|
public Object GetAllInstallations()
|
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var user = GetSession()?.User;
|
2023-02-16 12:57:06 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
return user is null
|
|
|
|
? _Unauthorized
|
|
|
|
: user.AccessibleInstallations();
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
|
|
|
|
[Returns<Folder[]>] // assuming swagger knows about arrays but not lists (JSON)
|
2023-03-15 13:38:06 +00:00
|
|
|
[Returns(Unauthorized)]
|
|
|
|
[HttpGet($"{nameof(GetAllFolders)}/")]
|
2023-02-16 12:57:06 +00:00
|
|
|
public Object GetAllFolders()
|
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var user = GetSession()?.User;
|
2023-02-24 12:59:56 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
return user is null
|
|
|
|
? _Unauthorized
|
|
|
|
: user.AccessibleFolders();
|
2023-02-24 12:59:56 +00:00
|
|
|
}
|
2023-03-15 13:38:06 +00:00
|
|
|
|
|
|
|
// [Returns<Folder[]>] // assuming swagger knows about arrays but not lists (JSON)
|
|
|
|
// [Returns(Unauthorized)]
|
|
|
|
// [HttpGet($"{nameof(GetUsersOfFolder)}/")]
|
|
|
|
// public Object GetUsersOfFolder(Int64 folderId)
|
|
|
|
// {
|
|
|
|
// var caller = GetCaller();
|
|
|
|
// if (caller == null)
|
|
|
|
// return new HttpResponseMessage(Unauthorized);
|
|
|
|
//
|
|
|
|
// var folder = Db.GetFolderById(folderId);
|
|
|
|
//
|
|
|
|
// if (folder is null || !caller.HasAccessTo(folder))
|
|
|
|
// return new HttpResponseMessage(Unauthorized);
|
|
|
|
//
|
|
|
|
// return descendantUsers;
|
|
|
|
// }
|
2023-02-24 12:59:56 +00:00
|
|
|
|
2023-03-08 12:40:34 +00:00
|
|
|
[Returns<TreeNode[]>] // assuming swagger knows about arrays but not lists (JSON)
|
2023-03-15 13:38:06 +00:00
|
|
|
[Returns(Unauthorized)]
|
|
|
|
[HttpGet($"{nameof(GetAllFoldersAndInstallations)}/")]
|
2023-03-08 12:40:34 +00:00
|
|
|
public Object GetAllFoldersAndInstallations()
|
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var user = GetSession()?.User;
|
2023-03-08 12:40:34 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
return user is null
|
|
|
|
? _Unauthorized
|
|
|
|
: user.AccessibleFoldersAndInstallations();
|
2023-03-08 12:40:34 +00:00
|
|
|
}
|
2023-02-24 12:59:56 +00:00
|
|
|
|
2023-03-09 15:33:14 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
|
|
|
|
[Returns(OK)]
|
|
|
|
[Returns(Unauthorized)]
|
|
|
|
[HttpPost($"{nameof(CreateUser)}/")]
|
2023-03-09 15:33:14 +00:00
|
|
|
public Object CreateUser(User newUser)
|
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var session = GetSession();
|
2023-02-24 12:59:56 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
return session.Create(newUser)
|
|
|
|
? newUser
|
|
|
|
: _Unauthorized ;
|
2023-03-09 15:33:14 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
[Returns(OK)]
|
|
|
|
[Returns(Unauthorized)]
|
|
|
|
[HttpPost($"{nameof(CreateInstallation)}/")]
|
2023-03-09 15:33:14 +00:00
|
|
|
public Object CreateInstallation(Installation installation)
|
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var session = GetSession();
|
2023-03-09 15:33:14 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
return session.Create(installation)
|
|
|
|
? installation
|
|
|
|
: _Unauthorized;
|
2023-03-09 15:33:14 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
[Returns(OK)]
|
|
|
|
[Returns(Unauthorized)]
|
|
|
|
[Returns(InternalServerError)]
|
|
|
|
[HttpPost($"{nameof(CreateFolder)}/")]
|
2023-03-09 15:33:14 +00:00
|
|
|
public Object CreateFolder(Folder folder)
|
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var session = GetSession();
|
2023-03-09 15:33:14 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
return session.Create(folder)
|
|
|
|
? folder
|
|
|
|
: _Unauthorized;
|
2023-03-09 15:33:14 +00:00
|
|
|
}
|
2023-02-24 12:59:56 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
[Returns(OK)]
|
|
|
|
[Returns(Unauthorized)]
|
|
|
|
[HttpPut($"{nameof(UpdateUser)}/")]
|
2023-02-16 12:57:06 +00:00
|
|
|
public Object UpdateUser(User updatedUser)
|
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var session = GetSession();
|
|
|
|
|
|
|
|
return session.Update(updatedUser)
|
|
|
|
? updatedUser
|
|
|
|
: _Unauthorized;
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
[Returns(OK)]
|
|
|
|
[Returns(Unauthorized)]
|
|
|
|
[HttpPut($"{nameof(UpdateInstallation)}/")]
|
2023-02-24 12:59:56 +00:00
|
|
|
public Object UpdateInstallation(Installation installation)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var session = GetSession();
|
2023-02-16 12:57:06 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
return session.Update(installation)
|
|
|
|
? installation
|
|
|
|
: _Unauthorized;
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 10:26:55 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
[Returns(OK)]
|
|
|
|
[Returns(Unauthorized)]
|
|
|
|
[HttpPut($"{nameof(UpdateFolder)}/")]
|
2023-02-24 11:58:47 +00:00
|
|
|
public Object UpdateFolder(Folder folder)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var session = GetSession();
|
2023-02-16 12:57:06 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
return session.Update(folder)
|
|
|
|
? folder
|
|
|
|
: _Unauthorized;
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
[Returns(OK)]
|
|
|
|
[Returns(Unauthorized)]
|
|
|
|
[HttpDelete($"{nameof(DeleteUser)}/")]
|
2023-02-16 12:57:06 +00:00
|
|
|
public Object DeleteUser(Int64 userId)
|
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var session = GetSession();
|
|
|
|
var user = Db.GetUserById(userId);
|
2023-02-24 11:58:47 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
return session.Delete(user)
|
|
|
|
? _Ok
|
|
|
|
: _Unauthorized;
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
[Returns(OK)]
|
|
|
|
[Returns(Unauthorized)]
|
|
|
|
[HttpDelete($"{nameof(DeleteInstallation)}/")]
|
2023-02-24 11:58:47 +00:00
|
|
|
public Object DeleteInstallation(Int64 installationId)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var session = GetSession();
|
|
|
|
var installation = Db.GetInstallationById(installationId);
|
2023-02-23 10:26:55 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
return session.Delete(installation)
|
|
|
|
? _Ok
|
|
|
|
: _Unauthorized;
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[ProducesResponseType(200)]
|
|
|
|
[ProducesResponseType(401)]
|
2023-03-15 13:38:06 +00:00
|
|
|
[HttpDelete($"{nameof(DeleteFolder)}/")]
|
2023-02-16 12:57:06 +00:00
|
|
|
public Object DeleteFolder(Int64 folderId)
|
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var session = GetSession();
|
2023-02-23 10:26:55 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
var folder = Db.GetFolderById(folderId);
|
|
|
|
|
|
|
|
return session.Delete(folder)
|
|
|
|
? _Ok
|
|
|
|
: _Unauthorized;
|
2023-02-16 12:57:06 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
private static Session? GetSession()
|
2023-02-24 11:58:47 +00:00
|
|
|
{
|
|
|
|
var ctxAccessor = new HttpContextAccessor();
|
2023-03-15 13:38:06 +00:00
|
|
|
return ctxAccessor.HttpContext?.Items["Session"] as Session;
|
2023-02-24 11:58:47 +00:00
|
|
|
}
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 11:58:47 +00:00
|
|
|
|
|
|
|
|