This commit is contained in:
@@ -9,7 +9,7 @@ default-run = "backend"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4"
|
||||
actix-web-lab = "^0"
|
||||
actix-web-lab = { version = "^0", features = ["spa"] }
|
||||
actix-files = "0.6"
|
||||
actix-cors = "0.6.4"
|
||||
actix-session = { version = "0.7.2", features = ["cookie-session"] }
|
||||
@@ -20,6 +20,7 @@ walkdir = "2"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
common = { path = "../common" }
|
||||
api-boundary = { path = "../api-boundary" }
|
||||
uuid = { version = "1", features = ["v4", "serde"] }
|
||||
rust-s3 = "0.32.3"
|
||||
futures = "0.3"
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
extern crate diesel;
|
||||
|
||||
use backend::models::*;
|
||||
use backend::*;
|
||||
use diesel::prelude::*;
|
||||
|
||||
type DbError = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
pub fn list_pictures(conn: &PgConnection) -> Result<Vec<Picture>, DbError> {
|
||||
use self::schema::pictures::dsl::*;
|
||||
|
||||
Ok(pictures
|
||||
.limit(50)
|
||||
.order_by(created_at.desc())
|
||||
.load::<Picture>(conn)?)
|
||||
}
|
||||
@@ -5,7 +5,6 @@ use actix_web::{
|
||||
web::{Data, Json},
|
||||
HttpResponse, Responder,
|
||||
};
|
||||
use actix_web_lab::__reexports::tokio;
|
||||
use common::OutputPicture;
|
||||
use mongodb::bson::oid::ObjectId;
|
||||
use s3::bucket::Bucket;
|
||||
@@ -113,7 +112,7 @@ async fn get_user_photos(
|
||||
return Ok(HttpResponse::Unauthorized().body("Not authorized"));
|
||||
};
|
||||
|
||||
let Ok(user) = db.get_user(&user_id).await else {
|
||||
let Ok(_user) = db.get_user(&user_id).await else {
|
||||
return Ok(HttpResponse::Unauthorized().body("Not authorized"));
|
||||
};
|
||||
let photos: Vec<OutputPicture> = db
|
||||
|
||||
@@ -5,39 +5,9 @@ use actix_web::{
|
||||
web::{Data, Json, Path},
|
||||
HttpResponse,
|
||||
};
|
||||
|
||||
use api_boundary::*;
|
||||
use mongodb::bson::oid::ObjectId;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct UserWrapper {
|
||||
user: User,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct UserWithToken {
|
||||
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
|
||||
pub id: Option<ObjectId>,
|
||||
pub username: String,
|
||||
pub email: String,
|
||||
pub password: String,
|
||||
pub token: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct UserTokenWrapper {
|
||||
user: UserWithToken,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Login {
|
||||
email: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct LoginWrapper {
|
||||
user: Login,
|
||||
}
|
||||
|
||||
// Return an opaque 500 while preserving the error's root cause for logging.
|
||||
fn e500<T>(e: T) -> actix_web::Error
|
||||
@@ -58,7 +28,7 @@ pub async fn is_logged_in(
|
||||
log::info!("success");
|
||||
return Ok(HttpResponse::Ok().json(UserTokenWrapper {
|
||||
user: UserWithToken {
|
||||
id: user.id,
|
||||
id: user_id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
password: user.password,
|
||||
@@ -109,15 +79,15 @@ pub async fn login(
|
||||
}
|
||||
|
||||
let token = user.username.clone();
|
||||
HttpResponse::Ok().json(UserTokenWrapper {
|
||||
user: UserWithToken {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
password: user.password,
|
||||
token,
|
||||
},
|
||||
})
|
||||
HttpResponse::Ok().json(ApiToken { token })
|
||||
}
|
||||
|
||||
#[post("/api/users/logout")]
|
||||
pub async fn logout(db: Data<MongoRepo>, session: Session) -> HttpResponse {
|
||||
if let Some(_user_id) = session.get::<String>("user_id").unwrap_or(None) {
|
||||
return HttpResponse::Ok().json(());
|
||||
}
|
||||
HttpResponse::BadRequest().json(())
|
||||
}
|
||||
|
||||
#[get("/api/user/{id}")]
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
// #[macro_use]
|
||||
// extern crate diesel;
|
||||
// extern crate dotenv;
|
||||
|
||||
// pub mod models;
|
||||
// pub mod schema;
|
||||
|
||||
// use self::models::NewPicture;
|
||||
// use diesel::pg::PgConnection;
|
||||
// use diesel::prelude::*;
|
||||
// use dotenv::dotenv;
|
||||
// use std::env;
|
||||
|
||||
// pub fn establish_connection() -> PgConnection {
|
||||
// dotenv().ok();
|
||||
|
||||
// let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
// PgConnection::establish(&database_url)
|
||||
// .unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
||||
// }
|
||||
|
||||
// pub fn create_picture(conn: &PgConnection, new_picture: NewPicture) -> usize {
|
||||
// use schema::pictures;
|
||||
|
||||
// diesel::insert_into(pictures::table)
|
||||
// .values(&new_picture)
|
||||
// .execute(conn)
|
||||
// .expect("Error saving new picture")
|
||||
// }
|
||||
@@ -5,7 +5,6 @@ mod repository;
|
||||
use api::photo_api::get_presigned_post_urls;
|
||||
use api::photo_api::get_user_photos;
|
||||
use api::user_api::{create_user, delete_user, get_user, is_logged_in, login, update_user};
|
||||
use common::OutputPicture;
|
||||
use repository::mongodb_repo::MongoRepo;
|
||||
|
||||
use s3::bucket::Bucket;
|
||||
@@ -21,18 +20,16 @@ use actix_web::{
|
||||
// get, middleware, post, web, App, Error, HttpRequest, HttpResponse, HttpServer,
|
||||
// get,
|
||||
// middleware,
|
||||
get,
|
||||
http,
|
||||
middleware,
|
||||
web,
|
||||
web::Data,
|
||||
App,
|
||||
HttpServer,
|
||||
};
|
||||
use actix_web::{Responder, Result};
|
||||
use actix_web_lab::web::spa;
|
||||
|
||||
use crate::api::photo_api::upload_done;
|
||||
use crate::api::user_api::logout;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
@@ -105,6 +102,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(get_user)
|
||||
.service(update_user)
|
||||
.service(delete_user)
|
||||
.service(logout)
|
||||
.service(
|
||||
spa()
|
||||
.index_file("./dist/index.html")
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
// table! {
|
||||
// pictures (id) {
|
||||
// id -> Int4,
|
||||
// filepath -> Varchar,
|
||||
// created_at -> Nullable<Int4>,
|
||||
// focal_length -> Nullable<Varchar>,
|
||||
// shutter_speed -> Nullable<Varchar>,
|
||||
// width -> Int4,
|
||||
// height -> Int4,
|
||||
// make -> Nullable<Varchar>,
|
||||
// model -> Nullable<Varchar>,
|
||||
// lens -> Nullable<Varchar>,
|
||||
// orientation -> Nullable<Varchar>,
|
||||
// fnumber -> Nullable<Float8>,
|
||||
// iso -> Nullable<Int4>,
|
||||
// exposure_program -> Nullable<Varchar>,
|
||||
// exposure_compensation -> Nullable<Varchar>,
|
||||
// thumbnail -> Nullable<Varchar>,
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user