| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- 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<ActionResult<List<Room>>> GetAllRoomsWithFilteredMessages(string customerId)
- {
- var rooms = await _roomService.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;
- }
-
- [HttpGet("support-rooms")]
- [Authorize(Roles = "Support")]
- public async Task<ActionResult<List<Room>>> GetSupportRooms(string supportId) =>
- await _roomService.GetRoomsForSupport(supportId);
-
- [HttpPost]
- [Authorize(Roles = "Support")]
- public async Task<IActionResult> CreateChat(Room room)
- {
- if(room == null)
- {
- throw new BadHttpRequestException("Object cannot be null");
- }
-
- await _roomService.CreateRoomAsync(room);
-
- return Ok(room);
- }
- }
- }
|