public async Task <IActionResult> AddItem(int id) { var user = await _authorizerService.Authorize(HttpContext); if (user == null) { return(Redirect(GlobalConstants.Default_Login_Page)); } var npc = _NPCsService.ById(id); if (npc == null) { return(Redirect(NPCs_Root_Path)); } var model = new AddItemToNPCInputModel { NPCId = id, NPCName = npc.Name, Items = _itemsService.All() }; return(View(model)); }
public void AddItem_AddsToNPC() { var options = new DbContextOptionsBuilder <GameInfoContext>() .UseInMemoryDatabase(databaseName: "Db_ForAddItem_ToNPC") .Options; var itemName = "Item Name"; var npc = new NPC() { Name = "NPC Name" }; var item = new Item() { Name = itemName, AcquiredFrom = "None", Usage = "None" }; using (var context = new GameInfoContext(options)) { context.NPCs.Add(npc); context.Items.Add(item); context.SaveChanges(); var model = new AddItemToNPCInputModel() { NPCId = 1, ItemName = itemName }; var service = new NPCsService(context, new ItemsService(context)); Assert.True(service.AddItemToNPC(model)); } }
public IActionResult AddItem(AddItemToNPCInputModel model) { var success = _NPCsService.AddItemToNPC(model); if (success) { return(RedirectToAction("Details", new { id = model.NPCId })); } return(View(model)); }
public bool AddItemToNPC(AddItemToNPCInputModel model) { var npc = this.ById(model.NPCId); if (npc == null) { return(false); } var itemToAdd = _itemsService.ByName(model.ItemName); npc.Loot.Add(itemToAdd); _db.SaveChanges(); return(true); }