Exemplo n.º 1
0
        /// <summary>
        /// Invokes the function.
        /// </summary>
        /// <param name="req"><see cref="HttpRequestMessage"/> instance.</param>
        /// <param name="log"><see cref="TraceWriter"/> instance.</param>
        /// <returns>Returns the <see cref="HttpResponseMessage"/> instance.</returns>
        public static async Task <HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");

            var request = await req.Content
                          .ReadAsAsync <CreateProductRequestModel>()
                          .ConfigureAwait(false);

            var response = new CreateProductResponseModel()
            {
                ProductId          = Guid.NewGuid(),
                ProductName        = request.ProductName,
                ProductDescription = request.ProductDescription,
                ProductPrice       = request.ProductPrice
            };

            var settings = new JsonSerializerSettings()
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                Formatting       = Formatting.Indented
            };
            var formatter = new JsonMediaTypeFormatter()
            {
                SerializerSettings = settings
            };

            return(req.CreateResponse(HttpStatusCode.Created, response, formatter));
        }
Exemplo n.º 2
0
        public async Task <CreateProductResponseModel> CreateProduct(CreateProductRequestModel productRequestModel)
        {
            if (productRequestModel == null)
            {
                throw new ArgumentNullException(nameof(productRequestModel));
            }

            var createProductResponseModel = new CreateProductResponseModel();

            try
            {
                await _inMemoryStorage.Save(
                    new Product
                {
                    Id          = productRequestModel.Id,
                    CreateDate  = DateTime.Now,
                    Name        = productRequestModel.Name,
                    CategoryId  = productRequestModel.CategoryId,
                    Description = productRequestModel.Description,
                    UserId      = productRequestModel.UserId,
                }, true)
                .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                createProductResponseModel.ErrorMessage = ex.Message;
            }

            return(createProductResponseModel);
        }
        /// <summary>
        /// Invokes the function.
        /// </summary>
        /// <param name="req"><see cref="HttpRequestMessage"/> instance.</param>
        /// <param name="productId">Product Id.</param>
        /// <param name="log"><see cref="TraceWriter"/> instance.</param>
        /// <returns>Returns the <see cref="HttpResponseMessage"/> instance.</returns>
        public static async Task <HttpResponseMessage> Run(HttpRequestMessage req, string productId, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");

            var response = new CreateProductResponseModel()
            {
                ProductId          = Guid.Parse(productId),
                ProductName        = "Product A",
                ProductDescription = "This is Product A",
                ProductPrice       = 123.45M
            };

            var settings = new JsonSerializerSettings()
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                Formatting       = Formatting.Indented
            };
            var formatter = new JsonMediaTypeFormatter()
            {
                SerializerSettings = settings
            };

            return(req.CreateResponse(HttpStatusCode.OK, response, formatter));
        }