Esempio n. 1
0
        public async Task <IActionResult> Put(
            [FromRoute] int studentId,
            [FromBody] StudentWithRoleReq request)
        {
            bool found = await _studentService.EditOperatorAsync(studentId, request);

            if (!found)
            {
                return(StatusCode(404));
            }
            return(StatusCode(204));
        }
Esempio n. 2
0
        /// <summary>
        /// Edit Student (as Operator)
        /// </summary>
        /// <param name="studentId">Student ID</param>
        /// <param name="model">Student DTO</param>
        /// <returns>Bool if Student was found</returns>
        public async Task <bool> EditOperatorAsync(int studentId, StudentWithRoleReq model)
        {
            // route id and model id differs
            if (studentId != model.StudentId)
            {
                throw new AppLogicException("Neplatný požadavek");
            }

            var student = await _context.Students
                          .Include(st => st.User)
                          .FirstOrDefaultAsync(st => st.StudentId == studentId);

            // student not found
            if (student == null)
            {
                return(false);
            }

            // student activation
            if (student.User.Role == UserRoles.StudentInact &&
                model.Role == UserRoles.Student)
            {
                student.User.Role = UserRoles.Student;
            }
            // student deactivation
            else if (student.User.Role == UserRoles.Student &&
                     model.Role == UserRoles.StudentInact)
            {
                await _jobApplicationService.CancelAllActiveAsync(studentId);

                student.User.Role = UserRoles.StudentInact;
            }
            // invalid role transation
            else
            {
                throw new AppLogicException("Neplatná změna role");
            }

            await _context.SaveChangesAsync();

            return(true);
        }