Skip to content

Commit

Permalink
#13 (account microservice) Add bank account
Browse files Browse the repository at this point in the history
  • Loading branch information
José Antonio Córdoba Gómez committed Jan 5, 2021
1 parent b390672 commit 2d5dc42
Show file tree
Hide file tree
Showing 10 changed files with 591 additions and 55 deletions.
58 changes: 58 additions & 0 deletions account/src/bankaccount_module/controller.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

use std::sync::{Arc,Mutex, RwLock};
use std::collections::HashMap;
use serde::{Serialize,Deserialize};
use std::fmt;


use chrono::{Local, NaiveDate, NaiveDateTime};

use crate::bankaccount_module::model::bankaccount::{Account};
use crate::bankaccount_module::*;
use crate::utils::*;




#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BankAccountController{
bank_accounts: Arc<RwLock<HashMap<String,Account>>>
}


impl BankAccountController{

pub fn new() -> BankAccountController {
BankAccountController {
bank_accounts: Arc::new(RwLock::new(HashMap::new()))
}
}

// HU 1: Add Bank Account as customer user
pub fn add_bank_account(&mut self,c: Account){
self.bank_accounts.write().unwrap().insert(c.get_id().to_string(), c);
}

// HU 2: Delete bank account as customer user
pub fn erase_bank_account(&mut self, bank_accout_id: String){
self.bank_accounts.write().unwrap().remove(&bank_accout_id);
}

pub fn get_bank_accounts(&self) -> &Arc<RwLock<HashMap<String,Account>>> {
&self.bank_accounts
}


/// The to_json method allows to account to produce it own JSON serialization
pub fn to_json(&self)->String {
serde_json::to_string_pretty(&self).unwrap()
}
}

/// The fmt method allows display all atributes of an BankAccountController
impl fmt::Display for BankAccountController{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({:?})", self.bank_accounts)
}
}

5 changes: 4 additions & 1 deletion account/src/bankaccount_module/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod lib;
pub mod message;
pub mod routes_handlers;
pub mod routes_handlers;
pub mod model;
pub mod test_galvanic;
pub mod controller;
121 changes: 121 additions & 0 deletions account/src/bankaccount_module/model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
pub mod bankaccount{


use chrono::{Local, NaiveDate, NaiveDateTime};
use std::fmt;
use serde::{Serialize,Deserialize};
use crate::utils::model::{Lib};

extern crate regex;
extern crate serde;
extern crate serde_json;


/// Bank Account model
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Account{
/// Identification of account, it should be an identification intern of app that it's consistent with database identificator
id: String,

/// International bank code that identifies (worldwide) tha bank associated to account (better than BIC)
/// Example of Swift: BSCHESMMXXX
swift: String,

/// International Bank Account Number only for Europe.
/// Example of IBAN: ES32 6688 05 1111222233344
iban:String,

/// Timestamp. Represent timestamp which account was added in our application. For statisticals use.
created_at: NaiveDateTime,

/// Status of account. User can disable an account for not operating (not interesting account)
status: bool,
}

//@todo
// funcion para comprobar un iban valido
impl Account{
/// Allocates a Account object and initializes it so that it represent the bankaccount needed.
/// # Usage
/// ```
/// let my_account = Account::new(id,swift,iban,date,status)
/// ```
pub fn new(new_id: String, new_swift: String, new_iban: String, new_created_at: NaiveDateTime, new_status: bool) -> Account {
Account {
id: new_id,
swift: new_swift,
iban: new_iban,
created_at: new_created_at,
status: new_status
}
}

/// Getter (inmutable) for id private attribute.
pub fn get_id(&self) -> &String{
&self.id
}

/// Getter (inmutable) for swift private attribute.
/// A bank could be fusioned with other bank and change a swift code
pub fn get_swift(&self) -> &String{
&self.swift
}

/// Muttable access for swift private attribute
pub fn set_swift(&mut self, new_swift: String) -> &String{
if Lib::check_swift(new_swift.clone()) {
self.swift=new_swift;
}

&self.swift
}

/// Getter (inmutable) for iban private attribute.
pub fn get_iban(&self) -> &String{
&self.iban
}

/// Muttable access for swift private attribute
/// A bank could be fusioned with other bank and change a iban code
pub fn set_iban(&mut self, new_iban: String) -> &String{
if Lib::check_iban(new_iban.clone()) {
self.iban=new_iban;
}
&self.iban
}


/// Getter (inmutable) for created_at private attribute.
pub fn get_date(&self) -> &NaiveDateTime{
&self.created_at
}

/// Getter (inmutable) for status private attribute.
pub fn get_status(&self) -> &bool{
&self.status
}

/// Enables an account
fn enable(&mut self) {
self.status = true
}

/// Disables an account
fn disable(&mut self) {
self.status = false
}

/// The to_json method allows to account to produce it own JSON serialization
pub fn to_json(&self)->String {
serde_json::to_string_pretty(&self).unwrap()
}

}

/// The fmt method allows display all atributes of an Account
impl fmt::Display for Account{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {}, {}, {}, {})", self.id, self.swift, self.iban, self.created_at, self.status)
}
}
}
111 changes: 66 additions & 45 deletions account/src/bankaccount_module/routes_handlers.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,74 @@

