Skip to content

Commit

Permalink
feat: blog get_meta_all accepts a prefix filter, resources get now ac…
Browse files Browse the repository at this point in the history
…cepts Country struct and returns actual lang of retrieved resource
  • Loading branch information
PetoMPP committed Dec 6, 2023
1 parent fabc316 commit 8650a27
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 45 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jwt = "0.16"
lazy_static = "1.4"
num-traits = "0.2"
num-derive = "0.4"
petompp-web-models = { git = "https://github.com/PetoMPP/petompp-web-models.git", branch = "0.4.4", features = [
petompp-web-models = { git = "https://github.com/PetoMPP/petompp-web-models.git", branch = "0.4.6", features = [
"api-errors",
] }
r2d2 = "0.8"
Expand Down
26 changes: 22 additions & 4 deletions src/controllers/blog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use petompp_web_models::{
country::Country,
},
};
use rocket::{get, post, routes, serde::json::Json, State};
use rocket::{delete, get, post, routes, serde::json::Json, State};

pub struct BlogController;

Expand All @@ -18,7 +18,7 @@ impl Controller for BlogController {
}

fn routes(&self) -> Vec<rocket::Route> {
routes![create_or_update, get_meta, get_meta_all]
routes![create_or_update, delete, get_meta, get_meta_all]
}
}

Expand All @@ -41,6 +41,23 @@ async fn create_or_update<'a>(
Ok(Json(ApiResponse::ok("ok")))
}

#[delete("/<name>/<lang>")]
async fn delete<'a>(
_claims: AdminClaims,
name: &'a str,
lang: &'a str,
blob_service: &'a State<AzureBlobService>,
) -> Result<Json<ApiResponse<'a, &'a str>>, ApiError<'a>> {
blob_service
.delete_blog_post(
&name.to_string(),
&Country::try_from(lang)
.map_err(|_| ApiError::from(Error::ValidationError(ValidationError::Country)))?,
)
.await?;
Ok(Json(ApiResponse::ok("ok")))
}

#[get("/meta/<name>/<lang>")]
async fn get_meta<'a>(
name: &'a str,
Expand All @@ -59,11 +76,12 @@ async fn get_meta<'a>(
)))
}

#[get("/meta")]
#[get("/meta?<prefix>")]
async fn get_meta_all<'a>(
blob_service: &'a State<AzureBlobService>,
prefix: Option<String>,
) -> Result<Json<ApiResponse<'a, Vec<BlogMetaData>>>, ApiError<'a>> {
Ok(Json(ApiResponse::ok(
blob_service.get_all_blog_meta().await?,
blob_service.get_all_blog_meta(prefix).await?,
)))
}
29 changes: 20 additions & 9 deletions src/controllers/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
};
use petompp_web_models::{
error::{ApiError, Error, ResourceDataValidationError, ValidationError},
models::{api_response::ApiResponse, resource_data::ResourceData},
models::{api_response::ApiResponse, country::Country, resource_data::ResourceData},
};
use rocket::{delete, get, post, put, routes, serde::json::Json};

Expand All @@ -17,17 +17,17 @@ impl Controller for ResourcesController {
}

fn routes(&self) -> Vec<rocket::Route> {
routes![get, get_all_keys, create, update, delete]
routes![get, get_all_keys, create, update, delete, delete_lang]
}
}

#[get("/<key>?<lang>")]
async fn get<'a>(
key: &'a str,
lang: &'a str,
lang: Country,
pool: &dyn ResourcesRepo,
) -> Result<Json<ApiResponse<'a, String>>, ApiError<'a>> {
Ok(Json(ApiResponse::ok(pool.get(key, lang)?)))
) -> Result<Json<ApiResponse<'a, (Country, String)>>, ApiError<'a>> {
Ok(Json(ApiResponse::ok(pool.get(key, &lang)?)))
}

#[get("/keys")]
Expand Down Expand Up @@ -77,11 +77,22 @@ async fn update<'a>(
}

#[delete("/<key>")]
async fn delete(
async fn delete<'a>(
_admin_claims: AdminClaims,
key: &str,
key: &'a str,
pool: &dyn ResourcesRepo,
) -> Result<&'static str, ApiError<'static>> {
) -> Result<Json<ApiResponse<'a, &'a str>>, ApiError<'a>> {
pool.delete(key)?;
Ok("OK")
Ok(Json(ApiResponse::ok("ok")))
}

#[delete("/<key>?<lang>")]
async fn delete_lang<'a>(
_admin_claims: AdminClaims,
key: &'a str,
lang: Country,
pool: &dyn ResourcesRepo,
) -> Result<Json<ApiResponse<'a, &'a str>>, ApiError<'a>> {
pool.delete_lang(key, &lang)?;
Ok(Json(ApiResponse::ok("ok")))
}
45 changes: 32 additions & 13 deletions src/repositories/resources/repo.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use crate::{models::resource_data::Resource, schema::resources, PgPool};
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
use petompp_web_models::error::{Error, ResourceDataValidationError, ValidationError};
use petompp_web_models::{
error::{Error, ResourceDataValidationError, ValidationError},
models::country::Country,
};
use rocket::{async_trait, http::Status, outcome::Outcome, request::FromRequest, Request};

