-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function layer to process HTTP request
- Loading branch information
Showing
5 changed files
with
137 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/AglCodingTest.Functions/AglCodingTestHttpTriggerFunction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using AglCodingTest.Extensions; | ||
using AglCodingTest.Models; | ||
using AglCodingTest.Services; | ||
using AglCodingTest.Services.ServiceOptions; | ||
using AglCodingTest.Settings; | ||
|
||
namespace AglCodingTest.Functions | ||
{ | ||
/// <summary> | ||
/// This represents the function entity to process for AGL coding test. | ||
/// </summary> | ||
public class AglCodingTestHttpTriggerFunction : IAglCodingTestHttpTriggerFunction | ||
{ | ||
private readonly AppSettings _appSettings; | ||
private readonly IAglPayloadLoadingService _service; | ||
|
||
private bool _disposed; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AglCodingTestHttpTriggerFunction"/> class. | ||
/// </summary> | ||
/// <param name="appSettings"><see cref="AppSettings"/> instance.</param> | ||
/// <param name="service"><see cref="IAglPayloadLoadingService"/> instance.</param> | ||
public AglCodingTestHttpTriggerFunction(AppSettings appSettings, IAglPayloadLoadingService service) | ||
{ | ||
this._appSettings = appSettings.ThrowIfNullOrDefault(); | ||
this._service = service.ThrowIfNullOrDefault(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<object> InvokeAsync<TIn, TOptions>(TIn input, TOptions options) | ||
where TOptions : ServiceOptionsBase | ||
{ | ||
var req = input as HttpRequestMessage; | ||
req.ThrowIfNullOrDefault(); | ||
|
||
options.ThrowIfNullOrDefault(); | ||
|
||
var serviceOptions = options as AglPayloadLoadingServiceOptions; | ||
serviceOptions.ThrowIfNullOrDefault(); | ||
|
||
await this._service.InvokeAsync(serviceOptions).ConfigureAwait(false); | ||
|
||
var catOwners = serviceOptions.People | ||
.Where(p => p.Pets.Any(q => q.PetType == PetType.Cat)) | ||
.OrderBy(p => p.GenderType) | ||
.Select(p => new { Gender = p.GenderType, Names = p.Pets.Where(q => q.PetType == PetType.Cat).OrderBy(q => q.Name).Select(q => q.Name) }) | ||
.Select(p => $"<h1>{p.Gender}</h1><ul>{string.Join(string.Empty, p.Names.Select(q => $"<li>{q}</li>"))}</ul>") | ||
.ToList(); | ||
|
||
var html = new StringBuilder(); | ||
html.AppendLine("<html><body>"); | ||
html.AppendLine(string.Join(string.Empty, catOwners)); | ||
html.AppendLine("</body></html>"); | ||
|
||
var res = req.CreateResponse(HttpStatusCode.OK, html.ToString(), "text/html"); | ||
|
||
return res; | ||
} | ||
|
||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
if (this._disposed) | ||
{ | ||
return; | ||
} | ||
|
||
this._disposed = true; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
src/AglCodingTest.Functions/IAglCodingTestHttpTriggerFunction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace AglCodingTest.Functions | ||
{ | ||
/// <summary> | ||
/// This provides interfaces to the <see cref="AglCodingTestHttpTriggerFunction"/> class. | ||
/// </summary> | ||
public interface IAglCodingTestHttpTriggerFunction : IFunction | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
using AglCodingTest.Services.ServiceOptions; | ||
|
||
namespace AglCodingTest.Functions | ||
{ | ||
/// <summary> | ||
/// This provides interfaces to all classes in the function layer. | ||
/// </summary> | ||
public interface IFunction : IDisposable | ||
{ | ||
/// <summary> | ||
/// Invokes the function. | ||
/// </summary> | ||
/// <typeparam name="TIn">Type of input instance.</typeparam> | ||
/// <typeparam name="TOptions">Type of service options instance.</typeparam> | ||
/// <param name="input"><see cref="TIn"/> instance.</param> | ||
/// <param name="options"><see cref="TOptions"/> instance.</param> | ||
/// <returns>Returns <see cref="TOut"/> instance.</returns> | ||
Task<object> InvokeAsync<TIn, TOptions>(TIn input, TOptions options) | ||
where TOptions : ServiceOptionsBase; | ||
} | ||
} |