From 13d2b1b1fbffb06c3389e1117365257377ea046a Mon Sep 17 00:00:00 2001 From: Pistasj Date: Sun, 28 May 2023 17:35:51 +0200 Subject: [PATCH] ahh shit --- Hubs/ChatHub.cs | 27 ++++- ...433_sqlite.local_migration_189.Designer.cs | 91 +++++++++++++++ ...230526180433_sqlite.local_migration_189.cs | 61 ++++++++++ Migrations/ChattingContextModelSnapshot.cs | 51 ++++++++ Model.cs | 24 +++- Pages/Chat.razor | 110 +++++++++++++++++- Pages/Index.razor | 3 +- wwwroot/css/site.css | 9 ++ 8 files changed, 369 insertions(+), 7 deletions(-) create mode 100644 Migrations/20230526180433_sqlite.local_migration_189.Designer.cs create mode 100644 Migrations/20230526180433_sqlite.local_migration_189.cs diff --git a/Hubs/ChatHub.cs b/Hubs/ChatHub.cs index b26866d..adbc10a 100644 --- a/Hubs/ChatHub.cs +++ b/Hubs/ChatHub.cs @@ -4,7 +4,7 @@ namespace LiveChat.Server.Hubs; public class ChatHub : Hub { - public async Task SendMessage(string user, string message) + public async Task SendMessage(string user, string message, string room) { using (var db = new ChattingContext()) { @@ -14,9 +14,32 @@ public class ChatHub : Hub Message = message, }; var dbSave = db.Messages.Add(messageData); - await Clients.All.SendAsync("ReceiveMessage", user, message); + await Clients.AllExcept("LiveChat Room Users").SendAsync("ReceiveMessage", user, message); db.SaveChanges(); } } + + public async Task SendMessageRoom(string user, string message, string room) + { + using (var db = new ChattingContext()) + { + var messageData = new RoomChatMessage() + { + User = user, + Message = message, + RoomId = room + }; + var dbSave = db.RoomChatMessages.Add(messageData); + await Clients.Group(room).SendAsync("ReceiveMessage", user, message); + db.SaveChanges(); + } + } + + public async Task JoinRoom(string room, string user) + { + await Groups.AddToGroupAsync(Context.ConnectionId, room); + await Groups.AddToGroupAsync(Context.ConnectionId, "Folk i rom"); + await Clients.Caller.SendAsync("ConnectionIdReceive", Context.ConnectionId); + } } diff --git a/Migrations/20230526180433_sqlite.local_migration_189.Designer.cs b/Migrations/20230526180433_sqlite.local_migration_189.Designer.cs new file mode 100644 index 0000000..c283e9c --- /dev/null +++ b/Migrations/20230526180433_sqlite.local_migration_189.Designer.cs @@ -0,0 +1,91 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace LiveChat.Migrations +{ + [DbContext(typeof(ChattingContext))] + [Migration("20230526180433_sqlite.local_migration_189")] + partial class sqlitelocal_migration_189 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.4"); + + modelBuilder.Entity("ChatMessage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("Message") + .HasColumnType("TEXT"); + + b.Property("User") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Messages"); + }); + + modelBuilder.Entity("Room", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("RoomJoinCode") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Rooms"); + }); + + modelBuilder.Entity("RoomChatMessage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("Message") + .HasColumnType("TEXT"); + + b.Property("RoomId") + .HasColumnType("TEXT"); + + b.Property("User") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("RoomId") + .IsUnique(); + + b.ToTable("RoomChatMessages"); + }); + + modelBuilder.Entity("RoomChatMessage", b => + { + b.HasOne("Room", "Room") + .WithOne("RoomChatMessage") + .HasForeignKey("RoomChatMessage", "RoomId"); + + b.Navigation("Room"); + }); + + modelBuilder.Entity("Room", b => + { + b.Navigation("RoomChatMessage"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20230526180433_sqlite.local_migration_189.cs b/Migrations/20230526180433_sqlite.local_migration_189.cs new file mode 100644 index 0000000..2b8c37a --- /dev/null +++ b/Migrations/20230526180433_sqlite.local_migration_189.cs @@ -0,0 +1,61 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LiveChat.Migrations +{ + /// + public partial class sqlitelocal_migration_189 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Rooms", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + RoomJoinCode = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Rooms", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "RoomChatMessages", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + User = table.Column(type: "TEXT", nullable: true), + Message = table.Column(type: "TEXT", nullable: true), + RoomId = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_RoomChatMessages", x => x.Id); + table.ForeignKey( + name: "FK_RoomChatMessages_Rooms_RoomId", + column: x => x.RoomId, + principalTable: "Rooms", + principalColumn: "Id"); + }); + + migrationBuilder.CreateIndex( + name: "IX_RoomChatMessages_RoomId", + table: "RoomChatMessages", + column: "RoomId", + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "RoomChatMessages"); + + migrationBuilder.DropTable( + name: "Rooms"); + } + } +} diff --git a/Migrations/ChattingContextModelSnapshot.cs b/Migrations/ChattingContextModelSnapshot.cs index 252c8f2..231193d 100644 --- a/Migrations/ChattingContextModelSnapshot.cs +++ b/Migrations/ChattingContextModelSnapshot.cs @@ -31,6 +31,57 @@ namespace LiveChat.Migrations b.ToTable("Messages"); }); + + modelBuilder.Entity("Room", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("RoomJoinCode") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Rooms"); + }); + + modelBuilder.Entity("RoomChatMessage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("Message") + .HasColumnType("TEXT"); + + b.Property("RoomId") + .HasColumnType("TEXT"); + + b.Property("User") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("RoomId") + .IsUnique(); + + b.ToTable("RoomChatMessages"); + }); + + modelBuilder.Entity("RoomChatMessage", b => + { + b.HasOne("Room", "Room") + .WithOne("RoomChatMessage") + .HasForeignKey("RoomChatMessage", "RoomId"); + + b.Navigation("Room"); + }); + + modelBuilder.Entity("Room", b => + { + b.Navigation("RoomChatMessage"); + }); #pragma warning restore 612, 618 } } diff --git a/Model.cs b/Model.cs index ae24265..bf00761 100644 --- a/Model.cs +++ b/Model.cs @@ -9,6 +9,10 @@ public class ChattingContext : DbContext { public DbSet Messages { get; set; } + public DbSet Rooms { get; set; } + + public DbSet RoomChatMessages { get; set; } + public string DbPath { get; } public ChattingContext() @@ -30,9 +34,9 @@ public class Room public string? RoomJoinCode { get; set; } - // koble tilsammen ChatMessage til Room + // koble tilsammen RoomChatMessage til Room // https://www.entityframeworktutorial.net/efcore/one-to-many-conventions-entity-framework-core.aspx - public ChatMessage? ChatMessage { get; set; } + public RoomChatMessage? RoomChatMessage { get; set; } } @@ -44,4 +48,20 @@ public class ChatMessage public string? User { get; set; } public string? Message { get; set; } +} + +public class RoomChatMessage +{ + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public string? Id { get; set; } + + public string? User { get; set; } + public string? Message { get; set; } + public string? RoomId { get; set; } + + // koble tilsammen RoomChatMessage til Room + // https://www.entityframeworktutorial.net/efcore/one-to-many-conventions-entity-framework-core.aspx + public Room? Room { get; set; } + } \ No newline at end of file diff --git a/Pages/Chat.razor b/Pages/Chat.razor index 8fcd58b..8ca4232 100644 --- a/Pages/Chat.razor +++ b/Pages/Chat.razor @@ -1,13 +1,119 @@ @page "/chat/{room?}" +@using Microsoft.AspNetCore.SignalR.Client; +@using LiveChat.Models; +@inject NavigationManager Navigation + +Chat room -Chat room @if(Room is null or "") { -

