Skip to content

Commit ce5e08b

Browse files
committed
fixes 423
1 parent 5fa6885 commit ce5e08b

11 files changed

Lines changed: 60 additions & 45 deletions

File tree

MangoAPI.BusinessLogic/ApiCommands/Communities/CreateChannelCommandHandler.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
using MangoAPI.Application.Interfaces;
2-
using System;
1+
using System;
32
using System.Threading;
43
using System.Threading.Tasks;
5-
using MangoAPI.BusinessLogic.HubConfig;
6-
using MangoAPI.BusinessLogic.Models;
74
using MangoAPI.BusinessLogic.Responses;
85
using MangoAPI.Domain.Entities;
96
using MangoAPI.Domain.Enums;
107
using MangoAPI.Infrastructure.Database;
118
using MediatR;
12-
using Microsoft.AspNetCore.SignalR;
139

1410
namespace MangoAPI.BusinessLogic.ApiCommands.Communities;
1511

@@ -18,20 +14,21 @@ public class CreateChannelCommandHandler
1814
{
1915
private const string DefaultChannelImage = "default_group_logo.png";
2016
private readonly MangoDbContext dbContext;
21-
private readonly IHubContext<ChatHub, IHubClient> hubContext;
17+
// private readonly IHubContext<ChatHub, IHubClient> hubContext;
2218
private readonly ResponseFactory<CreateCommunityResponse> responseFactory;
23-
private readonly IBlobServiceSettings blobServiceSettings;
19+
// private readonly IBlobServiceSettings blobServiceSettings;
2420

2521
public CreateChannelCommandHandler(
2622
MangoDbContext dbContext,
27-
IHubContext<ChatHub, IHubClient> hubContext,
28-
ResponseFactory<CreateCommunityResponse> responseFactory,
29-
IBlobServiceSettings blobServiceSettings)
23+
// IHubContext<ChatHub, IHubClient> hubContext,
24+
ResponseFactory<CreateCommunityResponse> responseFactory
25+
// IBlobServiceSettings blobServiceSettings
26+
)
3027
{
3128
this.dbContext = dbContext;
32-
this.hubContext = hubContext;
29+
// this.hubContext = hubContext;
3330
this.responseFactory = responseFactory;
34-
this.blobServiceSettings = blobServiceSettings;
31+
// this.blobServiceSettings = blobServiceSettings;
3532
}
3633

3734
public async Task<Result<CreateCommunityResponse>> Handle(
@@ -54,10 +51,10 @@ public async Task<Result<CreateCommunityResponse>> Handle(
5451

5552
await dbContext.SaveChangesAsync(cancellationToken);
5653

57-
var chatLogoImageUrl = $"{blobServiceSettings.MangoBlobAccess}/{DefaultChannelImage}";
54+
// var chatLogoImageUrl = $"{blobServiceSettings.MangoBlobAccess}/{DefaultChannelImage}";
5855

59-
var chatDto = chat.ToChatDto(chatLogoImageUrl);
60-
await hubContext.Clients.Group(request.UserId.ToString()).UpdateUserChatsAsync(chatDto);
56+
// var chatDto = chat.ToChatDto(chatLogoImageUrl);
57+
// await hubContext.Clients.Group(request.UserId.ToString()).PrivateChatCreatedAsync(chatDto);
6158

6259
return responseFactory.SuccessResponse(CreateCommunityResponse.FromSuccess(chat));
6360
}

MangoAPI.BusinessLogic/ApiCommands/Communities/CreateChatCommandHandler.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using MangoAPI.Application.Interfaces;
12
using System;
23
using System.Linq;
34
using System.Threading;
@@ -21,15 +22,18 @@ public class CreateChatCommandHandler
2122
private readonly MangoDbContext dbContext;
2223
private readonly IHubContext<ChatHub, IHubClient> hubContext;
2324
private readonly ResponseFactory<CreateCommunityResponse> responseFactory;
25+
private readonly IBlobServiceSettings blobServiceSettings;
2426

2527
public CreateChatCommandHandler(
2628
MangoDbContext dbContext,
2729
IHubContext<ChatHub, IHubClient> hubContext,
28-
ResponseFactory<CreateCommunityResponse> responseFactory)
30+
ResponseFactory<CreateCommunityResponse> responseFactory,
31+
IBlobServiceSettings blobServiceSettings)
2932
{
3033
this.dbContext = dbContext;
3134
this.hubContext = hubContext;
3235
this.responseFactory = responseFactory;
36+
this.blobServiceSettings = blobServiceSettings;
3337
}
3438

