Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ChatController.cs 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. [HttpPost]
  44. [Authorize(Roles = "Customer")]
  45. public async Task<IActionResult> CreateChat(Room room)
  46. {
  47. if(room == null)
  48. {
  49. throw new BadHttpRequestException("Object cannot be null");
  50. }
  51. await _roomService.CreateRoomAsync(room);
  52. return Ok(room);
  53. }
  54. }
  55. }