using Amazon.Lambda.Core;
using Amazon.Lambda.Annotations;
using Amazon.Lambda.Annotations.APIGateway;
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace BlueprintBaseName._1;
///
/// A collection of sample Lambda functions that provide a REST api for doing simple math calculations.
///
public class Functions
{
private ICalculatorService _calculatorService;
///
/// Default constructor.
///
///
/// The implementation that we
/// instantiated in will be injected here.
///
/// As an alternative, a dependency could be injected into each
/// Lambda function handler via the [FromServices] attribute.
///
public Functions(ICalculatorService calculatorService)
{
_calculatorService = calculatorService;
}
///
/// Root route that provides information about the other requests that can be made.
///
/// API descriptions.
[LambdaFunction()]
[HttpApi(LambdaHttpMethod.Get, "/")]
public string Default()
{
var docs = @"Lambda Calculator Home:
You can make the following requests to invoke other Lambda functions perform calculator operations:
/add/{x}/{y}
/subtract/{x}/{y}
/multiply/{x}/{y}
/divide/{x}/{y}
";
return docs;
}
///
/// Perform x + y
///
///
///
/// Sum of x and y.
[LambdaFunction()]
[HttpApi(LambdaHttpMethod.Get, "/add/{x}/{y}")]
public int Add(int x, int y, ILambdaContext context)
{
var sum = _calculatorService.Add(x, y);
context.Logger.LogInformation($"{x} plus {y} is {sum}");
return sum;
}
///
/// Perform x - y.
///
///
///
/// x subtract y
[LambdaFunction()]
[HttpApi(LambdaHttpMethod.Get, "/subtract/{x}/{y}")]
public int Subtract(int x, int y, ILambdaContext context)
{
var difference = _calculatorService.Subtract(x, y);
context.Logger.LogInformation($"{x} subtract {y} is {difference}");
return difference;
}
///
/// Perform x * y.
///
///
///
/// x multiply y
[LambdaFunction()]
[HttpApi(LambdaHttpMethod.Get, "/multiply/{x}/{y}")]
public int Multiply(int x, int y, ILambdaContext context)
{
var product = _calculatorService.Multiply(x, y);
context.Logger.LogInformation($"{x} multiplied by {y} is {product}");
return product;
}
///
/// Perform x / y.
///
///
///
/// x divide y
[LambdaFunction()]
[HttpApi(LambdaHttpMethod.Get, "/divide/{x}/{y}")]
public int Divide(int x, int y, ILambdaContext context)
{
var quotient = _calculatorService.Divide(x, y);
context.Logger.LogInformation($"{x} divided by {y} is {quotient}");
return quotient;
}
}