public string CreateCharItem(PostCharItem model) { var entity = new CharItem() { CharId = model.CharId, ItemId = model.ItemId }; using (var ctx = new ApplicationDbContext()) { if (ctx.CharItems.Count(e => e.Character.CharId == model.CharId && e.Item.ItemId == model.ItemId) > 0) { return("Combination already exists"); } if (ctx.Characters.Count(e => e.CharId == model.CharId) == 0) { return($"Character {model.CharId} NOT found in table"); } if (ctx.Items.Count(e => e.ItemId == model.ItemId) == 0) { return($"Item {model.ItemId} NOT found in table"); } ctx.CharItems.Add(entity); if (ctx.SaveChanges() == 1) { return(null); } return("Character/Item Combination NOT created - unknown error"); } }
public IHttpActionResult PostCharItem(PostCharItem charItem) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreateCharItemService(); string errorText = service.CreateCharItem(charItem); if (errorText == null) { return(Ok("Character/Item Combination created")); } return(BadRequest(errorText)); }