Devhq provide a lot of services
First we have to make same file and folders

📁 In IStudent.cs
using Univercity.Models;
namespace Univercity.Services.Interfaces
{
public interface IStudent
{
Task<List<Student>> GetStudents();
Task<Student> GetStudent(string id);
Task<Student> CreateStudent(Student student);
Task UpdateStudent(string id, Student studentIn);
Task DeleteStudent(string id);
}
}
C#This is all are interface for adding and deleting data from mongodb database
Now Go to 📁StudentService.cs
Where we will write all serivce for student and also interface implementation will there
using MongoDB.Driver;
using Univercity.DbContext;
using Univercity.Models;
using Univercity.Services.Interfaces;
namespace Univercity.Services
{
public class StudentServices : IStudent
{
private readonly IMongoCollection<Student> _students;
public StudentServices(MongoDbContext context)
{
_students = context.StudentDb;
}
public Task<Student> CreateStudent(Student student)
{
throw new NotImplementedException();
}
public Task DeleteStudent(string id)
{
throw new NotImplementedException();
}
public Task<Student> GetStudent(string id)
{
throw new NotImplementedException();
}
public Task<List<Student>> GetStudents()
{
throw new NotImplementedException();
}
public Task UpdateStudent(string id, Student studentIn)
{
throw new NotImplementedException();
}
}
}
C#Selected line are the connection of database
🧑💻 Create Student Function
public async Task<Student> CreateStudent(Student student)
{
await _students.InsertOneAsync(student);
return student;
}
C#🧑💻 Get All Students Function
public async Task<List<Student>> GetStudents()
{
var students = await _students.Find(e=>true).ToListAsync();
return students;
}
C#🧑💻 Get One Student Function
public async Task<Student> GetStudent(string id)
{
var student = await _students.Find<Student>(e=>e.Id==id).FirstOrDefaultAsync();
return student;
}
C#🧑💻 Delete One Students Function
public async Task DeleteStudent(string id)
{
await _students.DeleteOneAsync(student => student.Id == id);
}
C#🧑💻 Update Students Function
public async Task UpdateStudent(string id, Student studentIn)
{
await _students.ReplaceOneAsync(student => student.Id == id, studentIn);
}
C#After adding all function final code will look like this
using MongoDB.Driver;
using Univercity.DbContext;
using Univercity.Models;
using Univercity.Services.Interfaces;
namespace Univercity.Services
{
public class StudentServices : IStudent
{
private readonly IMongoCollection<Student> _students;
public StudentServices(MongoDbContext context)
{
_students = context.StudentDb;
}
public async Task<Student> CreateStudent(Student student)
{
await _students.InsertOneAsync(student);
return student;
}
public async Task DeleteStudent(string id)
{
await _students.DeleteOneAsync(student => student.Id == id);
}
public async Task<Student> GetStudent(string id)
{
var student = await _students.Find<Student>(e=>e.Id==id).FirstOrDefaultAsync();
return student;
}
public async Task<List<Student>> GetStudents()
{
var students = await _students.Find(e=>true).ToListAsync();
return students;
}
public async Task UpdateStudent(string id, Student studentIn)
{
await _students.ReplaceOneAsync(student => student.Id == id, studentIn);
}
}
}
C#