-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
LinkedInService.cs
115 lines (99 loc) · 4.09 KB
/
LinkedInService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace LinkedInNamespace
{
public class LinkedInService
{
private static LinkedInService _instance;
private readonly ConcurrentDictionary<string, User> _users;
private readonly ConcurrentDictionary<string, JobPosting> _jobPostings;
private readonly ConcurrentDictionary<string, List<Notification>> _notifications;
private LinkedInService()
{
_users = new ConcurrentDictionary<string, User>();
_jobPostings = new ConcurrentDictionary<string, JobPosting>();
_notifications = new ConcurrentDictionary<string, List<Notification>>();
}
public static LinkedInService GetInstance()
{
return _instance ??= new LinkedInService();
}
public void RegisterUser(User user)
{
_users[user.Id] = user;
}
public User LoginUser(string email, string password)
{
return _users.Values.FirstOrDefault(user => user.Email == email && user.Password == password);
}
public void UpdateUserProfile(User user)
{
_users[user.Id] = user;
}
public void SendConnectionRequest(User sender, User receiver)
{
var connection = new Connection(sender, DateTime.Now);
receiver.Connections.Add(connection);
var notification = new Notification(GenerateNotificationId(), receiver, NotificationType.ConnectionRequest,
$"New connection request from {sender.Name}", DateTime.Now);
AddNotification(receiver.Id, notification);
}
public void AcceptConnectionRequest(User user, User connectionUser)
{
user.Connections.Add(new Connection(connectionUser, DateTime.Now));
}
public List<User> SearchUsers(string keyword)
{
return _users.Values.Where(user => user.Name.Contains(keyword, StringComparison.OrdinalIgnoreCase)).ToList();
}
public void PostJobListing(JobPosting jobPosting)
{
_jobPostings[jobPosting.Id] = jobPosting;
foreach (var user in _users.Values)
{
var notification = new Notification(GenerateNotificationId(), user, NotificationType.JobPosting,
$"New job posting: {jobPosting.Title}", DateTime.Now);
AddNotification(user.Id, notification);
}
}
public List<JobPosting> SearchJobPostings(string keyword)
{
return _jobPostings.Values.Where(posting =>
posting.Title.Contains(keyword, StringComparison.OrdinalIgnoreCase) ||
posting.Description.Contains(keyword, StringComparison.OrdinalIgnoreCase))
.ToList();
}
public void SendMessage(User sender, User receiver, string content)
{
var message = new Message(GenerateMessageId(), sender, receiver, content, DateTime.Now);
receiver.Inbox.Add(message);
sender.SentMessages.Add(message);
var notification = new Notification(GenerateNotificationId(), receiver, NotificationType.Message,
$"New message from {sender.Name}", DateTime.Now);
AddNotification(receiver.Id, notification);
}
private void AddNotification(string userId, Notification notification)
{
_notifications.AddOrUpdate(userId, new List<Notification> { notification },
(key, existingList) =>
{
existingList.Add(notification);
return existingList;
});
}
public List<Notification> GetNotifications(string userId)
{
return _notifications.GetValueOrDefault(userId, new List<Notification>());
}
private string GenerateNotificationId()
{
return Guid.NewGuid().ToString();
}
private string GenerateMessageId()
{
return Guid.NewGuid().ToString();
}
}
}