Ingen rom ble avgitt. Lag et rom og putt det i URLen.

+

Fant ikke rom i URL. Lag et rom og putt det i URLen.

} +

Rom @Room

+ +

Din tilkoblings ID: @connectionId. Brukernavn: @username

+ +@foreach (var msg in msgs) +{ +
@msg
+} + +
+ @if (messageModel == null) + { +

laster inn meldingsboks

+ } + else + { + + +
+ +
+ +
+ + +
+
+ } +
+ @code { [Parameter] public string? Room { get; set; } + + private HubConnection? hubConnection; + + private List msgs = new List(); + + private bool ErViInne = false; + + private string connectionId = ""; + + private string username = ""; + + private MessageModel messageModel = new MessageModel() { User = "Something", Message = "" }; + + + protected override async Task OnInitializedAsync() + { + hubConnection = new HubConnectionBuilder() + .WithUrl(Navigation.ToAbsoluteUri("/chathub")) + .Build(); + + await hubConnection.StartAsync(); + + // lag tilfeldig navn + username = "Bruker" + new Random().Next(1, 1000); + + if (Room != null || Room != "") + { + // sjekk om rom finnes + // hvis ikke, lag et rom + using (var db = new ChattingContext()) + { + var room = db.Rooms.FirstOrDefault(r => r.RoomJoinCode == Room); + if (room is null) + { + Console.WriteLine("Lager rom?!?"); + room = new Room() { RoomJoinCode = Room }; + db.Rooms.Add(room); + db.SaveChanges(); + } + } + } + + + // bli med i rom + await hubConnection.SendAsync("JoinRoom", Room, username); + ErViInne = true; + messageModel.User = username; + + hubConnection.On("ReceiveMessage", (user, message) => + { + var encodedMsg = $"{user}: {message}"; + msgs.Add(encodedMsg); + InvokeAsync(StateHasChanged); + }); + } + + private async Task HandleSubmit() + { + messageModel.User = username; + await hubConnection.SendAsync("SendMessageRoom", messageModel.User, messageModel.Message, Room); + } + + public bool IsConnected => + hubConnection?.State == HubConnectionState.Connected; + + public async ValueTask DisposeAsync() + { + if (hubConnection is not null) + { + await hubConnection.DisposeAsync(); + } + } } diff --git a/Pages/Index.razor b/Pages/Index.razor index 3414a33..baf05ff 100644 --- a/Pages/Index.razor +++ b/Pages/Index.razor @@ -12,11 +12,11 @@