Skip to content

Commit 5ec8d16

Browse files
authored
Merge pull request #14 from suba327777/feature/card-db
[WIP]Feature/card db
2 parents 6d625a0 + ee94100 commit 5ec8d16

File tree

33 files changed

+858
-101
lines changed

33 files changed

+858
-101
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
-- Your SQL goes here
22
CREATE TABLE account (
33
id bigserial NOT NULL PRIMARY KEY,
4-
username varchar(255) NOT NULL UNIQUE,
4+
username varchar(255) NOT NULL ,
55
grade INTEGER CHECK (grade >= 1 AND grade <= 4) NOT NULL,
6-
card_type varchar(255) NOT NULL,
7-
card_id bytea NOT NULL,
6+
expiration_date TIMESTAMPTZ NOT NULL,
87
created_at TIMESTAMPTZ NOT NULL
98
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- This file should undo anything in `up.sql`
2+
DROP TABLE card;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Your SQL goes here
2+
CREATE TABLE card (
3+
id bigserial NOT NULL PRIMARY KEY,
4+
account_id bigserial NOT NULL,
5+
card_name varchar(255) NOT NULL,
6+
card_number bytea NOT NULL,
7+
created_at TIMESTAMPTZ NOT NULL,
8+
FOREIGN KEY (account_id) REFERENCES account(id)
9+
);

src/domain/object/account.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::Id;
2-
use chrono::{Local, NaiveDateTime};
2+
use crate::utils::time::create_time;
3+
use chrono::NaiveDateTime;
34

45
pub type AccountId = Id<Account>;
56

@@ -8,50 +9,38 @@ pub struct Account {
89
pub id: AccountId,
910
pub username: String,
1011
pub grade: i32,
11-
pub card_type: String,
12-
pub card_id: Vec<u8>,
12+
pub expiration_date: NaiveDateTime,
1313
pub created_at: NaiveDateTime,
1414
}
1515

1616
impl Account {
17-
pub fn create(username: String, grade: i32, card_type: String) -> Self {
17+
pub fn new(username: String, grade: i32, expiration_date: NaiveDateTime) -> Self {
1818
Self {
1919
id: Default::default(),
2020
username,
2121
grade,
22-
card_type,
23-
card_id: create_card_id(),
22+
expiration_date,
2423
created_at: create_time(),
2524
}
2625
}
2726
}
2827

29-
fn create_card_id() -> Vec<u8> {
30-
//TODO
31-
let card_id: Vec<u8> = vec![1, 16, 3, 16, 197, 20, 106, 38];
32-
card_id
33-
}
34-
35-
fn create_time() -> NaiveDateTime {
36-
let local_now = Local::now();
37-
local_now.naive_local()
38-
}
39-
4028
#[cfg(test)]
4129
mod tests {
4230
use super::*;
31+
use chrono::Duration;
4332

4433
#[test]
4534
fn test_create_account() {
4635
let username = "test_user".to_string();
4736
let grade = 4;
48-
let card_type = "Suica".to_string();
49-
let card_id = [1, 16, 3, 16, 197, 20, 106, 38].to_vec();
37+
let current_time = create_time();
38+
let expiration_date = current_time + Duration::hours(1);
5039

51-
let account = Account::create(username.clone(), grade, card_type.clone());
40+
let account = Account::new(username.clone(), grade, expiration_date);
5241

5342
assert_eq!(account.id.get(), 0);
5443
assert_eq!(account.username, username);
55-
assert_eq!(account.card_id, card_id);
44+
assert_eq!(account.expiration_date, expiration_date);
5645
}
5746
}

src/domain/object/card.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use super::{account::AccountId, Id};
2+
use crate::utils::time::create_time;
3+
use chrono::NaiveDateTime;
4+
5+
pub type CardId = Id<Card>;
6+
7+
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
8+
pub struct Card {
9+
pub id: CardId,
10+
pub account_id: AccountId,
11+
pub card_name: String,
12+
pub card_number: Vec<u8>,
13+
pub created_at: NaiveDateTime,
14+
}
15+
16+
impl Card {
17+
pub fn new(account_id: AccountId, card_name: String, card_number: Vec<u8>) -> Self {
18+
Self {
19+
id: Default::default(),
20+
account_id,
21+
card_name,
22+
card_number,
23+
created_at: create_time(),
24+
}
25+
}
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
32+
#[test]
33+
fn test_create_card() {
34+
let account_id = AccountId::new(1);
35+
let card_name = "suica".to_string();
36+
let card_number = [1, 16, 3, 16, 197, 20, 106, 38].to_vec();
37+
38+
let card = Card::new(account_id, card_name, card_number);
39+
40+
assert_eq!(card.account_id.get(), 1);
41+
assert_eq!(card.card_name, "suica");
42+
assert_eq!(card.card_number, vec![1, 16, 3, 16, 197, 20, 106, 38])
43+
}
44+
}

src/domain/object/mod.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
use serde::{Deserialize, Deserializer, Serialize};
12
use std::marker::PhantomData;
23

34
pub mod account;
5+
pub mod card;
46
mod mqtt;
57

6-
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
8+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Serialize)]
79
pub struct Id<T> {
810
id: i64,
911
_phantom: PhantomData<T>,
@@ -26,3 +28,13 @@ impl<T> Default for Id<T> {
2628
Id::new(0)
2729
}
2830
}
31+
32+
impl<'de, T> Deserialize<'de> for Id<T> {
33+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
34+
where
35+
D: Deserializer<'de>,
36+
{
37+
let id = i64::deserialize(deserializer)?;
38+
Ok(Id::new(id))
39+
}
40+
}

