Example #1
0
        /// <summary>
        /// Update/Insert the given input object
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <returns></returns>
        public T UpsertRecord <T>(T input) where T : IStaticType, new()
        {
            var actualType = input.GetType();
            var entityName = actualType.Name;
            var type       = _dataType.FromType(actualType);

            if (type == null)
            {
                throw new ArgumentException("Given entityName does not exists.");
            }

            input.__updatedAt = DateTime.Now;

            if (input.Id == 0)
            {
                input.__createdAt = DateTime.Now;

                NancyBlackDatabase.ObjectCreating(this, entityName, input);

                _db.Insert(input, actualType);

                NancyBlackDatabase.ObjectCreated(this, entityName, input);
            }
            else
            {
                NancyBlackDatabase.ObjectUpdating(this, entityName, input);

                _db.Update(input, actualType);

                NancyBlackDatabase.ObjectUpdated(this, entityName, input);
            }

            return(input);
        }
Example #2
0
        /// <summary>
        /// Update Record FAST, this will directly update the record to database
        /// </summary>
        /// <param name="entityName"></param>
        /// <param name="inputObject"></param>
        /// <returns></returns>
        public dynamic UpsertStaticRecord(string entityName, dynamic inputObject)
        {
            var type = _dataType.FromName(entityName);

            if (type == null)
            {
                throw new ArgumentException("Given entityName does not exists.");
            }

            var actualType = type.GetCompiledType();

            if (inputObject is JObject)
            {
                inputObject = ((JObject)inputObject).ToObject(actualType);
            }

            if (inputObject.Id == 0)
            {
                inputObject.__createdAt = DateTime.Now;
                inputObject.__updatedAt = DateTime.Now;

                NancyBlackDatabase.ObjectCreating(this, entityName, inputObject);

                _db.Insert((object)inputObject, actualType);

                NancyBlackDatabase.ObjectCreated(this, entityName, inputObject);
            }
            else
            {
                inputObject.__updatedAt = DateTime.Now;

                NancyBlackDatabase.ObjectUpdating(this, entityName, inputObject);

                _db.Update((object)inputObject, actualType);

                NancyBlackDatabase.ObjectUpdated(this, entityName, inputObject);
            }

            return(inputObject);
        }
Example #3
0
        /// <summary>
        /// Upserts the specified entity name.
        /// </summary>
        /// <param name="entityName">Name of the entity.</param>
        /// <param name="input">Data to be saved, can be anything including anonymous type. But Anonymous Type must include Id parameter</param>
        public JObject UpsertRecord(string entityName, object inputObject)
        {
            var type = _dataType.FromName(entityName);

            if (type is StaticDataType)
            {
                return(JObject.FromObject(this.UpsertStaticRecord(entityName, inputObject)));
            }

            JObject jObject = inputObject as JObject;

            if (jObject == null)
            {
                jObject = JObject.FromObject(inputObject);
            }

            List <JProperty> removed = new List <JProperty>();

            // converts complex properties to Json
            foreach (var prop in jObject.Properties().ToList()) // to-list to allow us to add property
            {
                if (prop.Value.Type == JTokenType.Array || prop.Value.Type == JTokenType.Object)
                {
                    // array or objects are converted to JSON when stored in table
                    jObject["js_" + prop.Name] = prop.Value.ToString(Formatting.None);

                    prop.Remove();
                    removed.Add(prop);
                }
            }

            type = _dataType.FromJObject(entityName, jObject);
            var actualType = type.GetCompiledType();

            jObject["__updatedAt"] = DateTime.Now;

            int id = 0;

            if (jObject.Property("id") != null) // try to get Id
            {
                id = (int)jObject["id"];

                jObject["Id"] = id;
                jObject.Remove("id");
            }

            if (jObject.Property("Id") != null)
            {
                id = (int)jObject["Id"];
            }

            if (id == 0)
            {
                jObject["__createdAt"] = DateTime.Now;

                // needs to convert to object to get Id later
                dynamic toInsert = jObject.ToObject(actualType);

                NancyBlackDatabase.ObjectCreating(this, entityName, toInsert);

                _db.Insert(toInsert, actualType);
                jObject["Id"] = toInsert.Id;

                NancyBlackDatabase.ObjectCreated(this, entityName, toInsert);
            }
            else
            {
                NancyBlackDatabase.ObjectUpdating(this, entityName, jObject);

                _db.Update(jObject.ToObject(actualType), actualType);

                NancyBlackDatabase.ObjectUpdated(this, entityName, jObject);
            }

            // remove "js_" properties
            foreach (var prop in jObject.Properties().ToList()) // to-list to allow us to add/remove property
            {
                if (prop.Name.StartsWith("js_"))
                {
                    prop.Remove();
                }
            }

            // add removed complex properties back
            foreach (var prop in removed)
            {
                jObject.Add(prop.Name, prop.Value);
            }

            return(jObject);
        }