You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChatController.cs 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using AutoMapper;
  2. using Diligent.WebAPI.Business.Services;
  3. using Diligent.WebAPI.Data.Entities;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.AspNetCore.Mvc;
  6. namespace Diligent.WebAPI.Host.Controllers
  7. {
  8. [ApiVersion("1.0")]
  9. [ApiController]
  10. [Route("v{version:apiVersion}/[controller]")]
  11. public class ChatController : ControllerBase
  12. {
  13. private readonly RoomService _roomService;
  14. public ChatController(RoomService roomService)
  15. {
  16. _roomService = roomService;
  17. }
  18. [Authorize(Roles = "Customer,Support")]
  19. [HttpGet("rooms-with-filtered-messages")]
  20. public async Task<ActionResult<List<Room>>> GetAllRoomsWithFilteredMessages(string customerId)
  21. {
  22. var rooms = await _roomService.GetRoomsAsync();
  23. foreach (var room in rooms)
  24. {
  25. List<Message> msg = new();
  26. var customer = room.Customers.Where(c => c.CustomerId == customerId).FirstOrDefault();
  27. if (customer is not null)
  28. {
  29. foreach (var message in room.Messages)
  30. {
  31. if (message.CreatedAtUtc >= customer.DateOfEnteringRoom)
  32. msg.Add(message);
  33. }
  34. room.Messages = msg;
  35. }
  36. else
  37. {
  38. room.Messages = new List<Message>();
  39. }
  40. }
  41. return rooms;
  42. }
  43. [HttpGet("support-rooms")]
  44. [Authorize(Roles = "Support")]
  45. public async Task<ActionResult<List<Room>>> GetSupportRooms(string supportId) =>
  46. await _roomService.GetRoomsForSupport(supportId);
  47. [HttpPost]
  48. [Authorize(Roles = "Support")]
  49. public async Task<IActionResult> CreateChat(Room room)
  50. {
  51. if(room == null)
  52. {
  53. throw new BadHttpRequestException("Object cannot be null");
  54. }
  55. await _roomService.CreateRoomAsync(room);
  56. return Ok(room);
  57. }
  58. }
  59. }