Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

2.1 KB
Raw Blame
using Grand.Business.Core.Interfaces.Marketing.Courses;
using Grand.Domain.Courses;
using Grand.Domain.Customers;
using Grand.Web.Features.Models.Customers;
using Grand.Web.Models.Customer;
using Grand.Mediator;

namespace Grand.Web.Features.Handlers.Customers;

public class GetCoursesHandler : IRequestHandler<GetCourses, CoursesModel>
{
    private readonly ICourseActionService _courseActionService;
    private readonly ICourseLessonService _courseLessonService;
    private readonly ICourseLevelService _courseLevelService;
    private readonly ICourseService _courseService;

    public GetCoursesHandler(ICourseService courseService,
        ICourseLevelService courseLevelService,
        ICourseLessonService courseLessonService,
        ICourseActionService courseActionService)
    {
        _courseService = courseService;
        _courseLevelService = courseLevelService;
        _courseLessonService = courseLessonService;
        _courseActionService = courseActionService;
    }

    public async Task<CoursesModel> Handle(GetCourses request, CancellationToken cancellationToken)
    {
        var model = new CoursesModel {
            CustomerId = request.Customer.Id
        };
        var courses = await _courseService.GetByCustomer(request.Customer, request.Store.Id);
        foreach (var item in courses)
        {
            var level = await _courseLevelService.GetById(item.LevelId);
            model.CourseList.Add(new CoursesModel.Course {
                Id = item.Id,
                Name = item.Name,
                SeName = item.SeName,
                ShortDescription = item.ShortDescription,
                Level = level?.Name,
                Approved = await IsApprovedCourse(item, request.Customer)
            });
        }

        return model;
    }

    private async Task<bool> IsApprovedCourse(Course course, Customer customer)
    {
        var lessons = await _courseLessonService.GetByCourseId(course.Id);
        foreach (var item in lessons.Where(x => x.Published))
            if (!await _courseActionService.CustomerLessonCompleted(customer.Id, item.Id))
                return false;
        return true;
    }
}