예제 #1
0
        public async Task GetPeriod(string name)
        {
            // Get period from the database.

            Period period = await BotUtils.GetPeriodFromDb(name);

            if (!await BotUtils.ReplyAsync_ValidatePeriod(Context, period))
            {
                return;
            }

            await GetPeriod(period);
        }
예제 #2
0
        public async Task SetPeriodDescription(string name, string description)
        {
            // Make sure that the given period exists.

            Period period = await BotUtils.GetPeriodFromDb(name);

            if (!await BotUtils.ReplyAsync_ValidatePeriod(Context, period))
            {
                return;
            }

            using (SQLiteCommand cmd = new SQLiteCommand("UPDATE Period SET description = $description WHERE id = $id;")) {
                cmd.Parameters.AddWithValue("$id", period.id);
                cmd.Parameters.AddWithValue("$description", description);

                await Database.ExecuteNonQuery(cmd);
            }

            await BotUtils.ReplyAsync_Success(Context, string.Format("Successfully updated description for period **{0}**.", period.GetName()));
        }
예제 #3
0
        public async Task AddPeriod(string name, string startDate, string endDate)
        {
            // Make sure we don't already have a period with the same name.

            Period p = await BotUtils.GetPeriodFromDb(name);

            if (!(p is null))
            {
                await BotUtils.ReplyAsync_Warning(Context, string.Format("The period **{0}** already exists.", p.GetName()));

                return;
            }

            startDate = startDate.ToLower();
            endDate   = endDate.ToLower();

            long start_ts = 0;
            long end_ts   = 0;

            // Try to parse the dates and convert them to timestamps.

            if (Period.TryParseDate(startDate, out DateTime start_dt) && Period.TryParseDate(endDate, out DateTime end_dt))
            {
                start_ts = new DateTimeOffset(start_dt).ToUnixTimeSeconds();
                end_ts   = new DateTimeOffset(end_dt).ToUnixTimeSeconds();

                if (end_ts >= start_ts)
                {
                    // If the end date is "now", update the previous period that ended "now" to end at the start date of this period.

                    if (endDate == "now")
                    {
                        using (SQLiteCommand cmd = new SQLiteCommand("UPDATE Period SET end_ts = $end_ts WHERE end_ts=\"now\";")) {
                            cmd.Parameters.AddWithValue("$end_ts", start_ts.ToString());

                            await Database.ExecuteNonQuery(cmd);
                        }
                    }

                    // Insert the new period into the database.

                    using (SQLiteCommand cmd = new SQLiteCommand("INSERT OR IGNORE INTO Period(name, start_ts, end_ts) VALUES ($name, $start_ts, $end_ts);")) {
                        cmd.Parameters.AddWithValue("$name", name.ToLower());
                        cmd.Parameters.AddWithValue("$start_ts", start_ts.ToString()); // Period can't start with "now", but can use the "now" timestamp
                        cmd.Parameters.AddWithValue("$end_ts", endDate == "now" ? "now" : end_ts.ToString());

                        await Database.ExecuteNonQuery(cmd);
                    }

                    await BotUtils.ReplyAsync_Success(Context, string.Format("Successfully created new period **{0}**.", StringUtils.ToTitleCase(name)));
                }
                else
                {
                    await BotUtils.ReplyAsync_Error(Context, "The period end date must occur after the period start date.");
                }
            }
            else
            {
                await BotUtils.ReplyAsync_Error(Context, string.Format("Invalid date format provided. Please use the format \"{0}\".", Period.DATE_FORMAT));
            }
        }