This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
/
UserService.cs
73 lines (65 loc) · 2.58 KB
/
UserService.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
using System;
using System.Collections.Generic;
using System.Linq;
using Raven.Client;
using RavenOverflow.Core.Entities;
using RavenOverflow.Core.Services;
namespace RavenOverflow.Services
{
public class UserService : RavenDbBaseService, IUserService
{
public UserService(IDocumentSession documentSession) : base(documentSession)
{
}
#region IUserService Members
public User CreateOrUpdate(OAuthData oAuthData, string userName, string fullName, string email)
{
// Lets find an existing user for the provider OR the email address if the provider doesn't exist.
User user = DocumentSession
.Query<User>()
.SingleOrDefault(x =>
x.OAuthData.Any(y => y.Id == oAuthData.Id &&
y.OAuthProvider == oAuthData.OAuthProvider)) ??
DocumentSession
.Query<User>()
.SingleOrDefault(x => x.Email == email);
if (user != null)
{
// User exists, so lets update the OAuth data, for this user.
if (user.OAuthData != null)
{
OAuthData existingProvider =
user.OAuthData.SingleOrDefault(x => x.OAuthProvider == oAuthData.OAuthProvider);
if (existingProvider != null)
{
user.OAuthData.Remove(existingProvider);
}
}
else
{
user.OAuthData = new List<OAuthData>();
}
user.OAuthData.Add(oAuthData);
}
else
{
// Ok. No user at all. We create one and store it.
user = new User
{
DisplayName = userName,
Email = email,
Id = null,
FullName = fullName,
CreatedOn = DateTime.UtcNow,
IsActive = true,
OAuthData = new List<OAuthData>(),
FavoriteTags = new List<string> {"ravendb", "c#", "asp.net-mvc3"}
};
user.OAuthData.Add(oAuthData);
}
DocumentSession.Store(user);
return user;
}
#endregion
}
}