public async Task<IHttpActionResult> PostInvite(WebApiSucksSoMuch inviteEmail) { if (inviteEmail.Email == "") { string error = "noEmail"; return Ok(error); } var user = db.Users.Find(User.Identity.GetUserId()); InviteCode code = new InviteCode(); var invite = db.Invitation.FirstOrDefault(i => i.InvitedEmail == inviteEmail.Email && i.HouseholdId == user.HouseholdId); if(invite == null) { invite = new Invitaton() { Code = code.CreateCode(), HouseholdId = user.HouseholdId, InvitedEmail = inviteEmail.Email }; db.Invitation.Add(invite); } var invitedUserExists = db.Users.Any(u => u.Email == inviteEmail.Email); //var url = invitedUserExists ? Url.Route("Go to join view", "") : Url.Route("Go to register and join view", ""); var mailer = new EmailService(); var message = new IdentityMessage() { Subject = "You've been invited", Destination = inviteEmail.Email, Body = "You've been invited to join " + user.DisplayName + "'s household. This is your join code: " + invite.Code }; await db.SaveChangesAsync(); await mailer.SendAsync(message); return Ok(invite); }
public async Task<IHttpActionResult> JoinHousehold(Invitaton inviteInfo) { var user = db.Users.Find(User.Identity.GetUserId()); var outuser = db.Users.Where(u => u.Email == inviteInfo.InvitedEmail).FirstOrDefault(); var invite = db.Invitation.Where(i => i.Code == inviteInfo.Code && i.InvitedEmail == inviteInfo.InvitedEmail).FirstOrDefault(); if(invite != null){ if(user != null) { user.HouseholdId = invite.HouseholdId; user.JoinedHousehold = DateTimeOffset.Now; } else { outuser.HouseholdId = invite.HouseholdId; user.JoinedHousehold = DateTimeOffset.Now; } db.Invitation.Remove(invite); } else { return NotFound(); } await db.SaveChangesAsync(); if (user != null) { return Ok(user.Email + " has joined the " + user.Household.Name + " household."); } return Ok(outuser.Email + " is now registered and a member of the " + outuser.Household.Name + " household."); }