Пример #1
0
 public Task <int> GetCount(
     BakingRightTypeParameter type,
     AccountParameter baker,
     Int32Parameter cycle,
     Int32Parameter level,
     Int32NullParameter slots,
     Int32NullParameter priority,
     BakingRightStatusParameter status)
 {
     return(BakingRights.GetCount(type, baker, cycle, level, slots, priority, status));
 }
Пример #2
0
        public async Task <ActionResult <IEnumerable <BakingRight> > > Get(
            BakingRightTypeParameter type,
            AccountParameter baker,
            Int32Parameter cycle,
            Int32Parameter level,
            Int32NullParameter slots,
            Int32NullParameter priority,
            BakingRightStatusParameter status,
            SelectParameter select,
            SortParameter sort,
            OffsetParameter offset,
            [Range(0, 10000)] int limit = 100)
        {
            #region validate
            if (sort != null && !sort.Validate("level"))
            {
                return(new BadRequest($"{nameof(sort)}", "Sorting by the specified field is not allowed."));
            }
            #endregion

            if (select == null)
            {
                return(Ok(await BakingRights.Get(type, baker, cycle, level, slots, priority, status, sort, offset, limit)));
            }

            if (select.Values != null)
            {
                if (select.Values.Length == 1)
                {
                    return(Ok(await BakingRights.Get(type, baker, cycle, level, slots, priority, status, sort, offset, limit, select.Values[0])));
                }
                else
                {
                    return(Ok(await BakingRights.Get(type, baker, cycle, level, slots, priority, status, sort, offset, limit, select.Values)));
                }
            }
            else
            {
                if (select.Fields.Length == 1)
                {
                    return(Ok(await BakingRights.Get(type, baker, cycle, level, slots, priority, status, sort, offset, limit, select.Fields[0])));
                }
                else
                {
                    return(Ok(new SelectionResponse
                    {
                        Cols = select.Fields,
                        Rows = await BakingRights.Get(type, baker, cycle, level, slots, priority, status, sort, offset, limit, select.Fields)
                    }));
                }
            }
        }
Пример #3
0
        public async Task <ActionResult <IEnumerable <Commitment> > > Get(
            bool?activated,
            Int32NullParameter activationLevel,
            Int64Parameter balance,
            SelectParameter select,
            SortParameter sort,
            OffsetParameter offset,
            [Range(0, 10000)] int limit = 100)
        {
            #region validate
            if (sort != null && !sort.Validate("activationLevel", "balance"))
            {
                return(new BadRequest($"{nameof(sort)}", "Sorting by the specified field is not allowed."));
            }
            #endregion

            if (select == null)
            {
                return(Ok(await Commitments.Get(activated, activationLevel, balance, sort, offset, limit)));
            }

            if (select.Values != null)
            {
                if (select.Values.Length == 1)
                {
                    return(Ok(await Commitments.Get(activated, activationLevel, balance, sort, offset, limit, select.Values[0])));
                }
                else
                {
                    return(Ok(await Commitments.Get(activated, activationLevel, balance, sort, offset, limit, select.Values)));
                }
            }
            else
            {
                if (select.Fields.Length == 1)
                {
                    return(Ok(await Commitments.Get(activated, activationLevel, balance, sort, offset, limit, select.Fields[0])));
                }
                else
                {
                    return(Ok(new SelectionResponse
                    {
                        Cols = select.Fields,
                        Rows = await Commitments.Get(activated, activationLevel, balance, sort, offset, limit, select.Fields)
                    }));
                }
            }
        }
