Exemplo n.º 1
0
        public IHttpActionResult UpdateCharItemById(EditCharItem editCharItem)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var    service   = CreateCharItemService();
            string errorText = service.UpdateCharItemById(editCharItem);

            if (errorText == null)
            {
                return(Ok("Update completed"));
            }

            return(BadRequest(errorText));
        }
Exemplo n.º 2
0
        public string UpdateCharItemById(EditCharItem model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                if (ctx.CharItems.Count(e => e.CharItemId == model.CharItemId) == 0)
                {
                    return("Record not found in table");
                }
                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");
                }
                if (ctx.CharItems.Count(e => e.CharId == model.CharId && e.ItemId == model.ItemId) != 0)
                {
                    return("Combination already exists in table");
                }
                var entity =
                    ctx
                    .CharItems
                    .Single(e => e.CharItemId == model.CharItemId);

                entity.CharId = model.CharId;
                entity.ItemId = model.ItemId;

                if (ctx.SaveChanges() == 1)
                {
                    return(null);
                }

                return("Update failed - unknown error");
            }
        }