Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Add unimplemented API methods #35

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Add unimplemented API methods #35

wants to merge 12 commits into from

Conversation

grahhnt
Copy link
Collaborator

@grahhnt grahhnt commented Aug 19, 2022

No description provided.

@grahhnt grahhnt requested a review from jokil123 August 19, 2022 20:12
Copy link
Collaborator

@jokil123 jokil123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistent use of APIRespose Struct
Use match instead of if
View comments

pub visible: Option<bool>,
}

pub struct APIResponse<R>(pub Status, pub Option<R>);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why implement APIResponse twice?

Err(_) => Err(Status::BadRequest),
}
}

#[get("/server/<id_type>/<id>")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this function to server.rs and delete get.res so its consistent with the other route files

#[get("/builders")]
pub async fn builders_get_all(
conn: Connection<'_, Db>,
) -> Result<Json<Vec<plotsystem_builders::Model>>, APIResponse<String>> {
Copy link
Collaborator

@jokil123 jokil123 Aug 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not return an APIResponse in both cases (like in function builders_get) for more code consistency

#[get("/city_projects")]
pub async fn city_get_all(
conn: Connection<'_, Db>,
) -> Result<Json<Vec<plotsystem_city_projects::Model>>, Status> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider returning APIResponses here aswell

Comment on lines +53 to +57
if (builder.is_none()) {
Ok(APIResponse(Status::NotFound, None))
} else {
Ok(APIResponse(Status::Ok, Some(Json(builder.unwrap()))))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use match syntax here instead.

Suggested change
if (builder.is_none()) {
Ok(APIResponse(Status::NotFound, None))
} else {
Ok(APIResponse(Status::Ok, Some(Json(builder.unwrap()))))
}
match builder {
Some(b) => {
Ok(APIResponse(Status::Ok, Some(Json(b.unwrap()))))
},
None => {
Ok(APIResponse(Status::NotFound, None))
}
}

match db_get::builder::by_uuid(db, parsed_uuid.unwrap()).await {
Ok(builder) => {
if (builder.is_none()) {
Ok(APIResponse(Status::NotFound, None))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unsucessful API request shouldnt return the Ok() Variant. Return an error instead.

Suggested change
Ok(APIResponse(Status::NotFound, None))
Err(APIResponse(Status::NotFound, None))

Comment on lines +115 to +122
// all modify request values are optional; if one is omitted, don't change it
#[derive(Deserialize)]
pub struct EditCityJson {
pub country_id: Option<i32>,
pub name: Option<String>,
pub description: Option<String>,
pub visible: Option<bool>,
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can be merged with the other struct somehow .

Comment on lines +74 to +79
if !country {
return Err(APIResponse(
Status::Unauthorized,
Some("You don't have access to this country.".to_owned()),
));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not neccesarily but maybe replace this with the match syntax

Comment on lines +186 to +204
if !city_json.country_id.is_none() {
city.country_id = Set(city_json.country_id.unwrap().to_owned());
modified = true;
}

if !city_json.name.is_none() {
city.name = Set(city_json.name.as_ref().unwrap().to_owned());
modified = true;
}

if !city_json.description.is_none() {
city.description = Set(city_json.description.as_ref().unwrap().to_owned());
modified = true;
}

if !city_json.visible.is_none() {
city.visible = Set(city_json.visible.unwrap().to_owned());
modified = true;
}
Copy link
Collaborator

@jokil123 jokil123 Aug 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use match syntax for Option<T> and Result<T, Err> types

Comment on lines +164 to +182
if !city_json.country_id.is_none() {
match db_get::api_keys::country_related_to_api_key(
db,
&api_key,
city_json.country_id.unwrap(),
)
.await
{
Ok(country) => {
if !country {
return Err(APIResponse(
Status::Unauthorized,
Some("You don't have access to this country.".to_owned()),
));
}
}
Err(e) => return Err(APIResponse(Status::BadRequest, Some(e.to_string()))),
};
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

match syntax

@MineFact
Copy link

MineFact commented Sep 2, 2022

@jokil123 grahhnt is currently on LOA till the end of the month so it could take a while till he has time to take a look at it again. I will try to find out if anyone else knows Rust and has interest in helping out.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants