// private DecoderContext db = new DecoderContext();
 public int? AddDecoder(Decoder decoder)
 {
     DecoderContext db = null;
     try
     {
         db = new DecoderContext();
         decoder.UpdateFieldsFromXml();
         db.Decoders.Add(decoder);
         db.SaveChanges();
         return decoder.Id;
     }
     finally
     {
         db?.Dispose();
     }
 }
        public bool UpdateDecoder(Decoder updatedDecoder)
        {
            DecoderContext db = null;
            var mustRefreshToolbox = false;
            try
            {
                db = new DecoderContext();
                updatedDecoder.UpdateFieldsFromXml();
                var decoder = db.Decoders.Find(updatedDecoder.Id);
                if (decoder != null)
                {
                    /*decoder.Name = updatedDecoder.Name;
                    decoder.Version = updatedDecoder.Version;
                    decoder.Category = updatedDecoder.Category;
                    decoder.Tags = updatedDecoder.Tags;
                    decoder.Xml = updatedDecoder.Xml;
                    decoder.Code = updatedDecoder.Code;
                    decoder.FrenchSpec = updatedDecoder.FrenchSpec;
                    decoder.UpdateFieldsFromXml();*/
                    if (decoder.Name != updatedDecoder.Name || decoder.Version != updatedDecoder.Version
                        || decoder.Category != updatedDecoder.Category
                        || decoder.Parameters != updatedDecoder.Parameters || decoder.Tags != updatedDecoder.Tags
                        || decoder.Editable != updatedDecoder.Editable)
                    {
                        mustRefreshToolbox = true;
                    }

                    //TODO DO NOT OVERRIDE CODE AND SPEC

                    db.Entry(decoder).CurrentValues.SetValues(updatedDecoder);
                    db.SaveChanges();
                    return mustRefreshToolbox;
                }
                else
                {
                    throw new KeyNotFoundException();
                }
            }
            finally
            {
                db?.Dispose();
            }
        }
 public IHttpActionResult PostDecoder(Decoder decoder)
 {
     try
     {
         if (!ModelState.IsValid)
         {
             return BadRequest(ModelState);
         }
         var services = new DecoderServices();
         if (decoder.Id == null)
         {
             var decoderId = services.AddDecoder(decoder);
             MyHub.NotifyRefresh();
             return Json(new { id = decoderId });
         }
         else
         {
             var mustRefreshToolbox = services.UpdateDecoder(decoder);
             if (mustRefreshToolbox)
             {
                 MyHub.NotifyRefresh();
             }
             return Json(new { id = decoder.Id });
         }
     }
     catch (Exception e)
     {
         return InternalServerError(e);
     }
 }