Skip to content
Snippets Groups Projects
SurveyService.cs 7.53 KiB
Newer Older
using AutoMapper;
using Microsoft.EntityFrameworkCore;
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 SurveyService : ISurveyService
    {
        private readonly Tsi1Context _context;
        private readonly IMapper _mapper;

        public SurveyService(Tsi1Context context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        public async Task<ServiceResult<bool>> CreateGlobalSurvey(SurveyCreateDto newSurvey, int tenantId)
        {
            var result = new ServiceResult<bool>();

            var tenant = await _context.Tenants.FirstOrDefaultAsync(x => x.Id == tenantId);

            if (tenant == null)
            {
                result.HasError = true;
                result.AddMessage(string.Format(ErrorMessages.TenantDoesNotExist, tenantId));
                return result;
            }

            var survey = _mapper.Map<Survey>(newSurvey);

esantangelo's avatar
esantangelo committed
            if (!survey.SurveyQuestions.Any())
            {
                result.HasError = true;
esantangelo's avatar
esantangelo committed
                result.AddMessage(ErrorMessages.SurveyHasNoQuestions);
esantangelo's avatar
esantangelo committed
                return result;
            }

            survey.Tenant = tenant;
            _context.Surveys.Add(survey);
            
            await _context.SaveChangesAsync();

            return result;
        }

        public async Task<ServiceResult<bool>> DeleteGlobalSurvey(int surveyId, string userType, int tenantId)
        {
            var result = new ServiceResult<bool>();

            var survey = await _context.Surveys
                .Include(x => x.Tenant)
                .FirstOrDefaultAsync(x => x.Id == surveyId);

            if (survey == null)
            {
                result.HasError = true;
                result.AddMessage(string.Format(ErrorMessages.SurveyDoesNotExist, surveyId));
                return result;
            }

            if (userType == UserTypes.FacultyAdmin && survey.Tenant.Id != tenantId)
            {
                result.HasError = true;
                result.AddMessage("No se puede borrar una encuesta de otra facultad");
                return result;
            }

            _context.Surveys.Remove(survey);
          
            await _context.SaveChangesAsync();

            return result;
        }

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

            var tenant = await _context.Tenants.FirstOrDefaultAsync(x => x.Id == tenantId);
            var tenantAdmin = await _context.Tenants.FirstOrDefaultAsync(x => x.Name == TenantAdmin.Name);

            if (tenant == null || userType == UserTypes.UdelarAdmin && tenantAdmin.Id == tenantId)
            {
                result.HasError = true;
                result.AddMessage(string.Format(ErrorMessages.TenantDoesNotExist, tenantId));
                return result;
            }

            var surveys = await _context.Surveys
                .Include(x => x.Tenant)
                .Where(x => x.IsGlobal && x.Tenant.Id == tenantId)
                .ToListAsync();

            result.Data = _mapper.Map<List<SurveyPreviewDto>>(surveys);

            return result;
        }

        public async Task<ServiceResult<SurveyDetailDto>> GetMySurvey(int surveyId, int userId)
        {
            var result = new ServiceResult<SurveyDetailDto>();

            var survey = await _context.Surveys
                .Include(x => x.SurveyQuestions)
                .FirstOrDefaultAsync(x => x.Id == surveyId);
esantangelo's avatar
esantangelo committed
        
            if (survey == null)
            {
                result.HasError = true;
                result.AddMessage(string.Format(ErrorMessages.SurveyDoesNotExist, surveyId));
                return result;
            }

esantangelo's avatar
esantangelo committed
            var surveyResponse = await _context.SurveyResponses
                .Include(x => x.SurveyAnswers)
                .FirstOrDefaultAsync(x => x.SurveyId == surveyId && x.UserId == userId);

            var surveyResponseDto = _mapper.Map<SurveyResponseDetailDto>(surveyResponse);
            var surveyDto = _mapper.Map<SurveyDetailDto>(survey);
            surveyDto.SurveyResponse = surveyResponseDto;

            result.Data = surveyDto;
            return result;
        }

        public async Task<ServiceResult<bool>> Complete(SurveyResponseCreateDto surveyResponse)
        {
            var result = new ServiceResult<bool>();

            var survey = await _context.Surveys
                .FirstOrDefaultAsync(x => x.Id == surveyResponse.SurveyId);

            if (survey == null)
            {
                result.HasError = true;
                result.AddMessage(string.Format(ErrorMessages.SurveyDoesNotExist, surveyResponse.SurveyId));
                return result;
            }

            var existingSurveyResponse = await _context.SurveyResponses
                .Include(x => x.SurveyAnswers)
                .FirstOrDefaultAsync(x => x.SurveyId == surveyResponse.SurveyId && x.UserId == surveyResponse.UserId);

            if (existingSurveyResponse != null)
            {
esantangelo's avatar
esantangelo committed
                result.HasError = true;
                result.AddMessage(string.Format(ErrorMessages.SurveyResponseAlreadyExist, surveyResponse.UserId, surveyResponse.SurveyId));
                return result;
esantangelo's avatar
esantangelo committed
            var newSurveyResponse = _mapper.Map<SurveyResponse>(surveyResponse);
            _context.Add(newSurveyResponse);
            

esantangelo's avatar
esantangelo committed
            await _context.SaveChangesAsync();

            return result;
        }

        public async Task<ServiceResult<List<SurveyResponseDetailDto>>> GetAllResponses(int surveyId, string userType, int tenantId)
        {
            var result = new ServiceResult<List<SurveyResponseDetailDto>>();

            var tenant = await _context.Tenants.FirstOrDefaultAsync(x => x.Id == tenantId);

            if (tenant == null)
            {
                result.HasError = true;
                result.AddMessage(string.Format(ErrorMessages.TenantDoesNotExist, tenantId));
                return result;
            }

            var survey = await _context.Surveys.FirstOrDefaultAsync(x => x.Id == surveyId);

            if (survey == null)
            {
                result.HasError = true;
                result.AddMessage(string.Format(ErrorMessages.SurveyDoesNotExist, surveyId));
                return result;
            }

            if (userType == UserTypes.Professor)
            {
                result.HasError = true;
                result.AddMessage(string.Format(ErrorMessages.InvalidSurvey, surveyId));
                return result;
            }

            var surveyResponses = await _context.SurveyResponses
                .Include(x => x.SurveyAnswers)
                .Where(x => x.SurveyId == surveyId)
                .ToListAsync();

            result.Data = _mapper.Map<List<SurveyResponseDetailDto>>(surveyResponses);

            return result;
        }

        public async Task<ServiceResult<List<AnswerOptionDetailDto>>> GetAnswerOptions()
        {
            var result = new ServiceResult<List<AnswerOptionDetailDto>>();

            var answerOptions = await _context.AnswerOptions.AsNoTracking().ToListAsync();

            result.Data = _mapper.Map<List<AnswerOptionDetailDto>>(answerOptions);

            return result;
        }