Skip to content

Commit

Permalink
Merge pull request #439 from NielsPilgaard/feature/remove-db-category
Browse files Browse the repository at this point in the history
minor cleanup and bug fixes
  • Loading branch information
NielsPilgaard authored Jan 6, 2025
2 parents 3e525b0 + 4d8fdd5 commit d092358
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public static WebApplicationBuilder AddDatabase(this WebApplicationBuilder build
{
var connectionString = GetConnectionString(builder.Configuration);

builder.Services
.AddSqlServer<JordnaerDbContext>(connectionString,
optionsBuilder => optionsBuilder.UseAzureSqlDefaults());
builder.Services.AddAzureSql<JordnaerDbContext>(connectionString);

builder.Services.AddHealthChecks().AddSqlServer(connectionString);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public static WebApplicationBuilder AddSerilog(this WebApplicationBuilder builde
{
if (!builder.Environment.IsDevelopment())
{

builder.Services
.AddOptions<GrafanaLokiOptions>()
.BindConfiguration(GrafanaLokiOptions.SectionName)
Expand Down
22 changes: 22 additions & 0 deletions src/shared/Jordnaer.Shared/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Jordnaer.Shared.Extensions;
public static class EnumExtensions
{
public static DisplayAttribute? GetDisplayAttribute(this Enum enumValue)
{
return enumValue.GetType()
.GetField(enumValue.ToString())?.GetCustomAttribute<DisplayAttribute>();
}

public static string GetDisplayName(this Enum enumValue)
{
return enumValue.GetDisplayAttribute()?.Name ?? enumValue.ToString();
}
}
4 changes: 0 additions & 4 deletions src/web/Jordnaer/Components/Routes.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
@using Blazr.RenderState
@inject Blazr.RenderState.IBlazrRenderStateService RenderStateService


@* @if (RenderStateService.IsPreRender && RenderStateService.RenderState is not BlazrRenderState.None)
{ TODO: Figure out how to make a good loader
<LoadingScreen />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,8 @@ public static WebApplicationBuilder AddDatabase(this WebApplicationBuilder build
{
var connectionString = GetConnectionString(builder.Configuration);

builder.Services
.AddDbContextFactory<JordnaerDbContext>(
optionsBuilder =>
optionsBuilder.UseSqlServer(connectionString,
contextOptionsBuilder =>
contextOptionsBuilder.UseAzureSqlDefaults()),
builder.Services.AddDbContextFactory<JordnaerDbContext>(
optionsBuilder => optionsBuilder.UseAzureSql(connectionString),
ServiceLifetime.Scoped);

builder.Services.AddHealthChecks().AddSqlServer(connectionString);
Expand Down
21 changes: 10 additions & 11 deletions src/web/Jordnaer/Features/Chat/ChatMessageList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@
public bool LastMessageWasSentSuccessfullyByCurrentUser { get; set; }

private MudTextField<string> _messageInput = new();
private bool _isActiveChatPublished => ActiveChat?.Messages.Count is not 0;

private bool IsMessageFromSelf(ChatMessageDto message) => message.SenderId == CurrentUser.Id;

Expand Down Expand Up @@ -210,26 +209,26 @@
}

var message = new ChatMessageDto
{
ChatId = ActiveChat.Id,
Id = NewId.NextGuid(),
SentUtc = DateTime.UtcNow,
SenderId = CurrentUser.Id!,
Text = _messageInput.Value
};
{
ChatId = ActiveChat.Id,
Id = NewId.NextGuid(),
SentUtc = DateTime.UtcNow,
SenderId = CurrentUser.Id!,
Text = _messageInput.Value
};

await _messageInput.BlurAsync();
await _messageInput.Clear();

ActiveChat.Messages.Add(message);

if (_isActiveChatPublished)
if (ActiveChat.Messages.Count is 1) // If we have 1 message, we just started the chat
{
await ChatService.SendMessageAsync(message);
await ChatService.StartChatAsync(ActiveChat.ToStartChatCommand(CurrentUser.Id!));
}
else
{
await ChatService.StartChatAsync(ActiveChat.ToStartChatCommand(CurrentUser.Id!));
await ChatService.SendMessageAsync(message);
}

await _messageInput.FocusAsync();
Expand Down
28 changes: 16 additions & 12 deletions src/web/Jordnaer/Features/Chat/ChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,25 @@ public async Task<int> GetUnreadMessageCountAsync(string userId, CancellationTok
return unreadMessageCount;
}

private const string NotFound = "Chat does not exist, unable to get messages for it.";

public async Task<OneOf<List<ChatMessageDto>, Error<string>>> GetChatMessagesAsync(string userId, Guid chatId, int skip, int take, CancellationToken cancellationToken = default)
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await IsCurrentUserPartOfChat(userId, chatId, context, cancellationToken))

var chat = await context.Chats
.AsNoTracking()
.AsSingleQuery()
.Include(chat => chat.Recipients)
.FirstOrDefaultAsync(x => x.Id == chatId, cancellationToken);
if (chat is null)
{
logger.LogDebug(NotFound);
return new Error<string>(NotFound);
}

// Check if any of the recipients are the user we're fetching messages for
if (chat.Recipients.All(x => x.Id != userId))
{
logger.LogWarning(
"Tried to get chat messages for chat {ChatId} and UserId {UserId}, but that user is not part of that chat. Access denied.", chatId, userId);
Expand Down Expand Up @@ -182,15 +197,4 @@ public async Task<OneOf<Guid, NotFound>> GetChatByUserIdsAsync(string currentUse
? new NotFound()
: existingChat.Id;
}

private static async Task<bool> IsCurrentUserPartOfChat(string userId, Guid chatId, JordnaerDbContext context, CancellationToken cancellationToken = default)
{
var chat = await context
.Chats
.AsNoTracking()
.FirstOrDefaultAsync(chat => chat.Id == chatId &&
chat.Recipients.Any(recipient => recipient.Id == userId), cancellationToken);

return chat != null;
}
}
4 changes: 2 additions & 2 deletions src/web/Jordnaer/Features/Profile/EditChildProfileTabs.razor
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@

private void RemoveChild(MudTabPanel obj)
{
var tab = Parent.ChildProfiles.Find(e => e.Id == (Guid) obj.ID);
var tab = Parent.ChildProfiles.Find(e => e.Id == (Guid?) obj.ID);
if (tab is null)
{
return;
}
Parent.ChildProfiles.Remove(tab);

var editChildProfileComponent = _editChildProfileComponents.Find(e => e.ChildProfile.Id == (Guid) obj.ID);
var editChildProfileComponent = _editChildProfileComponents.Find(e => e.ChildProfile.Id == (Guid?) obj.ID);
if (editChildProfileComponent is null)
{
return;
Expand Down
1 change: 0 additions & 1 deletion src/web/Jordnaer/Jordnaer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.23.0" />
<PackageReference Include="Blazr.RenderState.Server" Version="1.0.0" />
<PackageReference Include="Grafana.OpenTelemetry" Version="1.1.0" />
<PackageReference Include="HtmlSanitizer" Version="8.1.870" />
<PackageReference Include="Markdig" Version="0.38.0" />
Expand Down
3 changes: 3 additions & 0 deletions src/web/Jordnaer/Pages/Chat/ChatPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@inject NavigationManager Navigation
@inject IJSRuntime JsRuntime

@using System.Diagnostics
@implements IAsyncDisposable

@attribute [Authorize]
Expand Down Expand Up @@ -203,6 +204,8 @@

private async Task SelectChat(ChatDto chat)
{
Debug.Assert(chat is not null);

_activeChat = chat;

await LoadMessages();
Expand Down
3 changes: 1 addition & 2 deletions src/web/Jordnaer/Pages/Profile/MyProfile.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@inject IProfileCache ProfileCache
@inject IProfileService ProfileService
@inject ISnackbar Snackbar
@inject IBlazrRenderStateService RenderStateService

@attribute [Authorize]

Expand Down Expand Up @@ -135,7 +134,7 @@

protected override async Task OnInitializedAsync()
{
if (RenderStateService.IsPreRender)
if (!RendererInfo.IsInteractive)
{
return;
}
Expand Down
3 changes: 0 additions & 3 deletions src/web/Jordnaer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Azure.Storage.Blobs;
using Blazored.LocalStorage;
using Blazored.SessionStorage;
using Blazr.RenderState.Server;
using Jordnaer.Components.Account;
using Jordnaer.Extensions;
using Jordnaer.Features.Category;
Expand Down Expand Up @@ -74,8 +73,6 @@
builder.AddGroupServices();
builder.AddGroupSearchServices();

builder.AddBlazrRenderStateServerServices();

builder.AddCategoryServices();
builder.AddProfileServices();
builder.AddChatServices();
Expand Down
1 change: 0 additions & 1 deletion src/web/Jordnaer/_Imports.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@using Blazored.LocalStorage
@using Blazored.SessionStorage
@using Blazr.RenderState
@using Jordnaer
@using Jordnaer.Components
@using Jordnaer.Components.Account.Pages
Expand Down

0 comments on commit d092358

Please sign in to comment.