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 CommunicationService : ICommunicationService { private readonly Tsi1Context _context; private readonly IMapper _mapper; public CommunicationService(Tsi1Context context, IMapper mapper) { _context = context; _mapper = mapper; } public async Task<ServiceResult<bool>> Create(CommunicationCreateDto newCommunication, int id) { var result = new ServiceResult<bool>(); var isGlobal = newCommunication.IsGlobal; var commmunication = _mapper.Map<Communication>(newCommunication); var validationResult = await this.CreateValidation(id, isGlobal, commmunication); if (validationResult.HasError) { return validationResult; } _context.Communications.Add(commmunication); await _context.SaveChangesAsync(); return result; } public async Task<ServiceResult<bool>> Delete(int communicationId) { var result = new ServiceResult<bool>(); var commmunication = await _context.Communications.FirstOrDefaultAsync(x => x.Id == communicationId); if (commmunication == null) { result.HasError = true; result.AddMessage(string.Format(ErrorMessages.CommunicationDoesNotExist, communicationId)); return result; } _context.Communications.Remove(commmunication); await _context.SaveChangesAsync(); return result; } public async Task<ServiceResult<List<CommunicationPreviewDto>>> GetMyCommunications(int userId) { var result = new ServiceResult<List<CommunicationPreviewDto>>(); var user = await _context.Users.AsNoTracking().FirstOrDefaultAsync(x => x.Id == userId); var courseIds = await _context.StudentCourses .AsNoTracking() .Where(x => x.StudentId == user.StudentId) .Select(x => x.CourseId) .ToListAsync(); var communications = await _context.Communications .AsNoTracking() .Include(x => x.Tenant) .Include(x => x.Course) .Where(x => (x.Tenant.Id == userId || courseIds.Contains(x.Course.Id)) && x.ValidUntil >= DateTime.Now) .ToListAsync(); result.Data = _mapper.Map<List<CommunicationPreviewDto>>(communications); return result; } public async Task<ServiceResult<bool>> TenantValidation(int tenantId, int courseId) { var result = new ServiceResult<bool>(); var tenantAdmin = await _context.Tenants.AsNoTracking().FirstOrDefaultAsync(x => x.Name == TenantAdmin.Name); var course = await _context.Courses.AsNoTracking().FirstOrDefaultAsync(x => x.Id == courseId); if (tenantAdmin.Id != tenantId) { result.HasError = course.TenantId != tenantId; result.AddMessage(string.Format(ErrorMessages.InvalidTenant, course.TenantId)); } return result; } private async Task<ServiceResult<bool>> CreateValidation(int id, bool isGlobal, Communication commmunication) { var result = new ServiceResult<bool>(); if (isGlobal) { var tenant = await _context.Tenants.FirstOrDefaultAsync(x => x.Id == id && x.Name != TenantAdmin.Name); if (tenant == null) { result.HasError = true; result.AddMessage(string.Format(ErrorMessages.TenantDoesNotExist, id)); return result; } commmunication.Tenant = tenant; } else { var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == id); if (course == null) { result.HasError = true; result.AddMessage(string.Format(ErrorMessages.CourseDoesNotExist, id)); return result; } commmunication.Course = course; } return result; } } }