src/domain/repository/card.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use super::super::object::{
2+
account::AccountId,
3+
card::{Card, CardId},
4+
};
5+
use anyhow;
6+
pub trait CardRepository {
7+
fn insert(&self, card: &Card) -> anyhow::Result<()>;
8+
fn list(&self, account_id: &AccountId) -> anyhow::Result<Vec<Card>>;
9+
fn find_by_id(&self, card_id: &CardId, account_id: &AccountId) -> anyhow::Result<Card>;
10+
#[allow(dead_code)]
11+
fn find_by_card_number(&self, card_number: &[u8]) -> anyhow::Result<bool>;
12+
fn delete(&self, card: &Card) -> anyhow::Result<()>;
13+
}

src/domain/repository/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod account;
2+
pub mod card;

src/infrastructures/database/models.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use chrono::NaiveDateTime;
66
pub struct NewAccountEntity {
77
pub username: String,
88
pub grade: i32,
9-
pub card_type: String,
10-
pub card_id: Vec<u8>,
9+
pub expiration_date: NaiveDateTime,
1110
pub created_at: NaiveDateTime,
1211
}
1312

@@ -17,7 +16,25 @@ pub struct AccountEntity {
1716
pub id: i64,
1817
pub username: String,
1918
pub grade: i32,
20-
pub card_type: String,
21-
pub card_id: Vec<u8>,
19+
pub expiration_date: NaiveDateTime,
20+
pub created_at: NaiveDateTime,
21+
}
22+
23+
#[derive(Debug, Insertable)]
24+
#[table_name = "card"]
25+
pub struct NewCardEntity {
26+
pub account_id: i64,
27+
pub card_name: String,
28+
pub card_number: Vec<u8>,
29+
pub created_at: NaiveDateTime,
30+
}
31+
32+
#[derive(Debug, Queryable, Identifiable, AsChangeset)]
33+
#[table_name = "card"]
34+
pub struct CardEntity {
35+
pub id: i64,
36+
pub account_id: i64,
37+
pub card_name: String,
38+
pub card_number: Vec<u8>,
2239
pub created_at: NaiveDateTime,
2340
}

src/infrastructures/database/schema.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@ diesel::table! {
66
#[max_length = 255]
77
username -> Varchar,
88
grade -> Int4,
9+
expiration_date -> Timestamptz,
10+
created_at -> Timestamptz,
11+
}
12+
}
13+
14+
diesel::table! {
15+
card (id) {
16+
id -> Int8,
17+
account_id -> Int8,
918
#[max_length = 255]
10-
card_type -> Varchar,
11-
card_id -> Bytea,
19+
card_name -> Varchar,
20+
card_number -> Bytea,
1221
created_at -> Timestamptz,
1322
}
1423
}
24+
25+
diesel::joinable!(card -> account (account_id));
26+
27+
diesel::allow_tables_to_appear_in_same_query!(account, card,);

src/infrastructures/iot/mqtt_client.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::time::Duration;
77

88
type MessageHandler = Arc<dyn Fn(&Message) + Send + Sync>;
99

10+
#[allow(dead_code)]
1011
pub trait MessageListener: Fn(Message) + Send + Sync + 'static {}
1112
impl<T> MessageListener for T where T: Fn(Message) + Send + Sync + 'static {}
1213

src/infrastructures/repository/account.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ impl NewAccountEntity {
1010
pub fn new(
1111
username: String,
1212
grade: i32,
13-
card_type: String,
14-
card_id: Vec<u8>,
13+
expiration_date: NaiveDateTime,
1514
created_at: NaiveDateTime,
1615
) -> Self {
1716
Self {
1817
username,
1918
grade,
20-
card_type,
21-
card_id,
19+
expiration_date,
2220
created_at,
2321
}
2422
}
@@ -27,8 +25,7 @@ impl NewAccountEntity {
2725
NewAccountEntity {
2826
username: model.username.to_owned(),
2927
grade: model.grade.to_owned(),
30-
card_type: model.card_type.to_owned(),
31-
card_id: model.card_id.to_owned(),
28+
expiration_date: model.expiration_date.to_owned(),
3229
created_at: model.created_at.to_owned(),
3330
}
3431
}
@@ -40,8 +37,7 @@ impl AccountEntity {
4037
id: model.id.get(),
4138
username: model.username.to_owned(),
4239
grade: model.grade.to_owned(),
43-
card_type: model.card_type.to_owned(),
44-
card_id: model.card_id.to_owned(),
40+
expiration_date: model.expiration_date.to_owned(),
4541
created_at: model.created_at.to_owned(),
4642
}
4743
}
@@ -50,8 +46,7 @@ impl AccountEntity {
5046
id: AccountId::new(self.id),
5147
username: self.username.to_owned(),
5248
grade: self.grade.to_owned(),
53-
card_type: self.card_type.to_owned(),
54-
card_id: self.card_id.to_owned(),
49+
expiration_date: self.expiration_date.to_owned(),
5550
created_at: self.created_at.to_owned(),
5651
}
5752
}

0 commit comments

Comments
 (0)