|
| 1 | +using AskAMuslimAPI.Configurations; |
| 2 | +using AskAMuslimAPI.Services.Interfaces; |
| 3 | +using AutoMapper; |
| 4 | +using EdufyAPI.DTOs.LessonDTOs; |
| 5 | +using EdufyAPI.Helpers; |
| 6 | +using EdufyAPI.Models; |
| 7 | +using EdufyAPI.Repository.Interfaces; |
| 8 | +using EdufyAPI.Services.Interfaces; |
| 9 | +using Microsoft.AspNetCore.Mvc; |
| 10 | +using Microsoft.Extensions.Options; |
| 11 | + |
| 12 | +namespace AskAMuslimAPI.Services |
| 13 | +{ |
| 14 | + public class LessonService( |
| 15 | + ICacheService cacheService, |
| 16 | + IUnitOfWork unitOfWork, |
| 17 | + IMapper mapper, |
| 18 | + IOptions<CacheSettings> cacheSettings, |
| 19 | + IHttpContextAccessor httpContextAccessor) : ILessonService |
| 20 | + { |
| 21 | + private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor; |
| 22 | + private readonly ICacheService _cacheService = cacheService; |
| 23 | + private readonly IUnitOfWork _unitOfWork = unitOfWork; |
| 24 | + private readonly IMapper _mapper = mapper; |
| 25 | + private readonly string ThumbnailsFolderName = "lesson-thumbnails"; |
| 26 | + private readonly string VideosFolderName = "lesson-videos"; |
| 27 | + private readonly CacheSettings _cacheSettings = cacheSettings.Value; |
| 28 | + |
| 29 | + public async Task<ActionResult<LessonReadDTO>> CreateLesson(LessonCreateDTO createLessonDto) |
| 30 | + { |
| 31 | + if (string.IsNullOrEmpty(createLessonDto.CourseId)) |
| 32 | + return new BadRequestObjectResult("Course ID is required."); |
| 33 | + |
| 34 | + try |
| 35 | + { |
| 36 | + var lesson = _mapper.Map<Lesson>(createLessonDto); |
| 37 | + await _unitOfWork.LessonRepository.AddAsync(lesson); |
| 38 | + await _unitOfWork.SaveChangesAsync(); |
| 39 | + |
| 40 | + var lessonReadDto = _mapper.Map<LessonReadDTO>(lesson); |
| 41 | + |
| 42 | + // Invalidate cache for the course, and re-fetch the lessons |
| 43 | + await _cacheService.RemoveDataAsync($"CourseLessons_{createLessonDto.CourseId}"); |
| 44 | + await _cacheService.RemoveDataAsync("AllLessons"); |
| 45 | + |
| 46 | + return lessonReadDto; |
| 47 | + } |
| 48 | + catch (Exception ex) |
| 49 | + { |
| 50 | + return new BadRequestObjectResult($"Error retrieving course: {ex.Message}"); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public async Task<ActionResult<IEnumerable<LessonReadDTO>>> GetAllLessons() |
| 55 | + { |
| 56 | + var lessons = await _unitOfWork.LessonRepository.GetAllAsync(); |
| 57 | + if (!lessons.Any()) |
| 58 | + { |
| 59 | + return new List<LessonReadDTO>(); |
| 60 | + } |
| 61 | + |
| 62 | + try |
| 63 | + { |
| 64 | + // Check if lessons are cached |
| 65 | + var cachedLessons = await _cacheService.GetDataAsync<IEnumerable<LessonReadDTO>>("AllLessons"); |
| 66 | + if (cachedLessons != null) |
| 67 | + { |
| 68 | + return new ActionResult<IEnumerable<LessonReadDTO>>(cachedLessons); |
| 69 | + } |
| 70 | + |
| 71 | + var lessonDtos = _mapper.Map<IEnumerable<LessonReadDTO>>(lessons); |
| 72 | + var request = _httpContextAccessor.HttpContext?.Request; |
| 73 | + |
| 74 | + foreach (var lessonDto in lessonDtos) |
| 75 | + { |
| 76 | + |
| 77 | + if (!string.IsNullOrEmpty(lessonDto.ThumbnailUrl)) |
| 78 | + { |
| 79 | + lessonDto.ThumbnailUrl = ConstructFileUrlHelper.ConstructFileUrl(request, ThumbnailsFolderName, lessonDto.ThumbnailUrl); |
| 80 | + } |
| 81 | + |
| 82 | + if (!string.IsNullOrEmpty(lessonDto.VideoUrl)) |
| 83 | + { |
| 84 | + lessonDto.VideoUrl = ConstructFileUrlHelper.ConstructFileUrl(request, VideosFolderName, lessonDto.VideoUrl); |
| 85 | + } |
| 86 | + |
| 87 | + await _cacheService.SetDataAsync($"Lesson_{lessonDto.Id}", lessonDto, DateTimeOffset.Now.AddMinutes(_cacheSettings.LessonCacheMinutes)); |
| 88 | + await _cacheService.SetDataAsync($"CourseLessons_{lessonDto.CourseId}", lessonDtos, DateTimeOffset.Now.AddMinutes(_cacheSettings.LessonCacheMinutes)); |
| 89 | + } |
| 90 | + |
| 91 | + // Cache the lessons |
| 92 | + await _cacheService.SetDataAsync("AllLessons", lessonDtos, DateTimeOffset.Now.AddMinutes(_cacheSettings.LessonCacheMinutes)); |
| 93 | + |
| 94 | + return new ActionResult<IEnumerable<LessonReadDTO>>(lessonDtos); |
| 95 | + } |
| 96 | + catch (Exception ex) |
| 97 | + { |
| 98 | + return new BadRequestObjectResult($"Error retrieving lessons: {ex.Message}"); |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + public Task<ActionResult<IEnumerable<LessonReadDTO>>> GetCourseLessons(string courseId) |
| 104 | + { |
| 105 | + throw new NotImplementedException(); |
| 106 | + } |
| 107 | + |
| 108 | + public Task<ActionResult<LessonReadDTO>> GetLessonByID(string id) |
| 109 | + { |
| 110 | + throw new NotImplementedException(); |
| 111 | + } |
| 112 | + public Task<IActionResult> UpdateLesson(string id, LessonUpdateDTO updateLessonDto) |
| 113 | + { |
| 114 | + throw new NotImplementedException(); |
| 115 | + } |
| 116 | + public Task<IActionResult> DeleteLesson(string id) |
| 117 | + { |
| 118 | + throw new NotImplementedException(); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + } |
| 123 | +} |
0 commit comments