Skip to content

Commit f9e82b8

Browse files
use the same Function code from .NET 6 for .NET 8
1 parent 938a758 commit f9e82b8

File tree

4 files changed

+159
-221
lines changed

4 files changed

+159
-221
lines changed

src/NET8/DeleteProduct/Function.cs

+43-59
Original file line numberDiff line numberDiff line change
@@ -3,85 +3,69 @@
33
using System.Net;
44
using System.Net.Http;
55
using System.Text.Json;
6-
using System.Text.Json.Serialization;
76
using System.Threading.Tasks;
7+
88
using Amazon.Lambda.APIGatewayEvents;
99
using Amazon.Lambda.Core;
10-
using Amazon.Lambda.RuntimeSupport;
11-
using Amazon.Lambda.Serialization.SystemTextJson;
12-
using Amazon.XRay.Recorder.Handlers.AwsSdk;
13-
using Shared;
1410
using Shared.DataAccess;
1511

16-
public class Function
17-
{
18-
private static ProductsDAO dataAccess;
12+
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
1913

20-
static Function()
21-
{
22-
AWSSDKHandler.RegisterXRayForAllServices();
23-
dataAccess = new DynamoDbProducts();
24-
}
25-
26-
/// <summary>
27-
/// The main entry point for the custom runtime.
28-
/// </summary>
29-
/// <param name="args"></param>
30-
private static async Task Main()
31-
{
32-
Func<APIGatewayHttpApiV2ProxyRequest, ILambdaContext, Task<APIGatewayHttpApiV2ProxyResponse>> handler = FunctionHandler;
33-
await LambdaBootstrapBuilder.Create(handler, new SourceGeneratorLambdaJsonSerializer<CustomJsonSerializerContext>(options => {
34-
options.PropertyNameCaseInsensitive = true;
35-
}))
36-
.Build()
37-
.RunAsync();
38-
}
39-
40-
public static async Task<APIGatewayHttpApiV2ProxyResponse> FunctionHandler(APIGatewayHttpApiV2ProxyRequest apigProxyEvent, ILambdaContext context)
14+
namespace DeleteProduct
15+
{
16+
public class Function
4117
{
42-
if (!apigProxyEvent.RequestContext.Http.Method.Equals(HttpMethod.Delete.Method))
18+
private readonly ProductsDAO dataAccess;
19+
public Function()
4320
{
44-
return new APIGatewayHttpApiV2ProxyResponse
45-
{
46-
Body = "Only DELETE allowed",
47-
StatusCode = (int)HttpStatusCode.MethodNotAllowed,
48-
};
21+
this.dataAccess = new DynamoDbProducts();
4922
}
5023

51-
try
24+
public async Task<APIGatewayHttpApiV2ProxyResponse> FunctionHandler(APIGatewayHttpApiV2ProxyRequest apigProxyEvent,
25+
ILambdaContext context)
5226
{
53-
context.Logger.LogLine(JsonSerializer.Serialize(apigProxyEvent, CustomJsonSerializerContext.Default.APIGatewayHttpApiV2ProxyRequest));
54-
55-
var id = apigProxyEvent.PathParameters["id"];
56-
57-
var product = await dataAccess.GetProduct(id);
58-
59-
if (product == null)
27+
if (!apigProxyEvent.RequestContext.Http.Method.Equals(HttpMethod.Delete.Method))
6028
{
6129
return new APIGatewayHttpApiV2ProxyResponse
6230
{
63-
Body = "Not Found",
64-
StatusCode = (int)HttpStatusCode.NotFound,
31+
Body = "Only DELETE allowed",
32+
StatusCode = (int)HttpStatusCode.MethodNotAllowed,
6533
};
6634
}
6735

68-
await dataAccess.DeleteProduct(product.Id);
69-
70-
return new APIGatewayHttpApiV2ProxyResponse
36+
try
7137
{
72-
StatusCode = (int)HttpStatusCode.OK,
73-
Body = $"Product with id {id} deleted"
74-
};
75-
}
76-
catch (Exception e)
77-
{
78-
context.Logger.LogError($"Error deleting product {e.Message} {e.StackTrace}");
38+
var id = apigProxyEvent.PathParameters["id"];
39+
40+
var product = await dataAccess.GetProduct(id);
41+
42+
if (product == null)
43+
{
44+
return new APIGatewayHttpApiV2ProxyResponse
45+
{
46+
Body = "Not Found",
47+
StatusCode = (int)HttpStatusCode.NotFound,
48+
};
49+
}
7950

80-
return new APIGatewayHttpApiV2ProxyResponse
51+
await dataAccess.DeleteProduct(product.Id);
52+
53+
return new APIGatewayHttpApiV2ProxyResponse
54+
{
55+
StatusCode = (int)HttpStatusCode.OK,
56+
Body = $"Product with id {id} deleted"
57+
};
58+
}
59+
catch (Exception e)
8160
{
82-
Body = "Not Found",
83-
StatusCode = (int)HttpStatusCode.InternalServerError,
84-
};
61+
context.Logger.LogLine($"Error deleting product {e.Message} {e.StackTrace}");
62+
63+
return new APIGatewayHttpApiV2ProxyResponse
64+
{
65+
Body = "Not Found",
66+
StatusCode = (int)HttpStatusCode.InternalServerError,
67+
};
68+
}
8569
}
8670
}
8771
}