use warp::*;
use std::collections::HashMap;
use std::sync::{Arc,Mutex, RwLock};
// use log::{debug, error, log_enabled, info, Level};

use crate::bankaccount_module::message::Msg;

pub async fn get_accunts_handler(s: String) -> Result<impl warp::Reply, warp::Rejection>{
let mut mesg = Msg::new();
mesg.msg = String::from("okk");
let response = Ok(warp::reply::with_status(warp::reply::json(&mesg), http::StatusCode::OK));
println!("ok");
return response;

// let result = c.get_users();
// let users = result.read().unwrap();

// // If there is at least 1 User
// if users.len() > 0 {
// let response = Ok(warp::reply::with_status(warp::reply::json(result), http::StatusCode::OK));
// println!("ok");
// return response;
// }else{
// let response = Ok(warp::reply::with_status(warp::reply::json(result), http::StatusCode::NOT_FOUND));
// println!("err");
// return response;


use warp::*;
use std::collections::HashMap;
use std::sync::{Arc,Mutex, RwLock};
// use log::{debug, error, log_enabled, info, Level};



use chrono::{Local, NaiveDate, NaiveDateTime};
use crate::bankaccount_module::controller::{BankAccountController};
use crate::bankaccount_module::message::Msg;
use crate::bankaccount_module::model::bankaccount::{Account};

pub async fn get_accounts_handler(bac: BankAccountController) -> Result<impl warp::Reply, warp::Rejection>{
let mut mesg = Msg::new();
mesg.msg = String::from("okk");
let response = Ok(warp::reply::with_status(warp::reply::json(&mesg), http::StatusCode::OK));
println!("ok");
return response;

// let result = c.get_users();
// let users = result.read().unwrap();

// // If there is at least 1 User
// if users.len() > 0 {
// let response = Ok(warp::reply::with_status(warp::reply::json(result), http::StatusCode::OK));
// println!("ok");
// return response;
// }else{
// let response = Ok(warp::reply::with_status(warp::reply::json(result), http::StatusCode::NOT_FOUND));
// println!("err");
// return response;
// }
}



// HU 1: add bank account
pub async fn add_accounts_handler(c: Account, mut bac: BankAccountController) -> Result<impl warp::Reply, warp::Rejection>{
let mut mesg = Msg::new();
bac.add_bank_account(c);

mesg.msg = format!("{}", "Bank account added successfully");
let response = Ok(warp::reply::with_status(warp::reply::json(&mesg), http::StatusCode::OK));
return response;
}


// pub async fn not_responding(req: HttpRequest) -> Result<HttpResponse, Error> {
// log::info!("Received request from uri: {}", req.uri());
// let mut mesg = Msg::new();
// mesg.msg = String::from("You are not allowed to be here");
// return Ok(HttpResponse::BadRequest().json(mesg));
// }
}


// pub async fn not_responding(req: HttpRequest) -> Result<HttpResponse, Error> {
// log::info!("Received request from uri: {}", req.uri());
// let mut mesg = Msg::new();
// mesg.msg = String::from("You are not allowed to be here");
// return Ok(HttpResponse::BadRequest().json(mesg));
// }
// pub async fn healthcheck(req: HttpRequest) -> Result<HttpResponse, Error> {
// log::info!("Received request from uri: {}", req.uri());
// let mut mesg = Msg::new();
// mesg.msg = String::from("I am alive");
// return Ok(HttpResponse::Ok().json(mesg));
// }


// pub async fn healthcheck(req: HttpRequest) -> Result<HttpResponse, Error> {
// log::info!("Received request from uri: {}", req.uri());
// let mut mesg = Msg::new();
// mesg.msg = String::from("I am alive");
// return Ok(HttpResponse::Ok().json(mesg));
// }
// pub async fn get_account( req: HttpRequest) -> Result<HttpResponse, Error> {
// log::info!("Received request from uri: {}", req.uri());
// let mut mesg = Msg::new();
// mesg.msg = String::from("Here is your account");
// return Ok(HttpResponse::Ok().json(mesg));
// }


// pub async fn get_account( req: HttpRequest) -> Result<HttpResponse, Error> {
// log::info!("Received request from uri: {}", req.uri());
// let mut mesg = Msg::new();
// mesg.msg = String::from("Here is your account");
// return Ok(HttpResponse::Ok().json(mesg));
// }
Loading

0 comments on commit 2d5dc42

Please sign in to comment.