Skip to content

Instantly share code, notes, and snippets.

@phwt
Created November 27, 2021 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phwt/7728bab1483bc4cf8cfb5d10ad3bdc51 to your computer and use it in GitHub Desktop.
Save phwt/7728bab1483bc4cf8cfb5d10ad3bdc51 to your computer and use it in GitHub Desktop.

Here's my implementation of the accepted answer below with a slight difference:

  • Wrapped the fields/classes in #region to make it collapsible
  • Implement only the Route part as I still want to keep some values at the method.
  • Avoid string concatenation as I want the values to be readable.
using Operation = Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes.OpenApiOperationAttribute;
using Parameter = Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes.OpenApiParameterAttribute;
using RequestBody = Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes.OpenApiRequestBodyAttribute;
using ResponseBody = Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes.OpenApiResponseWithBodyAttribute;

namespace AzureFunctionsREST.API.Functions
{

    public class ReporterFunction : BaseFunction
    {
        #region Metadata
        private const string DefaultContentType = "application/json";
        private static class Route
        {
            public const string ParameterName = "reporterId";
            public const string Base = "reporter";
            public const string WithParameter = "reporter/{reporterId:required}";
        }
        #endregion

        private readonly IReporterRepository _reporterRepository;

        public ReporterFunction(IReporterRepository reporterRepository)
        {
            _reporterRepository = reporterRepository;
        }

        [Function("ReporterList")]
        [Operation(tags: Route.Base, Summary = "Retrieve all reporters")]
        [ResponseBody(statusCode: HttpStatusCode.OK, contentType: DefaultContentType, bodyType: typeof(Reporter[]), Description = "All reporters")]
        public async Task<HttpResponseData> List([HttpTrigger(AuthorizationLevel.Function, "get", Route = Route.Base)] HttpRequestData req,
                                                 FunctionContext executionContext)
        {
            // ...
        }

        [Function("ReporterGet")]
        [Operation(tags: Route.Base, Summary = "Retrieve reporter")]
        [Parameter(name: Route.ParameterName, In = ParameterLocation.Path, Required = true, Type = typeof(string))]
        [ResponseBody(statusCode: HttpStatusCode.OK, contentType: DefaultContentType, bodyType: typeof(Reporter), Description = "Matched reporter")]
        public async Task<HttpResponseData> Get([HttpTrigger(AuthorizationLevel.Function, "get", Route = Route.WithParameter)] HttpRequestData req,
                                                FunctionContext executionContext)
        {
            // ...
        }

        [Function("ReporterPost")]
        [Operation(tags: Route.Base, Summary = "Create a new reporter")]
        [RequestBody(contentType: DefaultContentType, bodyType: typeof(ReporterRequest))]
        [ResponseBody(statusCode: HttpStatusCode.Created, contentType: DefaultContentType, bodyType: typeof(Reporter), Description = "Created reporter")]
        public async Task<HttpResponseData> Post([HttpTrigger(AuthorizationLevel.Function, "post", Route = Route.Base)] HttpRequestData req,
                                                 FunctionContext executionContext)
        {
            // ...
        }

        [Function("ReporterPut")]
        [Operation(tags: Route.Base, Summary = "Update a reporter")]
        [Parameter(name: Route.ParameterName, In = ParameterLocation.Path, Required = true, Type = typeof(string))]
        [RequestBody(contentType: DefaultContentType, bodyType: typeof(ReporterRequest))]
        [ResponseBody(statusCode: HttpStatusCode.OK, contentType: DefaultContentType, bodyType: typeof(Reporter), Description = "Updated reporter")]
        public async Task<HttpResponseData> Put([HttpTrigger(AuthorizationLevel.Function, "put", Route = Route.WithParameter)] HttpRequestData req,
                                                 FunctionContext executionContext)
        {
            // ...
        }

        [Function("ReporterDelete")]
        [Operation(tags: Route.Base, Summary = "Delete a reporter")]
        [Parameter(name: Route.ParameterName, In = ParameterLocation.Path, Required = true, Type = typeof(string))]
        [ResponseBody(statusCode: HttpStatusCode.OK, contentType: DefaultContentType, bodyType: typeof(Reporter), Description = "Deleted reporter")]
        public async Task<HttpResponseData> Delete([HttpTrigger(AuthorizationLevel.Function, "delete", Route = Route.WithParameter)] HttpRequestData req,
                                                   FunctionContext executionContext)
        {
            // ...
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment