예제 #1
0
        public NumberPlateTrigger FunctionHandler(NumberPlateTrigger payload, ILambdaContext context)
        {
            context.Logger.LogLine($"Process NOOP: process has started. Request={JsonConvert.SerializeObject(payload)}");
            string msg = "";

            if (payload.numberPlate.detected == true)
            {
                /////////////////////////////////////////////////////////////
                //
                // TODO: Read the credit value from the database and decrement it
                //
                /////////////////////////////////////////////////////////////
                msg = "Number plate " + payload.numberPlate.numberPlateString + " was charged $$" + payload.charge + ".";
            }
            else
            {
                msg = "Number plate " + payload.numberPlate.numberPlateString + " was not found. This will require manual resolution.";
                /////////////////////////////////////////////////////////////
                //
                // TODO: Return 'errorUnknownNumberPlate' error
                //
                /////////////////////////////////////////////////////////////
            }
            context.Logger.LogLine(msg);
            return(payload);
        }
        public async Task <NumberPlateTrigger> FunctionHandler(NumberPlateTrigger payload, ILambdaContext context)
        {
            Random rand = new Random((Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);

            if (rand.NextDouble() > double.Parse(Environment.GetEnvironmentVariable("RandomProcessingErrorProbability")))
            {
                string message = "Congratulations! A random processing error occurred!";
                context.Logger.LogLine(message);
                ////////////////////////////////////////////////////////////
                //
                // TODO: Return 'RandomProcessingError' error
                ///
                /////////////////////////////////////////////////////////////
            }
            Table    table;
            Document document;

            try
            {
                table    = Table.LoadTable(dbClient, Environment.GetEnvironmentVariable("DDBTableName"));
                document = await table.GetItemAsync(payload.numberPlate.numberPlateString);

                if (document != null)
                {
                    if (float.Parse(document["credit"]) > payload.charge)
                    {
                        var item = document;
                        item["credit"] = float.Parse(document["credit"]) - payload.charge;
                        Expression expr = new Expression();
                        expr.ExpressionStatement = "credit >= :charge";
                        expr.ExpressionAttributeValues[":charge"] = payload.charge;

                        // Optional parameters.
                        UpdateItemOperationConfig config = new UpdateItemOperationConfig
                        {
                            ConditionalExpression = expr,
                            ReturnValues          = ReturnValues.AllNewAttributes
                        };
                        Document updatedRecord = await table.UpdateItemAsync(item, config);

                        //
                        // Success!
                        //
                        context.Logger.LogLine("Charge of $$" + payload.charge + " deducted from credit for " + payload.numberPlate.numberPlateString);
                    }
                    else
                    {
                        string message = "Driver for number plate " + payload.numberPlate.numberPlateString + "(" + document["ownerFirstName"] + ")" + document["ownerLastName"] + ") has insufficient credit (" + document["credit"] + ") for a charge of " + payload.charge;
                        context.Logger.LogLine(message);
                        /////////////////////////////////////////////////////////////
                        //
                        // TODO: Return 'InsufficientCreditError' error
                        //
                        /////////////////////////////////////////////////////////////
                    }
                }
                else
                {
                    string message = "Number plate " + payload.numberPlate.numberPlateString + "was not found. This will require manual resolution.";
                    context.Logger.LogLine(message);
                    /////////////////////////////////////////////////////////////
                    //
                    // TODO: Return 'UnknownNumberPlateError' error
                    //
                    /////////////////////////////////////////////////////////////
                }
            }
            catch (AmazonDynamoDBException e)
            {
                context.Logger.LogLine(e.StackTrace);
                ////////////////////////////////////////////////////////////
                //
                // TODO: Return 'DatabaseAccessError' error
                ///
                /////////////////////////////////////////////////////////////
            }
            catch (Exception e)
            {
                context.Logger.LogLine(e.StackTrace);
                ////////////////////////////////////////////////////////////
                //
                // TODO: Return 'GenericError' error
                ///
                /////////////////////////////////////////////////////////////
            }

            return(payload);
        }