| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- 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;
- }
-
- [HttpPost]
- [Authorize(Roles = "Customer")]
- public async Task<IActionResult> CreateChat(Room room)
- {
- if(room == null)
- {
- throw new BadHttpRequestException("Object cannot be null");
- }
-
- await _roomService.CreateRoomAsync(room);
-
- return Ok(room);
- }
- }
- }
|