| 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); | |||||
| } | |||||
| } |
| 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(); | |||||
| } | |||||
| } |
| 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(); | |||||
| } | |||||
| } | |||||
| } |
| 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(); | |||||
| } | |||||
| } | |||||
| } |
| 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); | |||||
| } | |||||
| } |
| 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); | |||||
| } | |||||
| } | |||||
| } |
| <PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" /> | <PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" /> | ||||
| <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> | <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> | ||||
| <PackageReference Include="Serilog.Sinks.Seq" Version="5.1.1" /> | <PackageReference Include="Serilog.Sinks.Seq" Version="5.1.1" /> | ||||
| <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> | |||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> |
| builder.Configuration.GetSection("WebApiDB")); | builder.Configuration.GetSection("WebApiDB")); | ||||
| builder.Services.AddAutoMapper(typeof(RequestMappingProfile)); | 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.AddScoped<ICustomerService, CustomerService>(); | ||||
| builder.Services.AddSingleton<RoomService>(); | builder.Services.AddSingleton<RoomService>(); | ||||
| builder.Services.AddSingleton<RequestService>(); | builder.Services.AddSingleton<RequestService>(); | ||||
| builder.Services.AddSingleton<AuthorizationService>(); | builder.Services.AddSingleton<AuthorizationService>(); |
| using Diligent.WebAPI.Business.Services; | |||||
| using Diligent.WebAPI.Business.Interfaces; | |||||
| using Diligent.WebAPI.Host.Mediator.Request.Queries; | using Diligent.WebAPI.Host.Mediator.Request.Queries; | ||||
| using MediatR; | using MediatR; | ||||
| { | { | ||||
| public class GetAllRequestHandler : IRequestHandler<GetAllRequestsQuery, List<Data.Entities.Request>> | 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) => | public async Task<List<Data.Entities.Request>> Handle(GetAllRequestsQuery request, CancellationToken cancellationToken) => | ||||
| await _requestService.GetRequestsAsync(); | |||||
| await _requestRepository.GetAll(); | |||||
| } | } | ||||
| } | } |
| builder.ConfigureData(builder.Configuration); | builder.ConfigureData(builder.Configuration); | ||||
| var collection = builder.Services; | var collection = builder.Services; | ||||
| builder.Services.AddSwaggerGen(); | |||||
| Diligent.WebAPI.Host.Extensions.ApiConfiguration.ConfigureServices(collection); | Diligent.WebAPI.Host.Extensions.ApiConfiguration.ConfigureServices(collection); | ||||
| builder.ConfigureValidationMiddleware(); | builder.ConfigureValidationMiddleware(); | ||||
| var app = builder.Build(); | var app = builder.Build(); | ||||
| app.UseAuthentication(); | app.UseAuthentication(); | ||||
| app.UseAuthorization(); | app.UseAuthorization(); | ||||
| app.SetupData(); | app.SetupData(); | ||||
| if (app.Environment.IsDevelopment()) | |||||
| { | |||||
| app.UseSwagger(); | |||||
| app.UseSwaggerUI(); | |||||
| } | |||||
| app.MapHub<ChatHub>("/chatHub"); | app.MapHub<ChatHub>("/chatHub"); | ||||
| app.MapHub<ConnectionHub>("/statusHub"); | app.MapHub<ConnectionHub>("/statusHub"); |
| .editorconfig = .editorconfig | .editorconfig = .editorconfig | ||||
| EndProjectSection | EndProjectSection | ||||
| EndProject | EndProject | ||||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{52163977-0F9F-4318-9C98-401D4F31AA65}" | |||||
| EndProject | |||||
| Global | Global | ||||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||||
| Debug|Any CPU = Debug|Any CPU | Debug|Any CPU = Debug|Any CPU | ||||
| {69367FC8-603A-4E4B-BAD7-99D24308D5F2}.Debug|Any CPU.Build.0 = Debug|Any CPU | {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.ActiveCfg = Release|Any CPU | ||||
| {69367FC8-603A-4E4B-BAD7-99D24308D5F2}.Release|Any CPU.Build.0 = 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 | EndGlobalSection | ||||
| GlobalSection(SolutionProperties) = preSolution | GlobalSection(SolutionProperties) = preSolution | ||||
| HideSolutionNode = FALSE | HideSolutionNode = FALSE |
| 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(); | |||||
| } | |||||
| } | |||||
| } |
| <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> |
| namespace Tests | |||||
| { | |||||
| public class Tests | |||||
| { | |||||
| [SetUp] | |||||
| public void Setup() | |||||
| { | |||||
| } | |||||
| [Test] | |||||
| public void Test1() | |||||
| { | |||||
| Assert.Pass(); | |||||
| } | |||||
| } | |||||
| } |
| global using NUnit.Framework; |