/// <summary>
        /// Heart of the getProductItems function.
        /// </summary>
        /// <param name="productIds"></param>
        /// <returns></returns>
        private ILookup <string, ProductItem> Core_getProductItems(string[] productIds, int[] itemIds = null)
        {
            productIds = productIds ?? new string[0];
            itemIds    = itemIds ?? new int[0];

            // Build condition
            MySqlConditionBuilder condition = new MySqlConditionBuilder("product", MySqlDbType.String, productIds);

            condition.And();
            condition.NewGroup();
            foreach (var id in itemIds)
            {
                condition.Or()
                .Column("id")
                .Equals(id, MySqlDbType.Int32);
            }
            condition.EndGroup();

            var itemPrimary = ProductItem.indexes.First(x => x.Type == Index.IndexType.PRIMARY).Columns[0];

            // Ignore default item
            condition.And()
            .Column(itemPrimary.Column)
            .NotEquals(0, itemPrimary.Type);

            return(Connection.Select <ProductItem>(condition).ToLookup(x => x.ProductId));
        }
Example #2
0
        /// <summary>
        /// Tries to find and return all items of a product type during a timespan that aren't reserved.
        /// </summary>
        /// <param name="productId">The product type whose items to return.</param>
        /// <param name="span">The time period to test for unreserved items.</param>
        /// <returns>A subset of all items of the given product type, or null if the product type has no items.</returns>
        private List <ProductItem> Core_GetUnreservedItems(string productId, DateTimeSpan span)
        {
            List <ProductItem> items = Core_getProductItems(new string[] { productId })[productId].ToList();

            if (!items.Any())
            {
                return(null);
            }

            var condition = new MySqlConditionBuilder();

            condition.NewGroup();
            foreach (var item in items)
            {
                condition.Or()
                .Column("product_item")
                .Equals(item.Id, item.GetIndex("PRIMARY").Columns[0].Type);
            }
            condition.EndGroup();
            condition.And()
            .Column("end")
            .GreaterThanOrEqual()
            .Operand(span.Start, MySqlDbType.DateTime);
            condition.And()
            .Column("start")
            .LessThanOrEqual()
            .Operand(span.End, MySqlDbType.DateTime);

            List <LoanItem> loans = Connection.Select <LoanItem>(condition).ToList();

            foreach (var loan in loans)
            {
                if (!items.Any(x => x.Id == loan.ProductItem))
                {
                    continue;
                }
                var loanSpan = new DateTimeSpan(loan.Start, loan.End);
                if (span.Overlaps(loanSpan))
                {
                    items.Remove(items.First(x => x.Id == loan.ProductItem));
                }
            }
            return(items);
        }
        public static MySqlConditionBuilder CreateCondition(JObject criteria, MySqlConditionBuilder condition = null)
        {
            condition = condition ?? new MySqlConditionBuilder();
            condition.NewGroup();
            int i = 0;

            foreach (KeyValuePair <string, JToken> pair in criteria)
            {
                if (i > 0)
                {
                    condition.And();
                }

                condition.Column(pair.Key);
                string   value    = (string)pair.Value;
                string[] operands = value.Split("OR");
                foreach (string operand in operands)
                {
                    string[] split = operand.Split(" ");
                    if (split[0] == "LIKE")
                    {
                        condition.Like(split[1]);
                    }
                    else
                    {
                        condition.Equals(operand, MySql.Data.MySqlClient.MySqlDbType.String);
                    }
                    if (operands.Last() != operand)
                    {
                        condition.Or();
                    }
                }
                i++;
            }
            return(condition.EndGroup());
        }
Example #4
0
        public JObject getLoans(JObject request)
        {
            // Get arguments
            request.TryGetValue("columns", out JToken requestColumns);
            request.TryGetValue("productItemIds", out JToken requestProductItems);
            request.TryGetValue("userId", out JToken requestUserId);
            request.TryGetValue("loanItemID", out JToken requestLoanId);
            request.TryGetValue("start", out JToken requestStart);
            request.TryGetValue("end", out JToken requestEnd);

            // Verify arguments
            List <string> failedVerifications = new List <string>();

            if (requestColumns != null && (requestColumns.Type != JTokenType.Array || requestColumns.Any(x => x.Type != JTokenType.String)))
            {
                failedVerifications.Add("columns");
            }
            if (requestProductItems != null && (requestProductItems.Type != JTokenType.Array || requestProductItems.Any(x => x.Type != JTokenType.String)))
            {
                failedVerifications.Add("productItemIds");
            }
            if (requestUserId != null && requestUserId.Type != JTokenType.String)
            {
                failedVerifications.Add("userId");
            }

            if (failedVerifications.Any())
            {
                return(Templates.InvalidArguments(failedVerifications.ToArray()));
            }

            // Parse arguments
            DateTime start;
            DateTime end;

            try { start = requestStart == null ? new DateTime() : DateTime.Parse(requestStart.ToString()); }
            catch (Exception) { return(Templates.InvalidArgument("Unable to parse 'start'")); }
            try { end = requestEnd == null ? new DateTime() : DateTime.Parse(requestEnd.ToString()); }
            catch (Exception) { return(Templates.InvalidArgument("Unable to parse 'end'")); }

            // Prepare values
            if (requestColumns == null || requestColumns.Count() == 0)
            {
                requestColumns = new JArray(LoanItem.metadata.Select(x => x.Column));
            }

            // Build condition
            var condition = new MySqlConditionBuilder();

            if (requestProductItems != null && requestProductItems.Any())
            {
                condition.NewGroup();
                foreach (var productItem in requestProductItems)
                {
                    condition.Or()
                    .Column("product_item")
                    .Equals(productItem, MySqlDbType.String);
                }
                condition.EndGroup();
            }
            // Filter by specific loan id
            if (requestLoanId != null)
            {
                condition.And().Column("id").Equals(requestLoanId, MySqlDbType.Int32);
            }
            // Filter by user
            if (requestUserId != null)
            {
                condition.And().Column("user").Equals(requestUserId, MySqlDbType.String);
            }
            // Automatically limit results to current user only if their permission is User
            if (CurrentUser.Permission <= UserPermission.User)
            {
                condition.And().Column("user").Equals(CurrentUser.Username, MySqlDbType.String);
            }
            // Select only relevant loans
            if (requestStart != null)
            {
                condition.And().Column("end").GreaterThanOrEqual().Operand(start, MySqlDbType.DateTime);
            }
            if (requestEnd != null)
            {
                condition.And().Column("start").LessThanOrEqual().Operand(end, MySqlDbType.DateTime);
            }

            // Get loans
            var loans = Connection.Select <LoanItem>(requestColumns.ToObject <string[]>(), condition);

            // Build base response
            var     responseData = new JArray();
            JObject response     = new JObject()
            {
                { "reason", null },
                { "responseData", responseData }
            };

            // Populate responseData
            foreach (var loanData in loans)
            {
                var item = new JObject();
                for (int i = 0; i < requestColumns.Count(); i++)
                {
                    if (loanData[i] is DateTime)
                    {
                        item[(string)requestColumns[i]] = new JValue(((DateTime)loanData[i]).ToUniversalTime().Subtract(Epoch).TotalMilliseconds);
                    }
                    else
                    {
                        item[(string)requestColumns[i]] = new JValue(loanData[i]);
                    }
                }
                responseData.Add(item);
            }

            return(response);
        }