def post_issue_route(): """Create one or more issues. :raise 400: created issue violates integrity checks :raise 500: unexpected server error """ database = get_database() created_issues = [] errors = [] with database: try: for issue in to_issue_list(request): created_issue = create_issue(database.cursor(), issue) created_issues.append(created_issue) # TODO: Validate database state against Prolog rules. except sqlite3.IntegrityError as error: errors.append("failed to create rows in sqlite") errors.append(str(error)) return payload({}, errors, 400) except Exception as error: errors.append("unexpected server error") errors.append(str(error)) return payload({}, errors, 500) return payload(created_issues)
def delete_issue_route(id): database = get_database() with database: try: cursor = database.cursor() cursor.execute("DELETE FROM issue WHERE id = ?", (id, )) cursor.execute("DELETE FROM tag WHERE issue_id = ?", (id, )) except Exception as error: errors = [] errors.append("unexpected server error") errors.append(str(error)) return payload({}, errors, 500) return payload()
def put_issue_route(): """Replace one or more issues. :raise 400: created issue violates integrity checks :raise 500: unexpected server error """ database = get_database() updated_issues = [] errors = [] with database: try: cursor = database.cursor() for issue in to_issue_list(request): # Check if the issue exists in the database. if get_issue(cursor, issue["id"]) is None: create_issue(cursor, issue) else: # PUT has replace semantics so we must ensure that all # fields are being updated. For updating a subset of # fields, PATCH should be used. if "title" not in issue: issue["title"] = "" if "description" not in issue: issue["description"] = "" if "tags" not in issue: issue["tags"] = [] updated_issues.append( replace_issue( cursor, issue["id"], issue, ), ) # TODO: Validate database state against Prolog rules. except sqlite3.IntegrityError as error: errors.append("failed to create rows in sqlite") errors.append(str(error)) return payload({}, errors, 400) except Exception as error: errors.append("unexpected server error") errors.append(str(error)) return payload({}, errors, 500) return updated_issues
def get_issue_route(id): """Get issue by id. :param id: issue id :raise 404: issue not found :raise 500: unexpected server error """ try: database = get_database().cursor() issue = get_issue(cursor, id) except Exception as error: errors.append("unexpected server error") errors.append(str(error)) return payload({}, errors, 500) if issue is None: errors = [f"issue #{id} does not exist"] return payload(errors=errors, status_code=404) return payload(issue)
def patch_issue_route(id): """Patch an issue. :raise 400: created issue violates integrity checks :raise 500: unexpected server error """ database = get_database() updated_issues = [] errors = [] with database: try: cursor = database.cursor() for issue in to_issue_list(request): if get_issue(cursor, issue["id"]) is None: errors.append([f"issue #{issue['id']} does not exist"]) return payload({}, errors, 404) else: updated_issues.append( patch_issue( cursor, issue["id"], issue, ), ) # TODO: Validate database state against Prolog rules. except sqlite3.IntegrityError as error: errors.append("failed to create rows in sqlite") errors.append(str(error)) return payload({}, errors, 400) except Exception as error: errors.append("unexpected server error") errors.append(str(error)) return payload({}, errors, 500) return updated_issues