public override async Task <JsonObject> DoWork(Server server, HttpListenerContext context, JsonObject json)
        {
            var userId = await VerifyVk.Verify(json, context);

            var com = await DB.CommandAsync(Consts.SELECT_USER_STAT);

            try {
                com.Parameters.Add(Consts.VK_ID, MySqlDbType.UInt64).Value = userId;

                var reader = com.ExecuteReader();

                if (await reader.ReadAsync())
                {
                    return new JsonObject(Json.Ok())
                           {
                               ["runsAtCurrentMonthCount"] = reader.GetInt64(0),
                               ["totalRunCount"]           = reader.GetInt64(1),
                               ["favouriteCategoryName"]   = reader.IsDBNull(2) ? null : reader.GetString(2),
                               ["balance"] = reader.GetInt64(3)
                           }
                }
                ;

                throw new Ex07_AccessDenied();
            } finally { await com.Connection.CloseAsync(); }
        }
        public override async Task <JsonObject> DoWork(Server server, HttpListenerContext context, JsonObject json)
        {
            var userId = await VerifyVk.Verify(json, context);

            var com = await DB.CommandAsync(Consts.SELECT_QUIZZES);

            try {
                com.Parameters.Add(Consts.CATEGORY_ID, MySqlDbType.UInt64).Value = (ulong)json["categoryId"];
                com.Parameters.Add(Consts.VK_ID, MySqlDbType.Int64).Value        = userId;

                var reader = com.ExecuteReader();

                var array = new JsonArray();

                while (await reader.ReadAsync())
                {
                    array.Add(new JsonObject {
                        ["id"]         = reader.GetUInt64(0),
                        ["title"]      = reader.GetString(1),
                        ["difficulty"] = reader.GetInt32(2),
                        ["run"]        = reader.IsDBNull(3) ? null : JsonValue.Parse(reader.GetString(3)) as JsonObject
                    });
                }

                return(new JsonObject(Json.Ok())
                {
                    ["quizzes"] = array
                });
            } finally { await com.Connection.CloseAsync(); }
        }
Esempio n. 3
0
        public override async Task <JsonObject> DoWork(Server server, HttpListenerContext context, JsonObject json)
        {
            var userId = await VerifyVk.Verify(json, context);

            var com = await DB.CommandAsync(Consts.SELECT_DISCOUNTS);

            try {
                var reader = com.ExecuteReader();

                var array = new JsonArray();

                while (await reader.ReadAsync())
                {
                    array.Add(new JsonObject {
                        ["id"]    = reader.GetUInt64(0),
                        ["title"] = reader.GetString(1),
                        ["price"] = reader.GetInt32(2),
                        ["shop"]  = new JsonObject {
                            ["id"]     = reader.GetUInt64(3),
                            ["name"]   = reader.GetString(4),
                            ["imgUrl"] = reader.IsDBNull(5) ? null : reader.GetString(5),
                            ["url"]    = reader.GetString(6)
                        }
                    });
                }

                return(new JsonObject(Json.Ok())
                {
                    ["discounts"] = array, ["balance"] = await DB.GetUserBalanceAsync(userId)
                });
            } finally { await com.Connection.CloseAsync(); }
        }
        public override async Task <JsonObject> DoWork(Server server, HttpListenerContext context, JsonObject json)
        {
            await VerifyVk.Verify(json, context);

            var com = await DB.CommandAsync(Consts.SELECT_CATEGORIES);

            try {
                var reader = com.ExecuteReader();

                var array = new JsonArray();

                while (await reader.ReadAsync())
                {
                    array.Add(new JsonObject {
                        ["id"]     = reader.GetUInt64(0),
                        ["name"]   = reader.GetString(1),
                        ["imgUrl"] = reader.IsDBNull(2) ? "" : reader.GetString(2)
                    });
                }

                return(new JsonObject(Json.Ok())
                {
                    ["categories"] = array
                });
            } finally { await com.Connection.CloseAsync(); }
        }
        public override async Task <JsonObject> DoWork(Server server, HttpListenerContext context, JsonObject json)
        {
            var userId = await VerifyVk.Verify(json, context);

            var com = await DB.CommandAsync(Consts.SELECT_LAST_RUN);

            try {
                com.Parameters.Add(Consts.QUIZ_ID, MySqlDbType.UInt64).Value = (ulong)json["quizId"];
                com.Parameters.Add(Consts.VK_ID, MySqlDbType.UInt64).Value   = userId;

                var reader = com.ExecuteReader();

                if (await reader.ReadAsync())
                {
                    if (DateTime.Now.Subtract(reader.GetDateTime(3)).TotalSeconds < Consts.COOLDOWN_DURATION)
                    {
                        throw new Ex08_CooldownException();
                    }
                }
            } finally { await com.Connection.CloseAsync(); }

            com = await DB.CommandAsync(Consts.SELECT_QUESTIONS);

            try {
                com.Parameters.Add(Consts.QUIZ_ID, MySqlDbType.UInt64).Value = (ulong)json["quizId"];

                var reader = com.ExecuteReader();

                if (!reader.HasRows)
                {
                    throw new Ex09_QuizNotFound();
                }

                var array = new JsonArray();

                while (await reader.ReadAsync())
                {
                    array.Add(new JsonObject {
                        ["id"]      = reader.GetUInt64(0),
                        ["text"]    = reader.GetString(1),
                        ["options"] = JsonValue.Parse(reader.GetString(3)) as JsonArray,
                        ["type"]    = reader.GetInt32(5)
                    });
                }

                return(new JsonObject(Json.Ok())
                {
                    ["questions"] = array
                });
            } finally { await com.Connection.CloseAsync(); }
        }
