Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CustomerService.cs 2.4KB

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