From e8c93cb348a33fa630d3d9aec9067689dc51fb90 Mon Sep 17 00:00:00 2001 From: TypoMustakes Date: Wed, 24 Apr 2024 11:14:55 +0200 Subject: [PATCH] SignalR working? --- .../Controllers/CustomerController.cs | 10 ++++++++- .../Controllers/EmployeeController.cs | 10 ++++++++- .../Controllers/MaintainerTeamController.cs | 10 ++++++++- .../Controllers/ServiceController.cs | 14 ++++++++++--- .../Services/SignalRHub.cs | 21 +++++++++++++++++++ WD7UVN_HFT_2023241.Endpoint/Startup.cs | 4 ++++ .../WD7UVN_HFT_2023241.Endpoint.csproj | 9 ++++---- 7 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 WD7UVN_HFT_2023241.Endpoint/Services/SignalRHub.cs diff --git a/WD7UVN_HFT_2023241.Endpoint/Controllers/CustomerController.cs b/WD7UVN_HFT_2023241.Endpoint/Controllers/CustomerController.cs index 8f45632..4cf5734 100644 --- a/WD7UVN_HFT_2023241.Endpoint/Controllers/CustomerController.cs +++ b/WD7UVN_HFT_2023241.Endpoint/Controllers/CustomerController.cs @@ -3,6 +3,8 @@ using WD7UVN_HFT_2023241.Logic; using System.Linq; using WD7UVN_HFT_2023241.Models; using System; +using Microsoft.AspNetCore.SignalR; +using WD7UVN_HFT_2023241.Endpoint.Services; namespace WD7UVN_HFT_2023241.Endpoint { @@ -11,10 +13,12 @@ namespace WD7UVN_HFT_2023241.Endpoint public class CustomerController : ControllerBase { public ILogicServices LogicServices { get; set; } + IHubContext hub; - public CustomerController(ILogicServices LogicServices) + public CustomerController(ILogicServices LogicServices, IHubContext hub) { this.LogicServices = LogicServices; + this.hub = hub; } [HttpGet()] @@ -47,19 +51,23 @@ namespace WD7UVN_HFT_2023241.Endpoint public void PutCustomer([FromBody] Customer e) { LogicServices.CRUDOperations.CreateCustomer(e); + hub.Clients.All.SendAsync("CustomerCreated", e); } [HttpPost()] public void UpdateCustomer([FromBody] Customer e) { LogicServices.CRUDOperations.UpdateCustomer(e); + hub.Clients.All.SendAsync("CustomerUpdated", e); } //HttpClient does not support sending data in the body of a DELETE request. Instead, we can send the data in the URL like with a GET request. [HttpDelete("{id}")] public void DeleteCustomer(int id) { + Customer customer = LogicServices.CRUDOperations.ReadCustomer(id); LogicServices.CRUDOperations.DeleteCustomer(id); + hub.Clients.All.SendAsync("CustomerDeleted", customer); } } } diff --git a/WD7UVN_HFT_2023241.Endpoint/Controllers/EmployeeController.cs b/WD7UVN_HFT_2023241.Endpoint/Controllers/EmployeeController.cs index 3a10b24..4817972 100644 --- a/WD7UVN_HFT_2023241.Endpoint/Controllers/EmployeeController.cs +++ b/WD7UVN_HFT_2023241.Endpoint/Controllers/EmployeeController.cs @@ -3,6 +3,8 @@ using WD7UVN_HFT_2023241.Logic; using System.Linq; using System; using WD7UVN_HFT_2023241.Models; +using Microsoft.AspNetCore.SignalR; +using WD7UVN_HFT_2023241.Endpoint.Services; namespace WD7UVN_HFT_2023241.Endpoint { @@ -11,10 +13,12 @@ namespace WD7UVN_HFT_2023241.Endpoint public class EmployeeController : ControllerBase { public ILogicServices LogicServices { get; set; } + IHubContext hub; - public EmployeeController(ILogicServices LogicServices) + public EmployeeController(ILogicServices LogicServices, IHubContext hub) { this.LogicServices = LogicServices; + this.hub = hub; } [HttpGet()] @@ -47,19 +51,23 @@ namespace WD7UVN_HFT_2023241.Endpoint public void PutEmployee([FromBody] Employee e) { LogicServices.CRUDOperations.CreateEmployee(e); + hub.Clients.All.SendAsync("EmployeeCreated", e); } [HttpPost()] public void UpdateEmployee([FromBody] Employee e) { LogicServices.CRUDOperations.UpdateEmployee(e); + hub.Clients.All.SendAsync("EmployeeUpdated", e); } //HttpClient does not support sending data in the body of a DELETE request. Instead, we can send the data in the URL like with a GET request. [HttpDelete("{id}")] public void DeleteEmployee(int id) { + Employee e = LogicServices.CRUDOperations.ReadEmployee(id); LogicServices.CRUDOperations.DeleteEmployee(id); + hub.Clients.All.SendAsync("EmployeeDeleted", e); } } } diff --git a/WD7UVN_HFT_2023241.Endpoint/Controllers/MaintainerTeamController.cs b/WD7UVN_HFT_2023241.Endpoint/Controllers/MaintainerTeamController.cs index 3730f6c..5df0fee 100644 --- a/WD7UVN_HFT_2023241.Endpoint/Controllers/MaintainerTeamController.cs +++ b/WD7UVN_HFT_2023241.Endpoint/Controllers/MaintainerTeamController.cs @@ -3,6 +3,8 @@ using WD7UVN_HFT_2023241.Logic; using System.Linq; using System; using WD7UVN_HFT_2023241.Models; +using Microsoft.AspNetCore.SignalR; +using WD7UVN_HFT_2023241.Endpoint.Services; namespace WD7UVN_HFT_2023241.Endpoint { @@ -11,10 +13,12 @@ namespace WD7UVN_HFT_2023241.Endpoint public class MaintainerTeamController : ControllerBase { public ILogicServices LogicServices { get; set; } + IHubContext hub; - public MaintainerTeamController(ILogicServices LogicServices) + public MaintainerTeamController(ILogicServices LogicServices, IHubContext hub) { this.LogicServices = LogicServices; + this.hub = hub; } [HttpGet()] @@ -47,19 +51,23 @@ namespace WD7UVN_HFT_2023241.Endpoint public void PutMaintainerTeam([FromBody] MaintainerTeam e) { LogicServices.CRUDOperations.CreateMaintainerTeam(e); + hub.Clients.All.SendAsync("MaintainerTeamCreated", e); } [HttpPost()] public void UpdateMaintainerTeam([FromBody] MaintainerTeam e) { LogicServices.CRUDOperations.UpdateMaintainerTeam(e); + hub.Clients.All.SendAsync("MaintainerTeamUpdated", e); } //HttpClient does not support sending data in the body of a DELETE request. Instead, we can send the data in the URL like with a GET request. [HttpDelete("{id}")] public void DeleteMaintainerTeam(int id) { + MaintainerTeam maintainerTeam = LogicServices.CRUDOperations.ReadMaintainerTeam(id); LogicServices.CRUDOperations.DeleteMaintainerTeam(id); + hub.Clients.All.SendAsync("MaintainerTeamDeleted", maintainerTeam); } } } diff --git a/WD7UVN_HFT_2023241.Endpoint/Controllers/ServiceController.cs b/WD7UVN_HFT_2023241.Endpoint/Controllers/ServiceController.cs index d92127b..70a44bb 100644 --- a/WD7UVN_HFT_2023241.Endpoint/Controllers/ServiceController.cs +++ b/WD7UVN_HFT_2023241.Endpoint/Controllers/ServiceController.cs @@ -3,6 +3,8 @@ using WD7UVN_HFT_2023241.Logic; using System.Linq; using System; using WD7UVN_HFT_2023241.Models; +using Microsoft.AspNetCore.SignalR; +using WD7UVN_HFT_2023241.Endpoint.Services; namespace WD7UVN_HFT_2023241.Endpoint { @@ -11,10 +13,12 @@ namespace WD7UVN_HFT_2023241.Endpoint public class ServiceController : ControllerBase { public ILogicServices LogicServices { get; set; } + IHubContext hub; - public ServiceController(ILogicServices LogicServices) + public ServiceController(ILogicServices LogicServices, IHubContext hub) { this.LogicServices = LogicServices; + this.hub = hub; } [HttpGet()] @@ -43,23 +47,27 @@ namespace WD7UVN_HFT_2023241.Endpoint } } -[HttpPut()] + [HttpPut()] public void PutService([FromBody] Service e) { LogicServices.CRUDOperations.CreateService(e); + hub.Clients.All.SendAsync("ServiceCreated", e); } -[HttpPost()] + [HttpPost()] public void UpdateService([FromBody] Service e) { LogicServices.CRUDOperations.UpdateService(e); + hub.Clients.All.SendAsync("ServiceUpdated", e); } //HttpClient does not support sending data in the body of a DELETE request. Instead, we can send the data in the URL like with a GET request. [HttpDelete("{id}")] public void DeleteService(int id) { + Service service = LogicServices.CRUDOperations.ReadService(id); LogicServices.CRUDOperations.DeleteService(id); + hub.Clients.All.SendAsync("ServiceDeleted", service); } } } diff --git a/WD7UVN_HFT_2023241.Endpoint/Services/SignalRHub.cs b/WD7UVN_HFT_2023241.Endpoint/Services/SignalRHub.cs new file mode 100644 index 0000000..f857b35 --- /dev/null +++ b/WD7UVN_HFT_2023241.Endpoint/Services/SignalRHub.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; + +namespace WD7UVN_HFT_2023241.Endpoint.Services +{ + public class SignalRHub : Hub + { + public override Task OnConnectedAsync() + { + Clients.Caller.SendAsync("Connected", Context.ConnectionId); + return base.OnConnectedAsync(); + } + + public override Task OnDisconnectedAsync(Exception exception) + { + Clients.Caller.SendAsync("Disconnected", Context.ConnectionId); + return base.OnDisconnectedAsync(exception); + } + } +} diff --git a/WD7UVN_HFT_2023241.Endpoint/Startup.cs b/WD7UVN_HFT_2023241.Endpoint/Startup.cs index 9b43563..e5ce9ab 100644 --- a/WD7UVN_HFT_2023241.Endpoint/Startup.cs +++ b/WD7UVN_HFT_2023241.Endpoint/Startup.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; +using WD7UVN_HFT_2023241.Endpoint.Services; using WD7UVN_HFT_2023241.Logic; using WD7UVN_HFT_2023241.Repository; @@ -27,6 +28,8 @@ namespace WD7UVN_HFT_2023241.Endpoint services.AddTransient(); services.AddTransient(); + services.AddSignalR(); + services.AddControllers(); services.AddSwaggerGen(c => { @@ -61,6 +64,7 @@ namespace WD7UVN_HFT_2023241.Endpoint app.UseEndpoints(endpoints => { endpoints.MapControllers(); + endpoints.MapHub("/hub"); }); app.UseExceptionHandler(c => c.Run(async context => diff --git a/WD7UVN_HFT_2023241.Endpoint/WD7UVN_HFT_2023241.Endpoint.csproj b/WD7UVN_HFT_2023241.Endpoint/WD7UVN_HFT_2023241.Endpoint.csproj index 9023e9d..9a99fe8 100644 --- a/WD7UVN_HFT_2023241.Endpoint/WD7UVN_HFT_2023241.Endpoint.csproj +++ b/WD7UVN_HFT_2023241.Endpoint/WD7UVN_HFT_2023241.Endpoint.csproj @@ -6,12 +6,13 @@ + - - - - + + + +