DEV Community

John
John

Posted on Edited on

Error Interceptor

import { HttpErrorResponse, HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { ToastrService } from 'ngx-toastr';
import { catchError, throwError } from 'rxjs';

public interface IInstructorRepository
{
Task> GetAllAsync();
Task GetByIdAsync(int id);
Task AddAsync(Instructor instructor);
void Update(Instructor instructor);
void Delete(Instructor instructor);
Task SaveAsync();
}

public interface ICategoryRepository
{
Task> GetAllAsync();
Task GetByIdAsync(int id);
}

public interface ICourseRepository
{
Task> GetAllAsync();
Task GetByIdAsync(int id);
Task AddAsync(Course course);
void Update(Course course);
void Delete(Course course);
Task SaveAsync();
}

public interface ILessonRepository
{
Task> GetAllAsync();
Task GetByIdAsync(int id);
Task> GetByCourseIdAsync(int courseId);
Task AddAsync(Lesson lesson);
void Update(Lesson lesson);
void Delete(Lesson lesson);
Task SaveAsync();
}

public interface IEnrollmentRepository
{
Task> GetAllAsync();
Task GetByIdAsync(int id);
Task> GetByUserIdAsync(string userId);
Task AddAsync(Enrollment enrollment);
void Update(Enrollment enrollment);
Task SaveAsync();
}

public interface ILessonProgressRepository
{
Task GetByIdAsync(int id);
Task> GetByEnrollmentIdAsync(int enrollmentId);
Task AddAsync(LessonProgress progress);
void Update(LessonProgress progress);
Task SaveAsync();
}

public interface IReviewRepository
{
Task> GetByCourseIdAsync(int courseId);
Task GetByIdAsync(int id);
Task AddAsync(Review review);
void Update(Review review);
void Delete(Review review);
Task SaveAsync();
}

using Microsoft.EntityFrameworkCore;

public class InstructorRepository : IInstructorRepository
{
private readonly ApplicationDbContext _context;

public InstructorRepository(ApplicationDbContext context)
{
    _context = context;
}

public async Task<List<Instructor>> GetAllAsync()
{
    return await _context.Instructors
        .ToListAsync();
}

public async Task<Instructor?> GetByIdAsync(int id)
{
    return await _context.Instructors
        .FirstOrDefaultAsync(i => i.Id == id);
}

public async Task AddAsync(Instructor instructor)
{
    await _context.Instructors.AddAsync(instructor);
}

public void Update(Instructor instructor)
{
    _context.Instructors.Update(instructor);
}

public void Delete(Instructor instructor)
{
    _context.Instructors.Remove(instructor);
}

public async Task SaveAsync()
{
    await _context.SaveChangesAsync();
}
Enter fullscreen mode Exit fullscreen mode

}

using Microsoft.EntityFrameworkCore;

public class CategoryRepository : ICategoryRepository
{
private readonly ApplicationDbContext _context;

public CategoryRepository(ApplicationDbContext context)
{
    _context = context;
}

public async Task<List<Category>> GetAllAsync()
{
    return await _context.Categories
        .ToListAsync();
}

public async Task<Category?> GetByIdAsync(int id)
{
    return await _context.Categories
        .FirstOrDefaultAsync(c => c.Id == id);
}
Enter fullscreen mode Exit fullscreen mode

}

using Microsoft.EntityFrameworkCore;

public class CourseRepository : ICourseRepository
{
private readonly ApplicationDbContext _context;

public CourseRepository(ApplicationDbContext context)
{
    _context = context;
}

public async Task<List<Course>> GetAllAsync()
{
    return await _context.Courses
        .Include(c => c.Category)
        .Include(c => c.Instructor)
        .ToListAsync();
}

public async Task<Course?> GetByIdAsync(int id)
{
    return await _context.Courses
        .Include(c => c.Category)
        .Include(c => c.Instructor)
        .FirstOrDefaultAsync(c => c.Id == id);
}

public async Task AddAsync(Course course)
{
    await _context.Courses.AddAsync(course);
}

public void Update(Course course)
{
    _context.Courses.Update(course);
}

public void Delete(Course course)
{
    _context.Courses.Remove(course);
}

public async Task SaveAsync()
{
    await _context.SaveChangesAsync();
}
Enter fullscreen mode Exit fullscreen mode

}

using Microsoft.EntityFrameworkCore;

public class LessonRepository : ILessonRepository
{
private readonly ApplicationDbContext _context;

public LessonRepository(ApplicationDbContext context)
{
    _context = context;
}

public async Task<List<Lesson>> GetAllAsync()
{
    return await _context.Lessons
        .Include(l => l.Course)
        .ToListAsync();
}

public async Task<Lesson?> GetByIdAsync(int id)
{
    return await _context.Lessons
        .Include(l => l.Course)
        .FirstOrDefaultAsync(l => l.Id == id);
}

public async Task<List<Lesson>> GetByCourseIdAsync(int courseId)
{
    return await _context.Lessons
        .Where(l => l.CourseId == courseId)
        .OrderBy(l => l.LessonOrder)
        .ToListAsync();
}

public async Task AddAsync(Lesson lesson)
{
    await _context.Lessons.AddAsync(lesson);
}

public void Update(Lesson lesson)
{
    _context.Lessons.Update(lesson);
}

public void Delete(Lesson lesson)
{
    _context.Lessons.Remove(lesson);
}

public async Task SaveAsync()
{
    await _context.SaveChangesAsync();
}
Enter fullscreen mode Exit fullscreen mode

}

using Microsoft.EntityFrameworkCore;

public class EnrollmentRepository : IEnrollmentRepository
{
private readonly ApplicationDbContext _context;

public EnrollmentRepository(ApplicationDbContext context)
{
    _context = context;
}

public async Task<List<Enrollment>> GetAllAsync()
{
    return await _context.Enrollments
        .Include(e => e.User)
        .Include(e => e.Course)
            .ThenInclude(c => c.Instructor)
        .ToListAsync();
}

public async Task<Enrollment?> GetByIdAsync(int id)
{
    return await _context.Enrollments
        .Include(e => e.User)
        .Include(e => e.Course)
            .ThenInclude(c => c.Instructor)
        .FirstOrDefaultAsync(e => e.Id == id);
}

public async Task<List<Enrollment>> GetByUserIdAsync(string userId)
{
    return await _context.Enrollments
        .Include(e => e.Course)
            .ThenInclude(c => c.Instructor)
        .Where(e => e.UserId == userId)
        .ToListAsync();
}

public async Task AddAsync(Enrollment enrollment)
{
    await _context.Enrollments.AddAsync(enrollment);
}

public void Update(Enrollment enrollment)
{
    _context.Enrollments.Update(enrollment);
}

public async Task SaveAsync()
{
    await _context.SaveChangesAsync();
}
Enter fullscreen mode Exit fullscreen mode

}

using Microsoft.EntityFrameworkCore;

public class LessonProgressRepository : ILessonProgressRepository
{
private readonly ApplicationDbContext _context;

public LessonProgressRepository(ApplicationDbContext context)
{
    _context = context;
}

public async Task<LessonProgress?> GetByIdAsync(int id)
{
    return await _context.LessonProgresses
        .Include(lp => lp.User)
        .Include(lp => lp.Lesson)
        .Include(lp => lp.Enrollment)
        .FirstOrDefaultAsync(lp => lp.Id == id);
}

public async Task<List<LessonProgress>> GetByEnrollmentIdAsync(int enrollmentId)
{
    return await _context.LessonProgresses
        .Include(lp => lp.Lesson)
        .Where(lp => lp.EnrollmentId == enrollmentId)
        .OrderBy(lp => lp.Lesson.LessonOrder)
        .ToListAsync();
}

public async Task AddAsync(LessonProgress progress)
{
    await _context.LessonProgresses.AddAsync(progress);
}

public void Update(LessonProgress progress)
{
    _context.LessonProgresses.Update(progress);
}

public async Task SaveAsync()
{
    await _context.SaveChangesAsync();
}
Enter fullscreen mode Exit fullscreen mode

}

using Microsoft.EntityFrameworkCore;

public class ReviewRepository : IReviewRepository
{
private readonly ApplicationDbContext _context;

public ReviewRepository(ApplicationDbContext context)
{
    _context = context;
}

public async Task<List<Review>> GetByCourseIdAsync(int courseId)
{
    return await _context.Reviews
        .Include(r => r.User)
        .Include(r => r.Course)
        .Where(r => r.CourseId == courseId)
        .OrderByDescending(r => r.CreatedDate)
        .ToListAsync();
}

public async Task<Review?> GetByIdAsync(int id)
{
    return await _context.Reviews
        .Include(r => r.User)
        .Include(r => r.Course)
        .FirstOrDefaultAsync(r => r.Id == id);
}

public async Task AddAsync(Review review)
{
    await _context.Reviews.AddAsync(review);
}

public void Update(Review review)
{
    _context.Reviews.Update(review);
}

public void Delete(Review review)
{
    _context.Reviews.Remove(review);
}

public async Task SaveAsync()
{
    await _context.SaveChangesAsync();
}
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)