public async Task <IActionResult> InviteToProject([FromBody] UtilityInviteModel inviteModel) { //get the current user var user = await _userManager.GetUserAsync(HttpContext.User); //make sure the user who is making the request is a project administrator if (!_validation.userIsProjectAdministrator(user.Id, inviteModel.projectId)) { return(Unauthorized()); } var invitee = _usersRepo.GetUserByUserName(inviteModel.inviteeUserName); //if no user with the username exists if (invitee == null) { return(NotFound("User with the given username does not exist")); } //if the user is already part of the project if (_validation.userIsProjectMember(invitee.Id, inviteModel.projectId)) { return(BadRequest("This user is already a member of the project")); } //if the user has already been invited to the project if (_projectsRepo.HasUserBeenInvited(inviteModel.projectId, invitee.Id) == true) { return(BadRequest("This user has already been invited to join this project")); } //build project invitation object ProjectInvitation projectInvitation = new ProjectInvitation(); projectInvitation.ProjectId = inviteModel.projectId; projectInvitation.InviterId = user.Id; projectInvitation.InviteeId = invitee.Id; _projectsRepo.AddProjectInvite(projectInvitation); _projectsRepo.SaveChanges(); return(Ok(invitee)); }