using Metrolog_API.Models; using MongoDB.Driver; namespace Metrolog_API.Services { public class UserService { private readonly IMongoCollection _users; public UserService(IConfiguration config) { var client = new MongoClient(System.Environment.GetEnvironmentVariable("MONGODB_CONNECTION_STRING")); var database = client.GetDatabase(System.Environment.GetEnvironmentVariable("MONGODB_DATABASE")); _users = database.GetCollection("users"); } /// /// Read all items in collection ordered by "Surname" by page (100 items) /// /// Page number /// List of users on specified page public async Task> GetPageAsync(int page = 1) => await _users.Find(user => true).SortByDescending(x => x.Surname).Skip((page - 1) * 100).Limit(100).ToListAsync(); /// /// Get user by Email /// /// user Email /// User public async Task GetByEmailAsync(string email) => await _users.Find(user => user.Email == email).FirstOrDefaultAsync(); public async Task GetByIdAsync(string id) => await _users.Find(user => user.Id == id).FirstOrDefaultAsync(); /// /// Create user /// /// user public async Task CreateAsync(UserModel user) => await _users.InsertOneAsync(user); public async Task UpdateAsync(string id, UserModel calibration) => await _users.ReplaceOneAsync(c => c.Id == id, calibration); public async Task RemoveAsync(string id) => await _users.DeleteOneAsync(c => c.Id == id); } }