From fd907a8f95f26dcd09dac99234d8ec22a42b594e Mon Sep 17 00:00:00 2001 From: AnnieLeonia Date: Mon, 23 May 2022 22:36:25 +0200 Subject: [PATCH] add user achievements count (#351) --- client/src/components/UserAchievements.vue | 3 ++- server/migrations/20220523-achievements-level.sql | 2 ++ server/migrations/20220523-user-achievements-count.sql | 2 ++ server/schema.sql | 4 ++-- server/src/achievement/achievement-model.js | 10 ++++++---- 5 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 server/migrations/20220523-achievements-level.sql create mode 100644 server/migrations/20220523-user-achievements-count.sql diff --git a/client/src/components/UserAchievements.vue b/client/src/components/UserAchievements.vue index 88e92269..33955b84 100644 --- a/client/src/components/UserAchievements.vue +++ b/client/src/components/UserAchievements.vue @@ -3,7 +3,7 @@

Achievements

@@ -15,6 +15,7 @@
diff --git a/server/migrations/20220523-achievements-level.sql b/server/migrations/20220523-achievements-level.sql new file mode 100644 index 00000000..0d20fffc --- /dev/null +++ b/server/migrations/20220523-achievements-level.sql @@ -0,0 +1,2 @@ +ALTER TABLE achievements +DROP COLUMN level; \ No newline at end of file diff --git a/server/migrations/20220523-user-achievements-count.sql b/server/migrations/20220523-user-achievements-count.sql new file mode 100644 index 00000000..c0ce4348 --- /dev/null +++ b/server/migrations/20220523-user-achievements-count.sql @@ -0,0 +1,2 @@ +ALTER TABLE user_achievements +ADD COLUMN count INT NOT NULL DEFAULT 1; \ No newline at end of file diff --git a/server/schema.sql b/server/schema.sql index fb570783..677edfbf 100644 --- a/server/schema.sql +++ b/server/schema.sql @@ -67,7 +67,6 @@ CREATE TABLE achievements ( title VARCHAR(50) NOT NULL, icon VARCHAR(50), score INT, - level INT, created_at TIMESTAMP DEFAULT NOW(), @@ -76,9 +75,10 @@ CREATE TABLE achievements ( DROP TABLE IF EXISTS user_achievements CASCADE; CREATE TABLE user_achievements ( - popup BOOLEAN NOT NULL DEFAULT FALSE, user_id INT, achievement_name VARCHAR(36), + count INT NOT NULL DEFAULT 1, + popup BOOLEAN NOT NULL DEFAULT FALSE, created_at TIMESTAMP DEFAULT NOW(), diff --git a/server/src/achievement/achievement-model.js b/server/src/achievement/achievement-model.js index 7a25e945..470afc93 100644 --- a/server/src/achievement/achievement-model.js +++ b/server/src/achievement/achievement-model.js @@ -2,7 +2,9 @@ module.exports = db => ({ create: async (userId, name) => { const sql = ` INSERT INTO user_achievements (user_id, achievement_name) VALUES ($1, $2) - ON CONFLICT DO NOTHING RETURNING *`; + ON CONFLICT (user_id, achievement_name) DO UPDATE + SET count = user_achievements.count + 1 + RETURNING *`; const { rows, err } = await db.query(sql, [userId, name]); return { achievement: rows[0], err }; }, @@ -19,7 +21,7 @@ module.exports = db => ({ getByName: async (userId, name) => { const sql = ` - SELECT achievement_name AS name, title, icon, score, level, user_achievements.created_at + SELECT achievement_name AS name, title, icon, score, count, user_achievements.created_at FROM user_achievements FULL JOIN achievements ON user_achievements.achievement_name = achievements.name WHERE user_id = $1 AND achievement_name = $2 @@ -31,7 +33,7 @@ module.exports = db => ({ getNew: async userId => { const sql = ` - SELECT achievement_name AS name, title, icon, score, level, user_achievements.created_at + SELECT achievement_name AS name, title, icon, score, count, user_achievements.created_at FROM user_achievements FULL JOIN achievements ON user_achievements.achievement_name = achievements.name WHERE popup = 'f' AND user_id = $1 @@ -43,7 +45,7 @@ module.exports = db => ({ getAll: async userId => { const sql = ` - SELECT achievement_name AS name, title, icon, score, level, user_achievements.created_at + SELECT achievement_name AS name, title, icon, score, count, user_achievements.created_at FROM user_achievements FULL JOIN achievements ON user_achievements.achievement_name = achievements.name WHERE popup = 't' AND user_id = $1`;