public IHttpActionResult PostExternalLogin(ExternalLoginModel externalLogin) { // Validate request if (!ModelState.IsValid) { return BadRequest(ModelState); } // Check that the corresponding user exists if (!_userRepository.Any(u => u.Id == externalLogin.UserID)) { throw new Exception("Unable to add the external login to the database, as it does not correspond to a user"); } //Set up new ExternalLogin object, populated from input externalLogin ExternalLogin dbExternalLogin = new ExternalLogin(); dbExternalLogin.Update(externalLogin); // Add the new ExternalLogin object to the DB _externalLoginRepository.Add(dbExternalLogin); // Save the changes in the database try { _unitOfWork.Commit(); } catch (Exception e) { throw new Exception("Unable to add the externalLogin to the database", e); } // Set externalLogin ID in ExternalLoginModel object with the ID // that was set in the DB externalLogin after db.SaveChanges externalLogin.ExternalLoginID = dbExternalLogin.ExternalLoginID; return CreatedAtRoute("DefaultApi", new { id = externalLogin.ExternalLoginID }, externalLogin); }
public void Update(ExternalLoginModel externalLogin) { UserID = externalLogin.UserID; LoginProvider = externalLogin.LoginProvider; ProviderKey = externalLogin.ProviderKey; }
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); }