api: just return error on db failure

let's do conflict properly
This commit is contained in:
Peter Cai 2020-02-21 19:44:52 +08:00
parent 7046e51d01
commit ecf3998b5a
No known key found for this signature in database
GPG Key ID: 71F5FB4E4F3FD54F
1 changed files with 3 additions and 12 deletions

View File

@ -173,22 +173,13 @@ fn items_sync(db: DbConn, u: user::User, params: Json<SyncParams>) -> Custom<Jso
// First, update all items sent by client
for mut it in inner_params.items.into_iter() {
let old_updated_at = it.updated_at.clone();
// Always update updated_at for all items on server
it.updated_at =
Some(chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true));
if let Err(item::ItemOpError(_)) = item::SyncItem::items_insert(&db, &u, &it) {
// Well, we should try twice...
// TODO: make this more elegant (also handle differneces between db error and conflict)
// (if we were ever to implement a conflict feature)
if let Err(item::ItemOpError(_)) = item::SyncItem::items_insert(&db, &u, &it) {
// Let's not fail just because one of them...
// At least the client will know there's an error
// (maybe mistakes it for conflict)
it.updated_at = old_updated_at;
resp.unsaved.push(it);
}
if let Err(item::ItemOpError(e)) = item::SyncItem::items_insert(&db, &u, &it) {
// We can just return an error here
return error_resp(Status::InternalServerError, vec![e]);
} else {
resp.saved_items.push(it);
}