using AutoMapper; using Diligent.WebAPI.Business.Services; using Diligent.WebAPI.Data.Entities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Diligent.WebAPI.Host.Controllers { [ApiVersion("1.0")] [ApiController] [Route("v{version:apiVersion}/[controller]")] public class ChatController : ControllerBase { private readonly RoomService _roomService; public ChatController(RoomService roomService) { _roomService = roomService; } [Authorize(Roles = "Customer,Support")] [HttpGet("rooms-with-filtered-messages")] public async Task>> GetAllRoomsWithFilteredMessages(string customerId) { var rooms = await _roomService.GetRoomsAsync(); foreach (var room in rooms) { List 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(); } } return rooms; } [HttpPost] [Authorize(Roles = "Customer")] public async Task CreateChat(Room room) { if(room == null) { throw new BadHttpRequestException("Object cannot be null"); } await _roomService.CreateRoomAsync(room); return Ok(room); } } }