pub trait ResourcesRepo: Send + Sync {
fn get(&self, key: &str, lang: &str) -> Result<String, Error>;
fn get(&self, key: &str, lang: &Country) -> Result<(Country, String), Error>;
fn get_all(&self) -> Result<Vec<Resource>, Error>;
fn create(&self, data: &Resource) -> Result<Resource, Error>;
fn update(&self, data: &Resource) -> Result<Resource, Error>;
fn delete(&self, key: &str) -> Result<(), Error>;
fn delete_lang(&self, key: &str, lang: &Country) -> Result<(), Error>;
}

#[async_trait]
Expand All @@ -24,19 +28,22 @@ impl<'r> FromRequest<'r> for &'r dyn ResourcesRepo {
}

impl ResourcesRepo for PgPool {
fn get(&self, key: &str, lang: &str) -> Result<String, Error> {
fn get(&self, key: &str, lang: &Country) -> Result<(Country, String), Error> {
let mut conn = self.get()?;
let q = resources::dsl::resources.filter(resources::key.eq(key));
let res = match lang {
"pl" => {
let (pl, en) = q
.select((resources::pl, resources::en))
.get_result::<(Option<String>, String)>(&mut conn)?;
pl.unwrap_or(en)
}
_ => q.select(resources::en).get_result::<String>(&mut conn)?,
};
Ok(res)
Ok(match lang {
Country::Poland => q
.select((resources::pl, resources::en))
.get_result::<(Option<String>, String)>(&mut conn)
.map(|(pl, en)| match pl {
Some(pl) => (Country::Poland, pl),
None => (Country::UnitedKingdom, en),
})?,
_ => q
.select(resources::en)
.get_result::<String>(&mut conn)
.map(|en| (Country::UnitedKingdom, en))?,
})
}

fn get_all(&self) -> Result<Vec<Resource>, Error> {
Expand Down Expand Up @@ -89,4 +96,16 @@ impl ResourcesRepo for PgPool {
.execute(&mut conn)?;
Ok(())
}

fn delete_lang(&self, key: &str, lang: &Country) -> Result<(), Error> {
let mut conn = self.get()?;
match lang {
Country::UnitedKingdom => return Err(Error::ValidationError(ValidationError::Country)),
Country::Poland => diesel::update(resources::dsl::resources)
.filter(resources::dsl::key.eq(key))
.set(resources::dsl::pl.eq::<Option<String>>(None))
.execute(&mut conn)?,
};
Ok(())
}
}
58 changes: 42 additions & 16 deletions src/services/azure_blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,50 @@ impl AzureBlobService {
.map(|_| ())?)
}

pub async fn delete_img(&self, pattern: String) -> Result<usize, Error> {
async fn delete(&self, container: String, pattern: String) -> Result<usize, Error> {
Ok(self
.client
.clone()
.blob_service_client()
.container_client(Self::IMAGE_CONTAINER.to_string())
.container_client(container.clone())
.list_blobs()
.prefix(pattern)
.into_stream()
.fold(Result::<_, Error>::Ok(0), |acc, resp| async move {
let mut count = acc?;
for blob in resp?.blobs.blobs().cloned() {
self.client
.clone()
.blob_client(Self::IMAGE_CONTAINER.to_string(), blob.name)
.delete()
.await?;
count += 1;
.fold(Result::<_, Error>::Ok(0), |acc, resp| {
let container = container.clone();
async move {
let mut count = acc?;
for blob in resp?.blobs.blobs().cloned() {
self.client
.clone()
.blob_client(container.clone(), blob.name)
.delete()
.await?;
count += 1;
}
Ok(count)
}
Ok(count)
})
.await?)
}

pub async fn delete_img(&self, pattern: String) -> Result<usize, Error> {
Self::delete(self, Self::IMAGE_CONTAINER.to_string(), pattern).await
}

pub async fn delete_blog_post(
&self,
id: &String,
lang: &Country,
) -> Result<usize, Error> {
Self::delete(
self,
Self::BLOG_CONTAINER.to_string(),
format!("{}/{}.md", id, lang.key()),
)
.await
}

const BLOG_CONTAINER: &str = "blog";
pub async fn get_blog_meta(&self, id: &String, lang: &Country) -> Result<BlogMetaData, Error> {
let mut stream = self
Expand Down Expand Up @@ -135,17 +155,23 @@ impl AzureBlobService {
BlogMetaData::try_from(curr)
}

pub async fn get_all_blog_meta(&self) -> Result<Vec<BlogMetaData>, Error> {
let mut stream = self
pub async fn get_all_blog_meta(
&self,
prefix: Option<String>,
) -> Result<Vec<BlogMetaData>, Error> {
let mut builder = self
.client
.clone()
.blob_service_client()
.container_client(Self::BLOG_CONTAINER.to_string())
.list_blobs()
.include_metadata(true)
.include_tags(true)
.include_versions(true)
.into_stream();
.include_versions(true);
if let Some(prefix) = prefix {
builder = builder.prefix(prefix);
}
let mut stream = builder.into_stream();
let mut result = HashMap::new();
while let Some(resp) = stream.next().await {
for blob in resp?.blobs.blobs().cloned() {
Expand Down

0 comments on commit 8650a27

Please sign in to comment.