mirror of
https://github.com/Noratrieb/karlauth.git
synced 2026-01-14 14:25:02 +01:00
users crud
This commit is contained in:
parent
2f67d81e41
commit
1a8f223d39
13 changed files with 130 additions and 52 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1 +1,3 @@
|
||||||
/target
|
/target
|
||||||
|
.idea
|
||||||
|
*.iml
|
||||||
8
.idea/.gitignore
generated
vendored
8
.idea/.gitignore
generated
vendored
|
|
@ -1,8 +0,0 @@
|
||||||
# Default ignored files
|
|
||||||
/shelf/
|
|
||||||
/workspace.xml
|
|
||||||
# Datasource local storage ignored files
|
|
||||||
/dataSources/
|
|
||||||
/dataSources.local.xml
|
|
||||||
# Editor-based HTTP Client requests
|
|
||||||
/httpRequests/
|
|
||||||
11
.idea/karlauth.iml
generated
11
.idea/karlauth.iml
generated
|
|
@ -1,11 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<module type="CPP_MODULE" version="4">
|
|
||||||
<component name="NewModuleRootManager">
|
|
||||||
<content url="file://$MODULE_DIR$">
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
|
||||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
|
||||||
</content>
|
|
||||||
<orderEntry type="inheritedJdk" />
|
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
|
||||||
</component>
|
|
||||||
</module>
|
|
||||||
10
.idea/misc.xml
generated
10
.idea/misc.xml
generated
|
|
@ -1,10 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="MavenImportPreferences">
|
|
||||||
<option name="generalSettings">
|
|
||||||
<MavenGeneralSettings>
|
|
||||||
<option name="mavenHome" value="C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.1\plugins\maven\lib\maven3" />
|
|
||||||
</MavenGeneralSettings>
|
|
||||||
</option>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
|
|
@ -1,8 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="ProjectModuleManager">
|
|
||||||
<modules>
|
|
||||||
<module fileurl="file://$PROJECT_DIR$/.idea/karlauth.iml" filepath="$PROJECT_DIR$/.idea/karlauth.iml" />
|
|
||||||
</modules>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
6
.idea/vcs.xml
generated
6
.idea/vcs.xml
generated
|
|
@ -1,6 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="VcsDirectoryMappings">
|
|
||||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
3
migrations/2021-07-16-091329_add_users/down.sql
Normal file
3
migrations/2021-07-16-091329_add_users/down.sql
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
|
||||||
|
DROP TABLE users;
|
||||||
10
migrations/2021-07-16-091329_add_users/up.sql
Normal file
10
migrations/2021-07-16-091329_add_users/up.sql
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
-- Your SQL goes here
|
||||||
|
|
||||||
|
CREATE TABLE users
|
||||||
|
(
|
||||||
|
id SERIAL NOT NULL PRIMARY KEY,
|
||||||
|
first_name TEXT NOT NULL,
|
||||||
|
last_name TEXT NOT NULL,
|
||||||
|
email TEXT NOT NULL,
|
||||||
|
created_at TIMESTAMP NOT NULL
|
||||||
|
);
|
||||||
40
src/actions.rs
Normal file
40
src/actions.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
use super::models::{NewUser, User};
|
||||||
|
use crate::diesel::QueryDsl;
|
||||||
|
use crate::diesel::RunQueryDsl;
|
||||||
|
|
||||||
|
use crate::handlers::InputUser;
|
||||||
|
use crate::Pool;
|
||||||
|
use diesel::{delete, insert_into};
|
||||||
|
|
||||||
|
type DbResult<T> = Result<T, diesel::result::Error>;
|
||||||
|
|
||||||
|
pub fn get_all_users(db: &Pool) -> DbResult<User> {
|
||||||
|
use super::schema::users::dsl::*;
|
||||||
|
let conn = db.get().unwrap();
|
||||||
|
let items = users.load::<User>(&conn)?;
|
||||||
|
Ok(items)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_user_by_id(db: &Pool, id: i32) -> DbResult<User> {
|
||||||
|
use super::schema::users::dsl::*;
|
||||||
|
let conn = db.get().unwrap();
|
||||||
|
users.find(id).get_result::<User>(&conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_user(db: &Pool, user: InputUser) -> DbResult<User> {
|
||||||
|
use super::schema::users::dsl::*;
|
||||||
|
let conn = db.get().unwrap();
|
||||||
|
let new_user = NewUser {
|
||||||
|
first_name: &user.first_name,
|
||||||
|
last_name: &user.last_name,
|
||||||
|
email: &user.email,
|
||||||
|
created_at: chrono::Local::now().naive_local(),
|
||||||
|
};
|
||||||
|
insert_into(users).values(&new_user).get_result(&conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete_user(db: &Pool, id: i32) -> DbResult<usize> {
|
||||||
|
use super::schema::users::dsl::*;
|
||||||
|
let conn = db.get().unwrap();
|
||||||
|
delete(users.find(id)).execute(&conn)
|
||||||
|
}
|
||||||
|
|
@ -1,17 +1,45 @@
|
||||||
use actix_web::Responder;
|
use super::actions;
|
||||||
|
use super::models::{NewUser, User};
|
||||||
|
use super::Pool;
|
||||||
|
use actix_web::{web, HttpResponse, Responder};
|
||||||
|
use diesel::dsl::{delete, insert_into};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::vec::Vec;
|
||||||
|
|
||||||
pub async fn get_users() -> impl Responder {
|
type HttpResult = Result<HttpResponse, actix_web::Error>;
|
||||||
format!("hello from get users")
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct InputUser {
|
||||||
|
pub first_name: String,
|
||||||
|
pub last_name: String,
|
||||||
|
pub email: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_user_by_id() -> impl Responder {
|
pub async fn get_users(db: web::Data<Pool>) -> HttpResult {
|
||||||
format!("hello from get users by id")
|
Ok(web::block(move || actions::get_all_users(&db))
|
||||||
|
.await
|
||||||
|
.map(|user| user.into())
|
||||||
|
.map_err(|_| HttpResponse::InternalServerError())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn add_user() -> impl Responder {
|
pub async fn get_user_by_id(db: web::Data<Pool>, user_id: web::Path<i32>) -> HttpResult {
|
||||||
format!("hello from add user")
|
Ok(web::block(move || actions::get_user_by_id(&db, *user_id))
|
||||||
|
.await
|
||||||
|
.map(|user| user.into())
|
||||||
|
.map_err(|_| HttpResponse::InternalServerError())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_user() -> impl Responder {
|
pub async fn add_user(db: web::Data<Pool>, item: web::Json<InputUser>) -> HttpResult {
|
||||||
format!("hello from delete user")
|
Ok(web::block(move || actions::add_user(&db, *item))
|
||||||
|
.await
|
||||||
|
.map(|user| user.into())
|
||||||
|
.map_err(|_| HttpResponse::InternalServerError())?)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// handler for `DELETE /users/{id}`
|
||||||
|
pub async fn delete_user(db: web::Data<Pool>, user_id: web::Path<i32>) -> impl Responder {
|
||||||
|
Ok(web::block(move || actions::delete_user(&db, *user_id))
|
||||||
|
.await
|
||||||
|
.map(|count| HttpResponse::Ok().body(format!("Deleted {} user.", count)))
|
||||||
|
.map_err(|_| HttpResponse::InternalServerError())?)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,11 @@ use actix_web::{dev::ServiceRequest, web, App, Error, HttpServer};
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::r2d2::{self, ConnectionManager};
|
use diesel::r2d2::{self, ConnectionManager};
|
||||||
|
|
||||||
|
mod actions;
|
||||||
mod errors;
|
mod errors;
|
||||||
mod handlers;
|
mod handlers;
|
||||||
mod models;
|
mod models;
|
||||||
|
mod schema;
|
||||||
|
|
||||||
pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
|
pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
use crate::schema::*;
|
||||||
|
use actix_web::HttpResponse;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, Queryable)]
|
||||||
|
pub struct User {
|
||||||
|
pub id: i32,
|
||||||
|
pub first_name: String,
|
||||||
|
pub last_name: String,
|
||||||
|
pub email: String,
|
||||||
|
pub created_at: chrono::NaiveDateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Insertable, Debug)]
|
||||||
|
#[table_name = "users"]
|
||||||
|
pub struct NewUser<'a> {
|
||||||
|
pub first_name: &'a str,
|
||||||
|
pub last_name: &'a str,
|
||||||
|
pub email: &'a str,
|
||||||
|
pub created_at: chrono::NaiveDateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<User> for HttpResponse {
|
||||||
|
fn from(user: User) -> Self {
|
||||||
|
HttpResponse::Ok().json(user)
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/schema.rs
Normal file
9
src/schema.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
table! {
|
||||||
|
users (id) {
|
||||||
|
id -> Int4,
|
||||||
|
first_name -> Text,
|
||||||
|
last_name -> Text,
|
||||||
|
email -> Text,
|
||||||
|
created_at -> Timestamp,
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue