Skip to content

Commit 9fd21cd

Browse files
committed
feat: add problem_index to ContestProblem
1 parent 184c3fd commit 9fd21cd

File tree

7 files changed

+52
-26
lines changed

7 files changed

+52
-26
lines changed

atcoder-problems-backend/sql-client/src/contest_problem.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,54 @@ pub trait ContestProblemClient {
1414
#[async_trait]
1515
impl ContestProblemClient for PgPool {
1616
async fn insert_contest_problem(&self, contest_problems: &[ContestProblem]) -> Result<()> {
17-
let (contest_ids, problem_ids): (Vec<&str>, Vec<&str>) = contest_problems
17+
let contest_ids: Vec<&str> = contest_problems
1818
.iter()
19-
.map(|c| (c.contest_id.as_str(), c.problem_id.as_str()))
20-
.unzip();
19+
.map(|c| c.contest_id.as_str())
20+
.collect();
21+
let problem_ids: Vec<&str> = contest_problems
22+
.iter()
23+
.map(|c| c.problem_id.as_str())
24+
.collect();
25+
let problem_indexes: Vec<&str> = contest_problems
26+
.iter()
27+
.map(|c| c.problem_index.as_str())
28+
.collect();
2129

2230
sqlx::query(
2331
r"
24-
INSERT INTO contest_problem (contest_id, problem_id)
32+
INSERT INTO contest_problem (contest_id, problem_id, problem_index)
2533
VALUES (
2634
UNNEST($1::VARCHAR(255)[]),
27-
UNNEST($2::VARCHAR(255)[])
35+
UNNEST($2::VARCHAR(255)[]),
36+
UNNEST($3::VARCHAR(255)[])
2837
)
2938
ON CONFLICT DO NOTHING
3039
",
3140
)
3241
.bind(contest_ids)
3342
.bind(problem_ids)
43+
.bind(problem_indexes)
3444
.execute(self)
3545
.await?;
3646

3747
Ok(())
3848
}
3949

4050
async fn load_contest_problem(&self) -> Result<Vec<ContestProblem>> {
41-
let problems = sqlx::query("SELECT contest_id, problem_id FROM contest_problem")
42-
.try_map(|row: PgRow| {
43-
let contest_id: String = row.try_get("contest_id")?;
44-
let problem_id: String = row.try_get("problem_id")?;
45-
Ok(ContestProblem {
46-
contest_id,
47-
problem_id,
51+
let problems =
52+
sqlx::query("SELECT contest_id, problem_id, problem_index FROM contest_problem")
53+
.try_map(|row: PgRow| {
54+
let contest_id: String = row.try_get("contest_id")?;
55+
let problem_id: String = row.try_get("problem_id")?;
56+
let problem_index: String = row.try_get("problem_index")?;
57+
Ok(ContestProblem {
58+
contest_id,
59+
problem_id,
60+
problem_index,
61+
})
4862
})
49-
})
50-
.fetch_all(self)
51-
.await?;
63+
.fetch_all(self)
64+
.await?;
5265

5366
Ok(problems)
5467
}

atcoder-problems-backend/sql-client/src/models.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub struct UserSum {
104104
pub struct ContestProblem {
105105
pub contest_id: String,
106106
pub problem_id: String,
107+
pub problem_index: String,
107108
}
108109

109110
#[derive(PartialEq, Debug, Serialize)]

atcoder-problems-backend/sql-client/src/rated_point_sum.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ impl RatedPointSumClient for PgPool {
3333
.fetch_all(self);
3434

3535
let rated_problem_ids_fut =
36-
sqlx::query("SELECT contest_id, problem_id FROM contest_problem")
36+
sqlx::query("SELECT contest_id, problem_id, problem_index FROM contest_problem")
3737
.try_map(|row: PgRow| {
3838
let contest_id: String = row.try_get("contest_id")?;
3939
let problem_id: String = row.try_get("problem_id")?;
40+
let problem_index: String = row.try_get("problem_index")?;
4041
Ok(ContestProblem {
4142
contest_id,
4243
problem_id,
44+
problem_index,
4345
})
4446
})
4547
.fetch_all(self);

atcoder-problems-backend/sql-client/tests/test_contest_problem.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ fn create_problem(id: i32) -> ContestProblem {
77
ContestProblem {
88
contest_id: format!("contest{}", id),
99
problem_id: format!("problem{}", id),
10+
problem_index: format!("{}", id),
1011
}
1112
}
1213

atcoder-problems-backend/sql-client/tests/test_rated_point_sum.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,38 +80,45 @@ async fn setup_contest_problems(pool: &PgPool) {
8080
let problems = vec![
8181
ContestProblem {
8282
problem_id: "problem1".to_string(),
83+
problem_index: "1".to_string(),
8384
contest_id: RATED_CONTEST.to_string(),
8485
},
8586
ContestProblem {
8687
problem_id: "problem2".to_string(),
88+
problem_index: "2".to_string(),
8789
contest_id: UNRATED_CONTEST1.to_string(),
8890
},
8991
ContestProblem {
9092
problem_id: "problem3".to_string(),
93+
problem_index: "3".to_string(),
9194
contest_id: UNRATED_CONTEST1.to_string(),
9295
},
9396
ContestProblem {
9497
problem_id: "problem4".to_string(),
98+
problem_index: "4".to_string(),
9599
contest_id: RATED_CONTEST.to_string(),
96100
},
97101
ContestProblem {
98102
problem_id: "problem5".to_string(),
103+
problem_index: "5".to_string(),
99104
contest_id: SAME_CONTEST_RATED.to_string(),
100105
},
101106
ContestProblem {
102107
problem_id: "problem5".to_string(),
108+
problem_index: "5".to_string(),
103109
contest_id: SAME_CONTEST_UNRATED.to_string(),
104110
},
105111
];
106112

107113
for problem in problems {
108114
sqlx::query(
109115
r"
110-
INSERT INTO contest_problem (problem_id, contest_id)
111-
VALUES ($1, $2)
116+
INSERT INTO contest_problem (problem_id, problem_index, contest_id)
117+
VALUES ($1, $2, $3)
112118
",
113119
)
114120
.bind(problem.problem_id)
121+
.bind(problem.problem_index)
115122
.bind(problem.contest_id)
116123
.execute(pool)
117124
.await

atcoder-problems-backend/src/crawler/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,19 @@ impl AtCoderFetcher for AtCoderClient {
7979
contest_id: &str,
8080
) -> Result<(Vec<Problem>, Vec<ContestProblem>)> {
8181
info!("Fetching problems from {} ...", contest_id);
82-
let problems = self.fetch_problem_list(contest_id).await?;
83-
let problems = problems
84-
.into_iter()
85-
.map(convert_problem)
86-
.collect::<Vec<_>>();
87-
let contest_problem = problems
82+
let atcoder_problems = self.fetch_problem_list(contest_id).await?;
83+
let contest_problem = atcoder_problems
8884
.iter()
8985
.map(|problem| ContestProblem {
90-
problem_id: problem.id.clone(),
9186
contest_id: problem.contest_id.clone(),
87+
problem_id: problem.id.clone(),
88+
problem_index: problem.position.clone(),
9289
})
9390
.collect::<Vec<_>>();
91+
let problems = atcoder_problems
92+
.into_iter()
93+
.map(convert_problem)
94+
.collect::<Vec<_>>();
9495
Ok((problems, contest_problem))
9596
}
9697
}

config/database-definition.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ DROP TABLE IF EXISTS contest_problem;
112112
CREATE TABLE contest_problem (
113113
contest_id VARCHAR(255) NOT NULL,
114114
problem_id VARCHAR(255) NOT NULL,
115-
PRIMARY KEY (contest_id, problem_id)
115+
problem_index VARCHAR(255) NOT NULL,
116+
PRIMARY KEY (contest_id, problem_id, problem_index)
116117
);
117118

118119
DROP TABLE IF EXISTS max_streaks;

0 commit comments

Comments
 (0)