예제 #1
0
        public void Building_Update_Statement_will_return_correct_statement()
        {
            var model = new MultiUpdateModel()
            {
                PhotoIds = new List <int>()
                {
                    1, 2
                },
                AllowFullSizeDownload = true,
                EventId               = 10,
                LocationId            = 0,
                ReplaceExistingValues = true,
                Category              = CategoryType.Nature | CategoryType.Animals,
                Fields = MultiEditField.AllowFullSizeDownload | MultiEditField.EventId | MultiEditField.Category | MultiEditField.LocationId | MultiEditField.EventId
            };

            var fieldsAndValues = new Dictionary <string, object>();
            var repository      = new DataRepository();

            var actual   = repository.BuildUpdateStatement(model, ref fieldsAndValues);
            var expected =
                "UPDATE PHOTO AllowFullSizeDownload={0},Category={1},LocationId={2},EventId={3} WHERE ID IN (1,2)";

            Assert.IsTrue(actual.Equals(expected), "Der String ist ungültig");
        }
예제 #2
0
        public void Updating_multiple_photos_with_incorrect_ids_will_fail()
        {
            var photos =
                RequestHelper.CurrentDataRepository.Queryable <Photo>()
                .Where(p => p.MemberId == TestMemberId)
                .Take(3)
                .ToList();

            RequestHelper.SetupClaim(true);
            var model = new MultiUpdateModel();

            model.PhotoIds = photos.Select(p => p.Id).ToList();
            model.PhotoIds.Add(666);

            var result = RequestHelper.PutResult <Result <bool> >("/api/data/" + PostOrPutAction.MultiUpdate, model);

            Assert.IsTrue(!result.Data, "Es wurde nicht Falsch zurückgegeben");
        }
예제 #3
0
        internal string BuildUpdateStatement(MultiUpdateModel model, ref Dictionary <string, object> fieldsAndValues)
        {
            var sql          = string.Empty;
            var index        = 0;
            var setStatement = new List <string>();

            foreach (MultiEditField value in Enum.GetValues(typeof(MultiEditField)))
            {
                if (value == (model.Fields & value))
                {
                    var fieldAndValue = AddToFieldsAndValues(value, model);
                    if (!string.IsNullOrEmpty(fieldAndValue.Key))
                    {
                        var currentValue = fieldAndValue.Value;

                        if (value == MultiEditField.EventId ||
                            value == MultiEditField.LocationId ||
                            value == MultiEditField.License)
                        {
                            if ((int)currentValue == 0)
                            {
                                currentValue = null;
                            }
                            else
                            {
                                currentValue = (int)currentValue;
                            }
                        }
                        fieldsAndValues.Add(fieldAndValue.Key, currentValue);
                        setStatement.Add(string.Format("{0}={{{1}}}", fieldAndValue.Key, index));
                        index++;
                    }
                }
            }


            if (fieldsAndValues.Count == 0)
            {
                return(sql);
            }
            sql = string.Format("UPDATE PHOTO SET {0} WHERE ID IN ({1})", string.Join(",", setStatement), string.Join(",", model.PhotoIds));

            return(sql);
        }
예제 #4
0
        public void Updating_multiple_photos_will_succeed_and_change_the_values()
        {
            var photos =
                RequestHelper.CurrentDataRepository.Queryable <Photo>()
                .Where(p => p.MemberId == TestMemberId)
                .Take(3)
                .ToList();

            RequestHelper.SetupClaim(true);
            var model = new MultiUpdateModel();

            model.PhotoIds        = photos.Select(p => p.Id).ToList();
            model.AllowCommenting = true;

            var topics =
                RequestHelper.CurrentDataRepository.Queryable <Topic>()
                .Where(t => t.MemberId == TestMemberId)
                .Take(3)
                .ToList();

            model.Topics = topics.Select(t => t.Id).ToList();

            var testEvent = RequestHelper.CurrentDataRepository.Queryable <Event>().FirstOrDefault(e => e.MemberId == TestMemberId);

            if (testEvent != null)
            {
                model.EventId = testEvent.Id;
            }

            var testLocation = RequestHelper.CurrentDataRepository.Queryable <Location>().FirstOrDefault(e => e.MemberId == TestMemberId);

            if (testLocation != null)
            {
                model.LocationId = testLocation.Id;
            }

            model.License = LicenseType.CcZero;

            model.Fields = MultiEditField.License | MultiEditField.EventId | MultiEditField.Topics | MultiEditField.AllowCommenting;

            var result = RequestHelper.PutResult <Result <bool> >("/api/data/" + PostOrPutAction.MultiUpdate, model);

            Assert.IsTrue(result.Data, "Es wurde nicht Wahr zurückgegeben");
        }
예제 #5
0
        private KeyValuePair <string, object> AddToFieldsAndValues(MultiEditField value, MultiUpdateModel model)
        {
            var fieldAndValue = new KeyValuePair <string, object>();

            if ((!model.ReplaceExistingValues & (value == MultiEditField.Category)) ||
                value == MultiEditField.Topics)
            {
                return(fieldAndValue);
            }

            var fieldName = value.ToString();
            var field     = model.GetType().GetProperty(fieldName);

            fieldAndValue = new KeyValuePair <string, object>(fieldName, field.GetValue(model, null));

            if (field.PropertyType == typeof(bool))
            {
                if ((bool)fieldAndValue.Value)
                {
                    fieldAndValue = new KeyValuePair <string, object>(fieldName, 1);
                }
                else
                {
                    fieldAndValue = new KeyValuePair <string, object>(fieldName, 0);
                }
            }

            return(fieldAndValue);
        }
예제 #6
0
        public bool UpdateMultiplePhotos(MultiUpdateModel model)
        {
            using (var dbContextTransaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var photoIds = string.Join(",", model.PhotoIds.ToArray());

                    if (model.ToBeDeleted)
                    {
                        //Execute("DELETE FROM Photo WHERE ID IN (" + photoIds + ")");
                        //dbContextTransaction.Commit();
                        return(true);
                    }

                    var fieldsAndValues = new Dictionary <string, object>();
                    var updateStatement = BuildUpdateStatement(model, ref fieldsAndValues);

                    if (!string.IsNullOrEmpty(updateStatement))
                    {
                        Execute(updateStatement, fieldsAndValues.Values.ToArray());
                    }

                    if ((model.Fields & MultiEditField.Topics) == MultiEditField.Topics)
                    {
                        if (model.ReplaceExistingValues)
                        {
                            Execute("DELETE FROM PhotoTopic WHERE PhotoId in (" + photoIds + ")");
                        }

                        foreach (var photoId in model.PhotoIds)
                        {
                            foreach (var topicId in model.Topics)
                            {
                                Execute("INSERT INTO PhotoTopic (PhotoId,TopicId) VALUES ({0},{1})", photoId, topicId);
                            }
                        }
                    }
                    if (!model.ReplaceExistingValues)
                    {
                        if (MultiEditField.License == (MultiEditField.License & model.Fields) && model.License != LicenseType.None)
                        {
                            Execute("UPDATE Photo SET License = License | {0} WHERE ID IN (" + model.PhotoIds + ")",
                                    model.License);
                        }
                        if (MultiEditField.Category == (MultiEditField.Category & model.Fields) && model.Category != CategoryType.NotSet)
                        {
                            Execute("UPDATE Photo SET Category = Category | {0} WHERE ID IN (" + model.PhotoIds + ")",
                                    model.Category);
                        }
                    }

                    dbContextTransaction.Commit();
                    return(true);
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    dbContextTransaction.Rollback();
                    throw;
                }
            }
        }