dw about it fixed
This commit is contained in:
1
backend
1
backend
Submodule backend deleted from 3eb1f29485
16
backend/Cargo.toml
Normal file
16
backend/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "backend"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
default-run = "main"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
diesel = { version = "2.0.0",features = ["postgres","r2d2"] }
|
||||
dotenv = "0.15"
|
||||
chrono = "0.2.25"
|
||||
common = { path = "../common"}
|
||||
actix-web = "4.0.0-rc.1"
|
||||
tokio = { version = "1.17.0", features = ["full"] }
|
||||
yew = { version = "0.20.0", features = ["csr"] }
|
||||
21
backend/LICENSE
Normal file
21
backend/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Linly
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
8
backend/diesel.toml
Normal file
8
backend/diesel.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
# For documentation on how to configure this file,
|
||||
# see https://diesel.rs/guides/configuring-diesel-cli
|
||||
|
||||
[print_schema]
|
||||
file = "src/schema.rs"
|
||||
|
||||
[migrations_directory]
|
||||
dir = "migrations"
|
||||
0
backend/migrations/.keep
Normal file
0
backend/migrations/.keep
Normal file
@@ -0,0 +1,6 @@
|
||||
-- This file was automatically created by Diesel to setup helper functions
|
||||
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||
-- changes will be added to existing projects as new migrations.
|
||||
|
||||
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
|
||||
DROP FUNCTION IF EXISTS diesel_set_updated_at();
|
||||
@@ -0,0 +1,36 @@
|
||||
-- This file was automatically created by Diesel to setup helper functions
|
||||
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||
-- changes will be added to existing projects as new migrations.
|
||||
|
||||
|
||||
|
||||
|
||||
-- Sets up a trigger for the given table to automatically set a column called
|
||||
-- `updated_at` whenever the row is modified (unless `updated_at` was included
|
||||
-- in the modified columns)
|
||||
--
|
||||
-- # Example
|
||||
--
|
||||
-- ```sql
|
||||
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
|
||||
--
|
||||
-- SELECT diesel_manage_updated_at('users');
|
||||
-- ```
|
||||
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
|
||||
BEGIN
|
||||
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
|
||||
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
|
||||
BEGIN
|
||||
IF (
|
||||
NEW IS DISTINCT FROM OLD AND
|
||||
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
|
||||
) THEN
|
||||
NEW.updated_at := current_timestamp;
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
40
backend/src/admin_data.rs
Normal file
40
backend/src/admin_data.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use crate::models::{Admin, AdminEmail, NewAdmin, NewAdminEmail};
|
||||
use diesel::prelude::*;
|
||||
|
||||
pub fn listadmins(connection: &mut PgConnection) -> String {
|
||||
use crate::schema::admins::dsl::*;
|
||||
let query = admins.load::<Admin>(connection).expect("KANKER");
|
||||
let admin_list = query
|
||||
.iter()
|
||||
.map(|admin| format!("{} {}\n", admin.name, admin.address))
|
||||
.collect();
|
||||
return admin_list;
|
||||
}
|
||||
pub fn addmin(connection: &mut PgConnection, new_admin: NewAdmin) {
|
||||
use crate::schema::admins::dsl::*;
|
||||
diesel::insert_into(admins)
|
||||
.values(&new_admin)
|
||||
.execute(connection)
|
||||
.expect("LOL DEAD NOT WORKING INSERTION");
|
||||
}
|
||||
pub fn addmail(connection: &mut PgConnection, admin_email: String, inserted_id: i32) {
|
||||
use crate::schema::admin_emails::dsl::*;
|
||||
let inserted_email = NewAdminEmail {
|
||||
admin_id: &inserted_id,
|
||||
email: &admin_email,
|
||||
};
|
||||
diesel::insert_into(admin_emails)
|
||||
.values(inserted_email)
|
||||
.execute(connection)
|
||||
.expect("You FOOL! You didn't put an email in there");
|
||||
}
|
||||
|
||||
pub fn listadminmails(connection: &mut PgConnection) {
|
||||
use crate::schema::admin_emails::dsl::*;
|
||||
let results = admin_emails
|
||||
.load::<AdminEmail>(connection)
|
||||
.expect("hecc D:");
|
||||
for addmail in results {
|
||||
println!("{} {}", addmail.email, addmail.admin_id);
|
||||
}
|
||||
}
|
||||
21
backend/src/driver_data.rs
Normal file
21
backend/src/driver_data.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use diesel::prelude::*;
|
||||
use crate::models::{Driver,NewDriver};
|
||||
|
||||
pub fn listdrivers(connection: &mut PgConnection)
|
||||
{
|
||||
use crate::schema::drivers::dsl::*;
|
||||
let queury = drivers.load::<Driver>(connection).expect("KANKER");
|
||||
for driver in queury
|
||||
{
|
||||
println!("{} {}",driver.name ,driver.address);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn addriver(connection: &mut PgConnection, new_driver: NewDriver){
|
||||
|
||||
use crate::schema::drivers::dsl::*;
|
||||
diesel::insert_into(drivers)
|
||||
.values(&new_driver)
|
||||
.execute(connection)
|
||||
.expect("Couldn't insert new driver :)");
|
||||
}
|
||||
34
backend/src/lib.rs
Normal file
34
backend/src/lib.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use std::env;
|
||||
pub mod admin_data;
|
||||
pub mod driver_data;
|
||||
pub mod models;
|
||||
pub mod radar_data;
|
||||
pub mod schema;
|
||||
pub mod ticket_data;
|
||||
pub mod vehicle_data;
|
||||
use diesel::{Connection, PgConnection};
|
||||
use dotenv::dotenv;
|
||||
|
||||
//boiler plate :D
|
||||
pub fn establish_connection() -> PgConnection {
|
||||
dotenv().ok();
|
||||
let database_url = env::var("DATABASE_URL").expect("fix your .env idot");
|
||||
PgConnection::establish(&database_url)
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
||||
}
|
||||
//
|
||||
//Pool instead
|
||||
|
||||
// pub type PgPool = r2d2::Pool<ConnectionManager<PgConnection>>;
|
||||
// pub type PgPooledConnection = PooledConnection<ConnectionManager<PgConnection>>;
|
||||
// fn init_pool(database_url: &str) -> Result<PgPool, PoolError> {
|
||||
// let manager = ConnectionManager::<PgConnection>::new(database_url);
|
||||
// Pool::builder().build(manager)
|
||||
// }
|
||||
//
|
||||
// pub fn establish_connection() -> PgPool {
|
||||
// dotenv().ok();
|
||||
//
|
||||
// let database_url = env::var("DATABASE_URL").expect("pls obama put the link this time");
|
||||
// init_pool(&database_url).expect("Failed to create pool")
|
||||
// }
|
||||
106
backend/src/models.rs
Normal file
106
backend/src/models.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
use diesel::{prelude::*, data_types::PgDate};
|
||||
use crate::schema::*;
|
||||
|
||||
|
||||
|
||||
//Admins
|
||||
#[derive(Queryable,Debug,AsChangeset,Identifiable)]
|
||||
pub
|
||||
struct Admin {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub address: String
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = admins)]
|
||||
pub
|
||||
struct NewAdmin<'a> {
|
||||
pub id: i32,
|
||||
pub name: &'a str,
|
||||
pub address: &'a str
|
||||
}
|
||||
|
||||
#[derive(Queryable,AsChangeset,Associations)]
|
||||
#[diesel(belongs_to(Admin))]
|
||||
pub struct AdminEmail {
|
||||
pub admin_id: i32,
|
||||
pub email: String,
|
||||
}
|
||||
|
||||
#[derive(Insertable,Associations)]
|
||||
#[diesel(belongs_to(Admin))]
|
||||
#[diesel(table_name = admin_emails)]
|
||||
pub struct NewAdminEmail<'a> {
|
||||
pub admin_id: &'a i32,
|
||||
pub email: &'a str,
|
||||
}
|
||||
|
||||
|
||||
//Ticket things
|
||||
#[derive(Queryable)]
|
||||
pub struct Ticket {
|
||||
pub id: i32,
|
||||
pub category: String,
|
||||
pub description: String,
|
||||
pub issue_date: PgDate
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = tickets)]
|
||||
pub struct NewTicket<'a> {
|
||||
pub category: &'a str,
|
||||
pub description: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = issued_tickets)]
|
||||
pub struct IssuedTicket {
|
||||
ticket: i32,
|
||||
vehicle: String,
|
||||
driver: Option<i32>,
|
||||
officer: i32,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = auto_issued_tickets)]
|
||||
pub struct AutoIssuedTicket<'a> {
|
||||
ticket: &'a i32,
|
||||
vehicle: &'a str,
|
||||
driver: Option<i32>,
|
||||
radar: &'a i32,
|
||||
}
|
||||
|
||||
//Drivers
|
||||
#[derive(Queryable,AsChangeset,Identifiable)]
|
||||
pub struct Driver {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub address: String,
|
||||
pub reg_date: PgDate,
|
||||
pub birthdate: PgDate,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = drivers)]
|
||||
pub
|
||||
struct NewDriver<'a> {
|
||||
pub id: i32,
|
||||
pub name: &'a str,
|
||||
pub address: &'a str,
|
||||
reg_date: PgDate,
|
||||
birthdate: PgDate,
|
||||
}
|
||||
|
||||
//Vehicles
|
||||
#[derive(Queryable,AsChangeset)]
|
||||
#[diesel(belongs_to(Driver))]
|
||||
pub struct Vehicle {
|
||||
pub model: Option<String>,
|
||||
pub color: Option<String>,
|
||||
pub chasse_num: i32,
|
||||
pub plate_num: String,
|
||||
pub vehicle_type: String,
|
||||
pub owner: i32
|
||||
|
||||
}
|
||||
0
backend/src/radar_data.rs
Normal file
0
backend/src/radar_data.rs
Normal file
132
backend/src/schema.rs
Normal file
132
backend/src/schema.rs
Normal file
@@ -0,0 +1,132 @@
|
||||
// @generated automatically by Diesel CLI.
|
||||
|
||||
diesel::table! {
|
||||
admin_emails (admin_id, email) {
|
||||
admin_id -> Int4,
|
||||
email -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
admins (id) {
|
||||
id -> Int4,
|
||||
name -> Text,
|
||||
address -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
auto_issued_tickets (ticket) {
|
||||
ticket -> Int4,
|
||||
vehicle -> Text,
|
||||
driver -> Nullable<Int4>,
|
||||
radar -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
driver_phones (driver, number) {
|
||||
driver -> Int4,
|
||||
number -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
drivers (id) {
|
||||
id -> Int4,
|
||||
name -> Text,
|
||||
address -> Text,
|
||||
reg_date -> Date,
|
||||
birthdate -> Date,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
issued_tickets (ticket) {
|
||||
ticket -> Int4,
|
||||
vehicle -> Text,
|
||||
driver -> Nullable<Int4>,
|
||||
officer -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
officer_emails (officer, email) {
|
||||
officer -> Int4,
|
||||
email -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
officer_phones (officer, number) {
|
||||
officer -> Int4,
|
||||
number -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
officers (badge_num) {
|
||||
badge_num -> Int4,
|
||||
id -> Text,
|
||||
jurisdiction -> Nullable<Text>,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
radars (serialnumber) {
|
||||
serialnumber -> Int4,
|
||||
#[sql_name = "type"]
|
||||
type_ -> Nullable<Text>,
|
||||
address -> Nullable<Text>,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
tickets (id) {
|
||||
id -> Int4,
|
||||
category -> Text,
|
||||
description -> Text,
|
||||
issue_date -> Date,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
vehicles (plate_num) {
|
||||
model -> Nullable<Text>,
|
||||
color -> Nullable<Text>,
|
||||
chasse_num -> Nullable<Int4>,
|
||||
plate_num -> Text,
|
||||
vehicle_type -> Text,
|
||||
category -> Text,
|
||||
owner -> Nullable<Int4>,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::joinable!(admin_emails -> admins (admin_id));
|
||||
diesel::joinable!(auto_issued_tickets -> drivers (driver));
|
||||
diesel::joinable!(auto_issued_tickets -> radars (radar));
|
||||
diesel::joinable!(auto_issued_tickets -> tickets (ticket));
|
||||
diesel::joinable!(auto_issued_tickets -> vehicles (vehicle));
|
||||
diesel::joinable!(driver_phones -> drivers (driver));
|
||||
diesel::joinable!(issued_tickets -> drivers (driver));
|
||||
diesel::joinable!(issued_tickets -> officers (officer));
|
||||
diesel::joinable!(issued_tickets -> tickets (ticket));
|
||||
diesel::joinable!(issued_tickets -> vehicles (vehicle));
|
||||
diesel::joinable!(officer_emails -> officers (officer));
|
||||
diesel::joinable!(officer_phones -> officers (officer));
|
||||
diesel::joinable!(vehicles -> drivers (owner));
|
||||
|
||||
diesel::allow_tables_to_appear_in_same_query!(
|
||||
admin_emails,
|
||||
admins,
|
||||
auto_issued_tickets,
|
||||
driver_phones,
|
||||
drivers,
|
||||
issued_tickets,
|
||||
officer_emails,
|
||||
officer_phones,
|
||||
officers,
|
||||
radars,
|
||||
tickets,
|
||||
vehicles,
|
||||
);
|
||||
57
backend/src/ticket_data.rs
Normal file
57
backend/src/ticket_data.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use crate::models::{AutoIssuedTicket, IssuedTicket, NewTicket, Ticket};
|
||||
use common::CommonTicket;
|
||||
use diesel::prelude::*;
|
||||
|
||||
pub fn create_ticket(connection: &mut PgConnection, ticket: NewTicket) {
|
||||
use crate::schema::tickets::dsl::*;
|
||||
diesel::insert_into(tickets)
|
||||
.values(&ticket)
|
||||
.execute(connection)
|
||||
.expect("Didn't save ticket AAAAAA");
|
||||
}
|
||||
|
||||
pub fn get_tickets(connection: &mut PgConnection, amount: i64) -> Vec<CommonTicket> {
|
||||
use crate::schema::tickets::dsl::*;
|
||||
let results = tickets
|
||||
.limit(amount)
|
||||
.load::<Ticket>(connection)
|
||||
.expect("KANKER TIKET");
|
||||
return results
|
||||
.iter()
|
||||
.map(|ticket| CommonTicket {
|
||||
id: ticket.id,
|
||||
category: String::from(&ticket.category),
|
||||
description: String::from(&ticket.description),
|
||||
})
|
||||
.collect(); //Shoves everything to a vector
|
||||
}
|
||||
|
||||
pub fn get_ticket(connection: &mut PgConnection, tickid: i32) -> CommonTicket {
|
||||
use crate::schema::tickets::dsl::*;
|
||||
let ticket = &mut tickets
|
||||
.filter(id.eq(tickid))
|
||||
.limit(1)
|
||||
.load::<Ticket>(connection)
|
||||
.expect("no tickets :(")[0];
|
||||
return CommonTicket {
|
||||
id: ticket.id,
|
||||
category: String::from(&ticket.category),
|
||||
description: String::from(&ticket.description),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn issue_ticket(connection: &mut PgConnection, issued_ticket: IssuedTicket) {
|
||||
use crate::schema::issued_tickets::dsl::*;
|
||||
diesel::insert_into(issued_tickets)
|
||||
.values(issued_ticket)
|
||||
.execute(connection)
|
||||
.expect("PAINNNN IT NO WORKEY (issued tickets)");
|
||||
}
|
||||
|
||||
pub fn auto_issue_ticket(connection: &mut PgConnection, issued_ticket: AutoIssuedTicket) {
|
||||
use crate::schema::auto_issued_tickets::dsl::*;
|
||||
diesel::insert_into(auto_issued_tickets)
|
||||
.values(issued_ticket)
|
||||
.execute(connection)
|
||||
.expect("PAINNNN IT NO WORKEY (issued tickets)");
|
||||
}
|
||||
0
backend/src/vehicle_data.rs
Normal file
0
backend/src/vehicle_data.rs
Normal file
Reference in New Issue
Block a user