public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            try
            {
                log.LogInformation("C# HTTP trigger function processed a request.");

                var requestBody = await new StreamReader(req.Body).ReadToEndAsync();

                var requestBodyParsed = JObject.Parse(requestBody);

                ConsumerOperationsBodyValidator.ValidateBody(requestBodyParsed);

                var contractAddress = requestBodyParsed.Value <string>("contractAddress");

                var userId = requestBodyParsed.Value <string>("userId");

                var userProfile = await _userProfileService.GetUserProfile(userId);

                var keyStoreString = JsonConvert.SerializeObject(userProfile.UserKeyStore);

                var web3 = Web3Service.Initialize(keyStoreString, userProfile.Password);

                var retailerService = new RetailerService(web3, contractAddress);

                var createOrder = JsonConvert.DeserializeObject <CreateOrderFunction>(requestBody);
                createOrder.ClientEmail = userProfile.Email;

                var receiptForCreateOrderFunctionCall = await retailerService.CreateOrderRequestAndWaitForReceiptAsync(createOrder);

                var orderCreatedEvent = receiptForCreateOrderFunctionCall.DecodeAllEvents <OrderCreatedEventDTO>().FirstOrDefault().Event;

                log.LogInformation("Event:");
                log.LogInformation($" Message: {orderCreatedEvent.Message}");
                log.LogInformation($" Order ID: {orderCreatedEvent.OrderId}");
                log.LogInformation($" Status: {orderCreatedEvent.Status}");

                log.LogInformation($"Finished storing an int: Tx Hash: {receiptForCreateOrderFunctionCall.TransactionHash}");
                log.LogInformation($"Finished storing an int: Tx Status: {receiptForCreateOrderFunctionCall.Status.Value}");

                return(new OkObjectResult(orderCreatedEvent.OrderId));
            }
            catch (Exception e)
            {
                log.LogInformation($"The following Exception was raised in CreateOrder: {e.Message}");

                if (e is UserProfileException || e is SmartContractRevertException || e is RpcResponseException)
                {
                    return(new ContentResult
                    {
                        StatusCode = 401,
                        Content = Messages.UnAuthorizedMessage
                    });
                }

                return(new BadRequestObjectResult(e.Message));
            }
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            try
            {
                var requestBody = await new StreamReader(req.Body).ReadToEndAsync();

                log.LogInformation($"Request body: {requestBody}");

                var requestBodyParsed = JObject.Parse(requestBody);

                ConsumerOperationsBodyValidator.ValidateBody(requestBodyParsed);

                var contractAddress = requestBodyParsed.Value <string>("contractAddress");

                var userId = requestBodyParsed.Value <string>("userId");

                var userProfile = await _userProfileService.GetUserProfile(userId);

                var keyStoreString = JsonConvert.SerializeObject(userProfile.UserKeyStore);

                var web3 = Web3Service.Initialize(keyStoreString, userProfile.Password);

                var retailerService = new RetailerService(web3, contractAddress);

                var getOrderStatus = JsonConvert.DeserializeObject <GetOrderStatusFunction>(requestBody);

                var orderStatus = await retailerService.GetOrderStatusQueryAsync(getOrderStatus);

                log.LogInformation($"Order status: {orderStatus}");

                return(new OkObjectResult(new OrderStatusResponse
                {
                    status = orderStatus,
                    details = ((OrderStatus)orderStatus).ToString()
                }));
            }
            catch (Exception e)
            {
                log.LogInformation($"The following Exception was raised in GetOrderStatus: {e.Message}");

                if (e is UserProfileException || e is SmartContractRevertException || e is RpcResponseException)
                {
                    return(new ContentResult
                    {
                        StatusCode = 401,
                        Content = Messages.UnAuthorizedMessage
                    });
                }

                return(new BadRequestObjectResult(e.Message));
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            try
            {
                var requestBody = await new StreamReader(req.Body).ReadToEndAsync();

                log.LogInformation($"Request body: {requestBody}");

                var requestBodyParsed = JObject.Parse(requestBody);

                BaseBodyValidator.ValidateBody(requestBodyParsed);

                var contractAddress = requestBodyParsed.Value <string>("contractAddress");

                var web3 = Web3Service.Initialize();

                var stockService = new StockService(web3, contractAddress);

                var createItem = JsonConvert.DeserializeObject <CreateItemFunction>(requestBody);

                var receiptForCreateItemFunctionCall = await stockService.CreateItemRequestAndWaitForReceiptAsync(createItem);

                var itemCreatedEvent = receiptForCreateItemFunctionCall.DecodeAllEvents <ItemCreatedEventDTO>().FirstOrDefault().Event;

                log.LogInformation("Event:");
                log.LogInformation($" Message: {itemCreatedEvent.Message}");
                log.LogInformation($" Item ID: {itemCreatedEvent.Id}");

                return(new OkObjectResult(itemCreatedEvent.Id));
            }
            catch (Exception e)
            {
                log.LogInformation($"The following Exception was raised in CreateItem: {e.Message}");

                if (e is SmartContractRevertException || e is RpcResponseException)
                {
                    return(new ContentResult
                    {
                        StatusCode = 401,
                        Content = Messages.UnAuthorizedMessage
                    });
                }

                return(new BadRequestObjectResult(e.Message));
            }
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            try
            {
                var requestBody = await new StreamReader(req.Body).ReadToEndAsync();

                log.LogInformation($"Request body: {requestBody}");

                var requestBodyParsed = JObject.Parse(requestBody);

                BaseBodyValidator.ValidateBody(requestBodyParsed);

                var contractAddress = requestBodyParsed.Value <string>("contractAddress");

                var web3 = Web3Service.Initialize();

                var retailerService = new RetailerService(web3, contractAddress);

                var checkOrderItemsAvailability = JsonConvert.DeserializeObject <CheckOrderItemsAvailabilityFunction>(requestBody);

                await retailerService.CheckOrderItemsAvailabilityQueryAsync(checkOrderItemsAvailability);

                log.LogInformation($"Order {checkOrderItemsAvailability.OrderId} availability checked.");

                return(new OkResult());
            }
            catch (Exception e)
            {
                log.LogInformation($"The following Exception was raised in CheckOrderItemsAvailability: {e.Message}");

                if (e is SmartContractRevertException || e is RpcResponseException)
                {
                    return(new ContentResult
                    {
                        StatusCode = 401,
                        Content = Messages.UnAuthorizedMessage
                    });
                }

                return(new BadRequestObjectResult(e.Message));
            }
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            try
            {
                var requestBody = await new StreamReader(req.Body).ReadToEndAsync();

                log.LogInformation($"Request body: {requestBody}");

                var requestBodyParsed = JObject.Parse(requestBody);

                BaseBodyValidator.ValidateBody(requestBodyParsed);

                var contractAddress = requestBodyParsed.Value <string>("contractAddress");

                var web3 = Web3Service.Initialize();

                var stockService = new StockService(web3, contractAddress);

                var updateItemCategory = JsonConvert.DeserializeObject <UpdateItemCategoryFunction>(requestBody);

                await stockService.UpdateItemCategoryRequestAsync(updateItemCategory);

                log.LogInformation($"Category of the Item {updateItemCategory.Id} updated to: {updateItemCategory.Category}");

                return(new OkResult());
            }
            catch (Exception e)
            {
                log.LogInformation($"The following Exception was raised in UpdateItemCategory: {e.Message}");

                if (e is SmartContractRevertException || e is RpcResponseException)
                {
                    return(new ContentResult
                    {
                        StatusCode = 401,
                        Content = Messages.UnAuthorizedMessage
                    });
                }

                return(new BadRequestObjectResult(e.Message));
            }
        }
