2022-02-04 19:36:54 +00:00
|
|
|
use tracing::instrument;
|
|
|
|
|
|
|
|
use crate::api::api_prelude::*;
|
|
|
|
use crate::error::{Error, Result};
|
|
|
|
|
|
|
|
#[instrument]
|
|
|
|
#[get("")]
|
2022-02-04 20:28:53 +00:00
|
|
|
pub async fn list_bookmark_categories(state: web::Data<AppState>) -> Result<HttpResponse> {
|
|
|
|
let cats: Vec<bookmark_category::Model> =
|
|
|
|
BookmarkCategory::find().all(&state.db).await.unwrap();
|
2022-02-04 19:36:54 +00:00
|
|
|
let count = cats.len();
|
|
|
|
Ok(HttpResponse::Ok().json(ListObjects::new(cats, count)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument]
|
|
|
|
#[post("")]
|
2022-02-04 20:28:53 +00:00
|
|
|
pub async fn new_bookmark_category(
|
2022-02-04 19:36:54 +00:00
|
|
|
state: web::Data<AppState>,
|
2022-02-04 20:28:53 +00:00
|
|
|
data: web::Json<bookmark_category::Model>,
|
2022-02-04 19:36:54 +00:00
|
|
|
) -> Result<HttpResponse> {
|
2022-02-04 20:28:53 +00:00
|
|
|
let model = bookmark_category::ActiveModel {
|
2022-02-04 19:36:54 +00:00
|
|
|
id: NotSet,
|
|
|
|
category_name: Set(data.0.category_name),
|
2022-02-04 20:28:53 +00:00
|
|
|
glyph: Set(data.0.glyph),
|
2022-02-04 19:36:54 +00:00
|
|
|
active: Set(data.0.active),
|
|
|
|
};
|
|
|
|
let rec = model.insert(&state.db).await?;
|
|
|
|
Ok(HttpResponse::Ok().json(rec))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument]
|
|
|
|
#[get("{id}")]
|
2022-02-04 20:28:53 +00:00
|
|
|
pub async fn get_bookmark_category(
|
2022-02-04 19:36:54 +00:00
|
|
|
state: web::Data<AppState>,
|
|
|
|
id: web::Path<i32>,
|
|
|
|
) -> Result<HttpResponse> {
|
|
|
|
let id = id.into_inner();
|
2022-02-04 20:28:53 +00:00
|
|
|
let res: Option<bookmark_category::Model> =
|
|
|
|
BookmarkCategory::find_by_id(id).one(&state.db).await?;
|
2022-02-04 19:36:54 +00:00
|
|
|
match res {
|
|
|
|
Some(rec) => Ok(HttpResponse::Ok().json(rec)),
|
|
|
|
None => Err(Error::not_found()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument]
|
|
|
|
#[put("{id}")]
|
2022-02-04 20:28:53 +00:00
|
|
|
pub async fn update_bookmark_category(
|
2022-02-04 19:36:54 +00:00
|
|
|
state: web::Data<AppState>,
|
|
|
|
|
2022-02-04 20:28:53 +00:00
|
|
|
data: web::Json<bookmark_category::Model>,
|
2022-02-04 19:36:54 +00:00
|
|
|
id: web::Path<i32>,
|
|
|
|
) -> Result<HttpResponse> {
|
|
|
|
let id = id.into_inner();
|
2022-02-04 20:28:53 +00:00
|
|
|
let res: Option<bookmark_category::Model> =
|
|
|
|
BookmarkCategory::find_by_id(id).one(&state.db).await?;
|
2022-02-04 19:36:54 +00:00
|
|
|
match res {
|
|
|
|
Some(_rec) => {
|
|
|
|
let data = data.into_inner();
|
2022-02-04 20:28:53 +00:00
|
|
|
let ret = bookmark_category::ActiveModel {
|
2022-02-04 19:36:54 +00:00
|
|
|
id: Set(id),
|
|
|
|
active: Set(data.active),
|
2022-02-04 20:28:53 +00:00
|
|
|
glyph: Set(data.glyph),
|
2022-02-04 19:36:54 +00:00
|
|
|
category_name: Set(data.category_name),
|
|
|
|
};
|
|
|
|
let model = ret.update(&state.db).await?;
|
|
|
|
Ok(HttpResponse::Ok().json(model))
|
|
|
|
}
|
|
|
|
None => Err(Error::not_found()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument]
|
|
|
|
#[delete("{id}")]
|
2022-02-04 20:28:53 +00:00
|
|
|
pub async fn delete_bookmark_category(
|
2022-02-04 19:36:54 +00:00
|
|
|
state: web::Data<AppState>,
|
|
|
|
id: web::Path<i32>,
|
|
|
|
) -> Result<HttpResponse> {
|
2022-02-04 20:28:53 +00:00
|
|
|
BookmarkCategory::delete_many()
|
|
|
|
.filter(bookmark_category::Column::Id.eq(id.into_inner()))
|
2022-02-04 19:36:54 +00:00
|
|
|
.exec(&state.db)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().body(""))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument]
|
2022-02-04 20:28:53 +00:00
|
|
|
#[get("{id}/bookmarks")]
|
|
|
|
pub async fn bookmark_category_bookmarks(
|
2022-02-04 19:36:54 +00:00
|
|
|
state: web::Data<AppState>,
|
|
|
|
id: web::Path<i32>,
|
|
|
|
) -> Result<HttpResponse> {
|
2022-02-04 20:28:53 +00:00
|
|
|
let recs: Vec<bookmark::Model> = Bookmark::find()
|
|
|
|
.filter(bookmark::Column::BookmarkCategoryId.eq(id.into_inner()))
|
2022-02-04 19:36:54 +00:00
|
|
|
.all(&state.db)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let count = recs.len();
|
|
|
|
Ok(HttpResponse::Ok().json(ListObjects::new(recs, count)))
|
|
|
|
}
|
|
|
|
|
2022-02-04 20:28:53 +00:00
|
|
|
/// Routes for the bookmark endpoints. This binds up a scope with all endpoints for bookmarks, to make it easier to add them to the server.
|
2022-02-04 19:36:54 +00:00
|
|
|
pub fn routes() -> Scope {
|
2022-02-04 20:28:53 +00:00
|
|
|
web::scope("/bookmark_categories")
|
|
|
|
.service(bookmark_category_bookmarks)
|
|
|
|
.service(list_bookmark_categories)
|
|
|
|
.service(update_bookmark_category)
|
|
|
|
.service(delete_bookmark_category)
|
|
|
|
.service(new_bookmark_category)
|
|
|
|
.service(get_bookmark_category)
|
2022-02-04 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use crate::api::test_prelude::*;
|
|
|
|
use actix_web::http::Method;
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2022-02-04 20:28:53 +00:00
|
|
|
async fn test_list_bookmark_categories() -> Result<()> {
|
2022-02-04 19:36:54 +00:00
|
|
|
let state = setup_state().await?;
|
2022-02-04 20:28:53 +00:00
|
|
|
bookmark_category::ActiveModel {
|
|
|
|
category_name: Set("Bookmark 1".into()),
|
2022-02-04 19:36:54 +00:00
|
|
|
active: Set(true),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.insert(&state.db)
|
|
|
|
.await?;
|
2022-02-04 20:28:53 +00:00
|
|
|
bookmark_category::ActiveModel {
|
|
|
|
category_name: Set("Bookmark 2".into()),
|
2022-02-04 19:36:54 +00:00
|
|
|
active: Set(true),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.insert(&state.db)
|
|
|
|
.await?;
|
|
|
|
|
2022-02-05 06:11:24 +00:00
|
|
|
let mut req = actix_web::test::TestRequest::with_uri("/bookmark_categories")
|
2022-02-04 19:36:54 +00:00
|
|
|
.method(Method::GET)
|
|
|
|
.to_request();
|
|
|
|
let resp = call_endpoint!(req, state);
|
|
|
|
assert_eq!(resp.status(), 200);
|
2022-02-04 20:28:53 +00:00
|
|
|
let data = get_response!(resp, ListObjects<crate::entity::bookmark_category::Model>);
|
2022-02-04 19:36:54 +00:00
|
|
|
assert_eq!(2_usize, data.items.len());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2022-02-04 20:28:53 +00:00
|
|
|
async fn test_get_bookmark_categories() -> Result<()> {
|
2022-02-04 19:36:54 +00:00
|
|
|
let state = setup_state().await?;
|
2022-02-04 20:28:53 +00:00
|
|
|
let model = bookmark_category::ActiveModel {
|
|
|
|
category_name: Set("Bookmark 1".into()),
|
2022-02-04 19:36:54 +00:00
|
|
|
active: Set(true),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.insert(&state.db)
|
|
|
|
.await?;
|
|
|
|
|
2022-02-05 06:11:24 +00:00
|
|
|
let mut req =
|
2022-02-04 20:28:53 +00:00
|
|
|
actix_web::test::TestRequest::with_uri(&format!("/bookmark_categories/{}", model.id))
|
|
|
|
.method(Method::GET)
|
|
|
|
.to_request();
|
2022-02-04 19:36:54 +00:00
|
|
|
let resp = call_endpoint!(req, state);
|
|
|
|
let status = resp.status();
|
2022-02-04 20:28:53 +00:00
|
|
|
let mut data = get_response!(resp, crate::entity::bookmark_category::Model);
|
2022-02-04 19:36:54 +00:00
|
|
|
data.id = model.id;
|
|
|
|
assert_eq!(model, data);
|
|
|
|
assert_eq!(status, 200);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2022-02-04 20:28:53 +00:00
|
|
|
async fn test_new_bookmark_category() -> Result<()> {
|
|
|
|
let model = bookmark_category::Model {
|
2022-02-04 19:36:54 +00:00
|
|
|
id: 0,
|
|
|
|
category_name: "Some name".into(),
|
2022-02-04 20:28:53 +00:00
|
|
|
glyph: None,
|
2022-02-04 19:36:54 +00:00
|
|
|
active: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
let state = setup_state().await?;
|
|
|
|
|
2022-02-05 06:11:24 +00:00
|
|
|
let mut req = actix_web::test::TestRequest::with_uri("/bookmark_categories")
|
2022-02-04 19:36:54 +00:00
|
|
|
.method(Method::POST)
|
|
|
|
.set_json(model.clone())
|
|
|
|
.to_request();
|
|
|
|
|
|
|
|
let resp = call_endpoint!(req, state);
|
|
|
|
assert_eq!(resp.status(), 200);
|
2022-02-04 20:28:53 +00:00
|
|
|
let data = get_response!(resp, crate::entity::bookmark_category::Model);
|
2022-02-04 19:36:54 +00:00
|
|
|
assert_eq!(model, data);
|
2022-02-04 20:28:53 +00:00
|
|
|
assert_eq!(bookmark_category::Entity::find().count(&state.db).await?, 1);
|
2022-02-04 19:36:54 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2022-02-04 20:28:53 +00:00
|
|
|
async fn test_update_bookmark_category() -> Result<()> {
|
2022-02-04 19:36:54 +00:00
|
|
|
let state = setup_state().await?;
|
2022-02-04 20:28:53 +00:00
|
|
|
bookmark_category::ActiveModel {
|
2022-02-04 19:36:54 +00:00
|
|
|
category_name: Set("Some name".into()),
|
|
|
|
active: Set(true),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.insert(&state.db)
|
|
|
|
.await?;
|
|
|
|
|
2022-02-04 20:28:53 +00:00
|
|
|
let mut model = bookmark_category::ActiveModel {
|
2022-02-04 19:36:54 +00:00
|
|
|
category_name: Set("Some name".into()),
|
|
|
|
active: Set(true),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.insert(&state.db)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
model.category_name = "Another name".into();
|
|
|
|
|
2022-02-05 06:11:24 +00:00
|
|
|
let mut req =
|
2022-02-04 20:28:53 +00:00
|
|
|
actix_web::test::TestRequest::with_uri(&format!("/bookmark_categories/{}", model.id))
|
|
|
|
.method(Method::PUT)
|
|
|
|
.set_json(model.clone())
|
|
|
|
.to_request();
|
2022-02-04 19:36:54 +00:00
|
|
|
let resp = call_endpoint!(req, state);
|
|
|
|
assert_eq!(resp.status(), 200);
|
2022-02-04 20:28:53 +00:00
|
|
|
let mut data = get_response!(resp, crate::entity::bookmark_category::Model);
|
2022-02-04 19:36:54 +00:00
|
|
|
data.id = model.id;
|
|
|
|
assert_eq!(model, data, "Check API");
|
|
|
|
|
2022-02-04 20:28:53 +00:00
|
|
|
let db_model = bookmark_category::Entity::find_by_id(model.id)
|
2022-02-04 19:36:54 +00:00
|
|
|
.one(&state.db)
|
|
|
|
.await?
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(db_model, model, "Check DB");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2022-02-04 20:28:53 +00:00
|
|
|
async fn test_delete_bookmark_category() -> Result<()> {
|
2022-02-04 19:36:54 +00:00
|
|
|
let state = setup_state().await?;
|
2022-02-04 20:28:53 +00:00
|
|
|
let model = bookmark_category::ActiveModel {
|
2022-02-04 19:36:54 +00:00
|
|
|
category_name: Set("Some name".into()),
|
|
|
|
active: Set(true),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.insert(&state.db)
|
|
|
|
.await?;
|
|
|
|
|
2022-02-05 06:11:24 +00:00
|
|
|
let mut req =
|
2022-02-04 20:28:53 +00:00
|
|
|
actix_web::test::TestRequest::with_uri(&format!("/bookmark_categories/{}", model.id))
|
|
|
|
.method(Method::DELETE)
|
|
|
|
.to_request();
|
2022-02-04 19:36:54 +00:00
|
|
|
let resp = call_endpoint!(req, state);
|
|
|
|
assert_eq!(resp.status(), 200);
|
2022-02-04 20:28:53 +00:00
|
|
|
assert_eq!(bookmark_category::Entity::find().count(&state.db).await?, 0);
|
2022-02-04 19:36:54 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2022-02-04 20:28:53 +00:00
|
|
|
async fn test_bookmark_categories_bookmarks() -> Result<()> {
|
2022-02-04 19:36:54 +00:00
|
|
|
let state = setup_state().await?;
|
2022-02-04 20:28:53 +00:00
|
|
|
let category = bookmark_category::ActiveModel {
|
|
|
|
category_name: Set("Bookmark 1".into()),
|
2022-02-04 19:36:54 +00:00
|
|
|
active: Set(true),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.insert(&state.db)
|
|
|
|
.await?;
|
2022-02-04 20:28:53 +00:00
|
|
|
bookmark::ActiveModel {
|
|
|
|
bookmark_name: Set("Bookmark 1".into()),
|
2022-02-04 19:36:54 +00:00
|
|
|
url: Set("http://somewhere/".into()),
|
|
|
|
active: Set(true),
|
2022-02-04 20:28:53 +00:00
|
|
|
bookmark_category_id: Set(Some(category.id)),
|
2022-02-04 19:36:54 +00:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.insert(&state.db)
|
|
|
|
.await?;
|
2022-02-04 20:28:53 +00:00
|
|
|
bookmark::ActiveModel {
|
|
|
|
bookmark_name: Set("Bookmark 2".into()),
|
2022-02-04 19:36:54 +00:00
|
|
|
url: Set("http://somewhere/".into()),
|
2022-02-04 20:28:53 +00:00
|
|
|
bookmark_category_id: Set(Some(category.id)),
|
2022-02-04 19:36:54 +00:00
|
|
|
active: Set(true),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.insert(&state.db)
|
|
|
|
.await?;
|
2022-02-04 20:28:53 +00:00
|
|
|
bookmark::ActiveModel {
|
|
|
|
bookmark_name: Set("Bookmark 2".into()),
|
2022-02-04 19:36:54 +00:00
|
|
|
url: Set("http://somewhere/".into()),
|
|
|
|
active: Set(true),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.insert(&state.db)
|
|
|
|
.await?;
|
|
|
|
|
2022-02-05 06:11:24 +00:00
|
|
|
let mut req = actix_web::test::TestRequest::with_uri(&format!(
|
2022-02-04 20:28:53 +00:00
|
|
|
"/bookmark_categories/{}/bookmarks",
|
2022-02-04 19:36:54 +00:00
|
|
|
category.id
|
|
|
|
))
|
|
|
|
.method(Method::GET)
|
|
|
|
.to_request();
|
|
|
|
let resp = call_endpoint!(req, state);
|
|
|
|
assert_eq!(resp.status(), 200);
|
2022-02-04 20:28:53 +00:00
|
|
|
let data = get_response!(resp, ListObjects<crate::entity::bookmark::Model>);
|
2022-02-04 19:36:54 +00:00
|
|
|
assert_eq!(2_usize, data.items.len());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|