Esempio n. 6
0
        public override async Task <JsonObject> DoWork(Server server, HttpListenerContext context, JsonObject json)
        {
            var userId = await VerifyVk.Verify(json, context);

            var discount = await DB.GetDiscountByIdAsync(json["discountId"]);

            if (discount == null)
            {
                throw new Ex07_AccessDenied();
            }

            var balance = await DB.GetUserBalanceAsync(userId);

            if (balance < discount["price"])
            {
                throw new Ex07_AccessDenied();
            }

            var com = await DB.CommandAsync(Consts.INSERT_NEW_COUPON);

            JsonObject coupon;

            try {
                com.Parameters.Add(Consts.VK_ID, MySqlDbType.Int64).Value         = userId;
                com.Parameters.Add(Consts.DISCOUNT_ID, MySqlDbType.UInt64).Value  = (ulong)json["discountId"];
                com.Parameters.Add(Consts.COUPON, MySqlDbType.VarChar, 255).Value = Rand.Coupon();

                await com.ExecuteNonQueryAsync();

                coupon = await DB.GetCouponByIdAsync((ulong)com.LastInsertedId);
            } finally { await com.Connection.CloseAsync(); }

            await DB.UpdateBalanceAsync(userId, -discount["price"]);

            return(new JsonObject(Json.Ok())
            {
                ["coupon"] = coupon
            });
        }
        public override async Task <JsonObject> DoWork(Server server, HttpListenerContext context, JsonObject json)
        {
            var userId = await VerifyVk.Verify(json, context);

            var com = await DB.CommandAsync(Consts.SELECT_COUPONS);

            try {
                com.Parameters.Add(Consts.VK_ID, MySqlDbType.Int64).Value = userId;

                var reader = com.ExecuteReader();

                var array = new JsonArray();

                while (await reader.ReadAsync())
                {
                    array.Add(new JsonObject {
                        ["id"]        = reader.GetUInt64(0),
                        ["coupon"]    = reader.GetString(1),
                        ["createdAt"] = ((DateTimeOffset)reader.GetDateTime(2)).ToUnixTimeSeconds(),
                        ["discount"]  = new JsonObject {
                            ["id"]    = reader.GetUInt64(3),
                            ["title"] = reader.GetString(4),
                            ["price"] = reader.GetInt32(5),
                            ["shop"]  = new JsonObject {
                                ["id"]     = reader.GetUInt64(6),
                                ["name"]   = reader.GetString(7),
                                ["imgUrl"] = reader.IsDBNull(8) ? null : reader.GetString(8),
                                ["url"]    = reader.GetString(9)
                            }
                        }
                    });
                }

                return(new JsonObject(Json.Ok())
                {
                    ["coupons"] = array
                });
            } finally { await com.Connection.CloseAsync(); }
        }
