Ejemplo n.º 1
0
        private dynamic EnableSizingVariations(dynamic arg)
        {
            TableSecModule.ThrowIfNoPermission(this.Context, "Product", TableSecModule.PERMISSON_UPDATE);

            var sizes = (string)arg.body.Value.choices;

            this.SiteDatabase.Transaction(() =>
            {
                foreach (var item in this.SiteDatabase.Query <Product>()
                         .Where(p => p.IsVariation == false)
                         .AsEnumerable())
                {
                    item.HasVariation        = true;
                    item.VariationAttributes = JArray.FromObject(new[] { new
                                                                         {
                                                                             Name    = "Size",
                                                                             Choices = sizes
                                                                         } });

                    this.HandleProductSave(JObject.FromObject(new
                    {
                        body = new
                        {
                            Value = item
                        }
                    }));
                }
            });

            return(200);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles getting item by it's id
        /// </summary>
        /// <param name="db"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        protected dynamic HandleSingleItemRequest(NancyBlackDatabase db, dynamic args)
        {
            var tableName = (string)args.table_name;
            var id        = (int)args.item_id;

            TableSecModule.ThrowIfNoPermission(this.Context, tableName, TableSecModule.PERMISSON_QUERY);

            return(this.SiteDatabase.GetByIdAsJObject(tableName, id));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Handles Insert/Update Request
        /// </summary>
        /// <param name="action">The action.</param>
        /// <returns></returns>
        protected dynamic HandleInsertUpdateRequest(NancyBlackDatabase db, dynamic arg)
        {
            var entityName = (string)arg.table_name;

            if (arg.body == null)
            {
                return(400);
            }

            if (this.SiteDatabase.DataType.FromName(entityName) == null)
            {
                if (this.Request.Url.HostName != "localhost")
                {
                    return(403);
                }

                // enable all access for automatically created table
                TableSecModule.SetTableSec(this.Context, entityName, true, true, true, true);
            }

            dynamic fromClient = arg.body.Value as JObject;
            int?    id         = fromClient.id;

            if (id == null)
            {
                id = fromClient.Id;
            }

            if (id == null || id == 0)
            {
                TableSecModule.ThrowIfNoPermission(this.Context, entityName, TableSecModule.PERMISSON_CREATE);
            }
            else
            {
                TableSecModule.ThrowIfNoPermission(this.Context, entityName, TableSecModule.PERMISSON_UPDATE);
            }

            // special treatment for IContent
            if (typeof(IContent).IsAssignableFrom(db.DataType.FromName(entityName).GetCompiledType()))
            {
                if (id == null || id == 0)
                {
                    fromClient.CreatedBy = this.CurrentUser.Id;
                }
                else
                {
                    fromClient.UpdatedBy = this.CurrentUser.Id;
                }
            }

            dynamic record = db.UpsertRecord(entityName, fromClient);

            return(this.Negotiate
                   .WithContentType("application/json")
                   .WithModel((object)record));
        }
Ejemplo n.º 4
0
        private dynamic HandleProductSave(dynamic arg)
        {
            TableSecModule.ThrowIfNoPermission(this.Context, "Product", TableSecModule.PERMISSON_UPDATE);


            var input   = arg.body.Value as JObject;
            var product = input.ToObject <Product>();

            var dupe = this.SiteDatabase.Query <Product>()
                       .Where(p => p.Url == product.Url)
                       .FirstOrDefault();

            if (dupe != null && dupe.Id != product.Id)
            {
                throw new InvalidOperationException("Duplicate Url");
            }

            if (product.HasVariation == false)
            {
                this.SiteDatabase.UpsertRecord <Product>(product);
            }

            var attributes = product.VariationAttributes as JArray;

            if (attributes == null)
            {
                this.SiteDatabase.UpsertRecord <Product>(product);
                return(product);
            }


            // handle generation of product variation
            this.SiteDatabase.Transaction(() =>
            {
                List <string[]> variations = new List <string[]>();
                foreach (JObject prop in attributes)
                {
                    var choices = from item in prop["Choices"].ToString().Split(',')
                                  select item.Trim();

                    variations.Add(choices.ToArray());
                }

                Action <string> createProduct = (url) =>
                {
                    var newUrl = (product.Url + url).ToLowerInvariant();

                    // copy all information from current product to replace the variation product
                    var newProduct      = JObject.FromObject(product).ToObject <Product>();
                    var existingProduct = this.SiteDatabase.Query <Product>()
                                          .Where(p => p.Url == newUrl)
                                          .FirstOrDefault();

                    if (existingProduct == null)
                    {
                        newProduct.Id = 0;
                    }
                    else
                    {
                        newProduct.Id = existingProduct.Id;
                    }

                    newProduct.MasterProductId     = product.Id;
                    newProduct.IsVariation         = true;
                    newProduct.HasVariation        = false;
                    newProduct.VariationAttributes = null;
                    newProduct.Url = newUrl;

                    var parts = url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    var newProductAttributes = new JObject();

                    for (int i = 0; i < attributes.Count; i++)
                    {
                        newProductAttributes[attributes[i]["Name"].ToString()] = parts[i];
                    }

                    newProduct.Title     += " (Variation:" + url + ")";
                    newProduct.Attributes = newProductAttributes;
                    this.SiteDatabase.UpsertRecord <Product>(newProduct);
                };

                Action <string, int> dig = null;
                dig = (baseUrl, index) =>
                {
                    foreach (var choice in variations[index])
                    {
                        var currentUrl = baseUrl + "/" + choice;

                        if (index + 1 == variations.Count)
                        {
                            createProduct(currentUrl);
                        }
                        else
                        {
                            dig(currentUrl, index + 1);
                        }
                    }
                };

                dig("", 0);
                this.SiteDatabase.UpsertRecord <Product>(product);
            });

            return(product);
        }
Ejemplo n.º 5
0
        private dynamic HandleSummarizeRequest(dynamic arg)
        {
            var table = (string)arg.table_name;

            TableSecModule.ThrowIfNoPermission(this.Context, table, TableSecModule.PERMISSON_QUERY);

            var timeperiod    = (string)this.Request.Query.period;
            var select        = (string)this.Request.Query.select;
            var function      = (string)this.Request.Query.fn;
            var timeselect    = (string)this.Request.Query.time;
            var filter        = (string)this.Request.Query["$filter"];
            var minTimeString = (string)this.Request.Query.mintime;

            DateTime?minTime = null;

            if (minTimeString != "undefined" && minTimeString != null)
            {
                if (minTimeString == "ytd")
                {
                    minTime = new DateTime(DateTime.Now.Year, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                }
                else
                {
                    minTime = DateTime.ParseExact(minTimeString, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                }
            }

            if (timeselect == null)
            {
                timeselect = "__createdAt";
            }

            if (timeperiod == null)
            {
                timeperiod = "all";
                timeselect = "not-used";
            }

            var dt = this.SiteDatabase.DataType.FromName(table);

            if (dt == null)
            {
                return(404);
            }

            if (select == null || function == null || timeselect == null)
            {
                return(400);
            }

            var type        = dt.GetCompiledType();
            var sourceTable = this.SiteDatabase.Query(table, filter);

            if (minTimeString == "0")
            {
                var min = this.SiteDatabase.QueryAsJObject(table, oDataSort: timeselect).FirstOrDefault();
                if (min == null)
                {
                    return(400);
                }

                minTime = min.Value <DateTime>(timeselect);
            }


            var createSummarizer = typeof(SummarizeTimeSeriesFactory)
                                   .GetMethods().Single(m => m.Name == "Create" && m.IsStatic)
                                   .MakeGenericMethod(type); // calling this.Summarize<T>

            dynamic summarizer = createSummarizer.Invoke(this, new object[] {
                sourceTable,
                Enum.Parse(typeof(TimePeriod), timeperiod),
                timeselect,
                select,
                Enum.Parse(typeof(Function), function),
                minTime
            });

            return(summarizer.GetSummarySeries());
        }