Пример #4
0
 public async Task <IEnumerable <Commitment> > Get(
     bool?activated,
     Int32NullParameter activationLevel,
     Int64Parameter balance,
     SortParameter sort,
     OffsetParameter offset,
     int limit)
 {
     var sql = new SqlBuilder(@"SELECT * FROM ""Commitments""")
               .Filter("Level", activated == null ? null : new Int32NullParameter {
         Null = !activated
     })
               .Filter("Level", activationLevel)
               .Filter("Balance", balance)
               .Take(sort, offset, limit, x => x switch
     {
         "balance" => ("Balance", "Balance"),
         "activationLevel" => ("Level", "Level"),
         _ => ("Id", "Id")
     });
Пример #5
0
        public async Task <IEnumerable <BakingRight> > Get(
            BakingRightTypeParameter type,
            AccountParameter baker,
            Int32Parameter cycle,
            Int32Parameter level,
            Int32NullParameter slots,
            Int32NullParameter priority,
            BakingRightStatusParameter status,
            SortParameter sort,
            OffsetParameter offset,
            int limit)
        {
            var sql = new SqlBuilder(@"SELECT * FROM ""BakingRights""")
                      .Filter("Cycle", cycle)
                      .Filter("Level", level)
                      .Filter("BakerId", baker)
                      .Filter("Type", type)
                      .Filter("Status", status)
                      .Filter("Priority", priority)
                      .Filter("Slots", slots)
                      .Take(sort ?? new SortParameter {
                Asc = "level"
            }, offset, limit, x => ("Level", "Level"));

            using var db = GetConnection();
            var rows = await db.QueryAsync(sql.Query, sql.Params);

            var state = State.GetState();

            return(rows.Select(row => new BakingRight
            {
                Type = TypeToString(row.Type),
                Cycle = row.Cycle,
                Level = row.Level,
                Timestamp = row.Status == 0 ? state.Timestamp.AddMinutes(row.Level - state.Level) : Time[row.Level],
                Baker = Accounts.GetAlias(row.BakerId),
                Priority = row.Priority,
                Slots = row.Slots,
                Status = StatusToString(row.Status)
            }));
        }
Пример #6
0
        public async Task <int> GetCount(
            BakingRightTypeParameter type,
            AccountParameter baker,
            Int32Parameter cycle,
            Int32Parameter level,
            Int32NullParameter slots,
            Int32NullParameter priority,
            BakingRightStatusParameter status)
        {
            var sql = new SqlBuilder(@"SELECT COUNT(*) FROM ""BakingRights""")
                      .Filter("Cycle", cycle)
                      .Filter("Level", level)
                      .Filter("BakerId", baker)
                      .Filter("Type", type)
                      .Filter("Status", status)
                      .Filter("Priority", priority)
                      .Filter("Slots", slots);

            using var db = GetConnection();
            return(await db.QueryFirstAsync <int>(sql.Query, sql.Params));
        }
Пример #7
0
        public async Task <object[][]> Get(
            BakingRightTypeParameter type,
            AccountParameter baker,
            Int32Parameter cycle,
            Int32Parameter level,
            Int32NullParameter slots,
            Int32NullParameter priority,
            BakingRightStatusParameter status,
            SortParameter sort,
            OffsetParameter offset,
            int limit,
            string[] fields)
        {
            var columns = new HashSet <string>(fields.Length);

            foreach (var field in fields)
            {
                switch (field)
                {
                case "type": columns.Add(@"""Type"""); break;

                case "cycle": columns.Add(@"""Cycle"""); break;

                case "level": columns.Add(@"""Level"""); break;

                case "timestamp": columns.Add(@"""Level"""); columns.Add(@"""Status"""); break;

                case "baker": columns.Add(@"""BakerId"""); break;

                case "priority": columns.Add(@"""Priority"""); break;

                case "slots": columns.Add(@"""Slots"""); break;

                case "status": columns.Add(@"""Status"""); break;
                }
            }

            if (columns.Count == 0)
            {
                return(Array.Empty <object[]>());
            }

            var sql = new SqlBuilder($@"SELECT {string.Join(',', columns)} FROM ""BakingRights""")
                      .Filter("Cycle", cycle)
                      .Filter("Level", level)
                      .Filter("BakerId", baker)
                      .Filter("Type", type)
                      .Filter("Status", status)
                      .Filter("Priority", priority)
                      .Filter("Slots", slots)
                      .Take(sort ?? new SortParameter {
                Asc = "level"
            }, offset, limit, x => ("Level", "Level"));

            using var db = GetConnection();
            var rows = await db.QueryAsync(sql.Query, sql.Params);

            var result = new object[rows.Count()][];

            for (int i = 0; i < result.Length; i++)
            {
                result[i] = new object[fields.Length];
            }

            for (int i = 0, j = 0; i < fields.Length; j = 0, i++)
            {
                switch (fields[i])
                {
                case "type":
                    foreach (var row in rows)
                    {
                        result[j++][i] = BakingRightTypes.ToString(row.Type);
                    }
                    break;

                case "cycle":
                    foreach (var row in rows)
                    {
                        result[j++][i] = row.Cycle;
                    }
                    break;

                case "level":
                    foreach (var row in rows)
                    {
                        result[j++][i] = row.Level;
                    }
                    break;

                case "timestamp":
                    foreach (var row in rows)
                    {
                        result[j++][i] = Time[row.Level];
                    }
                    break;

                case "baker":
                    foreach (var row in rows)
                    {
                        result[j++][i] = await Accounts.GetAliasAsync(row.BakerId);
                    }
                    break;

                case "priority":
                    foreach (var row in rows)
                    {
                        result[j++][i] = row.Priority;
                    }
                    break;

                case "slots":
                    foreach (var row in rows)
                    {
                        result[j++][i] = row.Slots;
                    }
                    break;

                case "status":
                    foreach (var row in rows)
                    {
                        result[j++][i] = BakingRightStatuses.ToString(row.Status);
                    }
                    break;
                }
            }

            return(result);
        }
Пример #8
0
        public async Task <object[]> Get(
            BakingRightTypeParameter type,
            AccountParameter baker,
            Int32Parameter cycle,
            Int32Parameter level,
            Int32NullParameter slots,
            Int32NullParameter priority,
            BakingRightStatusParameter status,
            SortParameter sort,
            OffsetParameter offset,
            int limit,
            string field)
        {
            var columns = new HashSet <string>(1);

            switch (field)
            {
            case "type": columns.Add(@"""Type"""); break;

            case "cycle": columns.Add(@"""Cycle"""); break;

            case "level": columns.Add(@"""Level"""); break;

            case "timestamp": columns.Add(@"""Level"""); columns.Add(@"""Status"""); break;

            case "baker": columns.Add(@"""BakerId"""); break;

            case "priority": columns.Add(@"""Priority"""); break;

            case "slots": columns.Add(@"""Slots"""); break;

            case "status": columns.Add(@"""Status"""); break;
            }

            if (columns.Count == 0)
            {
                return(Array.Empty <object>());
            }

            var sql = new SqlBuilder($@"SELECT {string.Join(',', columns)} FROM ""BakingRights""")
                      .Filter("Cycle", cycle)
                      .Filter("Level", level)
                      .Filter("BakerId", baker)
                      .Filter("Type", type)
                      .Filter("Status", status)
                      .Filter("Priority", priority)
                      .Filter("Slots", slots)
                      .Take(sort ?? new SortParameter {
                Asc = "level"
            }, offset, limit, x => ("Level", "Level"));

            using var db = GetConnection();
            var rows = await db.QueryAsync(sql.Query, sql.Params);

            //TODO: optimize memory allocation
            var result = new object[rows.Count()];
            var j      = 0;

            switch (field)
            {
            case "type":
                foreach (var row in rows)
                {
                    result[j++] = TypeToString(row.Type);
                }
                break;

            case "cycle":
                foreach (var row in rows)
                {
                    result[j++] = row.Cycle;
                }
                break;

            case "level":
                foreach (var row in rows)
                {
                    result[j++] = row.Level;
                }
                break;

            case "timestamp":
                var state = State.GetState();
                foreach (var row in rows)
                {
                    result[j++] = row.Status == 0 ? state.Timestamp.AddMinutes(row.Level - state.Level) : Time[row.Level];
                }
                break;

            case "baker":
                foreach (var row in rows)
                {
                    result[j++] = await Accounts.GetAliasAsync(row.BakerId);
                }
                break;

            case "priority":
                foreach (var row in rows)
                {
                    result[j++] = row.Priority;
                }
                break;

            case "slots":
                foreach (var row in rows)
                {
                    result[j++] = row.Slots;
                }
                break;

            case "status":
                foreach (var row in rows)
                {
                    result[j++] = StatusToString(row.Status);
                }
                break;
            }

            return(result);
        }