Преглед изворни кода

Implemented room service as repository

feature/mongo-services-with-interfaces
Ermin Bronja пре 3 година
родитељ
комит
2f604c3c98

+ 4
- 5
Backend/Diligent.WebAPI.Business/Interfaces/IRoomRepository.cs Прегледај датотеку

@@ -1,14 +1,13 @@
using Diligent.WebAPI.Data.Entities;
using Diligent.WebAPI.Data.HelperModels;

namespace Diligent.WebAPI.Business.Interfaces
{
public interface IRoomRepository : IBaseRepository<Room>
{
Task<bool> AddCustomerToRoom(string customerId, string roomId);
Task<bool> AddMessage(string roomId, Message message);
Task<List<Message>> GetMessagesForSpecificRoom(string roomId);
Task<bool> LeaveChatRoom(string roomId, string userId);
Task AddCustomerToRoom(Room room, string customerId);
Task AddMessage(Room room, Message message);
Task LeaveChatRoom(Room room, CustomerDTO userConnection);
Task<List<Room>> GetRoomsWhichSupportCreated(string supportId);
Task<List<Room>?> GetRoomsWithFilteredMessages(string customerId);
}
}

+ 2
- 0
Backend/Diligent.WebAPI.Business/Services/RequestRepository.cs Прегледај датотеку

