Skip to content

Commit

Permalink
Made skeleton of chat microservice architecture, made creating chats …
Browse files Browse the repository at this point in the history
…when creating group, new Migration
  • Loading branch information
IlyaM1 committed Jan 13, 2024
1 parent 2a17c83 commit 58db1dd
Show file tree
Hide file tree
Showing 16 changed files with 918 additions and 41 deletions.
5 changes: 5 additions & 0 deletions AppDatabase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Backend.Auth.Dal.Models;
using Backend.Chat.Dal.Models;
using Backend.CoursePages.Dal.Models;
using Backend.Courses.Dal.Models;
using Backend.Progress.Dal.Models;
Expand Down Expand Up @@ -40,6 +41,10 @@ public class AppDatabase : DbContext
public DbSet<TaskAnswerModel> TaskAnswers{ get; set; }
public DbSet<TaskScoreModel> TaskScores { get; set; }

// Chat
public DbSet<ChatModel> Chats { get; set; }
public DbSet<MessageModel> Messages { get; set; }

public AppDatabase(DbContextOptions<AppDatabase> options, IConfiguration config) : base(options)
{
_config = config;
Expand Down
43 changes: 43 additions & 0 deletions Chat/Api/ChatController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Backend.Auth.Dto;
using Backend.Chat.Dto;
using Backend.Chat.Logic;

using Microsoft.AspNetCore.Mvc;

namespace Backend.Chat.Api;


[ApiController]
[Route(template: "/api/v1/chat")]
public class ChatController(ChatService chatService)
{
private readonly ChatService _chatService = chatService;

[HttpGet]
public async Task<ChatGetAllChatsDto> GetAllChats([FromHeader] UserAuthInfo authInfo)
{
return await _chatService.GetAllChatsAsync(authInfo);
}

[HttpGet]
[Route("{chatId}/message")]
public async Task<ChatGetAllMessagesDto> GetAllMessages([FromHeader] UserAuthInfo authInfo, [FromRoute] int chatId)
{
return await _chatService.GetAllMessagesAsync(authInfo, chatId);
}

[HttpGet]
[Route("message/{messageId}")]
public async Task<ChatGetOneMessageDto> GetOneMessage([FromRoute] int messageId)
{
return await _chatService.GetMessageAsync(messageId);
}

[HttpPost]
[Route("{chatId}/message")]
public async Task<IResult> SendMessage([FromHeader] UserAuthInfo authInfo, [FromRoute] int chatId, [FromBody] ChatSendMessageDto messageDto)
{
await _chatService.SendMessageAsync(authInfo, chatId, messageDto);
return Results.Ok();
}
}
14 changes: 14 additions & 0 deletions Chat/Dal/ChatRepo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Backend.Base.Dal;
using Backend.Base.Dal.Interfaces;
using Backend.Chat.Dal.Interfaces;
using Backend.Chat.Dal.Models;

namespace Backend.Chat.Dal;

public class ChatRepo : BaseRepo<ChatModel>, IChatRepo
{
public ChatRepo(AppDatabase database) : base(database)
{

}
}
9 changes: 9 additions & 0 deletions Chat/Dal/Interfaces/IChatRepo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Backend.Base.Dal.Interfaces;
using Backend.Chat.Dal.Models;

namespace Backend.Chat.Dal.Interfaces;

public interface IChatRepo : IBaseRepo<ChatModel>
{

}
9 changes: 9 additions & 0 deletions Chat/Dal/Interfaces/IMessageRepo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Backend.Base.Dal.Interfaces;
using Backend.Chat.Dal.Models;

namespace Backend.Chat.Dal.Interfaces;

public interface IMessageRepo : IBaseRepo<MessageModel>
{

}
13 changes: 13 additions & 0 deletions Chat/Dal/MessageRepo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Backend.Base.Dal;
using Backend.Chat.Dal.Interfaces;
using Backend.Chat.Dal.Models;

namespace Backend.Chat.Dal;

public class MessageRepo : BaseRepo<MessageModel>, IMessageRepo
{
public MessageRepo(AppDatabase database) : base(database)
{

}
}
9 changes: 9 additions & 0 deletions Chat/Dal/Models/ChatModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Backend.Base.Dal.Models;

namespace Backend.Chat.Dal.Models;

public class ChatModel : BaseModel
{
public int User1Id { get; set; }
public int User2Id { get; set; }
}
12 changes: 12 additions & 0 deletions Chat/Dal/Models/MessageModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Backend.Base.Dal.Models;

namespace Backend.Chat.Dal.Models;

public class MessageModel : BaseModel
{
public int ChatId { get; set; }
public int UserId { get; set; }
public string MessageText { get; set; }
public bool IsRead { get; set; }
public long CreatedAt { get; set; }
}
44 changes: 44 additions & 0 deletions Chat/Dto/ChatDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Backend.Base.Dto;

namespace Backend.Chat.Dto;

public class ChatGetAllChatsDto : BaseDto
{
public class ChatDto
{
public int ChatId { get; set; }
public string UserPhoto { get; set; }
public string UserName { get; set; }
public string UserSurname { get; set; }
public string UserPatronymic { get; set; }
public string LastMessageId { get; set; }
}

public List<ChatDto> Chats { get; set; }
}

public class MessageDto : BaseDto
{
public int SenderId { get; set; }
public int ReceiverId { get; set; }
public int MessageId { get; set; }
public string MessageText { get; set; }
public bool IsRead { get; set; }
public long CreatedAt { get; set; }
}

public class ChatGetAllMessagesDto : BaseDto
{
public List<MessageDto> Messages { get; set; }
}

public class ChatGetOneMessageDto : BaseDto
{
public MessageDto Message { get; set; }
}

public class ChatSendMessageDto : BaseDto
{
public int UserId { get; set; }
public string Text { get; set; }
}
32 changes: 32 additions & 0 deletions Chat/Logic/ChatService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Backend.Auth.Dto;
using Backend.Chat.Dto;

namespace Backend.Chat.Logic;

public class ChatService
{
public ChatService()
{

}

public async Task<ChatGetAllChatsDto> GetAllChatsAsync(UserAuthInfo authInfo)
{
throw new NotImplementedException();
}

public async Task<ChatGetAllMessagesDto> GetAllMessagesAsync(UserAuthInfo authInfo, int chatId)
{
throw new NotImplementedException();
}

public async Task<ChatGetOneMessageDto> GetMessageAsync(int messageId)
{
throw new NotImplementedException();
}

public async Task SendMessageAsync(UserAuthInfo authInfo, int chatId, ChatSendMessageDto chatSendMessageDto)
{
throw new NotImplementedException();
}
}
2 changes: 1 addition & 1 deletion Courses/Api/GroupController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task<IResult> ConnectToGroup([FromHeader] UserAuthInfo authInfo, [F
if (authInfo is null)
throw new BadHttpRequestException(statusCode: 401, message: "Авторизуйтесь прежде, чем добавляться в группу");

_groupService.ConnectToGroupAsync(authInfo, connectToGroupDto);
await _groupService.ConnectToGroupAsync(authInfo, connectToGroupDto);
return Results.Ok();
}

Expand Down
77 changes: 38 additions & 39 deletions Courses/Logic/GroupService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Backend.Auth.Dal.Interfaces;
using Backend.Auth.Dto;
using Backend.Chat.Dal.Interfaces;
using Backend.Chat.Dal.Models;
using Backend.Courses.Dal.Interfaces;
using Backend.Courses.Dal.Models;
using Backend.Courses.Dto;
Expand All @@ -8,43 +10,30 @@

namespace Backend.Courses.Logic;

public class GroupService
public class GroupService(
IGroupRepo groupRepo,
IStudentGroupsRepo studentGroupsRepo,
IStudentRepo studentRepo,
ITeacherRepo teacherRepo,
IAccountRepo accountRepo,
IGroupCoursesRepo groupCoursesRepo,
IChatRepo chatRepo,
ICourseRepo courseRepo,
CourseService courseService,
IHttpContextAccessor httpContextAccessor)
{
private IGroupRepo _groupRepo;
private IStudentGroupsRepo _studentGroupsRepo;
private IStudentRepo _studentRepo;
private ITeacherRepo _teacherRepo;
private IAccountRepo _accountRepo;
private ICourseRepo _courseRepo;
private IGroupCoursesRepo _groupCoursesRepo;

private CourseService _courseService;

private IHttpContextAccessor _httpContextAccessor;

public GroupService(
IGroupRepo groupRepo,
IStudentGroupsRepo studentGroupsRepo,
IStudentRepo studentRepo,
ITeacherRepo teacherRepo,
IAccountRepo accountRepo,
IGroupCoursesRepo groupCoursesRepo,
ICourseRepo courseRepo,
CourseService courseService,
IHttpContextAccessor httpContextAccessor)
{
_groupRepo = groupRepo;
_studentGroupsRepo = studentGroupsRepo;
_studentRepo = studentRepo;
_teacherRepo = teacherRepo;
_accountRepo = accountRepo;
_groupCoursesRepo = groupCoursesRepo;
_courseRepo = courseRepo;

_courseService = courseService;

_httpContextAccessor = httpContextAccessor;
}
private IGroupRepo _groupRepo = groupRepo;
private IStudentGroupsRepo _studentGroupsRepo = studentGroupsRepo;
private IStudentRepo _studentRepo = studentRepo;
private ITeacherRepo _teacherRepo = teacherRepo;
private IAccountRepo _accountRepo = accountRepo;
private ICourseRepo _courseRepo = courseRepo;
private IGroupCoursesRepo _groupCoursesRepo = groupCoursesRepo;
private IChatRepo _chatRepo = chatRepo;

private CourseService _courseService = courseService;

private IHttpContextAccessor _httpContextAccessor = httpContextAccessor;

public async Task<GroupGetDto> GetGroupByIdAsync(int id)
{
Expand Down Expand Up @@ -146,15 +135,25 @@ public async Task UpdateGroupAsync(int id, GroupUpdateDto updateGroupDto)
await _groupRepo.UpdateEntityAsync(id, updateGroupDto);
}

public void ConnectToGroupAsync(UserAuthInfo authInfo, GroupConnectDto connectToGroupDto)
public async Task ConnectToGroupAsync(UserAuthInfo authInfo, GroupConnectDto connectToGroupDto)
{
var accountType = authInfo.UserType;
if (accountType is not Auth.Enums.UserType.Student)
throw new BadHttpRequestException(statusCode: 400, message: "Добавляться в группы могут только ученики");

var userId = authInfo.Id;
var groupId = connectToGroupDto.GroupId;
AddStudentToGroupAsync(userId, groupId).RunSynchronously();
await AddStudentToGroupAsync(userId, groupId);

var group = await _groupRepo.GetEntityByIdAsync(groupId);
var teacherUserId = (await _teacherRepo.GetEntityByIdAsync(group.TeacherId)).UserId;
await CreateChatAfterConnectionToGroup(teacherUserId, userId);
}

private async Task CreateChatAfterConnectionToGroup(int teacherUserId, int studentUserId)
{
var newChat = new ChatModel() { User1Id = teacherUserId, User2Id = studentUserId };
await _chatRepo.CreateEntityAsync(newChat);
}

public async Task DeleteCourseFromGroup(int groupId, GroupAddCourseToGroupDto groupAddCourseToGroupDto)
Expand Down Expand Up @@ -204,7 +203,7 @@ public async Task AddStudentToGroupAsync(int userId, int groupId)
StudentId = await GetStudentIdByUserIdAsync(userId),
};

_studentGroupsRepo.CreateEntityAsync(newConnectionModel).RunSynchronously();
await _studentGroupsRepo.CreateEntityAsync(newConnectionModel);
}

public async Task<int> GetStudentIdByUserIdAsync(int userId)
Expand Down
17 changes: 16 additions & 1 deletion DependencyInjectionConfiguring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using Backend.Base.Services.Filters;
using Backend.Base.Services.Interfaces;
using Backend.Base.Services.ModelBinders;
using Backend.Chat.Dal;
using Backend.Chat.Dal.Interfaces;
using Backend.Chat.Logic;
using Backend.CoursePages.Dal;
using Backend.CoursePages.Dal.Interfaces;
using Backend.CoursePages.Logic;
Expand All @@ -17,7 +20,6 @@
using Backend.Progress.Logic;

using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;

namespace Backend;

Expand Down Expand Up @@ -61,6 +63,7 @@ private void RegisterDatabaseRepositories()
RegisterCoursesDbRepos();
RegisterCoursePagesDbRepos();
RegisterProgressDbRepos();
RegisterChatDbRepos();
}

private void RegisterAuthDbRepos()
Expand Down Expand Up @@ -101,13 +104,20 @@ private void RegisterProgressDbRepos()
_services.AddScoped<ITaskScoreRepo, TaskScoreRepo>();
}

private void RegisterChatDbRepos()
{
_services.AddScoped<IMessageRepo, MessageRepo>();
_services.AddScoped<IChatRepo, ChatRepo>();
}

private void RegisterBusinessServices()
{
RegisterHelperServices();
RegisterAuthLogic();
RegisterCoursesLogic();
RegisterCoursePagesLogic();
RegisterProgressLogic();
RegisterChatLogic();
}

private void RegisterAuthLogic()
Expand All @@ -134,6 +144,11 @@ private void RegisterProgressLogic()
_services.AddScoped<ProgressService>();
}

private void RegisterChatLogic()
{
_services.AddScoped<ChatService>();
}

private void RegisterHelperServices()
{
_services.AddScoped<IPasswordHelperService, PasswordHelperService>();
Expand Down
Loading

0 comments on commit 58db1dd

Please sign in to comment.