From ce50b7ef3e85a3c3ce2f4b64874aa470a30b0024 Mon Sep 17 00:00:00 2001 From: Yinyin Liu Date: Thu, 18 Jul 2024 09:37:40 +0200 Subject: [PATCH] test: edit user action backend --- csharp/App/Backend/Controller.cs | 17 +++++++++++ .../App/Backend/DataTypes/Methods/Session.cs | 28 +++++++++++++++++++ csharp/App/Backend/Database/Create.cs | 13 +++++++++ csharp/App/Backend/Database/Update.cs | 5 ++++ 4 files changed, 63 insertions(+) diff --git a/csharp/App/Backend/Controller.cs b/csharp/App/Backend/Controller.cs index 12887d12e..76fe02908 100644 --- a/csharp/App/Backend/Controller.cs +++ b/csharp/App/Backend/Controller.cs @@ -798,6 +798,23 @@ public class Controller : ControllerBase } + [HttpPost(nameof(UpdateAction))] + public async Task>> UpdateAction([FromBody] UserAction action, Token authToken) + { + var session = Db.GetSession(authToken); + var actionSuccess = await session.UpdateUserAction(action); + return actionSuccess ? Ok() : Unauthorized(); + } + + + [HttpPost(nameof(DeleteAction))] + public async Task>> DeleteAction([FromBody] UserAction action, Token authToken) + { + var session = Db.GetSession(authToken); + var actionSuccess = await session.DeleteUserAction(action); + return actionSuccess ? Ok() : Unauthorized(); + } + [HttpPost(nameof(EditInstallationConfig))] public async Task>> EditInstallationConfig([FromBody] Configuration config, Int64 installationId,Token authToken) diff --git a/csharp/App/Backend/DataTypes/Methods/Session.cs b/csharp/App/Backend/DataTypes/Methods/Session.cs index 930547eda..27603db4f 100644 --- a/csharp/App/Backend/DataTypes/Methods/Session.cs +++ b/csharp/App/Backend/DataTypes/Methods/Session.cs @@ -144,6 +144,34 @@ public static class SessionMethods Db.HandleAction(action); return true; } + + public static async Task UpdateUserAction(this Session? session, UserAction action) + { + var user = session?.User; + + if (user is null || user.UserType == 0) + return false; + + action.UserName = user.Name; + + Db.UpdateAction(action); + return true; + } + + + public static async Task DeleteUserAction(this Session? session, UserAction action) + { + var user = session?.User; + + if (user is null || user.UserType == 0) + return false; + + action.UserName = user.Name; + + Db.Delete(action); + Console.WriteLine("---------------Deleted the Action in the database-----------------"); + return true; + } public static Boolean Delete(this Session? session, Folder? folder) { diff --git a/csharp/App/Backend/Database/Create.cs b/csharp/App/Backend/Database/Create.cs index e39bc5172..ae6a3e5f5 100644 --- a/csharp/App/Backend/Database/Create.cs +++ b/csharp/App/Backend/Database/Create.cs @@ -108,6 +108,19 @@ public static partial class Db } } + public static void UpdateAction(UserAction updatedAction) + { + var existingAction = UserActions.FirstOrDefault(action => action.Id == updatedAction.Id); + if (existingAction != null) + { + existingAction.Description = updatedAction.Description; + existingAction.Timestamp = updatedAction.Timestamp; + //Update(existingAction); + Console.WriteLine("---------------Updated the Action in the database-----------------"); + } + } + + public static void HandleError(Error newError,int installationId) { //Find the total number of errors for this installation diff --git a/csharp/App/Backend/Database/Update.cs b/csharp/App/Backend/Database/Update.cs index 1528530ff..70def9fd1 100644 --- a/csharp/App/Backend/Database/Update.cs +++ b/csharp/App/Backend/Database/Update.cs @@ -27,6 +27,11 @@ public static partial class Db { return Update(obj: warning); } + + // public static Boolean Update(UserAction action) + // { + // return Update(obj: action); + // } public static Boolean Update(Installation installation) {