public static async Task <IActionResult> CreateCart(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1/carts")] HttpRequest req,
            [CosmosDB(
                 databaseName: "smartretailboxmanagement",
                 collectionName: "Terminals",
                 ConnectionStringSetting = "CosmosDbConnectionString")] DocumentClient client,
            ILogger log)
        {
            var parameter = await HttpRequestUtility.GetRequestParameterAsync <CreateCartParameter>(req);

            var userRepo = new UserRepository(StaticHttpClient.Instance);
            var user     = await userRepo.AuthenticateUserAsync();

            if (!user.result.IsSuccess)
            {
                return(new UnauthorizedResult());
            }

            var terminalRepo = new TerminalRepository(client);
            var terminal     = await terminalRepo.FindByBoxIdAsync(parameter.BoxId);

            if (!terminal.result.IsSuccess)
            {
                return(ActionResultFactory.CreateError <CreateCartResponse>(terminal.result));
            }

            var cartRepo = new CartRepository(new CartServiceApi(StaticHttpClient.Instance));
            var cart     = await cartRepo.CreateCartAsync(terminal.info.CompanyCode, terminal.info.StoreCode, terminal.info.TerminalNo, user.info.UserId, user.info.UserName);

            if (!cart.result.IsSuccess)
            {
                return(ActionResultFactory.CreateError <CreateCartResponse>(cart.result));
            }

            var boxRepo   = new BoxRepository(client);
            var updResult = await boxRepo.SetCartIdAsync(parameter.BoxId, parameter.DeviceId, user.info.UserId, cart.info.CartId);

            if (!updResult.IsSuccess)
            {
                return(ActionResultFactory.CreateError <CreateCartResponse>(updResult));
            }

            await BoxUtility.RequestUnLockAsync(terminal.info.BoxId, terminal.info.BoxDeviceId);

            return(new CreatedResult(
                       string.Empty,
                       new CreateCartResponse
            {
                CartId = cart.info.CartId
            }));
        }
Ejemplo n.º 2
0
        public static async Task <IActionResult> CreateCart(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = FunctionsRoute)] HttpRequest req,
            [CosmosDB(ConnectionStringSetting = CosmosDBConnectionStringSetting)] DocumentClient client,
            ILogger log) =>
        await DoAsync(log, nameof(CreateCart), HttpStatusCode.Created, async() =>
        {
            var param = await HttpRequestUtility.GetRequestParameterAsync <CreateCartParameter>(req);
            if (param == null)
            {
                return(new BadRequestObjectResult(Errors.ParameterError));
            }

            return(await new PosLogic(log).CreateCartAsync(param, client));
        });
Ejemplo n.º 3
0
        public static async Task <IActionResult> AddItems(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = FunctionsRoute + "/{cartId}/items")] HttpRequest req,
            [CosmosDB(ConnectionStringSetting = CosmosDBConnectionStringSetting)] DocumentClient client,
            string cartId,
            ILogger log) =>
        await DoAsync(log, nameof(AddItems), HttpStatusCode.OK, async() =>
        {
            var param = await HttpRequestUtility.GetRequestParameterAsync <AddItemsParameter>(req);
            if (param == null)
            {
                return(new BadRequestObjectResult(Errors.ParameterError));
            }

            return(await new PosLogic(log).AddItemsToCartAsync(param, client, cartId));
        });