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 { public RoomService(IOptions webApiDatabaseSettings) : base(webApiDatabaseSettings, "Room") { } public async Task> GetRoomsAsync() => await _mongoCollection.Find(_ => true).ToListAsync(); public async Task CreateRoomAsync(Room room) => await _mongoCollection.InsertOneAsync(room); public async Task 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 GetRoomAsync(string roomId) => await _mongoCollection.Find(r => r.Id == roomId).FirstOrDefaultAsync(); public async Task 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> GetMessagesForSpecificRoom(string roomId) { var room = await _mongoCollection.Find(r => r.Id == roomId).FirstOrDefaultAsync(); if (room is null) return new List(); return room.Messages; } public async Task> GetRoomsForSupport(string supportId) => await _mongoCollection.Find(k => k.CreatedBy == supportId).ToListAsync(); } }