src/NET8/GetProduct/Function.cs

+43-57
Original file line numberDiff line numberDiff line change
@@ -3,82 +3,68 @@
33
using System.Net;
44
using System.Net.Http;
55
using System.Text.Json;
6-
using System.Text.Json.Serialization;
76
using System.Threading.Tasks;
7+
88
using Amazon.Lambda.APIGatewayEvents;
99
using Amazon.Lambda.Core;
10-
using Amazon.Lambda.RuntimeSupport;
11-
using Amazon.Lambda.Serialization.SystemTextJson;
12-
using Amazon.XRay.Recorder.Handlers.AwsSdk;
13-
using Shared;
1410
using Shared.DataAccess;
1511

16-
public class Function
17-
{
18-
private static ProductsDAO dataAccess;
19-
20-
static Function()
21-
{
22-
AWSSDKHandler.RegisterXRayForAllServices();
23-
dataAccess = new DynamoDbProducts();
24-
}
25-
26-
/// <summary>
27-
/// The main entry point for the custom runtime.
28-
/// </summary>
29-
/// <param name="args"></param>
30-
private static async Task Main()
31-
{
32-
Func<APIGatewayHttpApiV2ProxyRequest, ILambdaContext, Task<APIGatewayHttpApiV2ProxyResponse>> handler = FunctionHandler;
33-
await LambdaBootstrapBuilder.Create(handler, new SourceGeneratorLambdaJsonSerializer<CustomJsonSerializerContext>(options => {
34-
options.PropertyNameCaseInsensitive = true;
35-
}))
36-
.Build()
37-
.RunAsync();
38-
}
12+
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
3913

40-
public static async Task<APIGatewayHttpApiV2ProxyResponse> FunctionHandler(APIGatewayHttpApiV2ProxyRequest apigProxyEvent, ILambdaContext context)
14+
namespace GetProduct
15+
{
16+
public class Function
4117
{
42-
if (!apigProxyEvent.RequestContext.Http.Method.Equals(HttpMethod.Get.Method))
18+
private readonly ProductsDAO dataAccess;
19+
public Function()
4320
{
44-
return new APIGatewayHttpApiV2ProxyResponse
45-
{
46-
Body = "Only GET allowed",
47-
StatusCode = (int)HttpStatusCode.MethodNotAllowed,
48-
};
21+
this.dataAccess = new DynamoDbProducts();
4922
}
5023

51-
try
24+
public async Task<APIGatewayHttpApiV2ProxyResponse> FunctionHandler(APIGatewayHttpApiV2ProxyRequest apigProxyEvent,
25+
ILambdaContext context)
5226
{
53-
var id = apigProxyEvent.PathParameters["id"];
54-
55-
var product = await dataAccess.GetProduct(id);
56-
57-
if (product == null)
27+
if (!apigProxyEvent.RequestContext.Http.Method.Equals(HttpMethod.Get.Method))
5828
{
5929
return new APIGatewayHttpApiV2ProxyResponse
6030
{
61-
Body = "Not Found",
62-
StatusCode = (int)HttpStatusCode.NotFound,
31+
Body = "Only GET allowed",
32+
StatusCode = (int)HttpStatusCode.MethodNotAllowed,
6333
};
6434
}
6535

66-
return new APIGatewayHttpApiV2ProxyResponse
36+
try
6737
{
68-
StatusCode = (int)HttpStatusCode.OK,
69-
Body = JsonSerializer.Serialize(product, CustomJsonSerializerContext.Default.Product),
70-
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
71-
};
72-
}
73-
catch (Exception e)
74-
{
75-
context.Logger.LogError($"Error getting product {e.Message} {e.StackTrace}");
38+
var id = apigProxyEvent.PathParameters["id"];
39+
40+
var product = await dataAccess.GetProduct(id);
41+
42+
if (product == null)
43+
{
44+
return new APIGatewayHttpApiV2ProxyResponse
45+
{
46+
Body = "Not Found",
47+
StatusCode = (int)HttpStatusCode.NotFound,
48+
};
49+
}
7650

77-
return new APIGatewayHttpApiV2ProxyResponse
51+
return new APIGatewayHttpApiV2ProxyResponse
52+
{
53+
StatusCode = (int)HttpStatusCode.OK,
54+
Body = JsonSerializer.Serialize(product),
55+
Headers = new Dictionary<string, string> {{"Content-Type", "application/json"}}
56+
};
57+
}
58+
catch (Exception e)
7859
{
79-
Body = "Not Found",
80-
StatusCode = (int)HttpStatusCode.InternalServerError,
81-
};
60+
context.Logger.LogLine($"Error getting product {e.Message} {e.StackTrace}");
61+
62+
return new APIGatewayHttpApiV2ProxyResponse
63+
{
64+
Body = "Not Found",
65+
StatusCode = (int)HttpStatusCode.InternalServerError,
66+
};
67+
}
8268
}
8369
}
84-
}
70+
}

