Esempio n. 1
0
        public async Task <Admin> Update(string id, PutAdminDTO updated)
        {
            Admin found = db.AdminsRepository.GetByID(id);

            if (found == null)
            {
                throw new HttpException("The Admin 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 " + foundByUserName.UserName + " already exists. " +
                                            "(The user with id: " + foundByUserName.Id + ")");
                }
                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." +
                                            "(The user with id: " + foundByJmbg.Id + ")");
                }
            }
            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;
            }

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

            emailsService.CreateMailForUserUpdate(found.Id);

            return(found);
        }
Esempio n. 2
0
        public async Task <HttpResponseMessage> PutAdmin(string id, [FromBody] PutAdminDTO 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("UserId: " + userId + ": Requesting Update for Admin Id: " + id);

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

            try
            {
                Admin saved = await adminsService.Update(id, updated);

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

                logger.Info("Success!");
                return(Request.CreateResponse(HttpStatusCode.OK, saved));
            }
            catch (Exception e)
            {
                logger.Error(e);
                return(Request.CreateResponse(HttpStatusCode.BadRequest, e));
            }
        }