Ejemplo n.º 1
0
        // GET api/command
        public ActionResult Get()
        {
            var context = new Context();

            // Get un-expired commands, set them as expired, return JSON list
            var commands = context.Commands.Where(c => !c.Used).ToList();
            if (!commands.Any()) return new JsonResult { Data = new List<CommandEntity>() };

            Debug.WriteLine("[GET] api/command :: called :: has pending commands");

            // Set the commands as used in the
            foreach (var command in commands)
                command.Used = true;
            context.SaveChanges();

            Debug.WriteLine("[GET] api/command :: called :: has pending commands :: {0} commands returned to client", commands.Count());
            return new JsonResult { Data = commands };
        }
Ejemplo n.º 2
0
        public ActionResult Post()
        {
            Debug.WriteLine("[POST] api/inbound :: called");

            if (ActionContext == null || ActionContext.ActionArguments == null || ActionContext.ActionArguments["email"] == null || !ActionContext.ActionArguments.ContainsKey("email"))
                goto end;

            var email = ActionContext.ActionArguments["email"] as Email;
            if (email == null) goto end;

            Debug.WriteLine("[POST] api/inbound :: called :: valid email");

            // le logic
            CommandEntity commandEntity = null;
            var commandStringValue = Regex.Replace(email.Subject, @"[\W]", "").ToLowerInvariant();
            switch (commandStringValue)
            {
                case "up": commandEntity = new CommandEntity(Command.Up); break;
                case "down": commandEntity = new CommandEntity(Command.Down); break;
                case "left": commandEntity = new CommandEntity(Command.Left); break;
                case "right": commandEntity = new CommandEntity(Command.Right); break;
                case "bieber": commandEntity = new CommandEntity(Command.Bieber); break;
                case "cyrus": commandEntity = new CommandEntity(Command.Cyrus); break;
            }

            if (commandEntity != null)
            {
                Debug.WriteLine("[POST] api/inbound :: called :: valid email :: command added to db");
                using (var context = new Context())
                {
                    context.Commands.Add(commandEntity);
                    context.SaveChanges();
                }
            }

            end:
            return new HttpStatusCodeResult(HttpStatusCode.OK);
        }