Exemple #1
0
        public IHttpActionResult PutExternalLogin(int id, ExternalLoginModel externalLogin)
        {
            // Allow only for authorized user
            var userToCheck = _userRepository.FirstOrDefault(u => u.UserName == User.Identity.Name);

            if (!userToCheck.Authorized)
            {
                return(Unauthorized());
            }

            // Validate the request
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != externalLogin.ExternalLoginID)
            {
                return(BadRequest());
            }

            // Get the DB externalLogin, update it according to the input ExternalLoginModel object,
            //   and then update the DB externalLogin in the database
            var dbExternalLogin = _externalLoginRepository.GetByID(id);

            if (dbExternalLogin == null)
            {
                return(NotFound());
            }
            dbExternalLogin.Update(externalLogin);
            _externalLoginRepository.Update(dbExternalLogin);

            // Save database changes
            try
            {
                _unitOfWork.Commit();
            }
            catch (DBConcurrencyException e)
            {
                if (!ExternalLoginExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw new Exception("Unable to update the externalLogin in the database", e);
                }
            }
            return(StatusCode(HttpStatusCode.NoContent));
        }