-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5be4393
commit 7bd4bab
Showing
60 changed files
with
3,258 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
DA_Business/Repository/ChatRepos/IRepository/IChatManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.