From dfd09673b252ea8a7348951389be9e2e1b19bc94 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Tue, 15 Aug 2023 00:21:38 +0300 Subject: [PATCH 01/19] Add IChatMessageRepository, ChatMessageRepository --- .../Repository/ChatMessageRepository.cs | 29 +++++++++++++++++++ .../Repository/IChatMessageRepository.cs | 13 +++++++++ 2 files changed, 42 insertions(+) create mode 100644 OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs create mode 100644 OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs diff --git a/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs b/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs new file mode 100644 index 0000000000..d5a1cbe874 --- /dev/null +++ b/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using OutOfSchool.Services.Models; +using OutOfSchool.Services.Models.ChatWorkshop; + +namespace OutOfSchool.Services.Repository; + +public class ChatMessageRepository : SensitiveEntityRepository, IChatMessageRepository +{ + private readonly OutOfSchoolDbContext db; + + public ChatMessageRepository(OutOfSchoolDbContext dbContext) + : base(dbContext) + { + db = dbContext; + } + + public async Task CountUnreadMessages(Guid workshopId) + { + return await db.ChatMessageWorkshops + .Include(chr => chr.ChatRoom) + .Where(x => x.ChatRoom.WorkshopId == workshopId && x.ReadDateTime == null && !x.SenderRoleIsProvider) + .CountAsync(); + } +} \ No newline at end of file diff --git a/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs b/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs new file mode 100644 index 0000000000..655ef5e7ac --- /dev/null +++ b/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using OutOfSchool.Services.Models.ChatWorkshop; + +namespace OutOfSchool.Services.Repository; + +public interface IChatMessageRepository : ISensitiveEntityRepository +{ + Task CountUnreadMessages(Guid workshopId); +} \ No newline at end of file From 2089fade0ed79d3c828a57f57d761b629a0f6568 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Fri, 18 Aug 2023 11:49:59 +0300 Subject: [PATCH 02/19] add UnreadMessages in Api/WorkShop/GetByProviderId --- .../Repository/ChatMessageRepository.cs | 2 +- .../Repository/IChatMessageRepository.cs | 7 +++++- .../ChatMessageWorkshopServiceTests.cs | 5 ++-- .../Models/Workshop/WorkshopCard.cs | 2 ++ .../Services/ChatMessageWorkshopService.cs | 20 +++++++++++++-- .../Services/Database/IWorkshopService.cs | 2 +- .../Services/Database/WorkshopService.cs | 25 +++++++++++++++---- .../Services/IChatMessageWorkshopService.cs | 7 ++++++ .../Services/IWorkshopServicesCombiner.cs | 2 +- .../Services/WorkshopServicesCombiner.cs | 2 +- OutOfSchool/OutOfSchool.WebApi/Startup.cs | 3 ++- 11 files changed, 62 insertions(+), 15 deletions(-) diff --git a/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs b/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs index d5a1cbe874..4c3c17fa84 100644 --- a/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs +++ b/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs @@ -19,7 +19,7 @@ public ChatMessageRepository(OutOfSchoolDbContext dbContext) db = dbContext; } - public async Task CountUnreadMessages(Guid workshopId) + public async Task CountUnreadMessagesAsync(Guid workshopId) { return await db.ChatMessageWorkshops .Include(chr => chr.ChatRoom) diff --git a/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs b/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs index 655ef5e7ac..2ff38f0d42 100644 --- a/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs +++ b/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs @@ -9,5 +9,10 @@ namespace OutOfSchool.Services.Repository; public interface IChatMessageRepository : ISensitiveEntityRepository { - Task CountUnreadMessages(Guid workshopId); + /// + /// Get a number of unread ChatMessages with specified WorkshopId. + /// + /// Workshop's key. + /// A representing the result of the asynchronous operation. The task result contains a that contains the number of unread messages for the specified Workshop. + Task CountUnreadMessagesAsync(Guid workshopId); } \ No newline at end of file diff --git a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs index 27d003bf23..987210beb2 100644 --- a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs +++ b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs @@ -35,7 +35,8 @@ public class ChatMessageWorkshopServiceTests WorkshopId = Guid.NewGuid(), }; - private IEntityRepository messageRepository; + //private IEntityRepository messageRepository; + private IChatMessageRepository messageRepository; private Mock roomServiceMock; private Mock> workshopHub; private Mock> loggerMock; @@ -60,7 +61,7 @@ public void SetUp() options = builder.Options; dbContext = new OutOfSchoolDbContext(options); - messageRepository = new EntityRepository(dbContext); + messageRepository = new ChatMessageRepository(dbContext); roomServiceMock = new Mock(); workshopHub = new Mock>(); loggerMock = new Mock>(); diff --git a/OutOfSchool/OutOfSchool.WebApi/Models/Workshop/WorkshopCard.cs b/OutOfSchool/OutOfSchool.WebApi/Models/Workshop/WorkshopCard.cs index 3bffbd2b87..966779ad4c 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Models/Workshop/WorkshopCard.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Models/Workshop/WorkshopCard.cs @@ -78,4 +78,6 @@ public class WorkshopProviderViewCard : WorkshopBaseCard public int AmountOfPendingApplications { get; set; } public WorkshopStatus Status { get; set; } + + public int UnreadMessages { get; set; } } \ No newline at end of file diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs index 6ddb44bf84..5cc418c3c7 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs @@ -1,8 +1,11 @@ using System.Linq.Expressions; using AutoMapper; using Microsoft.AspNetCore.SignalR; +using Microsoft.EntityFrameworkCore.Storage; +using Nest; using Newtonsoft.Json; using OutOfSchool.Services.Enums; +using OutOfSchool.Services.Repository; using OutOfSchool.WebApi.Models; using OutOfSchool.WebApi.Models.ChatWorkshop; @@ -13,7 +16,7 @@ namespace OutOfSchool.WebApi.Services; /// public class ChatMessageWorkshopService : IChatMessageWorkshopService { - private readonly IEntityRepository messageRepository; + private readonly IChatMessageRepository messageRepository; private readonly IChatRoomWorkshopService roomService; private readonly IHubContext workshopHub; private readonly ILogger logger; @@ -28,7 +31,7 @@ public class ChatMessageWorkshopService : IChatMessageWorkshopService /// Logger. /// Mapper. public ChatMessageWorkshopService( - IEntityRepository chatMessageRepository, + IChatMessageRepository chatMessageRepository, IChatRoomWorkshopService roomRepository, IHubContext workshopHub, ILogger logger, @@ -124,6 +127,19 @@ public async Task> GetMessagesForChatRoomAndSetRead } } + public async Task CountUnreadMessagesAsync(Guid workshopId) + { + try + { + return await messageRepository.CountUnreadMessagesAsync(workshopId).ConfigureAwait(false); + } + catch (Exception exception) + { + logger.LogError($"Getting number of unread messages with {nameof(workshopId)}:{workshopId} failed. Exception: {exception.Message}"); + throw; + } + } + private async Task> GetMessagesForChatRoomDomainModelAsync(Guid chatRoomId, OffsetFilter offsetFilter) { if (offsetFilter is null) diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Database/IWorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Database/IWorkshopService.cs index dfc651fe19..7fce9b5ae2 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Database/IWorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Database/IWorkshopService.cs @@ -103,7 +103,7 @@ public interface IWorkshopService /// Type of entity that must be return. /// A representing the result of the asynchronous operation. /// The task result contains a that contains elements from the input sequence. - Task> GetByProviderId(Guid id, ExcludeIdFilter filter) + Task> GetByProviderId(Guid id, ExcludeIdFilter filter) where T : WorkshopBaseCard; /// diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs index f5a3051d1b..640f5e3898 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs @@ -4,6 +4,7 @@ using Castle.Core.Internal; using H3Lib; using H3Lib.Extensions; +using Microsoft.Extensions.Logging; using Nest; using OutOfSchool.Common.Enums; using OutOfSchool.Services.Enums; @@ -38,6 +39,7 @@ public class WorkshopService : IWorkshopService private readonly IProviderAdminRepository providerAdminRepository; private readonly IAverageRatingService averageRatingService; private readonly IProviderRepository providerRepository; + private readonly IChatMessageWorkshopService chatMessageWorkshopService; /// /// Initializes a new instance of the class. @@ -61,7 +63,8 @@ public WorkshopService( IImageDependentEntityImagesInteractionService workshopImagesService, IProviderAdminRepository providerAdminRepository, IAverageRatingService averageRatingService, - IProviderRepository providerRepository) + IProviderRepository providerRepository, + IChatMessageWorkshopService chatMessageWorkshopService) { this.workshopRepository = workshopRepository; this.dateTimeRangeRepository = dateTimeRangeRepository; @@ -72,6 +75,7 @@ public WorkshopService( this.providerAdminRepository = providerAdminRepository; this.averageRatingService = averageRatingService; this.providerRepository = providerRepository; + this.chatMessageWorkshopService = chatMessageWorkshopService; } /// @@ -254,7 +258,7 @@ public async Task> GetWorkshopListByProviderAdminId(string } /// - public async Task> GetByProviderId(Guid id, ExcludeIdFilter filter) + public async Task> GetByProviderId(Guid id, ExcludeIdFilter filter) where T : WorkshopBaseCard { logger.LogInformation($"Getting Workshop by organization started. Looking ProviderId = {id}."); @@ -280,11 +284,22 @@ public async Task> GetByProviderId(Guid id, ExcludeIdFilter f ? $"There aren't Workshops for Provider with Id = {id}." : $"From Workshop table were successfully received {workshops.Count} records."); - var workshopBaseCards = mapper.Map>(workshops).ToList(); - var result = new SearchResult() + var workshopBaseCards = mapper.Map>(workshops).ToList(); + + if (workshopBaseCards.Any()) + { + foreach (var workshop in workshopBaseCards) + { + var x = await chatMessageWorkshopService.CountUnreadMessagesAsync(workshop.WorkshopId).ConfigureAwait(false); + logger.LogInformation($"Workshop.Id: {workshop.WorkshopId} has {x} unread messages."); + workshop.UnreadMessages = x; + } + } + + var result = new SearchResult() { TotalAmount = workshopBaseCardsCount, - Entities = await GetWorkshopsWithAverageRating(workshopBaseCards).ConfigureAwait(false), + Entities = await GetWorkshopsWithAverageRating(workshopBaseCards).ConfigureAwait(false), }; return result; diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/IChatMessageWorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/IChatMessageWorkshopService.cs index 68a9ae7edc..faa098465f 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/IChatMessageWorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/IChatMessageWorkshopService.cs @@ -40,4 +40,11 @@ public interface IChatMessageWorkshopService /// Role of current user. /// A representing the result of the asynchronous operation. The task result contains a that contains elements from the input sequence. Task> GetMessagesForChatRoomAndSetReadDateTimeIfItIsNullAsync(Guid chatRoomId, OffsetFilter offsetFilter, Role userRole); + + /// + /// Get a number of unread ChatMessages with specified WorkshopId. + /// + /// Workshop's key. + /// A representing the result of the asynchronous operation. The task result contains a that contains the number of unread messages for the specified Workshop. + Task CountUnreadMessagesAsync(Guid workshopId); } \ No newline at end of file diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopServicesCombiner.cs b/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopServicesCombiner.cs index d6c2227767..ff19a04a67 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopServicesCombiner.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopServicesCombiner.cs @@ -82,7 +82,7 @@ public interface IWorkshopServicesCombiner /// Type of entity that must be return. /// A representing the result of the asynchronous operation. /// The task result contains a that contains elements from the input sequence. - Task> GetByProviderId(Guid id, ExcludeIdFilter filter) + Task> GetByProviderId(Guid id, ExcludeIdFilter filter) where T : WorkshopBaseCard; /// diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopServicesCombiner.cs b/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopServicesCombiner.cs index ae8433b123..050b7a238c 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopServicesCombiner.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopServicesCombiner.cs @@ -228,7 +228,7 @@ public async Task> GetWorkshopListByProviderAdminId(string } /// - public async Task> GetByProviderId(Guid id, ExcludeIdFilter filter) + public async Task> GetByProviderId(Guid id, ExcludeIdFilter filter) where T : WorkshopBaseCard { var workshopBaseCards = await workshopService.GetByProviderId(id, filter).ConfigureAwait(false); diff --git a/OutOfSchool/OutOfSchool.WebApi/Startup.cs b/OutOfSchool/OutOfSchool.WebApi/Startup.cs index 9d0d9d5a78..570d38fe61 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Startup.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Startup.cs @@ -211,8 +211,8 @@ public static void AddApplicationServices(this WebApplicationBuilder builder) // entities services services.AddTransient(); - services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -279,6 +279,7 @@ public static void AddApplicationServices(this WebApplicationBuilder builder) services .AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); From 986a3a6407371dd4eb98a5b8d6c37385eb341679 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Fri, 18 Aug 2023 15:35:56 +0300 Subject: [PATCH 03/19] Rename variable name --- .../OutOfSchool.WebApi/Services/Database/WorkshopService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs index 640f5e3898..64180762ae 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs @@ -290,9 +290,9 @@ public async Task> GetByProviderId(Gui { foreach (var workshop in workshopBaseCards) { - var x = await chatMessageWorkshopService.CountUnreadMessagesAsync(workshop.WorkshopId).ConfigureAwait(false); + var messages = await chatMessageWorkshopService.CountUnreadMessagesAsync(workshop.WorkshopId).ConfigureAwait(false); logger.LogInformation($"Workshop.Id: {workshop.WorkshopId} has {x} unread messages."); - workshop.UnreadMessages = x; + workshop.UnreadMessages = messages; } } From 405647722a86f316a8f10871ea7c0dcd4b96c215 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Fri, 18 Aug 2023 15:51:29 +0300 Subject: [PATCH 04/19] Fix log variable error --- .../OutOfSchool.WebApi/Services/Database/WorkshopService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs index 64180762ae..a7d8b7983f 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs @@ -291,7 +291,7 @@ public async Task> GetByProviderId(Gui foreach (var workshop in workshopBaseCards) { var messages = await chatMessageWorkshopService.CountUnreadMessagesAsync(workshop.WorkshopId).ConfigureAwait(false); - logger.LogInformation($"Workshop.Id: {workshop.WorkshopId} has {x} unread messages."); + logger.LogInformation($"Workshop.Id: {workshop.WorkshopId} has {messages} unread messages."); workshop.UnreadMessages = messages; } } From 9b6e535f419ad6d81ad3222bbdf9332026242faf Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Wed, 23 Aug 2023 12:28:48 +0300 Subject: [PATCH 05/19] Fix type in generic GetByProviderId --- .../Controllers/V1/WorkshopController.cs | 6 +++--- .../Controllers/V2/WorkshopController.cs | 6 +++--- .../Services/Database/IWorkshopService.cs | 4 ++-- .../Services/Database/WorkshopService.cs | 10 +++++----- .../Services/IWorkshopServicesCombiner.cs | 4 ++-- .../Services/WorkshopServicesCombiner.cs | 4 ++-- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/WorkshopController.cs b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/WorkshopController.cs index 90245b596c..fa831009cb 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/WorkshopController.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/WorkshopController.cs @@ -135,14 +135,14 @@ public async Task GetWorkshopListByProviderAdminId(string provide /// Provider's id. /// Filter to get specified portion of workshop view cards for specified provider. /// Id of the excluded workshop could be specified. - /// , or no content. + /// , or no content. /// The list of found entities by given Id. /// No entity with given Id was found. /// Provider id is empty. /// If any server error occures. For example: Id was less than one. [AllowAnonymous] [HttpGet("{id}")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(SearchResult))] + [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(SearchResult))] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] @@ -153,7 +153,7 @@ public async Task GetByProviderId(Guid id, [FromQuery] ExcludeIdF return BadRequest("Provider id is empty."); } - var workshopCards = await combinedWorkshopService.GetByProviderId(id, filter) + var workshopCards = await combinedWorkshopService.GetByProviderId(id, filter) .ConfigureAwait(false); if (workshopCards.TotalAmount == 0) diff --git a/OutOfSchool/OutOfSchool.WebApi/Controllers/V2/WorkshopController.cs b/OutOfSchool/OutOfSchool.WebApi/Controllers/V2/WorkshopController.cs index 203a5a3968..e3637cc0a9 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Controllers/V2/WorkshopController.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Controllers/V2/WorkshopController.cs @@ -72,18 +72,18 @@ public async Task GetById(Guid id) /// /// Provider's id. /// Filter to get specified portion of workshops for specified provider. Ids of the excluded workshops could be specified. - /// , or no content. + /// , or no content. /// The list of found entities by given Id. /// No entity with given Id was found. /// If any server error occures. For example: Id was less than one. [AllowAnonymous] [HttpGet("{id}")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(SearchResult))] + [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(SearchResult))] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetByProviderId(Guid id, [FromQuery] ExcludeIdFilter filter) { - var workshopCards = await combinedWorkshopService.GetByProviderId(id, filter).ConfigureAwait(false); + var workshopCards = await combinedWorkshopService.GetByProviderId(id, filter).ConfigureAwait(false); if (workshopCards.TotalAmount == 0) { diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Database/IWorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Database/IWorkshopService.cs index 7fce9b5ae2..495b5b6c64 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Database/IWorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Database/IWorkshopService.cs @@ -103,8 +103,8 @@ public interface IWorkshopService /// Type of entity that must be return. /// A representing the result of the asynchronous operation. /// The task result contains a that contains elements from the input sequence. - Task> GetByProviderId(Guid id, ExcludeIdFilter filter) - where T : WorkshopBaseCard; + Task> GetByProviderId(Guid id, ExcludeIdFilter filter) + where T : WorkshopProviderViewCard; /// /// Get entities from the database that match filter's parameters. diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs index a7d8b7983f..0f92f7816d 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs @@ -258,8 +258,8 @@ public async Task> GetWorkshopListByProviderAdminId(string } /// - public async Task> GetByProviderId(Guid id, ExcludeIdFilter filter) - where T : WorkshopBaseCard + public async Task> GetByProviderId(Guid id, ExcludeIdFilter filter) + where T : WorkshopProviderViewCard { logger.LogInformation($"Getting Workshop by organization started. Looking ProviderId = {id}."); @@ -284,7 +284,7 @@ public async Task> GetByProviderId(Gui ? $"There aren't Workshops for Provider with Id = {id}." : $"From Workshop table were successfully received {workshops.Count} records."); - var workshopBaseCards = mapper.Map>(workshops).ToList(); + var workshopBaseCards = mapper.Map>(workshops).ToList(); if (workshopBaseCards.Any()) { @@ -296,10 +296,10 @@ public async Task> GetByProviderId(Gui } } - var result = new SearchResult() + var result = new SearchResult() { TotalAmount = workshopBaseCardsCount, - Entities = await GetWorkshopsWithAverageRating(workshopBaseCards).ConfigureAwait(false), + Entities = await GetWorkshopsWithAverageRating(workshopBaseCards).ConfigureAwait(false), }; return result; diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopServicesCombiner.cs b/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopServicesCombiner.cs index ff19a04a67..5da9038c9b 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopServicesCombiner.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopServicesCombiner.cs @@ -82,8 +82,8 @@ public interface IWorkshopServicesCombiner /// Type of entity that must be return. /// A representing the result of the asynchronous operation. /// The task result contains a that contains elements from the input sequence. - Task> GetByProviderId(Guid id, ExcludeIdFilter filter) - where T : WorkshopBaseCard; + Task> GetByProviderId(Guid id, ExcludeIdFilter filter) + where T : WorkshopProviderViewCard; /// /// Get all entities that matches filter's parameters. diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopServicesCombiner.cs b/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopServicesCombiner.cs index 050b7a238c..fbda28c9a3 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopServicesCombiner.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopServicesCombiner.cs @@ -228,8 +228,8 @@ public async Task> GetWorkshopListByProviderAdminId(string } /// - public async Task> GetByProviderId(Guid id, ExcludeIdFilter filter) - where T : WorkshopBaseCard + public async Task> GetByProviderId(Guid id, ExcludeIdFilter filter) + where T : WorkshopProviderViewCard { var workshopBaseCards = await workshopService.GetByProviderId(id, filter).ConfigureAwait(false); From af1941db291362e0e40f5baec75b366ec73a11d8 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Wed, 23 Aug 2023 12:51:01 +0300 Subject: [PATCH 06/19] Remove generic from GetByProviderId --- .../Controllers/V1/WorkshopController.cs | 4 ++-- .../Controllers/V2/WorkshopController.cs | 2 +- .../Services/Database/IWorkshopService.cs | 3 +-- .../Services/Database/WorkshopService.cs | 9 ++++----- .../Services/IWorkshopServicesCombiner.cs | 3 +-- .../OutOfSchool.WebApi/Services/ProviderAdminService.cs | 2 +- .../Services/WorkshopServicesCombiner.cs | 5 ++--- 7 files changed, 12 insertions(+), 16 deletions(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/WorkshopController.cs b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/WorkshopController.cs index fa831009cb..09abe00ad2 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/WorkshopController.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Controllers/V1/WorkshopController.cs @@ -153,7 +153,7 @@ public async Task GetByProviderId(Guid id, [FromQuery] ExcludeIdF return BadRequest("Provider id is empty."); } - var workshopCards = await combinedWorkshopService.GetByProviderId(id, filter) + var workshopCards = await combinedWorkshopService.GetByProviderId(id, filter) .ConfigureAwait(false); if (workshopCards.TotalAmount == 0) @@ -183,7 +183,7 @@ public async Task GetWorkshopProviderViewCardsByProviderId(Guid i return BadRequest("Provider id is empty."); } - var workshopProviderViewCards = await combinedWorkshopService.GetByProviderId(id, filter).ConfigureAwait(false); + var workshopProviderViewCards = await combinedWorkshopService.GetByProviderId(id, filter).ConfigureAwait(false); if (workshopProviderViewCards.TotalAmount == 0) { diff --git a/OutOfSchool/OutOfSchool.WebApi/Controllers/V2/WorkshopController.cs b/OutOfSchool/OutOfSchool.WebApi/Controllers/V2/WorkshopController.cs index e3637cc0a9..7d995e1121 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Controllers/V2/WorkshopController.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Controllers/V2/WorkshopController.cs @@ -83,7 +83,7 @@ public async Task GetById(Guid id) [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetByProviderId(Guid id, [FromQuery] ExcludeIdFilter filter) { - var workshopCards = await combinedWorkshopService.GetByProviderId(id, filter).ConfigureAwait(false); + var workshopCards = await combinedWorkshopService.GetByProviderId(id, filter).ConfigureAwait(false); if (workshopCards.TotalAmount == 0) { diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Database/IWorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Database/IWorkshopService.cs index 495b5b6c64..73c392be6a 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Database/IWorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Database/IWorkshopService.cs @@ -103,8 +103,7 @@ public interface IWorkshopService /// Type of entity that must be return. /// A representing the result of the asynchronous operation. /// The task result contains a that contains elements from the input sequence. - Task> GetByProviderId(Guid id, ExcludeIdFilter filter) - where T : WorkshopProviderViewCard; + Task> GetByProviderId(Guid id, ExcludeIdFilter filter); /// /// Get entities from the database that match filter's parameters. diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs index 0f92f7816d..6f8c9d9361 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs @@ -258,8 +258,7 @@ public async Task> GetWorkshopListByProviderAdminId(string } /// - public async Task> GetByProviderId(Guid id, ExcludeIdFilter filter) - where T : WorkshopProviderViewCard + public async Task> GetByProviderId(Guid id, ExcludeIdFilter filter) { logger.LogInformation($"Getting Workshop by organization started. Looking ProviderId = {id}."); @@ -284,7 +283,7 @@ public async Task> GetByProviderId(Guid id, ExcludeIdFilter f ? $"There aren't Workshops for Provider with Id = {id}." : $"From Workshop table were successfully received {workshops.Count} records."); - var workshopBaseCards = mapper.Map>(workshops).ToList(); + var workshopBaseCards = mapper.Map>(workshops).ToList(); if (workshopBaseCards.Any()) { @@ -296,10 +295,10 @@ public async Task> GetByProviderId(Guid id, ExcludeIdFilter f } } - var result = new SearchResult() + var result = new SearchResult() { TotalAmount = workshopBaseCardsCount, - Entities = await GetWorkshopsWithAverageRating(workshopBaseCards).ConfigureAwait(false), + Entities = await GetWorkshopsWithAverageRating(workshopBaseCards).ConfigureAwait(false), }; return result; diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopServicesCombiner.cs b/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopServicesCombiner.cs index 5da9038c9b..3ddb5cd79a 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopServicesCombiner.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/IWorkshopServicesCombiner.cs @@ -82,8 +82,7 @@ public interface IWorkshopServicesCombiner /// Type of entity that must be return. /// A representing the result of the asynchronous operation. /// The task result contains a that contains elements from the input sequence. - Task> GetByProviderId(Guid id, ExcludeIdFilter filter) - where T : WorkshopProviderViewCard; + Task> GetByProviderId(Guid id, ExcludeIdFilter filter); /// /// Get all entities that matches filter's parameters. diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/ProviderAdminService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/ProviderAdminService.cs index 4defed92cd..f06f3fa1af 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/ProviderAdminService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/ProviderAdminService.cs @@ -361,7 +361,7 @@ public async Task> GetWorkshopsThatProvid } var filter = new ExcludeIdFilter() { From = 0, Size = int.MaxValue }; - return await workshopService.GetByProviderId(providerAdmin.ProviderId, filter).ConfigureAwait(false); + return await workshopService.GetByProviderId(providerAdmin.ProviderId, filter).ConfigureAwait(false); } var pa = providersAdmins.SingleOrDefault(); diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopServicesCombiner.cs b/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopServicesCombiner.cs index fbda28c9a3..c71c4492db 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopServicesCombiner.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/WorkshopServicesCombiner.cs @@ -228,10 +228,9 @@ public async Task> GetWorkshopListByProviderAdminId(string } /// - public async Task> GetByProviderId(Guid id, ExcludeIdFilter filter) - where T : WorkshopProviderViewCard + public async Task> GetByProviderId(Guid id, ExcludeIdFilter filter) { - var workshopBaseCards = await workshopService.GetByProviderId(id, filter).ConfigureAwait(false); + var workshopBaseCards = await workshopService.GetByProviderId(id, filter).ConfigureAwait(false); return workshopBaseCards; } From 9e935a4ca3735946821f4c3871ccd815451a3cb7 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Mon, 28 Aug 2023 11:27:02 +0300 Subject: [PATCH 07/19] Fix Address mapping for Workshop to WorkshopProviderViewCard --- OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs b/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs index f0d0e116e3..e390174f49 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs @@ -234,8 +234,7 @@ public MappingProfile() .ForMember(dest => dest.TakenSeats, opt => opt.MapFrom(src => src.Applications.Count(x => x.Status == ApplicationStatus.Approved - || x.Status == ApplicationStatus.StudyingForYears))) - .ForMember(dest => dest.Address, opt => opt.Ignore()); + || x.Status == ApplicationStatus.StudyingForYears))); CreateMap().ReverseMap(); From b040af1e2e3c78738dfb4d9e90d16678685ced66 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Mon, 28 Aug 2023 23:04:30 +0300 Subject: [PATCH 08/19] Revert "Fix Address mapping for Workshop to WorkshopProviderViewCard" This reverts commit 9e935a4ca3735946821f4c3871ccd815451a3cb7. --- OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs b/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs index e390174f49..f0d0e116e3 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs @@ -234,7 +234,8 @@ public MappingProfile() .ForMember(dest => dest.TakenSeats, opt => opt.MapFrom(src => src.Applications.Count(x => x.Status == ApplicationStatus.Approved - || x.Status == ApplicationStatus.StudyingForYears))); + || x.Status == ApplicationStatus.StudyingForYears))) + .ForMember(dest => dest.Address, opt => opt.Ignore()); CreateMap().ReverseMap(); From 54e39f3ac046d6917db4402a474286cc6d059efa Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Thu, 31 Aug 2023 10:47:50 +0300 Subject: [PATCH 09/19] Fix commented-out code --- .../Services/ChatMessageWorkshopServiceTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs index 987210beb2..8b2c3c381b 100644 --- a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs +++ b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs @@ -35,7 +35,6 @@ public class ChatMessageWorkshopServiceTests WorkshopId = Guid.NewGuid(), }; - //private IEntityRepository messageRepository; private IChatMessageRepository messageRepository; private Mock roomServiceMock; private Mock> workshopHub; From 91d68b238dda11f9700a63becd0a145e9e73e810 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Thu, 31 Aug 2023 11:19:48 +0300 Subject: [PATCH 10/19] Remove excessive dbContext field --- .../Repository/ChatMessageRepository.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs b/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs index 4c3c17fa84..6b87f5aed0 100644 --- a/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs +++ b/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs @@ -11,17 +11,14 @@ namespace OutOfSchool.Services.Repository; public class ChatMessageRepository : SensitiveEntityRepository, IChatMessageRepository { - private readonly OutOfSchoolDbContext db; - public ChatMessageRepository(OutOfSchoolDbContext dbContext) : base(dbContext) { - db = dbContext; } public async Task CountUnreadMessagesAsync(Guid workshopId) { - return await db.ChatMessageWorkshops + return await dbContext.ChatMessageWorkshops .Include(chr => chr.ChatRoom) .Where(x => x.ChatRoom.WorkshopId == workshopId && x.ReadDateTime == null && !x.SenderRoleIsProvider) .CountAsync(); From 9103ca20e510b898ed009b55fefbc98521d956f8 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Thu, 31 Aug 2023 14:17:34 +0300 Subject: [PATCH 11/19] Remove excessive async / await --- .../Repository/ChatMessageRepository.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs b/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs index 6b87f5aed0..685c2c5baf 100644 --- a/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs +++ b/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs @@ -16,9 +16,9 @@ public ChatMessageRepository(OutOfSchoolDbContext dbContext) { } - public async Task CountUnreadMessagesAsync(Guid workshopId) + public Task CountUnreadMessagesAsync(Guid workshopId) { - return await dbContext.ChatMessageWorkshops + return dbContext.ChatMessageWorkshops .Include(chr => chr.ChatRoom) .Where(x => x.ChatRoom.WorkshopId == workshopId && x.ReadDateTime == null && !x.SenderRoleIsProvider) .CountAsync(); From 2c260446159568105b372fe836a355d283ffa060 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Fri, 1 Sep 2023 15:17:44 +0300 Subject: [PATCH 12/19] Fix tests --- .../Controllers/WorkshopControllerTests.cs | 29 ++++++++-------- .../Services/Database/WorkshopServiceTests.cs | 31 +++++++++-------- .../WorkshopProviderViewCardGenerator.cs | 33 +++++++++++++++++++ 3 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 OutOfSchool/Tests/OutOfSchool.Tests.Common/TestDataGenerators/WorkshopProviderViewCardGenerator.cs diff --git a/OutOfSchool/OutOfSchool.WebApi.Tests/Controllers/WorkshopControllerTests.cs b/OutOfSchool/OutOfSchool.WebApi.Tests/Controllers/WorkshopControllerTests.cs index b3eddc10bf..f165e4241c 100644 --- a/OutOfSchool/OutOfSchool.WebApi.Tests/Controllers/WorkshopControllerTests.cs +++ b/OutOfSchool/OutOfSchool.WebApi.Tests/Controllers/WorkshopControllerTests.cs @@ -47,6 +47,7 @@ public class WorkshopControllerTests private string userId; private Mock httpContextMoq; private List workshopBaseCards; + private List workshopProviderViewCards; private List workshopShortEntitiesList; private List workshopProviderViewCardList; @@ -65,6 +66,8 @@ public void OneTimeSetup() provider = WithProvider(); workshopCards = WithWorkshopCards(); workshopBaseCards = WorkshopBaseCardGenerator.Generate(5); + workshopProviderViewCards = WorkshopProviderViewCardGenerator.Generate(5); + workshopShortEntitiesList = ShortEntityDtoGenerator.Generate(10); workshopProviderViewCardList = WithWorkshopProviderViewCards(); @@ -132,8 +135,8 @@ public async Task GetByProviderId_WhenThereAreWorkshops_ShouldReturnOkResultObje { // Arrange var filter = new ExcludeIdFilter() { From = 0, Size = int.MaxValue }; - var searchResult = new SearchResult() { TotalAmount = 5, Entities = workshopBaseCards }; - workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) + var searchResult = new SearchResult() { TotalAmount = 5, Entities = workshopProviderViewCards }; + workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) .ReturnsAsync(searchResult); // Act @@ -143,7 +146,7 @@ public async Task GetByProviderId_WhenThereAreWorkshops_ShouldReturnOkResultObje workshopServiceMoq.VerifyAll(); Assert.That(result, Is.Not.Null); Assert.AreEqual(Ok, result.StatusCode); - Assert.AreEqual(workshops.Count, (result.Value as SearchResult).TotalAmount); + Assert.AreEqual(workshops.Count, (result.Value as SearchResult).TotalAmount); } [Test] @@ -151,8 +154,8 @@ public async Task GetByProviderId_WhenThereIsNoWorkshops_ShouldReturnNoContentRe { // Arrange var filter = new ExcludeIdFilter() { From = 0, Size = int.MaxValue }; - var emptySearchResult = new SearchResult() { TotalAmount = 0, Entities = new List() }; - workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) + var emptySearchResult = new SearchResult() { TotalAmount = 0, Entities = new List() }; + workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) .ReturnsAsync(emptySearchResult); // Act @@ -182,8 +185,8 @@ public async Task GetByProviderId_WhenThereIsExcludedId_ShouldReturnOkResultObje var expectedWorkshopCount = workshopBaseCards.Count - 1; var excludedId = workshopBaseCards.FirstOrDefault().WorkshopId; var filter = new ExcludeIdFilter() { From = 0, Size = int.MaxValue, ExcludedId = excludedId }; - var searchResult = new SearchResult() { TotalAmount = 4, Entities = workshopBaseCards.Skip(1).ToList() }; - workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) + var searchResult = new SearchResult() { TotalAmount = 4, Entities = workshopProviderViewCards.Skip(1).ToList() }; + workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) .ReturnsAsync(searchResult); // Act @@ -193,7 +196,7 @@ public async Task GetByProviderId_WhenThereIsExcludedId_ShouldReturnOkResultObje workshopServiceMoq.VerifyAll(); Assert.That(result, Is.Not.Null); Assert.AreEqual(Ok, result.StatusCode); - Assert.AreEqual(expectedWorkshopCount, (result.Value as SearchResult).TotalAmount); + Assert.AreEqual(expectedWorkshopCount, (result.Value as SearchResult).TotalAmount); } #endregion @@ -323,8 +326,8 @@ public async Task GetWorkshopProviderViewCardsByProviderId_WhenThereAreWorkshops { // Arrange var filter = new ExcludeIdFilter() { From = 0, Size = int.MaxValue }; - var searchResult = new SearchResult() { TotalAmount = 5, Entities = workshopProviderViewCardList }; - workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) + var searchResult = new SearchResult() { TotalAmount = 5, Entities = workshopProviderViewCards }; + workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) .ReturnsAsync(searchResult); // Act @@ -343,7 +346,7 @@ public async Task GetWorkshopProviderViewCardsByProviderId_WhenThereIsNoWorkshop // Arrange var filter = new ExcludeIdFilter() { From = 0, Size = int.MaxValue }; var emptySearchResult = new SearchResult() { TotalAmount = 0, Entities = new List() }; - workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) + workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) .ReturnsAsync(emptySearchResult); // Act @@ -363,7 +366,7 @@ public async Task GetWorkshopProviderViewCardsByProviderId_WhenSizeFilterIsProvi var filter = new ExcludeIdFilter() { From = 0, Size = expectedCount }; var expectedTotalAmount = 5; var searchResult = new SearchResult() { TotalAmount = expectedTotalAmount, Entities = workshopProviderViewCardList.Take(expectedCount).ToList() }; - workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) + workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) .ReturnsAsync(searchResult); // Act @@ -387,7 +390,7 @@ public async Task GetWorkshopProviderViewCardsByProviderId_WhenFromFilterIsProvi var expectedResult = workshopProviderViewCardList.Skip(skipCount).Take(expectedCount).ToList(); var filter = new ExcludeIdFilter() { From = skipCount, Size = expectedCount }; var searchResult = new SearchResult() { TotalAmount = expectedTotalAmount, Entities = expectedResult }; - workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) + workshopServiceMoq.Setup(x => x.GetByProviderId(It.IsAny(), It.IsAny())) .ReturnsAsync(searchResult); // Act diff --git a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/Database/WorkshopServiceTests.cs b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/Database/WorkshopServiceTests.cs index 46ba30064b..8b7eaf6586 100644 --- a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/Database/WorkshopServiceTests.cs +++ b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/Database/WorkshopServiceTests.cs @@ -40,6 +40,7 @@ public class WorkshopServiceTests private Mock providerAdminRepository; private Mock averageRatingServiceMock; private Mock providerRepositoryMock; + private Mock chatMessageWorkshopServiceMock; [SetUp] public void SetUp() @@ -54,6 +55,7 @@ public void SetUp() providerAdminRepository = new Mock(); averageRatingServiceMock = new Mock(); providerRepositoryMock = new Mock(); + chatMessageWorkshopServiceMock = new Mock(); workshopService = new WorkshopService( @@ -65,7 +67,8 @@ public void SetUp() workshopImagesMediator.Object, providerAdminRepository.Object, averageRatingServiceMock.Object, - providerRepositoryMock.Object); + providerRepositoryMock.Object, + chatMessageWorkshopServiceMock.Object); } #region Create @@ -190,34 +193,34 @@ public async Task GetByProviderId_WhenProviderWithIdExists_ShouldReturnEntities( { // Arrange var workshops = WithWorkshopsList().ToList(); - var expectedWorkshopBaseCards = workshops.Select(w => new WorkshopBaseCard() { ProviderId = w.ProviderId, WorkshopId = w.Id, }).ToList(); + var expectedWorkshopBaseCards = workshops.Select(w => new WorkshopProviderViewCard() { ProviderId = w.ProviderId, WorkshopId = w.Id, }).ToList(); SetupGetRepositoryCount(10); SetupGetByProviderById(workshops); - mapperMock.Setup(m => m.Map>(It.IsAny>())).Returns(expectedWorkshopBaseCards); + mapperMock.Setup(m => m.Map>(It.IsAny>())).Returns(expectedWorkshopBaseCards); // Act - var result = await workshopService.GetByProviderId(It.IsAny(), It.IsAny()).ConfigureAwait(false); + var result = await workshopService.GetByProviderId(It.IsAny(), It.IsAny()).ConfigureAwait(false); // Assert workshopRepository.VerifyAll(); mapperMock.VerifyAll(); - (result as SearchResult).TotalAmount.Should().Be(workshops.Count); - (result as SearchResult).Entities.Should().BeEquivalentTo(expectedWorkshopBaseCards); + (result as SearchResult).TotalAmount.Should().Be(workshops.Count); + (result as SearchResult).Entities.Should().BeEquivalentTo(expectedWorkshopBaseCards); } [Test] public async Task GetByProviderId_WhenThereIsNoEntityWithId_ShouldReturnEmptyList() { // Arrange - var emptyListWorkshopCards = new List(); + var emptyListWorkshopCards = new List(); SetupGetRepositoryCount(0); SetupGetByProviderById(new List()); - mapperMock.Setup(m => m.Map>(It.IsAny>())).Returns(emptyListWorkshopCards); + mapperMock.Setup(m => m.Map>(It.IsAny>())).Returns(emptyListWorkshopCards); // Act - var result = await workshopService.GetByProviderId(Guid.NewGuid(), It.IsAny()).ConfigureAwait(false); + var result = await workshopService.GetByProviderId(Guid.NewGuid(), It.IsAny()).ConfigureAwait(false); // Assert workshopRepository.VerifyAll(); @@ -232,22 +235,22 @@ public async Task GetByProviderId_WhenThereIsExcludedIds_ShouldReturnList() // Arrange var workshops = WithWorkshopsList().ToList(); var excludedIds = new List() { new Guid("b94f1989-c4e7-4878-ac86-21c4a402fb43"), new Guid("8c14044b-e30d-4b14-a18b-5b3b859ad676") }; - var expectedWorkshopBaseCards = workshops.Select(w => new WorkshopBaseCard() { ProviderId = w.ProviderId }) + var expectedWorkshopBaseCards = workshops.Select(w => new WorkshopProviderViewCard() { ProviderId = w.ProviderId }) .Where(w => !excludedIds.Any(excluded => w.WorkshopId != excluded)).ToList(); SetupGetRepositoryCount(10); SetupGetByProviderById(workshops); - mapperMock.Setup(m => m.Map>(It.IsAny>())).Returns(expectedWorkshopBaseCards); + mapperMock.Setup(m => m.Map>(It.IsAny>())).Returns(expectedWorkshopBaseCards); // Act - var result = await workshopService.GetByProviderId(It.IsAny(), It.IsAny()).ConfigureAwait(false); + var result = await workshopService.GetByProviderId(It.IsAny(), It.IsAny()).ConfigureAwait(false); // Assert workshopRepository.VerifyAll(); mapperMock.VerifyAll(); - (result as SearchResult).TotalAmount.Should().Be(workshops.Count); - (result as SearchResult).Entities.Should().BeEquivalentTo(expectedWorkshopBaseCards); + (result as SearchResult).TotalAmount.Should().Be(workshops.Count); + (result as SearchResult).Entities.Should().BeEquivalentTo(expectedWorkshopBaseCards); } #endregion diff --git a/OutOfSchool/Tests/OutOfSchool.Tests.Common/TestDataGenerators/WorkshopProviderViewCardGenerator.cs b/OutOfSchool/Tests/OutOfSchool.Tests.Common/TestDataGenerators/WorkshopProviderViewCardGenerator.cs new file mode 100644 index 0000000000..dd908f3fc5 --- /dev/null +++ b/OutOfSchool/Tests/OutOfSchool.Tests.Common/TestDataGenerators/WorkshopProviderViewCardGenerator.cs @@ -0,0 +1,33 @@ +using Bogus; +using OutOfSchool.Common.Enums; +using OutOfSchool.WebApi.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OutOfSchool.Tests.Common.TestDataGenerators; +public static class WorkshopProviderViewCardGenerator +{ + private static readonly Faker Faker = new Faker() + .RuleFor(x => x.Title, f => f.Company.CompanyName()) + .RuleFor(x => x.MinAge, f => f.Random.Number(1, 18)) + .RuleFor(x => x.MaxAge, f => f.Random.Number(1, 18)) + .RuleFor(x => x.Price, f => f.Random.Decimal(0, 10000)) + .RuleFor(x => x.WithDisabilityOptions, f => f.Random.Bool()) + .RuleFor(x => x.CoverImageId, f => f.Image.LoremFlickrUrl()) + .RuleFor(x => x.ProviderTitle, f => f.Company.CompanyName()) + .RuleFor(x => x.ProviderOwnership, f => f.PickRandom()) + .RuleFor(x => x.PayRate, f => f.PickRandom()) + .RuleFor(x => x.ProviderId, _ => Guid.NewGuid()) + .RuleFor(x => x.WorkshopId, _ => Guid.NewGuid()) + .RuleFor(x => x.AvailableSeats, f => f.Random.UInt(0, 100)) + .RuleFor(x => x.TakenSeats, f => f.Random.UInt(0, 100)) + .RuleFor(x => x.AmountOfPendingApplications, f => f.Random.Int(0, 100)) + .RuleFor(x => x.UnreadMessages, f => f.Random.Int(0, 100)); + + public static WorkshopProviderViewCard Generate() => Faker.Generate(); + + public static List Generate(int count) => new List(count); +} From c0ce7b763b696b37a82cd28f1370286777d8bda4 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Fri, 1 Sep 2023 15:42:57 +0300 Subject: [PATCH 13/19] Fix mapping for UnreadMessages property --- OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs b/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs index f0d0e116e3..b72864b0ae 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs @@ -235,7 +235,8 @@ public MappingProfile() src.Applications.Count(x => x.Status == ApplicationStatus.Approved || x.Status == ApplicationStatus.StudyingForYears))) - .ForMember(dest => dest.Address, opt => opt.Ignore()); + .ForMember(dest => dest.Address, opt => opt.Ignore()) + .ForMember(dest => dest.UnreadMessages, opt => opt.Ignore()); CreateMap().ReverseMap(); From 78c1b7410e4f6919d72bccc634b37e0eae1dac63 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Sun, 3 Sep 2023 16:24:35 +0300 Subject: [PATCH 14/19] Change method name CountUnreadMessagesAsync to CountUnreadMessages --- .../Repository/ChatMessageRepository.cs | 2 +- .../Repository/IChatMessageRepository.cs | 2 +- .../OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs | 4 ++-- .../OutOfSchool.WebApi/Services/Database/WorkshopService.cs | 2 +- .../Services/IChatMessageWorkshopService.cs | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs b/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs index 685c2c5baf..93f68ac1ff 100644 --- a/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs +++ b/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs @@ -16,7 +16,7 @@ public ChatMessageRepository(OutOfSchoolDbContext dbContext) { } - public Task CountUnreadMessagesAsync(Guid workshopId) + public Task CountUnreadMessages(Guid workshopId) { return dbContext.ChatMessageWorkshops .Include(chr => chr.ChatRoom) diff --git a/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs b/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs index 2ff38f0d42..23fc2c7034 100644 --- a/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs +++ b/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs @@ -14,5 +14,5 @@ public interface IChatMessageRepository : ISensitiveEntityRepository /// Workshop's key. /// A representing the result of the asynchronous operation. The task result contains a that contains the number of unread messages for the specified Workshop. - Task CountUnreadMessagesAsync(Guid workshopId); + Task CountUnreadMessages(Guid workshopId); } \ No newline at end of file diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs index 5cc418c3c7..9799033179 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs @@ -127,11 +127,11 @@ public async Task> GetMessagesForChatRoomAndSetRead } } - public async Task CountUnreadMessagesAsync(Guid workshopId) + public async Task CountUnreadMessages(Guid workshopId) { try { - return await messageRepository.CountUnreadMessagesAsync(workshopId).ConfigureAwait(false); + return await messageRepository.CountUnreadMessages(workshopId).ConfigureAwait(false); } catch (Exception exception) { diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs index 6f8c9d9361..5e43958114 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs @@ -289,7 +289,7 @@ public async Task> GetByProviderId(Guid i { foreach (var workshop in workshopBaseCards) { - var messages = await chatMessageWorkshopService.CountUnreadMessagesAsync(workshop.WorkshopId).ConfigureAwait(false); + var messages = await chatMessageWorkshopService.CountUnreadMessages(workshop.WorkshopId).ConfigureAwait(false); logger.LogInformation($"Workshop.Id: {workshop.WorkshopId} has {messages} unread messages."); workshop.UnreadMessages = messages; } diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/IChatMessageWorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/IChatMessageWorkshopService.cs index faa098465f..679be3f823 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/IChatMessageWorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/IChatMessageWorkshopService.cs @@ -46,5 +46,5 @@ public interface IChatMessageWorkshopService /// /// Workshop's key. /// A representing the result of the asynchronous operation. The task result contains a that contains the number of unread messages for the specified Workshop. - Task CountUnreadMessagesAsync(Guid workshopId); + Task CountUnreadMessages(Guid workshopId); } \ No newline at end of file From fb205dcc17bf9f563e0e745ed5f9fd7f535266d8 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Sun, 3 Sep 2023 18:37:02 +0300 Subject: [PATCH 15/19] Add test for CountUnreadMessages --- .../Services/ChatMessageWorkshopServiceTests.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs index 8b2c3c381b..b1affdce8e 100644 --- a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs +++ b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs @@ -27,6 +27,7 @@ public class ChatMessageWorkshopServiceTests { private static Guid chatRoomId1 = Guid.NewGuid(); private static Guid chatRoomId2 = Guid.NewGuid(); + private static Guid workshopId1 = Guid.NewGuid(); private static ChatMessageWorkshopCreateDto newMessage = new ChatMessageWorkshopCreateDto() { @@ -195,6 +196,22 @@ public async Task GetMessagesForChatRoomAndSetReadDateTimeIfItIsNullAsync_WhenCa } #endregion + #region CountUnreadMessages + [Test] + public async Task CountUnreadMessages_WhenCalledWithValidWorkshopId_ShouldReturnNumberOfUnreadMessages() + { + // Arrange + var existingWorkshopId = workshopId1; + + // Act + var result = await messageRepository.CountUnreadMessages(existingWorkshopId).ConfigureAwait(false); + + // Assert + Assert.AreEqual(0, result); + + } + #endregion + private void SeedDatabase() { using var context = new OutOfSchoolDbContext(options); From e6a9caf76145abd7951260b3416734cfe7cc5ddc Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Sun, 3 Sep 2023 19:14:41 +0300 Subject: [PATCH 16/19] Revert "Add test for CountUnreadMessages" This reverts commit fb205dcc17bf9f563e0e745ed5f9fd7f535266d8. --- .../Services/ChatMessageWorkshopServiceTests.cs | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs index b1affdce8e..8b2c3c381b 100644 --- a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs +++ b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs @@ -27,7 +27,6 @@ public class ChatMessageWorkshopServiceTests { private static Guid chatRoomId1 = Guid.NewGuid(); private static Guid chatRoomId2 = Guid.NewGuid(); - private static Guid workshopId1 = Guid.NewGuid(); private static ChatMessageWorkshopCreateDto newMessage = new ChatMessageWorkshopCreateDto() { @@ -196,22 +195,6 @@ public async Task GetMessagesForChatRoomAndSetReadDateTimeIfItIsNullAsync_WhenCa } #endregion - #region CountUnreadMessages - [Test] - public async Task CountUnreadMessages_WhenCalledWithValidWorkshopId_ShouldReturnNumberOfUnreadMessages() - { - // Arrange - var existingWorkshopId = workshopId1; - - // Act - var result = await messageRepository.CountUnreadMessages(existingWorkshopId).ConfigureAwait(false); - - // Assert - Assert.AreEqual(0, result); - - } - #endregion - private void SeedDatabase() { using var context = new OutOfSchoolDbContext(options); From f0c24b1b3cd559cab2a3c69efa5e9a73d39fdd97 Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Mon, 4 Sep 2023 16:22:38 +0300 Subject: [PATCH 17/19] Revert "Change method name CountUnreadMessagesAsync to CountUnreadMessages" This reverts commit 78c1b7410e4f6919d72bccc634b37e0eae1dac63. --- .../Repository/ChatMessageRepository.cs | 2 +- .../Repository/IChatMessageRepository.cs | 2 +- .../OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs | 4 ++-- .../OutOfSchool.WebApi/Services/Database/WorkshopService.cs | 2 +- .../Services/IChatMessageWorkshopService.cs | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs b/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs index 93f68ac1ff..685c2c5baf 100644 --- a/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs +++ b/OutOfSchool/OutOfSchool.DataAccess/Repository/ChatMessageRepository.cs @@ -16,7 +16,7 @@ public ChatMessageRepository(OutOfSchoolDbContext dbContext) { } - public Task CountUnreadMessages(Guid workshopId) + public Task CountUnreadMessagesAsync(Guid workshopId) { return dbContext.ChatMessageWorkshops .Include(chr => chr.ChatRoom) diff --git a/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs b/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs index 23fc2c7034..2ff38f0d42 100644 --- a/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs +++ b/OutOfSchool/OutOfSchool.DataAccess/Repository/IChatMessageRepository.cs @@ -14,5 +14,5 @@ public interface IChatMessageRepository : ISensitiveEntityRepository /// Workshop's key. /// A representing the result of the asynchronous operation. The task result contains a that contains the number of unread messages for the specified Workshop. - Task CountUnreadMessages(Guid workshopId); + Task CountUnreadMessagesAsync(Guid workshopId); } \ No newline at end of file diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs index 9799033179..5cc418c3c7 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/ChatMessageWorkshopService.cs @@ -127,11 +127,11 @@ public async Task> GetMessagesForChatRoomAndSetRead } } - public async Task CountUnreadMessages(Guid workshopId) + public async Task CountUnreadMessagesAsync(Guid workshopId) { try { - return await messageRepository.CountUnreadMessages(workshopId).ConfigureAwait(false); + return await messageRepository.CountUnreadMessagesAsync(workshopId).ConfigureAwait(false); } catch (Exception exception) { diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs index 5e43958114..6f8c9d9361 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/Database/WorkshopService.cs @@ -289,7 +289,7 @@ public async Task> GetByProviderId(Guid i { foreach (var workshop in workshopBaseCards) { - var messages = await chatMessageWorkshopService.CountUnreadMessages(workshop.WorkshopId).ConfigureAwait(false); + var messages = await chatMessageWorkshopService.CountUnreadMessagesAsync(workshop.WorkshopId).ConfigureAwait(false); logger.LogInformation($"Workshop.Id: {workshop.WorkshopId} has {messages} unread messages."); workshop.UnreadMessages = messages; } diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/IChatMessageWorkshopService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/IChatMessageWorkshopService.cs index 679be3f823..faa098465f 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/IChatMessageWorkshopService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/IChatMessageWorkshopService.cs @@ -46,5 +46,5 @@ public interface IChatMessageWorkshopService /// /// Workshop's key. /// A representing the result of the asynchronous operation. The task result contains a that contains the number of unread messages for the specified Workshop. - Task CountUnreadMessages(Guid workshopId); + Task CountUnreadMessagesAsync(Guid workshopId); } \ No newline at end of file From 4def6b9f6ed0ca29c9afbe174f4d47049751874e Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Tue, 5 Sep 2023 13:47:22 +0300 Subject: [PATCH 18/19] Add test for CountUnreadMessagesAsync --- .../ChatMessageWorkshopServiceTests.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs index 8b2c3c381b..8b5455988d 100644 --- a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs +++ b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs @@ -195,6 +195,30 @@ public async Task GetMessagesForChatRoomAndSetReadDateTimeIfItIsNullAsync_WhenCa } #endregion + #region CountUnreadMessagesAsync + [Test] + public async Task CountUnreadMessagesAsync_WhenCalledWithValidWorkshopId_ShouldReturnNumberOfUnreadMessages() + { + // Arrange + var workshopId = Guid.NewGuid(); + int expectedUnreadMessages = 7; + var messageRepositoryMock = new Mock(); + messageRepositoryMock.Setup(x => x.CountUnreadMessagesAsync(workshopId)).Returns(Task.FromResult(expectedUnreadMessages)); + var messageService = new ChatMessageWorkshopService( + messageRepositoryMock.Object, + roomServiceMock.Object, + workshopHub.Object, + loggerMock.Object, + mapper); + + // Act + var result = await messageService.CountUnreadMessagesAsync(workshopId).ConfigureAwait(false); + + // Assert + Assert.AreEqual(expectedUnreadMessages, result); + } + #endregion + private void SeedDatabase() { using var context = new OutOfSchoolDbContext(options); From a662c0ddc9df5ed0226c818873f926da2bbf8d9e Mon Sep 17 00:00:00 2001 From: Dmytro Kymacnynskyi Date: Tue, 5 Sep 2023 17:26:33 +0300 Subject: [PATCH 19/19] Add exception test for CountUnreadMessagesAsync --- .../ChatMessageWorkshopServiceTests.cs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs index 8b5455988d..1eb501262f 100644 --- a/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs +++ b/OutOfSchool/OutOfSchool.WebApi.Tests/Services/ChatMessageWorkshopServiceTests.cs @@ -217,6 +217,27 @@ public async Task CountUnreadMessagesAsync_WhenCalledWithValidWorkshopId_ShouldR // Assert Assert.AreEqual(expectedUnreadMessages, result); } + + [Test] + public void CountUnreadMessagesAsync_ThrowsException() + { + // Arrange + var exceptionText = "Test exception"; + var workshopId = Guid.NewGuid(); + var messageRepositoryMock = new Mock(); + messageRepositoryMock.Setup(x => x.CountUnreadMessagesAsync(workshopId)) + .Throws(new Exception(exceptionText)); + var messageService = new ChatMessageWorkshopService( + messageRepositoryMock.Object, + roomServiceMock.Object, + workshopHub.Object, + loggerMock.Object, + mapper); + + // Act and Assert + Exception ex = Assert.ThrowsAsync(async () => await messageService.CountUnreadMessagesAsync(workshopId)); + Assert.That(ex.Message, Is.EqualTo(exceptionText)); + } #endregion private void SeedDatabase()