update
This commit is contained in:
parent
dcf546fa9c
commit
1e67f223f7
10 changed files with 164 additions and 92 deletions
|
@ -1,12 +1,12 @@
|
|||
use std::cmp::Ordering;
|
||||
|
||||
use super::api_error;
|
||||
use super::FallibleApiResponse;
|
||||
use mongod::Referencable;
|
||||
use mongodb::bson::doc;
|
||||
use rocket::fs::NamedFile;
|
||||
use rocket::*;
|
||||
use serde_json::json;
|
||||
use rocket::fs::NamedFile;
|
||||
use mongod::Referencable;
|
||||
use super::FallibleApiResponse;
|
||||
use super::api_error;
|
||||
|
||||
use crate::library::Libary;
|
||||
|
||||
|
@ -80,4 +80,4 @@ pub async fn album_route(album_id: &str, lib: &State<Libary>) -> FallibleApiResp
|
|||
.insert("tracks".into(), tracks.into());
|
||||
|
||||
Ok(album)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use super::api_error;
|
||||
use super::FallibleApiResponse;
|
||||
use mongodb::bson::doc;
|
||||
use rocket::*;
|
||||
use super::FallibleApiResponse;
|
||||
use super::api_error;
|
||||
|
||||
use crate::library::Libary;
|
||||
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
use rocket::response::status::BadRequest;
|
||||
use serde_json::json;
|
||||
|
||||
pub mod artist;
|
||||
pub mod album;
|
||||
pub mod artist;
|
||||
pub mod track;
|
||||
pub mod user;
|
||||
|
||||
|
||||
type ApiError = BadRequest<serde_json::Value>;
|
||||
type FallibleApiResponse = Result<serde_json::Value, ApiError>;
|
||||
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
use rocket::*;
|
||||
use super::api_error;
|
||||
use super::FallibleApiResponse;
|
||||
use fs::NamedFile;
|
||||
use mongodb::bson::doc;
|
||||
use super::FallibleApiResponse;
|
||||
use super::api_error;
|
||||
use rocket::*;
|
||||
|
||||
use crate::library::Libary;
|
||||
|
||||
#[get("/track/<track_id>")]
|
||||
pub async fn track_route(track_id: &str, lib: &State<Libary>) -> FallibleApiResponse {
|
||||
Ok(serde_json::to_value(
|
||||
&lib.get_track_by_id(track_id)
|
||||
.await
|
||||
.ok_or_else(|| api_error("No track with that ID found"))?,
|
||||
)
|
||||
.unwrap())
|
||||
Ok(lib
|
||||
.get_track_by_id(track_id)
|
||||
.await
|
||||
.ok_or_else(|| api_error("No track with that ID found"))?
|
||||
.api()
|
||||
.await)
|
||||
}
|
||||
|
||||
#[get("/track/<track_id>/audio")]
|
||||
|
@ -22,4 +22,4 @@ pub async fn track_audio_route(track_id: &str, lib: &State<Libary>) -> Option<Na
|
|||
NamedFile::open(std::path::Path::new(&track.path))
|
||||
.await
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,18 @@
|
|||
|
||||
use rocket::http::Status;
|
||||
use crate::library::user::Session;
|
||||
use crate::library::user::User;
|
||||
use mongod::Model;
|
||||
use mongodb::bson::doc;
|
||||
use rocket::http::Status;
|
||||
use rocket::outcome::Outcome;
|
||||
use rocket::post;
|
||||
use rocket::request::FromRequest;
|
||||
use rocket::Request;
|
||||
use serde_json::json;
|
||||
use serde::Deserialize;
|
||||
use rocket::serde::json::Json;
|
||||
use crate::library::user::Session;
|
||||
use crate::library::user::User;
|
||||
use rocket::Request;
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use super::FallibleApiResponse;
|
||||
use super::api_error;
|
||||
use super::FallibleApiResponse;
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for User {
|
||||
|
@ -22,13 +21,13 @@ impl<'r> FromRequest<'r> for User {
|
|||
async fn from_request(request: &'r Request<'_>) -> rocket::request::Outcome<Self, Self::Error> {
|
||||
match request.headers().get_one("token") {
|
||||
Some(key) => {
|
||||
if let Some(session) = Session::find_one(doc! { "token": key} ).await {
|
||||
if let Some(session) = Session::find_one(doc! { "token": key}).await {
|
||||
let user = session.user.get().await;
|
||||
Outcome::Success(user)
|
||||
} else {
|
||||
Outcome::Error((Status::Unauthorized, ()))
|
||||
}
|
||||
},
|
||||
}
|
||||
None => Outcome::Error((Status::Unauthorized, ())),
|
||||
}
|
||||
}
|
||||
|
@ -37,15 +36,16 @@ impl<'r> FromRequest<'r> for User {
|
|||
#[derive(Deserialize)]
|
||||
pub struct LoginData {
|
||||
pub username: String,
|
||||
pub password: String
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[post("/login", data = "<login>")]
|
||||
pub async fn login_route(login: Json<LoginData>) -> FallibleApiResponse {
|
||||
let ses = User::login(&login.username, &login.password).await.ok_or_else(|| api_error("Login failed"))?;
|
||||
let ses = User::login(&login.username, &login.password)
|
||||
.await
|
||||
.ok_or_else(|| api_error("Login failed"))?;
|
||||
|
||||
Ok(json!({
|
||||
"token": ses.token
|
||||
}))
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue