What is CRUD operation LINQ PART 6 With Example Code

The StudentService class in the FreeWebApplication.OtherService namespace is a service class responsible for handling operations related to student entities.Let's break down its key functionalities



 

using FreeWebApplication.Models;
using FreeWebApplication.Repository;
 
namespace FreeWebApplication.OtherService {
    public class StudentService {
        private readonly IRepository<StudentModel> _studentRepository;
 
        public StudentService(IRepository<StudentModel> studentRepository)
    {
        _studentRepository = studentRepository;
    }
 
        public IEnumerable < StudentModel > GetAllStudents(QueryOptions < StudentModel > options)
    {
        return _studentRepository.List(new QueryOptions < StudentModel > ());
    }
        public int GetTotalPages(QueryOptions < StudentModel > options)
    {
        // Assuming List method from IRepository returns IQueryable
        var query = _studentRepository.List(options);
 
            // Calculate total pages based on the total number of items and page size
            int totalItems = query.Count();
            int totalPages = (int)Math.Ceiling(totalItems / (double)options.PageSize);
 
        return totalPages;
    }
        public StudentModel GetStudentById(int id)
    {
        return _studentRepository.Get(id);
    }
 
        public void AddStudent(StudentModel student)
    {
        student.CreateDate = DateTime.Now;
        student.IsActive = true;
        _studentRepository.Insert(student);
        _studentRepository.Save();
    }
 
        public void UpdateStudent(StudentModel student)
    {
        student.UpdateDate = DateTime.Now;
        _studentRepository.Update(student);
        _studentRepository.Save();
    }
 
        public void DeleteStudent(int id)
    {
        var student = _studentRepository.Get(id);
        if (student != null) {
            student.IsDeleted = true;
            student.IsActive = false;
            _studentRepository.Update(student);
            _studentRepository.Save();
        }
    }
}
}