Пример #1
0
        public async Task <HttpResponseMessage> PutTeacher(string id, [FromBody] PutTeacherDTO updated)
        {
            string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;

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

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

            try
            {
                TeacherDTOForAdmin saved = await teachersService.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));
            }
        }
Пример #2
0
        public async Task <TeacherDTOForAdmin> Update(string id, PutTeacherDTO updated)
        {
            Teacher found = db.TeachersRepository.GetByID(id);

            if (found == null)
            {
                throw new HttpException("The Teacher 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. " +
                                            "Leave blank if you don't want to change the user name.");
                }
                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.Gender != null)
            {
                found.Gender = (Genders)updated.Gender;
            }

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

            emailsService.CreateMailForUserUpdate(found.Id);

            TeacherDTOForAdmin updatedDTO = new TeacherDTOForAdmin();

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

            return(updatedDTO);
        }