-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserService.cs
49 lines (37 loc) · 1.42 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
using MongoDB.Driver;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Identity;
using TodoApi.Models;
namespace TodoApi.Services
{
public class UserService
{
private readonly IMongoCollection<User> _users;
public UserService(IMongoDatabaseSettings settings)
{
var client = new MongoClient(settings.ConnectionString);
var database = client.GetDatabase(settings.DatabaseName);
_users = database.GetCollection<User>(settings.UsersCollectionName);
}
public List<User> Get() =>
_users.Find(user => true).ToList();
public User Get(string id) =>
_users.Find<User>(user => user.Id == id).FirstOrDefault();
public User GetByEmail(string email) =>
_users.Find<User>(user => user.Email == email).FirstOrDefault();
public User Create(User user)
{
var passwordHasher = new PasswordHasher<User>();
user.Hash = passwordHasher.HashPassword(user, user.Password);
_users.InsertOne(user);
return user;
}
public void Update(string id, User userIn) =>
_users.ReplaceOne(user => user.Id == id, userIn);
public void Remove(User userIn) =>
_users.DeleteOne(user => user.Id == userIn.Id);
public void Remove(string id) =>
_users.DeleteOne(user => user.Id == id);
}
}