summaryrefslogtreecommitdiffstats
path: root/rust/src/api_server.rs
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-06-30 18:40:27 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-06-30 18:40:27 -0700
commitc88af3a8a92329a598287b5dd3457030e3b4529f (patch)
tree12db7b14015ec028f02c1c329612552db7f8b74f /rust/src/api_server.rs
parent08f7f1642eb8380c5b00f6a54e4b29e55713effd (diff)
downloadfatcat-c88af3a8a92329a598287b5dd3457030e3b4529f.tar.gz
fatcat-c88af3a8a92329a598287b5dd3457030e3b4529f.zip
generic changelog endpoints
Diffstat (limited to 'rust/src/api_server.rs')
-rw-r--r--rust/src/api_server.rs85
1 files changed, 78 insertions, 7 deletions
diff --git a/rust/src/api_server.rs b/rust/src/api_server.rs
index f895937e..fa767699 100644
--- a/rust/src/api_server.rs
+++ b/rust/src/api_server.rs
@@ -950,18 +950,50 @@ impl Server {
.load(&conn)?;
let entries = changes
- .iter()
- .map(|(row, _)| ChangelogEntry {
- index: row.id,
- editgroup_id: row.editgroup_id,
- timestamp: chrono::DateTime::from_utc(row.timestamp, chrono::Utc),
+ .into_iter()
+ .map(|(cl_row, eg_row)| ChangelogEntry {
+ index: cl_row.id,
+ editgroup: Some(eg_row.to_model_partial()),
+ editgroup_id: cl_row.editgroup_id,
+ timestamp: chrono::DateTime::from_utc(cl_row.timestamp, chrono::Utc),
})
.collect();
Ok(entries)
}
- /// "more" parameter isn't used, but could be to indicate that "expensive" database queries
- /// should be run
+ fn get_changelog_handler(&self, limit: Option<i64>) -> Result<Vec<ChangelogEntry>> {
+ let conn = self.db_pool.get().expect("db_pool error");
+ let limit = limit.unwrap_or(50);
+
+ let changes: Vec<(ChangelogRow, EditgroupRow)> = changelog::table
+ .inner_join(editgroup::table)
+ .order(changelog::id.desc())
+ .limit(limit)
+ .load(&conn)?;
+
+ let entries = changes
+ .into_iter()
+ .map(|(cl_row, eg_row)| ChangelogEntry {
+ index: cl_row.id,
+ editgroup: Some(eg_row.to_model_partial()),
+ editgroup_id: cl_row.editgroup_id,
+ timestamp: chrono::DateTime::from_utc(cl_row.timestamp, chrono::Utc),
+ })
+ .collect();
+ Ok(entries)
+ }
+
+ fn get_changelog_entry_handler(&self, id: i64) -> Result<ChangelogEntry> {
+ let conn = self.db_pool.get().expect("db_pool error");
+
+ let cl_row: ChangelogRow = changelog::table.find(id).first(&conn)?;
+ let editgroup = self.get_editgroup_handler(cl_row.editgroup_id)?;
+
+ let mut entry = cl_row.to_model();
+ entry.editgroup = Some(editgroup);
+ Ok(entry)
+ }
+
fn get_stats_handler(&self, more: Option<String>) -> Result<StatsResponse> {
let conn = self.db_pool.get().expect("db_pool error");
@@ -1304,6 +1336,45 @@ impl Api for Server {
Box::new(futures::done(Ok(ret)))
}
+ fn get_changelog(
+ &self,
+ limit: Option<i64>,
+ _context: &Context,
+ ) -> Box<Future<Item = GetChangelogResponse, Error = ApiError> + Send> {
+ let ret = match self.get_changelog_handler(limit) {
+ Ok(changelog) => GetChangelogResponse::Success(changelog),
+ Err(e) => {
+ error!("{}", e);
+ GetChangelogResponse::GenericError(ErrorResponse {
+ message: e.to_string(),
+ })
+ }
+ };
+ Box::new(futures::done(Ok(ret)))
+ }
+
+ fn get_changelog_entry(
+ &self,
+ id: i64,
+ _context: &Context,
+ ) -> Box<Future<Item = GetChangelogEntryResponse, Error = ApiError> + Send> {
+ let ret = match self.get_changelog_entry_handler(id) {
+ Ok(entry) => GetChangelogEntryResponse::FoundChangelogEntry(entry),
+ Err(Error(ErrorKind::Diesel(::diesel::result::Error::NotFound), _)) => {
+ GetChangelogEntryResponse::NotFound(ErrorResponse {
+ message: format!("No such changelog entry: {}", id),
+ })
+ }
+ Err(e) => {
+ error!("{}", e);
+ GetChangelogEntryResponse::GenericError(ErrorResponse {
+ message: e.to_string(),
+ })
+ }
+ };
+ Box::new(futures::done(Ok(ret)))
+ }
+
fn get_stats(
&self,
more: Option<String>,