Example #1
0
        public async Task <HttpResponseMessage> PutStudent(string id, [FromBody] PutStudentDTO updated)
        {
            string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;

            logger.Info("UserId: " + userId + ": Requesting Update for Student Id: " + id);

            if (updated.Id != id)
            {
                logger.Error("Updated student id " + updated.Id + " doesn't match the id " + id + " from the request (route).");
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Updated " +
                                              "student id " + updated.Id + " doesn't match the id " + id + " from the request (route)."));
            }

            try
            {
                StudentDTOForAdmin saved = await studentsService.Update(id, updated);

                if (saved == null)
                {
                    logger.Info("Failed!");
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Failed! Something went wrong."));
                }

                logger.Info("Success!");
                return(Request.CreateResponse(HttpStatusCode.OK, saved));
            }
            catch (Exception e)
            {
                logger.Error(e);
                return(Request.CreateResponse(HttpStatusCode.BadRequest, e));
            }
        }
        public async Task <IActionResult> PutStudent([FromRoute] int id, [FromBody] PutStudentDTO body)
        {
            var student = await _uow.Repository <Student>().Find(id);

            if (student == null)
            {
                return(NotFound());
            }

            student = _mapper.Map(body, student);
            _uow.Repository <Student>().Update(student);

            if (!_uow.Complete(1))
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
            return(Ok(_mapper.Map <SimpleStudentDTO>(student)));
        }
Example #3
0
        public async Task <IActionResult> PutStudent([FromRoute] Guid studentId, [FromBody] PutStudentDTO body)
        {
            try
            {
                var student = await _uow.StudentRepository.RetrieveById(studentId);

                if (student == null)
                {
                    return(NotFound());
                }
                student = _mapper.Map(body, student);
                _uow.StudentRepository.Update(student);

                _uow.Complete(true);
                return(Ok(_mapper.Map <StudentDTO>(student)));
            }
            catch (Exception e)
            {
                _logger.LogError($"Error in action `PutStudent()`. {e.Message}");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Example #4
0
        public async Task <StudentDTOForAdmin> Update(string id, PutStudentDTO updated)
        {
            Student found = db.StudentsRepository.GetByID(id);

            if (found == null)
            {
                throw new HttpException("The student with id: " + id + " was not found.");
            }
            if (updated.UserName != null)
            {
                ApplicationUser foundByUserName = await usersService.FindUserByUserName(updated.UserName);

                if (foundByUserName != null && foundByUserName.Id != found.Id)
                {
                    throw new HttpException("The username " + updated.UserName + " already exists.");
                }
                found.UserName = updated.UserName;
            }
            if (updated.Jmbg != null)
            {
                ApplicationUser foundByJmbg = usersService.GetByJmbg(updated.Jmbg);
                if (foundByJmbg != null && foundByJmbg.Id != found.Id)
                {
                    throw new HttpException("The user with JMBG: " + updated.Jmbg + " is already in the sistem." +
                                            "Leave blank if you don't want to change the JMBG.");
                }
            }
            if (updated.FirstName != null)
            {
                found.FirstName = updated.FirstName;
            }
            if (updated.LastName != null)
            {
                found.LastName = updated.LastName;
            }
            if (updated.Email != null)
            {
                found.Email = updated.Email;
            }
            if (updated.EmailConfirmed != null)
            {
                found.EmailConfirmed = (bool)updated.EmailConfirmed;
            }
            if (updated.PhoneNumber != null)
            {
                found.PhoneNumber = updated.PhoneNumber;
            }
            if (updated.PhoneNumberConfirmed != null)
            {
                found.PhoneNumberConfirmed = (bool)updated.PhoneNumberConfirmed;
            }
            if (updated.DayOfBirth != null)
            {
                found.DayOfBirth = (DateTime)updated.DayOfBirth;
            }
            if (updated.IsActive != null)
            {
                found.IsActive = (bool)updated.IsActive;
            }
            if (updated.FormId != null)
            {
                Form foundForm = formsService.GetByID((int)updated.FormId);

                if (foundForm == null)
                {
                    throw new HttpException("The Form with id: " + updated.FormId + " was not found.");
                }

                if (foundForm.Started.AddDays(360).CompareTo(DateTime.UtcNow) < 0)
                {
                    throw new HttpException("The Form with id: " + id + " was not created for this shool year. " +
                                            "This form is from: " + foundForm.Started.Year + ". Students must be assign to a form from this school year.");
                }

                found.Form = foundForm;
            }

            db.StudentsRepository.Update(found);
            db.Save();

            emailsService.CreateMailForUserUpdate(found.Id);
            emailsService.CreateMailForParentForStudentUpdate(found.Id);

            StudentDTOForAdmin updatedDTO = new StudentDTOForAdmin();

            updatedDTO = toDTO.ConvertToStudentDTOForAdmin(found, (List <IdentityUserRole>)found.Roles);

            return(updatedDTO);
        }