Skip to content

Commit

Permalink
Adding chat
Browse files Browse the repository at this point in the history
  • Loading branch information
KrystianKempski committed May 9, 2024
1 parent 5be4393 commit 7bd4bab
Show file tree
Hide file tree
Showing 60 changed files with 3,258 additions and 133 deletions.
11 changes: 11 additions & 0 deletions DA_Business/Repository/CharacterReps/CharacterRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public async Task<CharacterDTO> Create(CharacterDTO objDTO)
obj.TraitsAdv.Add(t);
}
});
//handle equipment
var equipment = await contex.Equipment.ToListAsync();
equipment.ForEach(e =>
{
if (obj.Equipment.Any(nt => nt.Id == e.Id))
{
var untracked = obj.Equipment.FirstOrDefault(nt => nt.Id == e.Id);
obj.Equipment.Remove(untracked);
obj.Equipment.Add(e);
}
});

var addedObj = contex.Characters.Add(obj);
await contex.SaveChangesAsync();
Expand Down
1 change: 1 addition & 0 deletions DA_Business/Repository/CharacterReps/RaceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public async Task<int> Delete(int id)
{
using var contex = await _db.CreateDbContextAsync();
var obj = await contex.Races.Include(u=>u.Traits).FirstOrDefaultAsync(u => u.Id == id);

if (obj is not null)
{
var traits = obj.Traits;
Expand Down
97 changes: 97 additions & 0 deletions DA_Business/Repository/ChatRepos/ChatManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using AutoMapper;
using DA_Business.Repository.CharacterReps.IRepository;
using DA_DataAccess;
using DA_DataAccess.CharacterClasses;
using DA_DataAccess.Chat;
using DA_DataAccess.Data;
using DA_Models.CharacterModels;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.JSInterop;
using System.Net.Http.Json;
using System.Security.Claims;
using Attribute = DA_DataAccess.CharacterClasses.Attribute;

namespace DA_Business.Repository.CharacterReps
{
public class ChatManager : IChatManager
{
private readonly AuthenticationStateProvider _authState;
private readonly HttpClient _httpClient;
private readonly IDbContextFactory<ApplicationDbContext> _db;
private readonly IJSRuntime _jsRuntime;
private readonly UserManager<ApplicationUser> _userManager;

public ChatManager(HttpClient httpClient, IDbContextFactory<ApplicationDbContext> db, IJSRuntime jsRuntime, AuthenticationStateProvider authState, UserManager<ApplicationUser> userManager)
{
_httpClient = httpClient;
_db = db;
_jsRuntime = jsRuntime;
_authState = authState;
_userManager = userManager;
}
public async Task<List<ChatMessage>> GetConversationAsync(string contactId)
{
using var contex = await _db.CreateDbContextAsync();
var user = (await _authState.GetAuthenticationStateAsync()).User;
var userId = user.Claims.Where(a => a.Type == ClaimTypes.NameIdentifier).Select(a => a.Value).FirstOrDefault();
var messages = await contex.ChatMessages
.Where(h => (h.FromUserId == contactId && h.ToUserId == userId) || (h.FromUserId == userId && h.ToUserId == contactId))
.OrderBy(a => a.CreatedDate)
.Include(a => a.FromUser)
.Include(a => a.ToUser)
.Select(x => new ChatMessage
{
FromUserId = x.FromUserId,
Message = x.Message,
CreatedDate = x.CreatedDate,
Id = x.Id,
ToUserId = x.ToUserId,
ToUser = x.ToUser,
FromUser = x.FromUser
}).ToListAsync();

return messages;
}
public async Task<ApplicationUser> GetUserDetailsAsync(string userId)
{
using var contex = await _db.CreateDbContextAsync();
// var user = await contex.ApplicationUsers.Where(user => user.Id == userId).FirstOrDefaultAsync();
var user = _userManager.Users.Where(user => user.Id == userId).FirstOrDefaultAsync();

return await user;
}
public async Task<List<ApplicationUser>> GetUsersAsync()
{
using var contex = await _db.CreateDbContextAsync();
var user = (await _authState.GetAuthenticationStateAsync()).User;

var userId = user.Claims.Where(a => a.Type == ClaimTypes.NameIdentifier).Select(a => a.Value).FirstOrDefault();
var allUsers = _userManager.Users.Where(user => user.Id != userId).ToListAsync();

return await allUsers;
}
public async Task SaveMessageAsync(ChatMessage message)
{
try
{

using var contex = await _db.CreateDbContextAsync();
var user = (await _authState.GetAuthenticationStateAsync()).User;
var userId = user.Claims.Where(a => a.Type == ClaimTypes.NameIdentifier).Select(a => a.Value).FirstOrDefault();
message.FromUserId = userId;
message.CreatedDate = DateTime.Now;
message.ToUser = await contex.ApplicationUsers.Where(user => user.Id == message.ToUserId).FirstOrDefaultAsync();
var result = contex.ChatMessages.AddAsync(message);
await contex.SaveChangesAsync();

}
catch(Exception ex)
{
;
}

}
}
}
19 changes: 19 additions & 0 deletions DA_Business/Repository/ChatRepos/IRepository/IChatManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using DA_DataAccess.Chat;
using DA_DataAccess;
using DA_Models.CharacterModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DA_Business.Repository.CharacterReps.IRepository
{
public interface IChatManager
{
Task<List<ApplicationUser>> GetUsersAsync();
Task SaveMessageAsync(ChatMessage message);
Task<List<ChatMessage>> GetConversationAsync(string contactId);
Task<ApplicationUser> GetUserDetailsAsync(string userId);
}
}
10 changes: 9 additions & 1 deletion DA_DataAccess/ApplicationUser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Identity;
using DA_DataAccess.Chat;
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -10,6 +11,13 @@ namespace DA_DataAccess
public class ApplicationUser : IdentityUser
{
public string Name { get; set; }
public virtual ICollection<ChatMessage> ChatMessagesFromUsers { get; set; }
public virtual ICollection<ChatMessage> ChatMessagesToUsers { get; set; }
public ApplicationUser()
{
ChatMessagesFromUsers = new HashSet<ChatMessage>();
ChatMessagesToUsers = new HashSet<ChatMessage>();
}

}
}
4 changes: 2 additions & 2 deletions DA_DataAccess/CharacterClasses/Equipment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class Equipment
{
[Key] public int Id { get; set; }

public ICollection<TraitEquipment>? Traits { get; set; }
public ICollection<TraitEquipment>? Traits { get; set; } = null;

public ICollection<Character>? Characters { get; set; }
public ICollection<Character>? Characters { get; set; } = null;

public string Name { get; set; } //for example "axe"
public int Index { get; set; }
Expand Down
21 changes: 21 additions & 0 deletions DA_DataAccess/Chat/ChatMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using MudBlazor.Charts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DA_DataAccess.Chat
{
public class ChatMessage
{
public long Id { get; set; }
public string FromUserId { get; set; }
public string ToUserId { get; set; }
public string Message { get; set; }
public DateTime CreatedDate { get; set; }
public virtual ApplicationUser FromUser { get; set; }
public virtual ApplicationUser ToUser { get; set; }
public bool IsNotice => Message.StartsWith("[Notice]");
}
}
19 changes: 17 additions & 2 deletions DA_DataAccess/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using DA_DataAccess.CharacterClasses;
using Abp.Domain.Entities;
using DA_DataAccess.CharacterClasses;
using DA_DataAccess.Chat;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
Expand All @@ -10,7 +12,7 @@ public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{

AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
}
public DbSet<Character> Characters { get; set; }
public DbSet<Attribute> Attributes { get; set; }
Expand All @@ -29,6 +31,8 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : ba
public DbSet<Profession> Professions { get; set; }
public DbSet<ProfessionSkill> ProfessionSkills { get; set; }

public DbSet<ChatMessage> ChatMessages { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
Expand All @@ -42,6 +46,17 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasOne(a => a.PassiveProfession)
.WithMany(y => y.PassiveSkills)
.HasForeignKey(a => a.PassiveProfessionId).OnDelete(DeleteBehavior.NoAction);

modelBuilder.Entity<ChatMessage>()
.HasOne(d => d.FromUser)
.WithMany(p => p.ChatMessagesFromUsers)
.HasForeignKey(d => d.FromUserId)
.OnDelete(DeleteBehavior.ClientSetNull);
modelBuilder.Entity<ChatMessage>()
.HasOne(d => d.ToUser)
.WithMany(p => p.ChatMessagesToUsers)
.HasForeignKey(d => d.ToUserId)
.OnDelete(DeleteBehavior.ClientSetNull);
}
}
}
Loading

0 comments on commit 7bd4bab

Please sign in to comment.