Esempio n. 6
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            log.LogInformation($"Request body: {requestBody}");

            var requestBodyParsed = JObject.Parse(requestBody);
            var contractAddress   = requestBodyParsed.Value <string>("contractAddress");

            var userId = requestBodyParsed.Value <string>("userId");
            var web3   = Web3Service.Initialize();

            var retailerService = new RetailerService(web3, contractAddress);

            var updateMinItemsPerOrder = JsonConvert.DeserializeObject <UpdateMinItemsPerOrderFunction>(requestBody);

            try
            {
                await retailerService.UpdateMinItemsPerOrderRequestAsync(updateMinItemsPerOrder);

                log.LogInformation($"The minimum amount allowed of an item has been updated to: {updateMinItemsPerOrder.MinItems}");

                return(new OkResult());
            }
            catch (Exception e)
            {
                log.LogInformation($"The following Exception was raised in UpdateMinItemsPerOrder: {e.Message}");

                if (e is SmartContractRevertException || e is RpcResponseException)
                {
                    return(new ContentResult
                    {
                        StatusCode = 401,
                        Content = Messages.UnAuthorizedMessage
                    });
                }

                return(new BadRequestObjectResult(e.Message));
            }
        }
Esempio n. 7
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            try
            {
                var requestBody = await new StreamReader(req.Body).ReadToEndAsync();

                log.LogInformation($"Request body: {requestBody}");

                var requestBodyParsed = JObject.Parse(requestBody);

                BaseBodyValidator.ValidateBody(requestBodyParsed);

                var contractAddress = requestBodyParsed.Value <string>("contractAddress");

                var web3 = Web3Service.Initialize();

                var retailerService = new RetailerService(web3, contractAddress);

                var newOrders = await retailerService.GetNewOrdersQueryAsync();

                log.LogInformation($"The amount of new orders is: {newOrders.Count}");

                return(new OkObjectResult(newOrders));
            }
            catch (Exception e)
            {
                log.LogInformation($"The following Exception was raised in GetNewOrders: {e.Message}");

                if (e is SmartContractRevertException || e is RpcResponseException)
                {
                    return(new ContentResult
                    {
                        StatusCode = 401,
                        Content = Messages.UnAuthorizedMessage
                    });
                }

                return(new BadRequestObjectResult(e.Message));
            }
        }