3539
public async Task<Result<CreateCommunityResponse>> Handle(
@@ -55,8 +59,8 @@ public async Task<Result<CreateCommunityResponse>> Handle(
5559
return responseFactory.ConflictResponse(errorMessage, errorDescription);
5660
}
5761

58-
var currentUserDisplayName = await dbContext.Users.Where(x => x.Id == request.UserId)
59-
.Select(x => x.DisplayName)
62+
var senderData = await dbContext.Users.Where(x => x.Id == request.UserId)
63+
.Select(x => new { x.DisplayName, x.ImageFileName })
6064
.FirstOrDefaultAsync(cancellationToken);
6165

6266
var userPrivateChats = await dbContext.Chats
@@ -74,8 +78,8 @@ public async Task<Result<CreateCommunityResponse>> Handle(
7478
return responseFactory.SuccessResponse(CreateCommunityResponse.FromSuccess(existingChat));
7579
}
7680

77-
var title = $"{currentUserDisplayName} / {partner.DisplayName}";
78-
var description = $"Direct chat between {currentUserDisplayName} and {partner.DisplayName}";
81+
var title = $"{senderData.DisplayName} / {partner.DisplayName}";
82+
var description = $"Direct chat between {senderData.DisplayName} and {partner.DisplayName}";
7983

8084
var chat = ChatEntity.Create(
8185
title,
@@ -93,8 +97,11 @@ public async Task<Result<CreateCommunityResponse>> Handle(
9397

9498
await dbContext.SaveChangesAsync(cancellationToken);
9599

96-
var chatDto = chat.ToChatDto();
97-
await hubContext.Clients.Group(request.UserId.ToString()).UpdateUserChatsAsync(chatDto);
100+
var partnerImageUrl = $"{blobServiceSettings.MangoBlobAccess}/{senderData.ImageFileName}";
101+
102+
var chatDto = chat.ToChatDto(partnerImageUrl, partner.DisplayName);
103+
104+
await hubContext.Clients.Group(request.PartnerId.ToString()).PrivateChatCreatedAsync(chatDto);
98105

99106
return responseFactory.SuccessResponse(CreateCommunityResponse.FromSuccess(chat));
100107
}

MangoAPI.BusinessLogic/HubConfig/IHubClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface IHubClient
1313
/// <summary>
1414
/// Updates client list of chats via SignalR.
1515
/// </summary>
16-
Task UpdateUserChatsAsync(Chat chat);
16+
Task PrivateChatCreatedAsync(Chat chat);
1717

1818
/// <summary>
1919
/// Notifies chat subscribers on the message delete via SignalR.

MangoAPI.BusinessLogic/Models/Chat.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public override string ToString()
4747

4848
public static class ChatEntityMapper
4949
{
50-
public static Chat ToChatDto(this ChatEntity entity, string chatLogoImageUrl = "")
50+
public static Chat ToChatDto(this ChatEntity entity, string chatLogoImageUrl, string partnerDisplayName)
5151
{
5252
return new Chat
5353
{
5454
ChatId = entity.Id,
55-
Title = entity.Title,
55+
Title = partnerDisplayName,
5656
CommunityType = entity.CommunityType,
5757
Description = entity.Description,
5858
MembersCount = entity.MembersCount,

MangoAPI.Client/src/app/components/chats/chats.component.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,17 @@ export class ChatsComponent implements OnInit {
151151
this.onMessageSendHandler(message);
152152
});
153153

154-
this.connection.on('UpdateUserChatsAsync', (chat: Chat) => {
154+
this.connection.on('PrivateChatCreatedAsync', (chat: Chat) => {
155155
this.chats.push(chat);
156+
157+
if (this.realTimeConnections.includes(chat.chatId)) {
158+
return;
159+
}
160+
161+
this.connection.invoke('JoinGroup', chat.chatId).then(() => {
162+
this.realTimeConnections.push(chat.chatId);
163+
console.log(`SignalR JoinGroup: ${chat.chatId}`);
164+
});
156165
});
157166

158167
this.connection.on('NotifyOnMessageDeleteAsync', (notification: DeleteMessageNotification) => {

MangoAPI.Domain/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
## Chat Entity
3333

3434
1. **Id**: Primary key of chat entity (GUID). Required.
35-
2. **Title**: Title of the chat as string. Can be up to 50 characters. Required.
36-
3. **ImageFileName**: Name of the image file. Must be a string up to 50 various characters. Required.
35+
2. **Title**: Title of the chat as string. Can be up to 100 characters. Required.
36+
3. **ImageFileName**: Name of the image file. Must be a string up to 100 various characters. Required.
3737
4. **Description**: Short description of the chat as string. Can be up to 150 various characters. Required.
3838
5. **MembersCount**: Number of members in the chat as integer. Must be greater or equal to zero. Required.
3939
6. **CommunityType**: Type of the chat as enum. Can be one of the following: "DirectChat = 1", "PublicChannel = 2".

MangoAPI.Infrastructure/Database/Configurations/ChatEntityConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public void Configure(EntityTypeBuilder<ChatEntity> builder)
1212

1313
builder.HasKey(x => x.Id);
1414

15-
builder.Property(x => x.Title).HasMaxLength(50).IsRequired();
16-
builder.Property(x => x.ImageFileName).HasMaxLength(50).IsRequired();
15+
builder.Property(x => x.Title).HasMaxLength(100).IsRequired();
16+
builder.Property(x => x.ImageFileName).HasMaxLength(100).IsRequired();
1717
builder.Property(x => x.Description).IsRequired().HasMaxLength(150);
1818
builder.Property(x => x.CommunityType).IsRequired();
1919
builder.Property(x => x.LastMessageAuthor).HasMaxLength(50);

MangoAPI.Infrastructure/Database/DesignTimeDbContextFactory.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.EntityFrameworkCore;
55
using Microsoft.EntityFrameworkCore.Design;
66
using Microsoft.Extensions.Configuration;
7+
using System;
78

89
namespace MangoAPI.Infrastructure.Database;
910

@@ -17,8 +18,9 @@ public DesignTimeDbContextFactory()
1718

1819
var configuration = new ConfigurationBuilder().AddJsonFile(appSettingsPath).Build();
1920

20-
mangoDatabaseUrl = configuration[EnvironmentConstants.DatabaseUrl]
21-
?? throw new AppSettingException(EnvironmentConstants.DatabaseUrl);
21+
mangoDatabaseUrl = Environment.GetEnvironmentVariable(EnvironmentConstants.DatabaseUrl)
22+
?? configuration[EnvironmentConstants.DatabaseUrl]
23+
?? throw new AppSettingException(EnvironmentConstants.DatabaseUrl);
2224
}
2325

2426
public MangoDbContext CreateDbContext(string[] args)
@@ -29,4 +31,4 @@ public MangoDbContext CreateDbContext(string[] args)
2931

3032
return new MangoDbContext(options.Options);
3133
}
32-
}
34+
}

MangoAPI.Infrastructure/Migrations/20230313185156_Initial.Designer.cs renamed to MangoAPI.Infrastructure/Migrations/20230314232517_Initial.Designer.cs

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MangoAPI.Infrastructure/Migrations/20230313185156_Initial.cs renamed to MangoAPI.Infrastructure/Migrations/20230314232517_Initial.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ protected override void Up(MigrationBuilder migrationBuilder)
1818
columns: table => new
1919
{
2020
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
21-
Title = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
21+
Title = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
2222
CommunityType = table.Column<int>(type: "int", nullable: false),
2323
Description = table.Column<string>(type: "nvarchar(150)", maxLength: 150, nullable: false),
24-
ImageFileName = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
24+
ImageFileName = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
2525
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
2626
MembersCount = table.Column<int>(type: "int", nullable: false),
2727
LastMessageAuthor = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
@@ -96,8 +96,8 @@ protected override void Up(MigrationBuilder migrationBuilder)
9696
schema: "mango",
9797
columns: table => new
9898
{
99-
ContactId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
10099
UserId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
100+
ContactId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
101101
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false)
102102
},
103103
constraints: table =>

0 commit comments

Comments
 (0)