Esempio n. 8
0
        public override async Task <JsonObject> DoWork(Server server, HttpListenerContext context, JsonObject json)
        {
            var userId = await VerifyVk.Verify(json, context);

            var com = await DB.CommandAsync(Consts.SELECT_LAST_RUN);

            try {
                com.Parameters.Add(Consts.QUIZ_ID, MySqlDbType.UInt64).Value = (ulong)json["quizId"];
                com.Parameters.Add(Consts.VK_ID, MySqlDbType.UInt64).Value   = userId;

                var reader = com.ExecuteReader();

                if (await reader.ReadAsync())
                {
                    if (DateTime.Now.Subtract(reader.GetDateTime(3)).TotalSeconds < Consts.COOLDOWN_DURATION)
                    {
                        throw new Ex08_CooldownException();
                    }
                }
            } finally { await com.Connection.CloseAsync(); }

            var answersFromJson = (JsonArray)json["answers"];
            var answersFromDb   = await DB.GetRightAnswersFromQuizAsync(json["quizId"], userId);

            var points = 0;

            foreach (var val in answersFromDb)
            {
                if (!(val is JsonObject answerFromDb))
                {
                    continue;
                }
                var answerFromJson = FindAnswerById(answersFromJson, answerFromDb["id"]);

                answerFromDb["answerFromClient"] = new JsonObject {
                    ["answer"] = answerFromJson?["answer"]
                };
                answerFromDb["right"] = answerFromJson != null &&
                                        CompareAnswers(answerFromDb["type"], answerFromDb, answerFromJson);

                if (answerFromDb["right"] && !answerFromDb["wasAnsweredRight"])
                {
                    points += answerFromDb["points"];
                }
            }

            com = await DB.CommandAsync(Consts.INSERT_NEW_RUN);

            long runId;

            try {
                com.Parameters.Add(Consts.VK_ID, MySqlDbType.Int64).Value    = userId;
                com.Parameters.Add(Consts.QUIZ_ID, MySqlDbType.UInt64).Value = (ulong)json["quizId"];

                com.ExecuteNonQuery();

                runId = com.LastInsertedId;
            } finally { await com.Connection.CloseAsync(); }

            foreach (var val in answersFromDb)
            {
                if (!(val is JsonObject answer))
                {
                    continue;
                }

                com = await DB.CommandAsync(Consts.INSERT_NEW_ANSWER);

                try {
                    com.Parameters.Add(Consts.RUN_ID, MySqlDbType.UInt64).Value = runId;
                    com.Parameters.Add(Consts.ANSWER, MySqlDbType.JSON).Value   =
                        ((JsonObject)answer["answerFromClient"]).ToString();
                    com.Parameters.Add(Consts.QUESTION_ID, MySqlDbType.UInt64).Value = (ulong)answer["id"];
                    com.Parameters.Add(Consts.IS_IT_RIGHT, MySqlDbType.Int16).Value  = (bool)answer["right"];

                    com.ExecuteNonQuery();
                } finally { await com.Connection.CloseAsync(); }
            }

            if (points > 0)
            {
                await DB.UpdateBalanceAsync(userId, points);
            }

            return(new JsonObject(Json.Ok())
            {
                ["newEpoints"] = points,
                ["rightAnswerCount"] = answersFromDb.Count(a => a.ContainsKey("right") && a["right"]),
                ["totalQuestionCount"] = answersFromDb.Count,
                ["books"] = await DB.GetBooksByQuizIdAsync(json["quizId"])
            });
        }
Esempio n. 9
0
        public override async Task <JsonObject> DoWork(Server server, HttpListenerContext context, JsonObject json)
        {
            await VerifyVk.Verify(json, context);

            return(await Task.Run(() => new JsonObject(Json.Ok()) { ["successful"] = true }));
        }