SignalR working?
This commit is contained in:
@@ -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<SignalRHub> hub;
|
||||
|
||||
public CustomerController(ILogicServices LogicServices)
|
||||
public CustomerController(ILogicServices LogicServices, IHubContext<SignalRHub> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<SignalRHub> hub;
|
||||
|
||||
public EmployeeController(ILogicServices LogicServices)
|
||||
public EmployeeController(ILogicServices LogicServices, IHubContext<SignalRHub> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<SignalRHub> hub;
|
||||
|
||||
public MaintainerTeamController(ILogicServices LogicServices)
|
||||
public MaintainerTeamController(ILogicServices LogicServices, IHubContext<SignalRHub> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<SignalRHub> hub;
|
||||
|
||||
public ServiceController(ILogicServices LogicServices)
|
||||
public ServiceController(ILogicServices LogicServices, IHubContext<SignalRHub> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
21
WD7UVN_HFT_2023241.Endpoint/Services/SignalRHub.cs
Normal file
21
WD7UVN_HFT_2023241.Endpoint/Services/SignalRHub.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<CompanyDbContext>();
|
||||
services.AddTransient<ICRUD, CRUD>();
|
||||
|
||||
services.AddSignalR();
|
||||
|
||||
services.AddControllers();
|
||||
services.AddSwaggerGen(c =>
|
||||
{
|
||||
@@ -61,6 +64,7 @@ namespace WD7UVN_HFT_2023241.Endpoint
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
endpoints.MapHub<SignalRHub>("/hub");
|
||||
});
|
||||
|
||||
app.UseExceptionHandler(c => c.Run(async context =>
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user