public async Task <IOperationResult <Subject> > RemoveSubject(int subjectId)
        {
            Subject subject = await _subjectRepository.Set.FindAsync(subjectId);

            if (subject == null)
            {
                return(BasicOperationResult <Subject> .Fail("Subject not found"));
            }

            var tutorRelations = await _tutorSubjectRepository.FindAll(ts => ts.SubjectId == subjectId);

            _tutorSubjectRepository.Set.RemoveRange(tutorRelations);
            await _tutorSubjectRepository.Save();

            _subjectRepository.Remove(subject);
            await _subjectRepository.Save();

            return(BasicOperationResult <Subject> .Ok(subject));
        }
        public async Task <IOperationResult <bool> > AssignSubjectToTutors(int tutorId, IEnumerable <int> subjectIds)
        {
            var tutor = await _userRepository.Find(u => u.Id == tutorId &&
                                                   u.UserRoles.Any(r => r.RoleId == (int)RoleTypes.Tutor),
                                                   u => u.UserRoles);

            if (tutor == null)
            {
                return(BasicOperationResult <bool> .Fail("El tutor no fue encontrado"));
            }

            bool subjectExists = await CheckIfSubjectIdsExists(subjectIds);

            if (!subjectExists)
            {
                return(BasicOperationResult <bool> .Fail("Revise que todas las materias que esta enviando, están registradas en el sistema"));
            }

            var tutorSubjects = await GetSubjectsForTutor(tutorId);

            var tutorSubjectIds = tutorSubjects.Select(s => s.Id);

            var idOfSubjectsToRemove = GetIdsThatHaveToBeRemoved(tutorSubjectIds, subjectIds);
            var idsOfSubjectsToAdd   = GetIdsThatHaveToBeAdded(tutorSubjectIds, subjectIds);

            var tutorSubjecsToRemove = await _tutorSubjectRepository.FindAll(ts =>
                                                                             idOfSubjectsToRemove.Contains(ts.SubjectId) && ts.TutorId == tutorId);

            _tutorSubjectRepository.Set.RemoveRange(tutorSubjecsToRemove);

            var tutorSubjectsToAdd = idsOfSubjectsToAdd.Select(id => new TutorSubject
            {
                SubjectId = id,
                TutorId   = tutorId
            });

            await _tutorSubjectRepository.Set.AddRangeAsync(tutorSubjectsToAdd);

            await _tutorSubjectRepository.Save();

            return(BasicOperationResult <bool> .Ok(true));
        }
Esempio n. 3
0
        public async Task <IOperationResult <ISet <User> > > GetTutorsBySubjectId(int subjectId)
        {
            var subjectExists = await _subjectRepository.Exists(s => s.Id == subjectId);

            if (!subjectExists)
            {
                return(BasicOperationResult <ISet <User> > .Fail("La materia que por la que intenta buscar tutores no existe en el sistema ya"));
            }

            var tutorList = await _tutorSubjectRepository.FindAll(t => t.SubjectId == subjectId,
                                                                  t => t.Tutor);

            var tutors = tutorList.Select(t => t.Tutor).Where(t => t.IsActive && t.IsEmailValidated).ToHashSet();

            return(BasicOperationResult <ISet <User> > .Ok(tutors));
        }