Skip to content

Commit 03648a2

Browse files
authored
Merge pull request #23 from harikrishnatp/feature/streaks
Feature/streaks
2 parents 896ee9e + e845df1 commit 03648a2

11 files changed

+123
-8
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE Member ADD COLUMN streak INT;
2+
ALTER TABLE Member ADD COLUMN max_streak INT;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE Member ADD COLUMN group_id INT;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE Streaks (
2+
id SERIAL PRIMARY KEY,
3+
streak INT NOT NULL DEFAULT 0,
4+
max_streak INT NOT NULL DEFAULT 0,
5+
FOREIGN KEY (id) REFERENCES Member(id) ON DELETE CASCADE
6+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE Member
2+
DROP COLUMN streak,
3+
DROP COLUMN max_streak;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE StreakUpdate (
2+
id SERIAL PRIMARY KEY,
3+
streak INT NOT NULL DEFAULT 0,
4+
max_streak INT NOT NULL DEFAULT 0,
5+
FOREIGN KEY (id) REFERENCES Member(id) ON DELETE CASCADE
6+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE Streaks;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS StreakUpdate;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE StreakUpdate (
2+
id SERIAL PRIMARY KEY,
3+
streak INT NOT NULL DEFAULT 0,
4+
max_streak INT NOT NULL DEFAULT 0,
5+
FOREIGN KEY (id) REFERENCES Member(id) ON DELETE CASCADE
6+
);

src/db/member.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ pub struct Member {
1515
pub year: i32,
1616
pub macaddress: String,
1717
pub discord_id: Option<String>,
18+
pub group_id: Option<i32>,
1819
}
20+
21+
#[derive(FromRow, SimpleObject)]
22+
pub struct StreakUpdate {
23+
pub id: i32,
24+
pub streak: Option<i32>,
25+
pub max_streak: Option<i32>,
26+
}

src/graphql/mutations.rs

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use sha2::Sha256;
1111

1212
type HmacSha256 = Hmac<Sha256>;
1313

14-
use crate::db::{member::Member, attendance::Attendance};
14+
use crate::db::{member::Member, attendance::Attendance, member::StreakUpdate};
1515

1616
pub struct MutationRoot;
1717

@@ -30,14 +30,15 @@ impl MutationRoot {
3030
year: i32,
3131
macaddress: String,
3232
discord_id: String,
33+
group_id: i32,
3334

3435
) -> Result<Member, sqlx::Error> {
3536
let pool = ctx.data::<Arc<PgPool>>().expect("Pool not found in context");
3637

3738

3839

3940
let member = sqlx::query_as::<_, Member>(
40-
"INSERT INTO Member (rollno, name, hostel, email, sex, year, macaddress, discord_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *"
41+
"INSERT INTO Member (rollno, name, hostel, email, sex, year, macaddress, discord_id, group_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *"
4142
)
4243
.bind(rollno)
4344
.bind(name)
@@ -47,6 +48,7 @@ impl MutationRoot {
4748
.bind(year)
4849
.bind(macaddress)
4950
.bind(discord_id)
51+
.bind(group_id)
5052
.fetch_one(pool.as_ref())
5153
.await?;
5254

@@ -61,6 +63,7 @@ impl MutationRoot {
6163
year: i32,
6264
macaddress: String,
6365
discord_id: String,
66+
group_id: i32,
6467
hmac_signature: String,
6568
) -> Result<Member,sqlx::Error> {
6669
let pool = ctx.data::<Arc<PgPool>>().expect("Pool not found in context");
@@ -69,7 +72,7 @@ impl MutationRoot {
6972

7073
let mut mac = HmacSha256::new_from_slice(secret_key.as_bytes()).expect("HMAC can take key of any size");
7174

72-
let message = format!("{}{}{}{}{}", id, hostel, year, macaddress, discord_id);
75+
let message = format!("{}{}{}{}{}{}", id, hostel, year, macaddress, discord_id, group_id);
7376
mac.update(message.as_bytes());
7477

7578
let expected_signature = mac.finalize().into_bytes();
@@ -91,8 +94,9 @@ impl MutationRoot {
9194
hostel = CASE WHEN $1 = '' THEN hostel ELSE $1 END,
9295
year = CASE WHEN $2 = 0 THEN year ELSE $2 END,
9396
macaddress = CASE WHEN $3 = '' THEN macaddress ELSE $3 END,
94-
discord_id = CASE WHEN $4 = '' THEN discord_id ELSE $4 END
95-
WHERE id = $5
97+
discord_id = CASE WHEN $4 = '' THEN discord_id ELSE $4 END,
98+
group_id = CASE WHEN $5 = 0 THEN group_id ELSE $5 END
99+
WHERE id = $6
96100
RETURNING *
97101
"
98102
)
@@ -101,6 +105,7 @@ impl MutationRoot {
101105
.bind(year)
102106
.bind(macaddress)
103107
.bind(discord_id)
108+
.bind(group_id)
104109
.bind(id)
105110
.fetch_one(pool.as_ref())
106111
.await?;
@@ -158,7 +163,6 @@ impl MutationRoot {
158163
mac.update(message.as_bytes());
159164

160165
let expected_signature = mac.finalize().into_bytes();
161-
162166

163167
// Convert the received HMAC signature from the client to bytes for comparison
164168
let received_signature = hex::decode(hmac_signature)
@@ -192,4 +196,68 @@ impl MutationRoot {
192196

193197
Ok(attendance)
194198
}
195-
}
199+
async fn update_streak(
200+
&self,
201+
ctx: &Context<'_>,
202+
id: i32,
203+
has_sent_update: bool,
204+
) -> Result<StreakUpdate, sqlx::Error> {
205+
let pool = ctx.data::<Arc<PgPool>>().expect("Pool not found in context");
206+
207+
let streak_info = sqlx::query_as::<_, StreakUpdate>(
208+
"
209+
SELECT id, streak, max_streak
210+
FROM StreakUpdate
211+
WHERE id = $1
212+
"
213+
)
214+
.bind(id)
215+
.fetch_optional(pool.as_ref())
216+
.await?;
217+
218+
match streak_info{
219+
Some(mut member) => {
220+
let current_streak = member.streak.unwrap_or(0);
221+
let max_streak = member.max_streak.unwrap_or(0);
222+
let (new_streak, new_max_streak) = if has_sent_update {
223+
let updated_streak = current_streak + 1;
224+
let updated_max_streak = updated_streak.max(max_streak);
225+
(updated_streak, updated_max_streak)
226+
} else {
227+
(0, max_streak)
228+
};
229+
let updated_member = sqlx::query_as::<_, StreakUpdate>(
230+
"
231+
UPDATE StreakUpdate
232+
SET streak = $1, max_streak = $2
233+
WHERE id = $3
234+
RETURNING *
235+
"
236+
)
237+
.bind(new_streak)
238+
.bind(new_max_streak)
239+
.bind(id)
240+
.fetch_one(pool.as_ref())
241+
.await?;
242+
243+
Ok(updated_member)
244+
},
245+
None => {
246+
let new_member = sqlx::query_as::<_, StreakUpdate>(
247+
"
248+
INSERT INTO StreakUpdate (id, streak, max_streak)
249+
VALUES ($1, $2, $3)
250+
RETURNING *
251+
"
252+
)
253+
.bind(id)
254+
.bind(0)
255+
.bind(0)
256+
.fetch_one(pool.as_ref())
257+
.await?;
258+
259+
Ok(new_member)
260+
}
261+
}
262+
}
263+
}

0 commit comments

Comments
 (0)