public async Task <IActionResult> PostInvitations([FromBody] InvitationModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var invite = _context.Invitations .Where(i => i.Email == model.email) .FirstOrDefault(); if (invite != null) { _context.Invitations.Remove(invite); _context.SaveChanges(); } invite = new Invitation() { Email = model.email, Type = model.type, InvKey = InvitationsHelper.CodeGenerator() }; _context.Invitations.Add(invite); await _context.SaveChangesAsync(); InvitationsHelper.SendEmail(invite); return(Ok()); }
/// <summary> /// Creates a code and inserts it into the database /// </summary> /// <returns></returns> public Invitation CreateCode(string email) { Invitation inv = new Invitation(); inv.Email = email; string code = InvitationsHelper.CodeGenerator(); using (SqlConnection conn = new SqlConnection(_connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand("DELETE Invitations WHERE email = @email; INSERT INTO Invitations VALUES(@email, @invKey)", conn); cmd.Parameters.AddWithValue("@email", email); cmd.Parameters.AddWithValue("@invKey", code); inv.InvID = Convert.ToInt32(cmd.ExecuteScalar()); } inv.InvKey = code; return(inv); }