Skip to content

Commit

Permalink
add user achievements count (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnieLeonia authored May 23, 2022
1 parent 8c3abc3 commit fd907a8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion client/src/components/UserAchievements.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<h2 class="user-achievements__title">Achievements</h2>
<div class="user-achievements">
<div
v-for="{ name, title, icon } in achievements"
v-for="{ name, title, icon, count } in achievements"
:key="name"
class="user-achievements__card"
>
Expand All @@ -15,6 +15,7 @@
<div class="banner">
<div class="banner__text async async--text">
{{ title || name || ". . ." }}
{{ count }}
</div>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions server/migrations/20220523-achievements-level.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE achievements
DROP COLUMN level;
2 changes: 2 additions & 0 deletions server/migrations/20220523-user-achievements-count.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE user_achievements
ADD COLUMN count INT NOT NULL DEFAULT 1;
4 changes: 2 additions & 2 deletions server/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ CREATE TABLE achievements (
title VARCHAR(50) NOT NULL,
icon VARCHAR(50),
score INT,
level INT,

created_at TIMESTAMP DEFAULT NOW(),

Expand All @@ -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(),

Expand Down
10 changes: 6 additions & 4 deletions server/src/achievement/achievement-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
},
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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`;
Expand Down

0 comments on commit fd907a8

Please sign in to comment.