| @@ -0,0 +1,11 @@ | |||
| namespace Diligent.WebAPI.Business.Interfaces | |||
| { | |||
| public interface IBaseRepository<TEntity> where TEntity : class | |||
| { | |||
| Task<List<TEntity>> GetAsync(); | |||
| Task<TEntity> GetByIdAsync(string id); | |||
| Task CreateAsync(TEntity entity); | |||
| Task UpdateAsync(string id, TEntity updateEntity); | |||
| Task RemoveAsync(string id); | |||
| } | |||
| } | |||
| @@ -0,0 +1,14 @@ | |||
| using Diligent.WebAPI.Data.Entities; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using System.Threading.Tasks; | |||
| namespace Diligent.WebAPI.Business.Interfaces | |||
| { | |||
| public interface IRequestRepository : IBaseRepository<Request> | |||
| { | |||
| Task<List<Request>> GetAll(); | |||
| } | |||
| } | |||
| @@ -0,0 +1,42 @@ | |||
| using Diligent.WebAPI.Business.Interfaces; | |||
| using Diligent.WebAPI.Data; | |||
| using MongoDB.Driver; | |||
| namespace Diligent.WebAPI.Business.Services | |||
| { | |||
| public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class | |||
| { | |||
| protected readonly IMongoDBContext _mongoContext; | |||
| protected IMongoCollection<TEntity> _dbCollection; | |||
| public BaseRepository(IMongoDBContext context) | |||
| { | |||
| _mongoContext = context; | |||
| _dbCollection = _mongoContext.GetCollection<TEntity>(typeof(TEntity).Name); | |||
| } | |||
| public Task CreateAsync(TEntity entity) | |||
| { | |||
| throw new NotImplementedException(); | |||
| } | |||
| public async Task<List<TEntity>> GetAsync() | |||
| { | |||
| var all = await _dbCollection.FindAsync(Builders<TEntity>.Filter.Empty); | |||
| return await all.ToListAsync(); | |||
| } | |||
| public Task<TEntity> GetByIdAsync(string id) | |||
| { | |||
| throw new NotImplementedException(); | |||
| } | |||
| public Task RemoveAsync(string id) | |||
| { | |||
| throw new NotImplementedException(); | |||
| } | |||
| public Task UpdateAsync(string id, TEntity updateEntity) | |||
| { | |||
| throw new NotImplementedException(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,28 @@ | |||
| using Diligent.WebAPI.Business.Interfaces; | |||
| using Diligent.WebAPI.Data; | |||
| using Diligent.WebAPI.Data.Entities; | |||
| using MongoDB.Driver; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using System.Threading.Tasks; | |||
| namespace Diligent.WebAPI.Business.Services | |||
| { | |||
| public class RequestRepository : BaseRepository<Request>, IRequestRepository | |||
| { | |||
| protected new IMongoCollection<Request> _dbCollection; | |||
| public RequestRepository(IMongoDBContext context) : base(context) | |||
| { | |||
| _dbCollection = _mongoContext.GetCollection<Request>(typeof(Request).Name); | |||
| } | |||
| public async Task<List<Request>> GetAll() | |||
| { | |||
| var all = await _dbCollection.FindAsync(Builders<Request>.Filter.Empty); | |||
| return await all.ToListAsync(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,14 @@ | |||
| using MongoDB.Driver; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using System.Threading.Tasks; | |||
| namespace Diligent.WebAPI.Data | |||
| { | |||
| public interface IMongoDBContext | |||
| { | |||
| IMongoCollection<Request> GetCollection<Request>(string id); | |||
| } | |||
| } | |||
| @@ -0,0 +1,25 @@ | |||
| using MongoDB.Driver; | |||
| namespace Diligent.WebAPI.Data | |||
| { | |||
| public class MongoDBContext : IMongoDBContext | |||
| { | |||
| private readonly IConfiguration _configuration; | |||
| private IMongoDatabase _db { get; set; } | |||
| private MongoClient _mongoClient { get; set; } | |||
| public IClientSessionHandle Session { get; set; } | |||
| public MongoDBContext(IConfiguration configuration) | |||
| { | |||
| _configuration = configuration; | |||
| var mongoDbSettings = _configuration.GetSection("WebApiDB"); | |||
| _mongoClient = new MongoClient(mongoDbSettings["ConnectionString"]); | |||
| _db = _mongoClient.GetDatabase(mongoDbSettings["DatabaseName"]); | |||
| } | |||
| public IMongoCollection<Request> GetCollection<Request>(string id) | |||
| { | |||
| return _db.GetCollection<Request>(id); | |||
| } | |||
| } | |||
| } | |||
| @@ -27,6 +27,7 @@ | |||
| <PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" /> | |||
| <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> | |||
| <PackageReference Include="Serilog.Sinks.Seq" Version="5.1.1" /> | |||
| <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| @@ -24,9 +24,13 @@ public static class WebAppBuilder | |||
| builder.Configuration.GetSection("WebApiDB")); | |||
| builder.Services.AddAutoMapper(typeof(RequestMappingProfile)); | |||
| builder.Services.AddScoped<IAuthenticationService, AuthenticationService>(); | |||
| builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>)); | |||
| builder.Services.AddScoped<IRequestRepository, RequestRepository>(); | |||
| builder.Services.AddScoped<IMongoDBContext, MongoDBContext>(); | |||
| builder.Services.AddScoped<IAuthenticationService, AuthenticationService>(); | |||
| builder.Services.AddScoped<ICustomerService, CustomerService>(); | |||
| builder.Services.AddSingleton<RoomService>(); | |||
| builder.Services.AddSingleton<RequestService>(); | |||
| builder.Services.AddSingleton<AuthorizationService>(); | |||
| @@ -1,4 +1,4 @@ | |||
| using Diligent.WebAPI.Business.Services; | |||
| using Diligent.WebAPI.Business.Interfaces; | |||
| using Diligent.WebAPI.Host.Mediator.Request.Queries; | |||
| using MediatR; | |||
| @@ -6,13 +6,13 @@ namespace Diligent.WebAPI.Host.Mediator.Request.Handlers | |||
| { | |||
| public class GetAllRequestHandler : IRequestHandler<GetAllRequestsQuery, List<Data.Entities.Request>> | |||
| { | |||
| private readonly RequestService _requestService; | |||
| private readonly IRequestRepository _requestRepository; | |||
| public GetAllRequestHandler(RequestService requestService) | |||
| public GetAllRequestHandler(IRequestRepository requestRepository) | |||
| { | |||
| _requestService = requestService; | |||
| _requestRepository = requestRepository; | |||
| } | |||
| public async Task<List<Data.Entities.Request>> Handle(GetAllRequestsQuery request, CancellationToken cancellationToken) => | |||
| await _requestService.GetRequestsAsync(); | |||
| await _requestRepository.GetAll(); | |||
| } | |||
| } | |||
| @@ -6,6 +6,7 @@ var builder = WebApplication.CreateBuilder(args); | |||
| builder.ConfigureData(builder.Configuration); | |||
| var collection = builder.Services; | |||
| builder.Services.AddSwaggerGen(); | |||
| Diligent.WebAPI.Host.Extensions.ApiConfiguration.ConfigureServices(collection); | |||
| builder.ConfigureValidationMiddleware(); | |||
| var app = builder.Build(); | |||
| @@ -16,6 +17,11 @@ app.MapControllers(); | |||
| app.UseAuthentication(); | |||
| app.UseAuthorization(); | |||
| app.SetupData(); | |||
| if (app.Environment.IsDevelopment()) | |||
| { | |||
| app.UseSwagger(); | |||
| app.UseSwaggerUI(); | |||
| } | |||
| app.MapHub<ChatHub>("/chatHub"); | |||
| app.MapHub<ConnectionHub>("/statusHub"); | |||
| @@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution | |||
| .editorconfig = .editorconfig | |||
| EndProjectSection | |||
| EndProject | |||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{52163977-0F9F-4318-9C98-401D4F31AA65}" | |||
| EndProject | |||
| Global | |||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | |||
| Debug|Any CPU = Debug|Any CPU | |||
| @@ -32,6 +34,10 @@ Global | |||
| {69367FC8-603A-4E4B-BAD7-99D24308D5F2}.Debug|Any CPU.Build.0 = Debug|Any CPU | |||
| {69367FC8-603A-4E4B-BAD7-99D24308D5F2}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
| {69367FC8-603A-4E4B-BAD7-99D24308D5F2}.Release|Any CPU.Build.0 = Release|Any CPU | |||
| {52163977-0F9F-4318-9C98-401D4F31AA65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||
| {52163977-0F9F-4318-9C98-401D4F31AA65}.Debug|Any CPU.Build.0 = Debug|Any CPU | |||
| {52163977-0F9F-4318-9C98-401D4F31AA65}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
| {52163977-0F9F-4318-9C98-401D4F31AA65}.Release|Any CPU.Build.0 = Release|Any CPU | |||
| EndGlobalSection | |||
| GlobalSection(SolutionProperties) = preSolution | |||
| HideSolutionNode = FALSE | |||
| @@ -0,0 +1,64 @@ | |||
| using Diligent.WebAPI.Business.Services; | |||
| using Diligent.WebAPI.Data.Entities; | |||
| using FluentAssertions; | |||
| using Microsoft.AspNetCore.Identity; | |||
| using Moq; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using System.Threading.Tasks; | |||
| namespace Tests | |||
| { | |||
| [TestFixture] | |||
| public class CustomerServiceTest | |||
| { | |||
| private CustomerService service; | |||
| [Test] | |||
| public async Task DeleteNotification_WhenUserIsNull_ReturnsFalse() | |||
| { | |||
| // Arrange | |||
| var userManagerMock = new Mock<UserManager<Customer>>(Mock.Of<IUserStore<Customer>>(), null, null, null, null, null, null, null, null); | |||
| userManagerMock.Setup(x => x.FindByIdAsync(It.IsAny<string>())).ReturnsAsync((Customer)null); | |||
| service = new CustomerService(userManagerMock.Object); | |||
| // Act | |||
| var result = await service.DeleteNotification("", ""); | |||
| // Assert | |||
| //Assert.IsFalse(result); | |||
| result.Should().BeFalse(); | |||
| } | |||
| [Test] | |||
| public async Task DeleteNotification_UserIsNotNullAndNotificationIsNull_ReturnsFalse() | |||
| { | |||
| // Arrange | |||
| var userManagerMock = new Mock<UserManager<Customer>>(Mock.Of<IUserStore<Customer>>(), null, null, null, null, null, null, null, null); | |||
| var customer = new Customer | |||
| { | |||
| FirstName = "User", | |||
| LastName = "Someone", | |||
| Notifications = new List<Notification> | |||
| { | |||
| new Notification | |||
| { | |||
| RoomId = "Room1", Count = 1 | |||
| } | |||
| } | |||
| }; | |||
| userManagerMock.Setup(x => x.FindByIdAsync(It.IsAny<string>())).ReturnsAsync(customer); | |||
| service = new CustomerService(userManagerMock.Object); | |||
| // Act | |||
| var result = await service.DeleteNotification("arg", "Room1"); | |||
| // Assert | |||
| result.Should().BeTrue(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,26 @@ | |||
| <Project Sdk="Microsoft.NET.Sdk"> | |||
| <PropertyGroup> | |||
| <TargetFramework>net6.0</TargetFramework> | |||
| <ImplicitUsings>enable</ImplicitUsings> | |||
| <Nullable>enable</Nullable> | |||
| <IsPackable>false</IsPackable> | |||
| </PropertyGroup> | |||
| <ItemGroup> | |||
| <PackageReference Include="FluentAssertions" Version="6.7.0" /> | |||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> | |||
| <PackageReference Include="Moq" Version="4.18.2" /> | |||
| <PackageReference Include="NUnit" Version="3.13.3" /> | |||
| <PackageReference Include="NUnit3TestAdapter" Version="4.2.1" /> | |||
| <PackageReference Include="NUnit.Analyzers" Version="3.3.0" /> | |||
| <PackageReference Include="coverlet.collector" Version="3.1.2" /> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <ProjectReference Include="..\Diligent.WebAPI.Business\Diligent.WebAPI.Business.csproj" /> | |||
| <ProjectReference Include="..\Diligent.WebAPI.Data\Diligent.WebAPI.Data.csproj" /> | |||
| </ItemGroup> | |||
| </Project> | |||
| @@ -0,0 +1,16 @@ | |||
| namespace Tests | |||
| { | |||
| public class Tests | |||
| { | |||
| [SetUp] | |||
| public void Setup() | |||
| { | |||
| } | |||
| [Test] | |||
| public void Test1() | |||
| { | |||
| Assert.Pass(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1 @@ | |||
| global using NUnit.Framework; | |||