示例#1
0
        public User AdjustUserInventory(string usernameOrID, Guid inventoryID, [FromBody] AdjustInventory inventoryUpdate)
        {
            UserDataModel user = null;

            if (!string.IsNullOrEmpty(usernameOrID))
            {
                if (Guid.TryParse(usernameOrID, out Guid userId))
                {
                    user = ChannelSession.Settings.GetUserData(userId);
                }
                else
                {
                    user = ChannelSession.Settings.GetUserDataByUsername(StreamingPlatformTypeEnum.All, usernameOrID);
                }
            }

            if (user == null)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new ObjectContent <Error>(new Error {
                        Message = $"Unable to find user: {usernameOrID}."
                    }, new JsonMediaTypeFormatter(), "application/json"),
                    ReasonPhrase = "User not found"
                };
                throw new HttpResponseException(resp);
            }
            return(AdjustInventory(user, inventoryID, inventoryUpdate));
        }
        public void DecreasingInventoryOnExistingProductPublishesExpectedEvent()
        {
            // Arrange
            Guid productId = Guid.NewGuid();
            var  command   = new AdjustInventory {
                ProductId = productId, Decrease = true, Quantity = 5
            };
            var expectedEvent = new { ProductId = productId, QuantityAdjustment = -5 };

            var repository = new InMemoryInventoryRepository();
            var handler    = new SpyEventHandler <InventoryAdjusted>();

            var sut = new AdjustInventoryService(repository, handler);

            repository.Save(new ProductInventory {
                Id = productId, Quantity = 20
            });

            // Act
            sut.Execute(command);

            // Assert
            Assert.Equal(
                expected: expectedEvent,
                actual: new { handler.HandledEvent.ProductId, handler.HandledEvent.QuantityAdjustment });
        }
        public User AdjustUserInventory(string username, Guid inventoryID, [FromBody] AdjustInventory inventoryUpdate)
        {
            UserDataViewModel user = ChannelSession.Settings.UserData.Values.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.InvariantCultureIgnoreCase));

            if (user == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return(AdjustInventory(user, inventoryID, inventoryUpdate));
        }
        public User AdjustUserInventory(uint userID, Guid inventoryID, [FromBody] AdjustInventory inventoryUpdate)
        {
            UserDataViewModel user = ChannelSession.Settings.UserData[userID];

            if (user == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return(AdjustInventory(user, inventoryID, inventoryUpdate));
        }
示例#5
0
        public static async Task <User> AdjustUserInventoryAsync(uint userId, Guid inventoryID, string itemName, int offsetAmount)
        {
            AdjustInventory adjustInventory = new AdjustInventory
            {
                Name   = itemName,
                Amount = offsetAmount
            };

            return(await RestClient.PutAsync <User>($"users/{userId}/inventory/{inventoryID}/adjust", adjustInventory));
        }
        public void Index()
        {
            var adjustInventory = new AdjustInventory
            {
                Decrease  = true,
                ProductId = Guid.NewGuid(),
                Quantity  = 7
            };

            _adjustInventoryService.Execute(adjustInventory);
        }
示例#7
0
        public ActionResult AdjustInventory(AdjustInventoryViewModel viewModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(nameof(Index), this.Populate(viewModel)));
            }

            AdjustInventory command = viewModel.Command;

            this.inventoryAdjuster.Execute(command);

            this.TempData["SuccessMessage"] = "Inventory successfully adjusted.";

            return(this.RedirectToAction(nameof(HomeController.Index), "Home"));
        }
        public User AdjustUserInventory(string username, Guid inventoryID, [FromBody] AdjustInventory inventoryUpdate)
        {
            UserDataViewModel user = ChannelSession.Settings.UserData.Values.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.InvariantCultureIgnoreCase));

            if (user == null)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new ObjectContent <Error>(new Error {
                        Message = $"Unable to find user: {username}."
                    }, new JsonMediaTypeFormatter(), "application/json"),
                    ReasonPhrase = "Username not found"
                };
                throw new HttpResponseException(resp);
            }
            return(AdjustInventory(user, inventoryID, inventoryUpdate));
        }
        public User AdjustUserInventory(uint userID, Guid inventoryID, [FromBody] AdjustInventory inventoryUpdate)
        {
            UserDataViewModel user = ChannelSession.Settings.UserData[userID];

            if (user == null)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new ObjectContent <Error>(new Error {
                        Message = $"Unable to find user: {userID.ToString()}."
                    }, new JsonMediaTypeFormatter(), "application/json"),
                    ReasonPhrase = "User ID not found"
                };
                throw new HttpResponseException(resp);
            }
            return(AdjustInventory(user, inventoryID, inventoryUpdate));
        }
        private User AdjustInventory(UserDataViewModel user, Guid inventoryID, [FromBody] AdjustInventory inventoryUpdate)
        {
            if (!ChannelSession.Settings.Inventories.ContainsKey(inventoryID))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            if (inventoryUpdate == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            UserInventoryViewModel inventory = ChannelSession.Settings.Inventories[inventoryID];

            if (string.IsNullOrEmpty(inventoryUpdate.Name) || !inventory.Items.ContainsKey(inventoryUpdate.Name))
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            if (inventoryUpdate.Amount < 0)
            {
                int quantityToRemove = inventoryUpdate.Amount * -1;
                if (!user.HasInventoryAmount(inventory, inventoryUpdate.Name, quantityToRemove))
                {
                    // If the request is to remove currency, but user doesn't have enough, fail
                    throw new HttpResponseException(HttpStatusCode.Forbidden);
                }

                user.SubtractInventoryAmount(inventory, inventoryUpdate.Name, quantityToRemove);
            }
            else if (inventoryUpdate.Amount > 0)
            {
                user.AddInventoryAmount(inventory, inventoryUpdate.Name, inventoryUpdate.Amount);
            }

            return(UserFromUserDataViewModel(user));
        }
示例#11
0
        private User AdjustInventory(UserDataModel user, Guid inventoryID, [FromBody] AdjustInventory inventoryUpdate)
        {
            if (!ChannelSession.Settings.Inventory.ContainsKey(inventoryID))
            {
                var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new ObjectContent <Error>(new Error {
                        Message = $"Unable to find inventory: {inventoryID.ToString()}."
                    }, new JsonMediaTypeFormatter(), "application/json"),
                    ReasonPhrase = "Inventory ID not found"
                };
                throw new HttpResponseException(resp);
            }

            if (inventoryUpdate == null)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new ObjectContent <Error>(new Error {
                        Message = "Unable to parse inventory adjustment from POST body."
                    }, new JsonMediaTypeFormatter(), "application/json"),
                    ReasonPhrase = "Invalid POST Body"
                };
                throw new HttpResponseException(resp);
            }

            InventoryModel inventory = ChannelSession.Settings.Inventory[inventoryID];

            if (string.IsNullOrEmpty(inventoryUpdate.Name) || !inventory.ItemExists(inventoryUpdate.Name))
            {
                var resp = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new ObjectContent <Error>(new Error {
                        Message = "Unable to find requested inventory item."
                    }, new JsonMediaTypeFormatter(), "application/json"),
                    ReasonPhrase = "Invalid Inventory Item"
                };
                throw new HttpResponseException(resp);
            }

            InventoryItemModel item = inventory.GetItem(inventoryUpdate.Name);

            if (inventoryUpdate.Amount < 0)
            {
                int quantityToRemove = inventoryUpdate.Amount * -1;
                if (!inventory.HasAmount(user, item, quantityToRemove))
                {
                    // If the request is to remove inventory, but user doesn't have enough, fail
                    var resp = new HttpResponseMessage(HttpStatusCode.Forbidden)
                    {
                        Content = new ObjectContent <Error>(new Error {
                            Message = "User does not have enough inventory to remove"
                        }, new JsonMediaTypeFormatter(), "application/json"),
                        ReasonPhrase = "Not Enough Inventory"
                    };
                    throw new HttpResponseException(resp);
                }

                inventory.SubtractAmount(user, item, quantityToRemove);
            }
            else if (inventoryUpdate.Amount > 0)
            {
                inventory.AddAmount(user, item, inventoryUpdate.Amount);
            }

            return(UserFromUserDataViewModel(user));
        }
示例#12
0
        public async Task <User> AdjustUserInventory(string usernameOrID, Guid inventoryID, [FromBody] AdjustInventory inventoryUpdate)
        {
            UserDataModel user = await UserController.GetUserData(usernameOrID);

            if (user == null)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new ObjectContent <Error>(new Error {
                        Message = $"Unable to find user: {usernameOrID}."
                    }, new JsonMediaTypeFormatter(), "application/json"),
                    ReasonPhrase = "User not found"
                };
                throw new HttpResponseException(resp);
            }
            return(AdjustInventory(user, inventoryID, inventoryUpdate));
        }