Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CustomerService.cs 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Diligent.WebAPI.Business.Interfaces;
  2. using Diligent.WebAPI.Data.Entities;
  3. using Microsoft.AspNetCore.Identity;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace Diligent.WebAPI.Business.Services
  10. {
  11. public class CustomerService : ICustomerService
  12. {
  13. private readonly UserManager<Customer> _customerManager;
  14. public CustomerService(UserManager<Customer> customerManager)
  15. {
  16. _customerManager = customerManager;
  17. }
  18. public async Task<Customer> GetCustomer(string username)
  19. {
  20. var customer = await _customerManager.FindByNameAsync(username);
  21. return customer;
  22. }
  23. public async Task<Customer> GetCustomerById(string id)
  24. {
  25. var customer = await _customerManager.FindByIdAsync(id);
  26. return customer;
  27. }
  28. public async Task<bool> AddNotification(string receiverId, string roomId)
  29. {
  30. var receiver = await GetCustomerById(receiverId);
  31. if (receiver == null)
  32. {
  33. return false;
  34. }
  35. var notificationExists = receiver.Notifications.Where(x => x.RoomId == roomId).FirstOrDefault();
  36. if (notificationExists == null)
  37. {
  38. var notification = new Notification
  39. {
  40. Count = 1,
  41. RoomId = roomId
  42. };
  43. receiver.Notifications.Add(notification);
  44. }
  45. else
  46. {
  47. notificationExists.Count++;
  48. }
  49. await _customerManager.UpdateAsync(receiver);
  50. return true;
  51. }
  52. public async Task<List<Notification>> ReadNotifications(string userId)
  53. {
  54. var user = await _customerManager.FindByIdAsync(userId);
  55. return user.Notifications;
  56. }
  57. public async Task<bool> DeleteNotification(string userId, string roomId)
  58. {
  59. var user = await GetCustomerById(userId);
  60. if (user == null)
  61. {
  62. return false;
  63. }
  64. var notification = user.Notifications.Where(x => x.RoomId == roomId).FirstOrDefault();
  65. if (notification == null)
  66. {
  67. return true;
  68. }
  69. user.Notifications.Remove(notification);
  70. await _customerManager.UpdateAsync(user);
  71. return true;
  72. }
  73. }
  74. }