-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUserService.cs
More file actions
46 lines (37 loc) · 1.49 KB
/
UserService.cs
File metadata and controls
46 lines (37 loc) · 1.49 KB
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
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using UserDataAPI.Models;
namespace UserDataAPI.Services;
public class MongoDbSettings
{
public string? ConnectionString { get; set; }
public string? DatabaseName { get; set; }
public string? UsersCollectionName { get; set; }
}
public class UserService
{
private readonly IMongoCollection<User> _usersCollection;
public UserService(IOptions<MongoDbSettings> settings)
{
var client = new MongoClient(settings.Value.ConnectionString);
var database = client.GetDatabase(settings.Value.DatabaseName);
_usersCollection = database.GetCollection<User>(settings.Value.UsersCollectionName);
}
public async Task<List<User>> GetAsync() =>
await _usersCollection.Find(_ => true).ToListAsync();
public async Task<User?> GetAsync(string id) =>
await _usersCollection.Find(u => u.Id == id).FirstOrDefaultAsync();
public async Task CreateAsync(User newUser) =>
await _usersCollection.InsertOneAsync(newUser);
public async Task<bool> UpdateAsync(string id, User updatedUser)
{
updatedUser.Id = id;
var result = await _usersCollection.ReplaceOneAsync(u => u.Id == id, updatedUser);
return result.IsAcknowledged && result.ModifiedCount > 0;
}
public async Task<bool> RemoveAsync(string id)
{
var result = await _usersCollection.DeleteOneAsync(u => u.Id == id);
return result.IsAcknowledged && result.DeletedCount > 0;
}
}