SignalR working?

This commit is contained in:
TypoMustakes
2024-04-24 11:14:55 +02:00
parent ce81ab4070
commit e8c93cb348
7 changed files with 68 additions and 10 deletions

View File

@@ -3,6 +3,8 @@ using WD7UVN_HFT_2023241.Logic;
using System.Linq; using System.Linq;
using WD7UVN_HFT_2023241.Models; using WD7UVN_HFT_2023241.Models;
using System; using System;
using Microsoft.AspNetCore.SignalR;
using WD7UVN_HFT_2023241.Endpoint.Services;
namespace WD7UVN_HFT_2023241.Endpoint namespace WD7UVN_HFT_2023241.Endpoint
{ {
@@ -11,10 +13,12 @@ namespace WD7UVN_HFT_2023241.Endpoint
public class CustomerController : ControllerBase public class CustomerController : ControllerBase
{ {
public ILogicServices LogicServices { get; set; } public ILogicServices LogicServices { get; set; }
IHubContext<SignalRHub> hub;
public CustomerController(ILogicServices LogicServices) public CustomerController(ILogicServices LogicServices, IHubContext<SignalRHub> hub)
{ {
this.LogicServices = LogicServices; this.LogicServices = LogicServices;
this.hub = hub;
} }
[HttpGet()] [HttpGet()]
@@ -47,19 +51,23 @@ namespace WD7UVN_HFT_2023241.Endpoint
public void PutCustomer([FromBody] Customer e) public void PutCustomer([FromBody] Customer e)
{ {
LogicServices.CRUDOperations.CreateCustomer(e); LogicServices.CRUDOperations.CreateCustomer(e);
hub.Clients.All.SendAsync("CustomerCreated", e);
} }
[HttpPost()] [HttpPost()]
public void UpdateCustomer([FromBody] Customer e) public void UpdateCustomer([FromBody] Customer e)
{ {
LogicServices.CRUDOperations.UpdateCustomer(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. //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}")] [HttpDelete("{id}")]
public void DeleteCustomer(int id) public void DeleteCustomer(int id)
{ {
Customer customer = LogicServices.CRUDOperations.ReadCustomer(id);
LogicServices.CRUDOperations.DeleteCustomer(id); LogicServices.CRUDOperations.DeleteCustomer(id);
hub.Clients.All.SendAsync("CustomerDeleted", customer);
} }
} }
} }

View File

@@ -3,6 +3,8 @@ using WD7UVN_HFT_2023241.Logic;
using System.Linq; using System.Linq;
using System; using System;
using WD7UVN_HFT_2023241.Models; using WD7UVN_HFT_2023241.Models;
using Microsoft.AspNetCore.SignalR;
using WD7UVN_HFT_2023241.Endpoint.Services;
namespace WD7UVN_HFT_2023241.Endpoint namespace WD7UVN_HFT_2023241.Endpoint
{ {
@@ -11,10 +13,12 @@ namespace WD7UVN_HFT_2023241.Endpoint
public class EmployeeController : ControllerBase public class EmployeeController : ControllerBase
{ {
public ILogicServices LogicServices { get; set; } public ILogicServices LogicServices { get; set; }
IHubContext<SignalRHub> hub;
public EmployeeController(ILogicServices LogicServices) public EmployeeController(ILogicServices LogicServices, IHubContext<SignalRHub> hub)
{ {
this.LogicServices = LogicServices; this.LogicServices = LogicServices;
this.hub = hub;
} }
[HttpGet()] [HttpGet()]
@@ -47,19 +51,23 @@ namespace WD7UVN_HFT_2023241.Endpoint
public void PutEmployee([FromBody] Employee e) public void PutEmployee([FromBody] Employee e)
{ {
LogicServices.CRUDOperations.CreateEmployee(e); LogicServices.CRUDOperations.CreateEmployee(e);
hub.Clients.All.SendAsync("EmployeeCreated", e);
} }
[HttpPost()] [HttpPost()]
public void UpdateEmployee([FromBody] Employee e) public void UpdateEmployee([FromBody] Employee e)
{ {
LogicServices.CRUDOperations.UpdateEmployee(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. //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}")] [HttpDelete("{id}")]
public void DeleteEmployee(int id) public void DeleteEmployee(int id)
{ {
Employee e = LogicServices.CRUDOperations.ReadEmployee(id);
LogicServices.CRUDOperations.DeleteEmployee(id); LogicServices.CRUDOperations.DeleteEmployee(id);
hub.Clients.All.SendAsync("EmployeeDeleted", e);
} }
} }
} }

View File

@@ -3,6 +3,8 @@ using WD7UVN_HFT_2023241.Logic;
using System.Linq; using System.Linq;
using System; using System;
using WD7UVN_HFT_2023241.Models; using WD7UVN_HFT_2023241.Models;
using Microsoft.AspNetCore.SignalR;
using WD7UVN_HFT_2023241.Endpoint.Services;
namespace WD7UVN_HFT_2023241.Endpoint namespace WD7UVN_HFT_2023241.Endpoint
{ {
@@ -11,10 +13,12 @@ namespace WD7UVN_HFT_2023241.Endpoint
public class MaintainerTeamController : ControllerBase public class MaintainerTeamController : ControllerBase
{ {
public ILogicServices LogicServices { get; set; } public ILogicServices LogicServices { get; set; }
IHubContext<SignalRHub> hub;
public MaintainerTeamController(ILogicServices LogicServices) public MaintainerTeamController(ILogicServices LogicServices, IHubContext<SignalRHub> hub)
{ {
this.LogicServices = LogicServices; this.LogicServices = LogicServices;
this.hub = hub;
} }
[HttpGet()] [HttpGet()]
@@ -47,19 +51,23 @@ namespace WD7UVN_HFT_2023241.Endpoint
public void PutMaintainerTeam([FromBody] MaintainerTeam e) public void PutMaintainerTeam([FromBody] MaintainerTeam e)
{ {
LogicServices.CRUDOperations.CreateMaintainerTeam(e); LogicServices.CRUDOperations.CreateMaintainerTeam(e);
hub.Clients.All.SendAsync("MaintainerTeamCreated", e);
} }
[HttpPost()] [HttpPost()]
public void UpdateMaintainerTeam([FromBody] MaintainerTeam e) public void UpdateMaintainerTeam([FromBody] MaintainerTeam e)
{ {
LogicServices.CRUDOperations.UpdateMaintainerTeam(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. //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}")] [HttpDelete("{id}")]
public void DeleteMaintainerTeam(int id) public void DeleteMaintainerTeam(int id)
{ {
MaintainerTeam maintainerTeam = LogicServices.CRUDOperations.ReadMaintainerTeam(id);
LogicServices.CRUDOperations.DeleteMaintainerTeam(id); LogicServices.CRUDOperations.DeleteMaintainerTeam(id);
hub.Clients.All.SendAsync("MaintainerTeamDeleted", maintainerTeam);
} }
} }
} }

View File

@@ -3,6 +3,8 @@ using WD7UVN_HFT_2023241.Logic;
using System.Linq; using System.Linq;
using System; using System;
using WD7UVN_HFT_2023241.Models; using WD7UVN_HFT_2023241.Models;
using Microsoft.AspNetCore.SignalR;
using WD7UVN_HFT_2023241.Endpoint.Services;
namespace WD7UVN_HFT_2023241.Endpoint namespace WD7UVN_HFT_2023241.Endpoint
{ {
@@ -11,10 +13,12 @@ namespace WD7UVN_HFT_2023241.Endpoint
public class ServiceController : ControllerBase public class ServiceController : ControllerBase
{ {
public ILogicServices LogicServices { get; set; } public ILogicServices LogicServices { get; set; }
IHubContext<SignalRHub> hub;
public ServiceController(ILogicServices LogicServices) public ServiceController(ILogicServices LogicServices, IHubContext<SignalRHub> hub)
{ {
this.LogicServices = LogicServices; this.LogicServices = LogicServices;
this.hub = hub;
} }
[HttpGet()] [HttpGet()]
@@ -43,23 +47,27 @@ namespace WD7UVN_HFT_2023241.Endpoint
} }
} }
[HttpPut()] [HttpPut()]
public void PutService([FromBody] Service e) public void PutService([FromBody] Service e)
{ {
LogicServices.CRUDOperations.CreateService(e); LogicServices.CRUDOperations.CreateService(e);
hub.Clients.All.SendAsync("ServiceCreated", e);
} }
[HttpPost()] [HttpPost()]
public void UpdateService([FromBody] Service e) public void UpdateService([FromBody] Service e)
{ {
LogicServices.CRUDOperations.UpdateService(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. //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}")] [HttpDelete("{id}")]
public void DeleteService(int id) public void DeleteService(int id)
{ {
Service service = LogicServices.CRUDOperations.ReadService(id);
LogicServices.CRUDOperations.DeleteService(id); LogicServices.CRUDOperations.DeleteService(id);
hub.Clients.All.SendAsync("ServiceDeleted", service);
} }
} }
} }

View File

@@ -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);
}
}
}

View File

@@ -6,6 +6,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using WD7UVN_HFT_2023241.Endpoint.Services;
using WD7UVN_HFT_2023241.Logic; using WD7UVN_HFT_2023241.Logic;
using WD7UVN_HFT_2023241.Repository; using WD7UVN_HFT_2023241.Repository;
@@ -27,6 +28,8 @@ namespace WD7UVN_HFT_2023241.Endpoint
services.AddTransient<CompanyDbContext>(); services.AddTransient<CompanyDbContext>();
services.AddTransient<ICRUD, CRUD>(); services.AddTransient<ICRUD, CRUD>();
services.AddSignalR();
services.AddControllers(); services.AddControllers();
services.AddSwaggerGen(c => services.AddSwaggerGen(c =>
{ {
@@ -61,6 +64,7 @@ namespace WD7UVN_HFT_2023241.Endpoint
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapControllers(); endpoints.MapControllers();
endpoints.MapHub<SignalRHub>("/hub");
}); });
app.UseExceptionHandler(c => c.Run(async context => app.UseExceptionHandler(c => c.Run(async context =>

View File

@@ -6,12 +6,13 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\WD7UVN_HFT_2023241.Logic\WD7UVN_HFT_2023241.Logic.csproj" /> <ProjectReference Include="..\WD7UVN_HFT_2023241.Logic\WD7UVN_HFT_2023241.Logic.csproj" />
<ProjectReference Include="..\WD7UVN_HFT_2023241.Repository\WD7UVN_HFT_2023241.Repository.csproj" /> <ProjectReference Include="..\WD7UVN_HFT_2023241.Repository\WD7UVN_HFT_2023241.Repository.csproj" />
<ProjectReference Include="..\WD7UVN_HFT_2023241.Models\WD7UVN_HFT_2023241.Models.csproj" /> <ProjectReference Include="..\WD7UVN_HFT_2023241.Models\WD7UVN_HFT_2023241.Models.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>