src/NET8/GetProducts/Function.cs

+29-45
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,48 @@
33
using System.Net;
44
using System.Net.Http;
55
using System.Text.Json;
6-
using System.Text.Json.Serialization;
76
using System.Threading.Tasks;
7+
88
using Amazon.Lambda.APIGatewayEvents;
99
using Amazon.Lambda.Core;
10-
using Amazon.Lambda.RuntimeSupport;
11-
using Amazon.Lambda.Serialization.SystemTextJson;
12-
using Amazon.XRay.Recorder.Handlers.AwsSdk;
13-
using Shared;
1410
using Shared.DataAccess;
1511

16-
namespace GetProducts;
12+
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
1713

18-
public class Function
14+
namespace GetProducts
1915
{
20-
static ProductsDAO dataAccess;
21-
22-
static Function()
16+
public class Function
2317
{
24-
AWSSDKHandler.RegisterXRayForAllServices();
25-
dataAccess = new DynamoDbProducts();
26-
}
27-
28-
/// <summary>
29-
/// The main entry point for the custom runtime.
30-
/// </summary>
31-
/// <param name="args"></param>
32-
private static async Task Main()
33-
{
34-
Func<APIGatewayHttpApiV2ProxyRequest, ILambdaContext, Task<APIGatewayHttpApiV2ProxyResponse>> handler = FunctionHandler;
35-
await LambdaBootstrapBuilder.Create(handler, new SourceGeneratorLambdaJsonSerializer<CustomJsonSerializerContext>(options => {
36-
options.PropertyNameCaseInsensitive = true;
37-
}))
38-
.Build()
39-
.RunAsync();
40-
}
18+
private readonly ProductsDAO dataAccess;
19+
public Function()
20+
{
21+
this.dataAccess = new DynamoDbProducts();
22+
}
4123

42-
public static async Task<APIGatewayHttpApiV2ProxyResponse> FunctionHandler(APIGatewayHttpApiV2ProxyRequest apigProxyEvent, ILambdaContext context)
43-
{
44-
if (!apigProxyEvent.RequestContext.Http.Method.Equals(HttpMethod.Get.Method))
24+
public async Task<APIGatewayHttpApiV2ProxyResponse> FunctionHandler(APIGatewayHttpApiV2ProxyRequest apigProxyEvent,
25+
ILambdaContext context)
4526
{
27+
if (!apigProxyEvent.RequestContext.Http.Method.Equals(HttpMethod.Get.Method))
28+
{
29+
return new APIGatewayHttpApiV2ProxyResponse
30+
{
31+
Body = "Only GET allowed",
32+
StatusCode = (int)HttpStatusCode.MethodNotAllowed,
33+
};
34+
}
35+
36+
context.Logger.LogLine($"Received {apigProxyEvent}");
37+
38+
var products = await dataAccess.GetAllProducts();
39+
40+
context.Logger.LogLine($"Found {products.Products.Count} product(s)");
41+
4642
return new APIGatewayHttpApiV2ProxyResponse
4743
{
48-
Body = "Only GET allowed",
49-
StatusCode = (int)HttpStatusCode.MethodNotAllowed,
44+
Body = JsonSerializer.Serialize(products),
45+
StatusCode = 200,
46+
Headers = new Dictionary<string, string> {{"Content-Type", "application/json"}}
5047
};
5148
}
52-
53-
context.Logger.LogInformation($"Received {apigProxyEvent}");
54-
55-
var products = await dataAccess.GetAllProducts();
56-
57-
context.Logger.LogInformation($"Found {products.Products.Count} product(s)");
58-
59-
return new APIGatewayHttpApiV2ProxyResponse
60-
{
61-
Body = JsonSerializer.Serialize(products, CustomJsonSerializerContext.Default.ProductWrapper),
62-
StatusCode = 200,
63-
Headers = new Dictionary<string, string> {{"Content-Type", "application/json"}}
64-
};
6549
}
6650
}

0 commit comments

Comments
 (0)