Skip to content
Snippets Groups Projects
CourseService.cs 5.58 KiB
Newer Older
esantangelo's avatar
esantangelo committed
using AutoMapper;
using Microsoft.EntityFrameworkCore;
esantangelo's avatar
esantangelo committed
using Microsoft.Extensions.FileProviders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.DataLayer;
using Tsi1.DataLayer.Entities;

namespace Tsi1.BusinessLayer.Services
{
    public class CourseService : ICourseService
    {
        private readonly Tsi1Context _context;

esantangelo's avatar
esantangelo committed
        private readonly IMapper _mapper;

        public CourseService(Tsi1Context context, IMapper mapper)
        {
            _context = context;
esantangelo's avatar
esantangelo committed
            _mapper = mapper;
        }

        public async Task<ServiceResult<Course>> Create(CourseCreateDto newCourse)
        {
            var result = new ServiceResult<Course>();

esantangelo's avatar
esantangelo committed
            var course = _mapper.Map<Course>(newCourse);
                
            _context.Courses.Add(course);
esantangelo's avatar
esantangelo committed
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.DuplicateCourseName, newCourse.Name);
                return result;
            }
            
            result.Data = course;
            return result;
        }

esantangelo's avatar
esantangelo committed
        public async Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(int userId, string userType)
        {
            var result = new ServiceResult<List<CoursePreviewDto>>();

            var courses = new List<Course>();

esantangelo's avatar
esantangelo committed
            var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == userId);

            if (user == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
                return result;
            }

            if (userType == UserTypes.Student)
            {
                courses = await _context.StudentCourses
                    .Include(x => x.Course)
esantangelo's avatar
esantangelo committed
                    .Where(x => x.StudentId == user.StudentId)
                    .Select(x => x.Course)
                    .ToListAsync();
            }
            else if (userType == UserTypes.Professor)
            {
                courses = await _context.ProfessorCourses
                    .Include(x => x.Course)
esantangelo's avatar
esantangelo committed
                    .Where(x => x.ProfessorId == user.ProfessorId)
                    .Select(x => x.Course)
                    .ToListAsync();
            }

esantangelo's avatar
esantangelo committed
            result.Data = _mapper.Map<List<CoursePreviewDto>>(courses);

            return result;
        }
esantangelo's avatar
esantangelo committed

        public async Task<ServiceResult<bool>> Matriculate(int userId, int courseId)
        {
            var result = new ServiceResult<bool>();

            var user = await _context.Users
                .Include(x => x.Student)
                .FirstOrDefaultAsync(x => x.Id == userId);

            if (user == null || user.Student == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
                return result;
            }

            var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == courseId);

            if (course == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.CourseDoesNotExist, courseId);
                return result;
            }

            var studentCourse = new StudentCourse
            {
                Course = course,
                Student = user.Student
esantangelo's avatar
esantangelo committed
            };

            _context.StudentCourses.Add(studentCourse);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.StudentCourseAlreadyExists, user.Username, course.Name);
                return result;
            }

            return result;
        }

        public async Task<ServiceResult<bool>> AddProfessorToCourse(ProfessorCourseDto professorCourseDto)
        {
            var result = new ServiceResult<bool>();

            var user = await _context.Users
                .Include(x => x.Professor)
                .FirstOrDefaultAsync(x => x.Id == professorCourseDto.UserId);

            if (user == null || user.Professor == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.UserDoesNotExist, user.Username);
                return result;
            }

            var course = await _context.Courses
                .FirstOrDefaultAsync(x => x.Id == professorCourseDto.CourseId);

            if (course == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.CourseDoesNotExist, professorCourseDto.CourseId);
                return result;
            }

            var professorCourse = new ProfessorCourse
            {
                Course = course,
                Professor = user.Professor
            };

            _context.ProfessorCourses.Add(professorCourse);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.ProfessorCourseAlreadyExists, user.Username, course.Name);
                return result;
            }
esantangelo's avatar
esantangelo committed

            return result;
        }