From b37fbdf40fdce83d1339f8d7240cd270fa0f246e Mon Sep 17 00:00:00 2001 From: Ren Adachi <49895682+moeyashi@users.noreply.github.com> Date: Wed, 20 Mar 2024 10:49:16 +0900 Subject: [PATCH] feat: add delete command #65 Signed-off-by: Ren Adachi <49895682+moeyashi@users.noreply.github.com> --- src/infra/repository/nita.js | 33 ++++++---------------------- src/slash-command/delete.js | 42 ++++++++++++++++++++++++++++++++++++ src/types.d.ts | 1 + 3 files changed, 49 insertions(+), 27 deletions(-) create mode 100644 src/slash-command/delete.js diff --git a/src/infra/repository/nita.js b/src/infra/repository/nita.js index 07799ba..d9e099d 100644 --- a/src/infra/repository/nita.js +++ b/src/infra/repository/nita.js @@ -2,14 +2,6 @@ import 'dotenv/config'; import postgres from 'postgres'; -/** - * @typedef {import('../../types').Nita} InsertNitaParameters - */ - -/** - * @typedef {import('../../types').Nita} UpdateNitaParameters - */ - /** * @returns {import('../../types').NitaRepository} */ @@ -24,9 +16,6 @@ export const postgresNitaRepository = () => { const sql = postgres(config); return { - /** - * @param {InsertNitaParameters} params - */ async insertNita(params) { const { trackCode, discordUserId, milliseconds, lastMilliseconds } = params; const results = await sql` @@ -34,9 +23,6 @@ export const postgresNitaRepository = () => { `; return results; }, - /** - * @param {UpdateNitaParameters} params - */ async updateNita(params) { const { trackCode, discordUserId, milliseconds, lastMilliseconds } = params; const results = await sql` @@ -44,21 +30,18 @@ export const postgresNitaRepository = () => { `; return results; }, - /** - * @param {string} discordUserId - * @param {string} trackCode - */ async deleteNita(discordUserId, trackCode) { const results = await sql` DELETE FROM nita WHERE discord_user_id = ${discordUserId} AND track_code = ${trackCode} `; return results; }, - /** - * @param {string} discordUserId - * @param {string} trackCode - * @returns {Promise} - */ + async deleteAllNita(discordUserId) { + const results = await sql` + DELETE FROM nita WHERE discord_user_id = ${discordUserId} + `; + return results; + }, async selectNitaByUserAndTrack(discordUserId, trackCode) { const results = await sql` SELECT milliseconds, last_milliseconds FROM nita WHERE discord_user_id = ${discordUserId} AND track_code = ${trackCode} @@ -73,10 +56,6 @@ export const postgresNitaRepository = () => { lastMilliseconds: results[0].last_milliseconds, }; }, - /** - * @param {string} discordUserId - * @returns {Promise} - */ async selectNitaByUser(discordUserId) { const results = await sql` SELECT track_code, milliseconds, last_milliseconds FROM nita WHERE discord_user_id = ${discordUserId} diff --git a/src/slash-command/delete.js b/src/slash-command/delete.js new file mode 100644 index 0000000..291e662 --- /dev/null +++ b/src/slash-command/delete.js @@ -0,0 +1,42 @@ +// @ts-check +import { SlashCommandBuilder } from 'discord.js'; +import { searchTrack } from '../const/track.js'; + +/** @type { import('../types').SlashCommand } */ +export default { + data: new SlashCommandBuilder() + .setName('delete') + .setDescription('自分の記録を削除します。コース名に`all`を入力することですべての記録を削除できます。') + .addStringOption(option => option.setName('track').setDescription('コース名').setRequired(true)), + execute: async (interaction, nitaRepository) => { + const trackQuery = interaction.options.getString('track'); + + if (!trackQuery) { + throw new Error('コース名を指定してください'); + } + + const discordUserId = interaction.member?.user?.id || interaction.user?.id; + if (!discordUserId) { + throw new Error('ユーザーIDが取得できませんでした'); + } + + if (trackQuery === 'all') { + await nitaRepository.deleteAllNita(discordUserId); + await interaction.reply({ + content: '全てのタイムを削除しました。', + }); + return; + } + + const track = searchTrack(trackQuery); + if (!track) { + throw new Error(`コースが見つかりませんでした。\n入力されたコース名: ${trackQuery}`); + } + + await nitaRepository.deleteNita(discordUserId, track.code); + await interaction.reply({ + content: `${track.trackName}のタイムを削除しました。`, + }); + return; + }, +}; diff --git a/src/types.d.ts b/src/types.d.ts index 84dbcca..2b7a8b5 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -17,6 +17,7 @@ export type NitaRepository = { insertNita: (params: InsertNitaParameters) => Promise; updateNita: (params: UpdateNitaParameters) => Promise; deleteNita: (discordUserId: string, trackCode: string) => Promise; + deleteAllNita: (discordUserId: string) => Promise; selectNitaByUserAndTrack: ( discordUserId: string, trackCode: string