Example #1
0
        public dynamic Put(int id, MvItemInput input)
        {
            if (ModelState.IsValid && input != null)
            {
                using (var cnt = new MvContext())
                {
                    var item = cnt.Items.Include("Values").Where(t => t.Id == id).SingleOrDefault();
                    if (item != null)
                    {
                        item.Modified = DateTime.Now;
                        var inputValues = input.Values.Where(t => t.Id > 0).ToDictionary(t => t.Id, t => t);

                        foreach (var value in item.Values.Where(t => inputValues.ContainsKey(t.Id)).ToArray())
                        {
                            var inputValue = inputValues[value.Id];
                            value.StringValue = inputValue.StringValue;
                            value.IntValue    = inputValue.IntValue;
                            value.DateValue   = inputValue.DateValue;
                            value.OptionId    = inputValue.OptionId;
                            value.Modified    = DateTime.Now;
                        }

                        var removeValues = item.Values.Where(t => !inputValues.ContainsKey(t.Id)).ToArray();
                        foreach (var value in removeValues)
                        {
                            cnt.Values.Remove(value);
                        }

                        var newValues = input.Values.Where(t => t.Id == 0).Select(t => new MvValue()
                        {
                            FieldId = t.FieldId, StringValue = t.StringValue, DateValue = t.DateValue, IntValue = t.IntValue, OptionId = t.OptionId, Modified = DateTime.Now
                        }).ToArray();
                        foreach (var value in newValues)
                        {
                            item.Values.Add(value);
                        }

                        cnt.SaveChanges();
                        return(new { Id = item.Id });
                    }
                }
            }
            return(new { });
        }
Example #2
0
 // POST api/values
 public dynamic Post(int listId, MvItemInput input)
 {
     if (ModelState.IsValid && input != null)
     {
         using (var cnt = new MvContext())
         {
             if (cnt.Lists.Any(t => t.Id == listId))
             {
                 var values = input.Values.Select(t => new MvValue()
                 {
                     FieldId = t.FieldId, StringValue = t.StringValue, DateValue = t.DateValue, IntValue = t.IntValue, OptionId = t.OptionId, Modified = DateTime.Now
                 }).ToList();
                 var item = new MvItem()
                 {
                     ListId = listId, Values = values, Modified = DateTime.Now
                 };
                 cnt.Items.Add(item);
                 cnt.SaveChanges();
                 return(new { Id = item.Id });
             }
         }
     }
     return(new { });
 }