public bool UpdateInstrument(InstrumentEdit model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity =
                    ctx
                    .Instruments
                    .Single(e => e.InstrumentId == model.InstrumentId && e.OwnerId == _userId);

                entity.InstrumentId = model.InstrumentId;
                bool updating = true;
                int  counter  = 0;
                while (updating)
                {
                    if (counter == 8)
                    {
                        updating = false;
                        break;
                    }
                    if (model.Description != null && counter == 0)
                    {
                        entity.Description = model.Description;
                    }
                    else if (model.Name != null && counter == 1)
                    {
                        entity.Name = model.Name;
                    }
                    else if (model.ModelName != null && counter == 2)
                    {
                        entity.ModelName = model.ModelName;
                    }
                    else if (model.Brand != null && counter == 3)
                    {
                        entity.Brand = model.Brand;
                    }
                    else if (model.ExpLvl != 0 && counter == 4)
                    {
                        entity.ExpLvl = model.ExpLvl;
                    }
                    else if (model.Quantity != 0 && counter == 5)
                    {
                        entity.Quantity = model.Quantity;
                    }
                    else if (model.Price != 0 && counter == 6)
                    {
                        entity.Price = model.Price;
                    }
                    else if (model.ClassificationId != 0 && counter == 7)
                    {
                        entity.ClassificationId = model.ClassificationId;
                    }
                    counter++;
                }
                return(ctx.SaveChanges() == 1);
            }
        }
Пример #2
0
        public IHttpActionResult Put(InstrumentEdit instrument)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var service = CreateInstrumentService();

            if (!service.UpdateInstrument(instrument))
            {
                return(InternalServerError());
            }

            return(Ok());
        }