@@ -4,12 +4,14 @@ using Diligent.WebAPI.Data.Entities;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Diligent.WebAPI.Business.Services
{
[ExcludeFromCodeCoverage]
public class RequestRepository : BaseRepository<Request>, IRequestRepository
{
protected new IMongoCollection<Request> _dbCollection;

+ 14
- 20
Backend/Diligent.WebAPI.Business/Services/RoomRepository.cs Прегледај датотеку

@@ -1,47 +1,41 @@
using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Data;
using Diligent.WebAPI.Data.Entities;
using Diligent.WebAPI.Data.HelperModels;
using MongoDB.Driver;

namespace Diligent.WebAPI.Business.Services
{
public class RoomRepository : BaseRepository<Room>, IRoomRepository
{
protected new IMongoCollection<Request> _dbCollection;
protected new IMongoCollection<Room> _dbCollection;

public RoomRepository(IMongoDBContext context) : base(context)
{
_dbCollection = _mongoContext.GetCollection<Request>(typeof(Request).Name);
_dbCollection = _mongoContext.GetCollection<Room>(typeof(Room).Name);
}

public Task<bool> AddCustomerToRoom(string customerId, string roomId)
public async Task AddCustomerToRoom(Room room, string customerId)
{
throw new NotImplementedException();
}
room.Customers.Add(new CustomerDTO { CustomerId = customerId, DateOfEnteringRoom = DateTime.Now });

public Task<bool> AddMessage(string roomId, Message message)
{
throw new NotImplementedException();
await _dbCollection.ReplaceOneAsync(x => x.Id == room.Id, room);
}

public Task<List<Message>> GetMessagesForSpecificRoom(string roomId)
public async Task AddMessage(Room room, Message message)
{
throw new NotImplementedException();
room.Messages.Add(message);
await _dbCollection.ReplaceOneAsync(x => x.Id == room.Id, room);
}

public Task<List<Room>> GetRoomsWhichSupportCreated(string supportId)
{
throw new NotImplementedException();
}
public async Task<List<Room>> GetRoomsWhichSupportCreated(string supportId) =>
await _dbCollection.Find(k => k.CreatedBy == supportId).ToListAsync();

public Task<List<Room>?> GetRoomsWithFilteredMessages(string customerId)
public async Task LeaveChatRoom(Room room, CustomerDTO userConnection)
{
throw new NotImplementedException();
}
room.Customers.Remove(userConnection);

public Task<bool> LeaveChatRoom(string roomId, string userId)
{
throw new NotImplementedException();
await _dbCollection.ReplaceOneAsync(x => x.Id == room.Id, room);
}
}
}

+ 0
- 115
Backend/Diligent.WebAPI.Business/Services/RoomService.cs Прегледај датотеку

@@ -1,115 +0,0 @@
using Diligent.WebAPI.Business.MongoServices;
using Diligent.WebAPI.Data;
using Diligent.WebAPI.Data.Entities;
using Diligent.WebAPI.Data.HelperModels;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace Diligent.WebAPI.Business.Services
{
public class RoomService : BaseMongo<Room>
{
public RoomService(IOptions<WebApiDatabaseSettings> webApiDatabaseSettings) :
base(webApiDatabaseSettings, "Room")
{ }
public async Task<List<Room>> GetRoomsAsync() =>
await _mongoCollection.Find(_ => true).ToListAsync();

public async Task CreateRoomAsync(Room room) =>
await _mongoCollection.InsertOneAsync(room);

public async Task<bool> AddCustomerToRoom(string customerId,string roomId)
{
var room = await _mongoCollection.Find(k => k.Id == roomId).FirstOrDefaultAsync();

if (room is null)
return false;

room.Customers.Add(new CustomerDTO { CustomerId = customerId, DateOfEnteringRoom = DateTime.Now});

await _mongoCollection.ReplaceOneAsync(x => x.Id == roomId, room);

return true;
}
public async Task<Room> GetRoomAsync(string roomId) =>
await _mongoCollection.Find(r => r.Id == roomId).FirstOrDefaultAsync();

public async Task<bool> AddMessage(string roomId,Message message)
{
var room = await GetRoomAsync(roomId);

if (room is null)
return false;

room.Messages.Add(message);
await _mongoCollection.ReplaceOneAsync(x => x.Id == roomId, room);

return true;
}

public async Task<List<Message>> GetMessagesForSpecificRoom(string roomId)
{
var room = await _mongoCollection.Find(r => r.Id == roomId).FirstOrDefaultAsync();

if (room is null) return new List<Message>();

return room.Messages;
}

public async Task<List<Room>> GetRoomsForSupport(string supportId) =>
await _mongoCollection.Find(k => k.CreatedBy == supportId).ToListAsync();

public async Task<bool> LeaveChatRoom(string roomId, string userId)
{
var room = await GetRoomAsync(roomId);

if (room == null)
{
return false;
}

var userConnection = room.Customers.Where(x => x.CustomerId == userId).FirstOrDefault();

if (userConnection == null)
{
return false;
}

room.Customers.Remove(userConnection);

await _mongoCollection.ReplaceOneAsync(x => x.Id == roomId, room);

return true;
}

public async Task<List<Room>> GetRoomsWhichSupportCreated(string supportId) =>
await _mongoCollection.Find(k => k.CreatedBy == supportId).ToListAsync();

public async Task<List<Room>?> GetRoomsWithFilteredMessages(string customerId)
{
var rooms = await GetRoomsAsync();
foreach (var room in rooms)
{

List<Message> msg = new();
var customer = room.Customers.Where(c => c.CustomerId == customerId).FirstOrDefault();

if (customer is not null)
{
foreach (var message in room.Messages)
{
if (message.CreatedAtUtc >= customer.DateOfEnteringRoom)
msg.Add(message);
}
room.Messages = msg;
}
else
{
room.Messages = new List<Message>();
}
}
return rooms;
}
}
}

+ 2
- 0
Backend/Diligent.WebAPI.Host/Controllers/RequestController.cs Прегледај датотеку

@@ -40,9 +40,11 @@ namespace Diligent.WebAPI.Host.Controllers
public async Task<ActionResult<List<CustomerRequestRoomReadDTO>>> GetCustomersForSpecificRequestRoom(string id) =>
await _mediator.Send(new GetSendersForSpecificRequestRoomQuery(id));

//public async Task<ActionResult> AcceptCustomerRequest(AcceptCustomerRequestCommand request)
[HttpPost("accept-request")]
public async Task<ActionResult> AcceptCustomerRequest(AcceptCustomerDTO acceptCustomerDTO)
{
//var id = await _mediator.Send(request);
var id = await _mediator.Send(new AcceptCustomerRequestCommand(acceptCustomerDTO.customerId, acceptCustomerDTO.roomId));
return Ok(id);
}

+ 0
- 1
Backend/Diligent.WebAPI.Host/Extensions/WebAppBuilder.cs Прегледај датотеку

@@ -32,7 +32,6 @@ public static class WebAppBuilder
builder.Services.AddScoped<IAuthenticationService, AuthenticationService>();
builder.Services.AddScoped<ICustomerService, CustomerService>();

builder.Services.AddSingleton<RoomService>();
builder.Services.AddSingleton<AuthorizationService>();

builder.Services.AddMediatR(Assembly.GetExecutingAssembly());

+ 12
- 7
Backend/Diligent.WebAPI.Host/Mediator/Messages/Handlers/AddMessageHandler.cs Прегледај датотеку

@@ -1,4 +1,5 @@
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Data.Entities;
using Diligent.WebAPI.Host.Exceptions;
using Diligent.WebAPI.Host.Mediator.Messages.Commands;
@@ -8,18 +9,22 @@ namespace Diligent.WebAPI.Host.Mediator.Messages.Handlers
{
public class AddMessageHandler : IRequestHandler<AddMessageCommand, Unit>
{
private readonly RoomService _roomService;
private readonly IRoomRepository _roomRepository;

public AddMessageHandler(RoomService roomService)
public AddMessageHandler(IRoomRepository roomRepository)
{
_roomService = roomService;
_roomRepository = roomRepository;
}
public async Task<Unit> Handle(AddMessageCommand request, CancellationToken cancellationToken)
{
var result = await _roomService.AddMessage(request.roomId,request.message);
var room = await _roomRepository.GetByIdAsync(request.roomId);

if (!result)
throw new NotFoundException("Room id doesn't exist");
if(room == null)
{
throw new NotFoundException("Room is not found");
}

await _roomRepository.AddMessage(room, request.message);

return new Unit();
}

+ 5
- 0
Backend/Diligent.WebAPI.Host/Mediator/Request/Commands/CreateRequestCommand.cs Прегледај датотеку

@@ -10,5 +10,10 @@ namespace Diligent.WebAPI.Host.Mediator.Request.Commands
{
Request = request;
}

//public string SenderId { get; set; }
//public string SenderUsername { get; set; }
//public string RoomId { get; set; }
//public string RoomName { get; set; }
}
}

+ 10
- 6
Backend/Diligent.WebAPI.Host/Mediator/Request/Handlers/AcceptCustomerRequestHandler.cs Прегледај датотеку

@@ -9,19 +9,23 @@ namespace Diligent.WebAPI.Host.Mediator.Request.Handlers
public class AcceptCustomerRequestHandler : IRequestHandler<AcceptCustomerRequestCommand, string?>
{
private readonly IRequestRepository _requestRepository;
private readonly RoomService _roomService;
private readonly IRoomRepository _roomRepository;

public AcceptCustomerRequestHandler(RoomService roomService, IRequestRepository requestRepository)
public AcceptCustomerRequestHandler(IRequestRepository requestRepository, IRoomRepository roomRepository)
{
_roomService = roomService;
_requestRepository = requestRepository;
_roomRepository = roomRepository;
}
public async Task<string?> Handle(AcceptCustomerRequestCommand request, CancellationToken cancellationToken)
{
var result1 = await _roomService.AddCustomerToRoom(request.CustomerId, request.RoomId);
var room = await _roomRepository.GetByIdAsync(request.RoomId);

if (!result1)
throw new NotFoundException($"Room with id={request.RoomId} doesn't exist");
if(room == null)
{
throw new NotFoundException("Room are not found");
}

await _roomRepository.AddCustomerToRoom(room, request.CustomerId);

var req = await _requestRepository.FindRequestAsync(request.CustomerId, request.RoomId);


+ 1
- 1
Backend/Diligent.WebAPI.Host/Mediator/Request/Handlers/CreateRequestHandler.cs Прегледај датотеку

@@ -23,7 +23,7 @@ namespace Diligent.WebAPI.Host.Mediator.Request.Handlers

if (req == null)
throw new BadHttpRequestException("Resource object cannot be null.");
var createdRequest = _mapper.Map<Data.Entities.Request>(req);
await _requestService.CreateAsync(createdRequest);


+ 4
- 5
Backend/Diligent.WebAPI.Host/Mediator/Request/Handlers/GetRoomsForWhichRequestExistHandler.cs Прегледај датотеку

@@ -1,6 +1,5 @@
using AutoMapper;
using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Data.Entities;
using Diligent.WebAPI.Host.DTOs.Request;
using Diligent.WebAPI.Host.Mediator.Request.Queries;
@@ -11,19 +10,19 @@ namespace Diligent.WebAPI.Host.Mediator.Request.Handlers
public class GetRoomsForWhichRequestExistHandler : IRequestHandler<GetRoomsForWhichRequestExistQuery, List<RequestRoomReadDTO>>
{
private readonly IRequestRepository _requestService;
private readonly RoomService _roomService;
private readonly IRoomRepository _roomRepository;
private readonly IMapper _mapper;

public GetRoomsForWhichRequestExistHandler(IRequestRepository requestService,RoomService roomService,IMapper mapper)
public GetRoomsForWhichRequestExistHandler(IRequestRepository requestService, IMapper mapper, IRoomRepository roomRepository)
{
_requestService = requestService;
_roomService = roomService;
_mapper = mapper;
_roomRepository = roomRepository;
}
public async Task<List<RequestRoomReadDTO>> Handle(GetRoomsForWhichRequestExistQuery request, CancellationToken cancellationToken)
{
var requests = await _requestService.GetAsync();
var rooms = await _roomService.GetRoomsAsync();
var rooms = await _roomRepository.GetAsync();
List<Room> temp = new();
foreach (var req in requests)
{

+ 26
- 8
Backend/Diligent.WebAPI.Host/Mediator/Rooms/Handlers/GetAllRoomsWithFilteredMessagesHandler.cs Прегледај датотеку

@@ -1,4 +1,5 @@
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Data.Entities;
using Diligent.WebAPI.Host.Mediator.Rooms.Queries;
using MediatR;
@@ -7,21 +8,38 @@ namespace Diligent.WebAPI.Host.Mediator.Chat.Handlers
{
public class GetAllRoomsWithFilteredMessagesHandler : IRequestHandler<GetAllRoomsWithFilteredMessagesQuery, List<Room>>
{
private readonly RoomService _roomService;
private readonly IRoomRepository _roomRepository;

public GetAllRoomsWithFilteredMessagesHandler(RoomService roomService)
public GetAllRoomsWithFilteredMessagesHandler(IRoomRepository roomRepository)
{
_roomService = roomService;
_roomRepository = roomRepository;
}
public async Task<List<Room>> Handle(GetAllRoomsWithFilteredMessagesQuery request, CancellationToken cancellationToken)
{
var rooms = await _roomService.GetRoomsWithFilteredMessages(request.CustomerId);
var rooms = await _roomRepository.GetAsync();

//if (rooms == null)
// throw new BadHttpRequestException("Please send a valid customer id");
foreach (var room in rooms)
{

return rooms;
List<Message> msg = new();
var customer = room.Customers.Where(c => c.CustomerId == request.CustomerId).FirstOrDefault();

if (customer is not null)
{
foreach (var message in room.Messages)
{
if (message.CreatedAtUtc >= customer.DateOfEnteringRoom)
msg.Add(message);
}
room.Messages = msg;
}
else
{
room.Messages = new List<Message>();
}
}

return rooms;
}
}
}

+ 6
- 6
Backend/Diligent.WebAPI.Host/Mediator/Rooms/Handlers/GetRoomsWhichSupportCreatedHandler.cs Прегледај датотеку

@@ -1,4 +1,5 @@
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Data.Entities;
using Diligent.WebAPI.Host.Mediator.Rooms.Queries;
using MediatR;
@@ -7,14 +8,13 @@ namespace Diligent.WebAPI.Host.Mediator.Chat.Handlers
{
public class GetRoomsWhichSupportCreatedHandler : IRequestHandler<GetRoomsWhichSupportCreatedQuery, List<Room>>
{
private readonly RoomService _roomService;
private readonly IRoomRepository _roomRepository;

public GetRoomsWhichSupportCreatedHandler(RoomService roomService)
public GetRoomsWhichSupportCreatedHandler(IRoomRepository roomRepository)
{
_roomService = roomService;
_roomRepository = roomRepository;
}
public async Task<List<Room>> Handle(GetRoomsWhichSupportCreatedQuery request, CancellationToken cancellationToken) =>
await _roomService.GetRoomsWhichSupportCreated(request.SupportId);
await _roomRepository.GetRoomsWhichSupportCreated(request.SupportId);
}
}

+ 15
- 9
Backend/Diligent.WebAPI.Host/Mediator/Rooms/Handlers/RemoveUserFromGroupHandler.cs Прегледај датотеку

@@ -1,4 +1,6 @@
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Host.Exceptions;
using Diligent.WebAPI.Host.Mediator.Rooms.Commands;
using MediatR;

@@ -6,27 +8,31 @@ namespace Diligent.WebAPI.Host.Mediator.Chat.Handlers
{
public class RemoveUserFromGroupHandler : IRequestHandler<RemoveUserFromGroupCommand, Unit>
{
private readonly RoomService _roomService;
private readonly IRoomRepository _roomRepository;

public RemoveUserFromGroupHandler(RoomService roomService)
public RemoveUserFromGroupHandler(IRoomRepository roomRepository)
{
_roomService = roomService;
_roomRepository = roomRepository;
}

public async Task<Unit> Handle(RemoveUserFromGroupCommand request, CancellationToken cancellationToken)
{
if (request == null)
var room = await _roomRepository.GetByIdAsync(request.RoomId);

if(room == null)
{
throw new BadHttpRequestException("Object cannot be null");
throw new NotFoundException("Room is not found");
}

var result = await _roomService.LeaveChatRoom(request.RoomId, request.UserId);
var userConnection = room.Customers.Where(x => x.CustomerId == request.UserId).FirstOrDefault();

if (!result)
if(userConnection == null)
{
throw new Exception("Problem with deleting user from group");
throw new NotFoundException("User connection is not found");
}

await _roomRepository.LeaveChatRoom(room, userConnection);

return new Unit();
}
}

Loading…
Откажи
Сачувај