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);
if (!survey.SurveyQuestions.Any())
{
result.HasError = true;
_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;
}
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);
if (survey == null)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.SurveyDoesNotExist, surveyId));
return result;
}
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.SurveyResponseAlreadyExist, surveyResponse.UserId, surveyResponse.SurveyId));
return result;
var newSurveyResponse = _mapper.Map<SurveyResponse>(surveyResponse);
_context.Add(newSurveyResponse);
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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;
}