Exemplo n.º 1
0
        /// <summary>
        /// Update teacher
        /// </summary>
        /// <param name="teacherUpdate"></param>
        /// <returns></returns>
        public async Task <TeacherDto> UpdateTeacher(TeacherUpdateDto teacherUpdate)
        {
            TeacherUser user = db.TeachersRepository.Get(a => a.Id == teacherUpdate.Id).FirstOrDefault();

            if (user == null)
            {
                // 404, Not found.
                // No reason for an exception I think
                return(null);
            }

            TeachersConverter.UpdateTeachersPersonalData(user, teacherUpdate);

            var result = await db.AuthRepository.UpdateUser(user);

            if (!result.Succeeded)
            {
                var ex = new UserUpdateException(result.Errors.ToArray());
                ex.Data.Add("IdentityResultErrors", result.Errors.ToArray());
                throw ex;
            }

            var updatedUser = db.TeachersRepository.Get(a => a.Id == teacherUpdate.Id).FirstOrDefault();

            return(TeachersConverter.TeacherToTeacherDto(updatedUser));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Retrieve all teachers
        /// </summary>
        /// <returns></returns>
        public IEnumerable <TeacherDto> GetAllTeachers()
        {
            logger.Info("Service received request for retrieving all teachers");

            return(db.TeachersRepository.Get()
                   // maybe won't work?
                   // also, without include it will take a number of roundtrips to the database...
                   .Select(t => TeachersConverter.TeacherToTeacherDto(t)));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Register a teacher
        /// </summary>
        /// <param name="userModel"></param>
        /// <returns></returns>
        public async Task <TeacherDto> RegisterTeacher(TeacherRegistrationDto userModel)
        {
            TeacherUser user = TeachersConverter.TeacherRegistrationDtoToTeacher(userModel);

            var result = await db.AuthRepository.RegisterTeacherUser(user, userModel.Password);

            if (!result.Succeeded)
            {
                var ex = new UserRegistrationException(result.Errors.ToArray());
                ex.Data.Add("IdentityResultErrors", result.Errors.ToArray());
                throw ex;
            }

            user = await db.AuthRepository.FindUserByUserName(userModel.UserName) as TeacherUser;

            return(TeachersConverter.TeacherToTeacherDto(user));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Delete a teacher user from the system
        /// </summary>
        /// <param name="teacherId"></param>
        /// <returns></returns>
        public async Task <TeacherDto> DeleteTeacher(int teacherId)
        {
            logger.Info("Service received request for deleting a teacher {teacherId}", teacherId);

            TeacherUser deletedTeacher = db.TeachersRepository.Get(t => t.Id == teacherId).FirstOrDefault();

            if (deletedTeacher == null)
            {
                return(null);
            }

            var result = await db.AuthRepository.DeleteUser(teacherId);

            if (!result.Succeeded)
            {
                logger.Error("User removal failed {errors}", result.Errors);
                //return null;
                throw new ConflictException("Delete teacher failed in auth repo");
            }

            return(TeachersConverter.TeacherToTeacherDto(deletedTeacher));
        }