示例#1
0
        public static void AddEntry(AOCEntry entry)
        {
            if (_engine == null)
                Initialize();

            var table = _engine.OpenXTable<string, AOCEntry>("entries");

            table[entry.Email] = entry;
            _engine.Commit();
        }
        private static void PostForm(IDictionary<string, string> parameters, HttpRequestEventArgs e)
        {
            var entry = new Model.AOCEntry();
            Console.WriteLine("Entry received");

            bool avail = false;
            int score = 0;
            int.TryParse(e.Request.Form["Score"] ?? string.Empty, out score);

            var a = e.Request.Form["Availability"];
            if (!string.IsNullOrEmpty(a))
            {
                a = a.ToLowerInvariant();
                avail = a == "checked" || a == "true";
            }

            entry.FirstName = e.Request.Form["FirstName"] ?? string.Empty;
            entry.LastName = e.Request.Form["LastName"] ?? string.Empty;
            entry.Email = e.Request.Form["Email"] ?? string.Empty;
            entry.Role = e.Request.Form["Role"] ?? string.Empty;
            entry.LineManager = e.Request.Form["LineManager"] ?? string.Empty;
            entry.Reason = e.Request.Form["Reason"] ?? string.Empty;
            entry.Answers = e.Request.Form["Answers"] ?? string.Empty;
            entry.Organisation = e.Request.Form["Organisation"] ?? string.Empty;
            entry.Region = e.Request.Form["Region"] ?? string.Empty;
            entry.Available = avail;
            entry.Score = score;
            entry.Answers = e.Request.Form["Answers"] ?? string.Empty;
            entry.Submitted = DateTime.UtcNow;

            Dictionary<string, object> errors = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            if (string.IsNullOrEmpty(entry.FirstName) || entry.FirstName.Length < 2)
                errors["FirstName"] = "Please enter a first name";
            if (string.IsNullOrEmpty(entry.LastName) || entry.LastName.Length < 2)
                errors["LastName"] = "Please enter a last name";
            if (string.IsNullOrEmpty(entry.Email) || entry.Email.Length < 5)
                errors["Email"] = "Please enter an email address";
            else if (!entry.Email.ValidateEmail())
                errors["Email"] = "Please enter a valid email address";
            if (string.IsNullOrEmpty(entry.Role) || entry.Role.Length < 2)
                errors["Role"] = "Please enter a role";
            if (string.IsNullOrEmpty(entry.LineManager) || entry.LineManager.Length < 2)
                errors["LineManager"] = "Please enter the name of your line manager";
            if (string.IsNullOrEmpty(entry.Reason) || entry.Reason.Length < 2)
                errors["Reason"] = "Please tell us why you should be an Agent of Change";
            if (string.IsNullOrEmpty(entry.Organisation) || entry.Organisation.Length < 2)
                errors["Organisation"] = "Please select an organisation";
            if (string.IsNullOrEmpty(entry.Region) || entry.Region.Length < 2)
                errors["Region"] = "Please select a region";

            if (errors.Count > 0)
            {
                errors["result"] = false;
                DefaultResponses.Json(e, errors);
                Console.WriteLine("Failed entry: {0}", JsonConvert.SerializeObject(errors));
            }
            else
            {
                Db.AddEntry(entry);
                if (string.IsNullOrEmpty(Config.SuccessRedirect))
                    DefaultResponses.Json(e, new Dictionary<string, object>() { { "result", true } });
                else
                    DefaultResponses.RedirectResponse(e, Config.SuccessRedirect);
                Console.WriteLine("Successful entry from {0}", entry.Email);
            }
        }