58 lines
1.3 KiB
Rust
58 lines
1.3 KiB
Rust
use crate::middlewares::error::ErrorResponse;
|
|
use crate::middlewares::user::get_user;
|
|
use crate::models::playlists::{NewPlaylist, Playlists};
|
|
use actix_web::{get, post, web, HttpRequest, HttpResponse, Scope};
|
|
use serde::Serialize;
|
|
|
|
pub fn routes() -> Scope {
|
|
web::scope("/me")
|
|
.service(me)
|
|
.service(me_playlists)
|
|
.service(create_playlist)
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
struct Response {
|
|
id: String,
|
|
name: String,
|
|
email: String,
|
|
}
|
|
|
|
#[get("")]
|
|
async fn me(req: HttpRequest) -> Result<HttpResponse, ErrorResponse> {
|
|
let user = get_user(req)?;
|
|
|
|
Ok(HttpResponse::Ok().json(Response {
|
|
id: user.id,
|
|
name: user.name,
|
|
email: user.email,
|
|
}))
|
|
}
|
|
|
|
#[get("/playlists")]
|
|
async fn me_playlists(req: HttpRequest) -> Result<HttpResponse, ErrorResponse> {
|
|
let user = get_user(req)?;
|
|
|
|
#[derive(Serialize)]
|
|
struct Response {
|
|
playlists: Vec<Playlists>,
|
|
}
|
|
|
|
Ok(HttpResponse::Ok().json(Response {
|
|
playlists: Playlists::find_for_user(&user.id, false)?,
|
|
}))
|
|
}
|
|
|
|
#[post("/playlists")]
|
|
async fn create_playlist(
|
|
req: HttpRequest,
|
|
mut playlist: web::Json<NewPlaylist>,
|
|
) -> Result<HttpResponse, ErrorResponse> {
|
|
let user = get_user(req)?;
|
|
|
|
playlist.creator_id = Option::from(user.id);
|
|
|
|
Playlists::create(playlist.into_inner())?;
|
|
|
|
Ok(HttpResponse::Accepted().finish())
|
|
}
|