Exemplo n.º 1
0
        public async Task <HttpResponseMessage> PutParent(string id, [FromBody] PutParentDTO updated)
        {
            string userId   = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;
            string userRole = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == ClaimTypes.Role).Value;

            logger.Info("UserRole: " + userRole + ", UserId: " + userId + ": Requesting Update for Parent Id: " + id);


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

            try
            {
                ParentDTOForAdmin saved = await parentsService.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));
            }
        }
Exemplo n.º 2
0
        public async Task <ParentDTOForAdmin> Update(string id, PutParentDTO updated)
        {
            Parent found = db.ParentsRepository.GetByID(id);

            if (found == null)
            {
                throw new HttpException("The parent 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.");
                }
            }
            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.MobilePhone != null)
            {
                found.MobilePhone = updated.MobilePhone;
            }

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

            emailsService.CreateMailForUserUpdate(found.Id);

            ParentDTOForAdmin updatedDTO = new ParentDTOForAdmin();

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

            return(updatedDTO);
        }