Esempio n. 1
0
        public async Task <IActionResult> Get(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req,
            [SignalR(HubName = "basket")] IAsyncCollector <SignalRMessage> signalRMessage)
        {
            string userId = _authorize.GetUserId(req);

            Models.Basket basket = _mongoBase.Find(Guid.Parse(userId));

            await signalRMessage.AddAsync(
                new SignalRMessage {
                Target    = "basket",
                UserId    = userId.ToString(),
                Arguments = new[] { JsonConvert.SerializeObject(basket) }
            });

            return(new OkObjectResult(basket));
        }
Esempio n. 2
0
        public async Task <IActionResult> Create(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
            [SignalR(HubName = "basket")] IAsyncCollector <SignalRMessage> signalRMessage)
        {
            string userId = _authorize.GetUserId(req);

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

            var input = JsonConvert.DeserializeObject <Models.Basket>(requestBody);

            List <Models.BrokerRules> invalids = input.Valid();

            if (invalids.Count > 0)
            {
                return(new BadRequestObjectResult(JsonConvert.SerializeObject(invalids)));
            }

            var basket = new Models.Basket
            {
                Id       = input.Id,
                UserId   = Guid.Parse(userId),
                Created  = DateTime.Now,
                IsActive = input.IsActive,
                Items    = input.Items
            };

            basket.UpdateTotal();

            _mongoBase.Insert(basket);

            await signalRMessage.AddAsync(
                new SignalRMessage {
                Target    = "basket",
                UserId    = userId,
                Arguments = new[] { JsonConvert.SerializeObject(basket) }
            });

            return(new OkObjectResult(JsonConvert.SerializeObject(basket)));
        }