Skip to content

Commit ea8bbac

Browse files
committed
Money
1 parent e536600 commit ea8bbac

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

src/main.rs

+50-7
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ async fn create(
191191
let now = Utc::now();
192192
let year_from_now = now + chrono::Duration::days(365);
193193
if !exists && auth {
194+
let payment_success = deduct_balance(&config.auth.db_location, &query.auth_name, 1);
195+
if !payment_success {
196+
return Err(actix_web::error::ErrorBadRequest("Insufficient funds"));
197+
}
194198
conn.execute(
195199
"INSERT INTO domains (domainName, registrant, registrar, status, nameservers, createdDate, expiryDate, lastUpdatedDate)
196200
VALUES (?, ?, ?, ?, ?, ?, ?, ?);",
@@ -209,6 +213,7 @@ async fn create(
209213
],
210214
)
211215
.unwrap();
216+
deduct_balance(&config.auth.db_location, &query.auth_name, 1);
212217
let domainInfo = DomainInfo {
213218
domainName: query.domainName.clone(),
214219
registrant: query.registrant.clone(),
@@ -299,13 +304,18 @@ async fn renew(
299304
}
300305

301306
// add a year to expiry date
302-
let new_expiry_date = expiry_date.parse::<DateTime<Utc>>().unwrap().checked_add_signed(chrono::Duration::days(365)).unwrap();
307+
let new_expiry_date = expiry_date.parse::<DateTime<Utc>>().unwrap().checked_add_signed(chrono::Duration::days(365)).unwrap();
308+
let payment_success = deduct_balance(&config.auth.db_location, &query.auth_name, 1);
309+
if payment_success {
303310
conn.execute(
304311
"UPDATE domains SET expiryDate = ? WHERE domainName = ?",
305312
params![&new_expiry_date.to_string(), &query.domainName],
306313
)
307-
.map_err(|e| actix_web::error::ErrorInternalServerError(e.to_string()))?;
314+
.map_err(|e| actix_web::error::ErrorInternalServerError(e.to_string()))?;
308315
Ok(HttpResponse::Ok().finish())
316+
} else {
317+
Err(actix_web::error::ErrorBadRequest("Insufficient funds"))
318+
}
309319
} else {
310320
Err(actix_web::error::ErrorBadRequest("Domain does not exist or authentication failed"))
311321
}
@@ -338,7 +348,7 @@ async fn update(
338348

339349
if exists && auth {
340350
conn.execute(
341-
"UPDATE domains SET nameservers = ?, registrant = ? WHERE domainName = ? AND registrar = ?",
351+
"UPDATE domains SET nameservers = ?, registrant = ? WHERE domainName = ? AND registrar = ?",
342352
params![&query.nameservers, &query.registrant, &query.domainName, &query.auth_name],
343353
)
344354
.map_err(|e| actix_web::error::ErrorInternalServerError(e.to_string()))?;
@@ -349,6 +359,38 @@ async fn update(
349359
}
350360

351361

362+
fn deduct_balance(db_path: &str, registrar: &str, amount: i32) -> bool {
363+
// get current balance
364+
// check if balance is enough
365+
// deduct balance
366+
let conn = connect_db(db_path).unwrap();
367+
368+
369+
let mut stmt = conn
370+
.prepare("SELECT balance FROM users WHERE name = ?")
371+
.unwrap();
372+
let balance: i32 = stmt.query_row(&[registrar], |row| row.get(0)).unwrap();
373+
374+
// if no rows are returned, then the user does not exist
375+
if balance == 0 {
376+
// error user does not exist
377+
return false
378+
}
379+
380+
if balance < amount {
381+
// error insufficient funds
382+
return false
383+
}
384+
385+
386+
conn.execute(
387+
"UPDATE users SET balance = balance - ? WHERE name = ?",
388+
params![amount, registrar],
389+
)
390+
.unwrap();
391+
true
392+
}
393+
352394

353395
fn write_to_zone_file(path: &str, config: &Config) {
354396
let conn = connect_db(&config.database.location).unwrap();
@@ -421,10 +463,11 @@ fn create_auth_tables(conn: &Connection) -> Result<()> {
421463
"CREATE TABLE IF NOT EXISTS users (
422464
id INTEGER PRIMARY KEY,
423465
name TEXT NOT NULL UNIQUE,
424-
key TEXT NOT NULL
466+
key TEXT NOT NULL,
467+
balance INTEGER
425468
)",
426469
[],
427-
)?; /*
470+
)?;/*
428471
conn.execute(
429472
"INSERT INTO users (name, key) VALUES ('NET_Domains', 'password');",
430473
[],
@@ -472,12 +515,12 @@ async fn main() -> std::io::Result<()> {
472515
let c2 = config.clone();
473516
let ip = &c2.server.ip;
474517
let port = &c2.server.port;
475-
518+
/*
476519
// Load SSL keys
477520
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
478521
builder.set_private_key_file("private.key", SslFiletype::PEM).unwrap();
479522
builder.set_certificate_chain_file("certificate.crt").unwrap();
480-
523+
*/
481524

482525

483526
HttpServer::new(move || {

0 commit comments

Comments
 (0)