Skip to content

Commit 845234f

Browse files
authored
Merge pull request #25 from suba327777/main
feat/update account
2 parents 5ec8d16 + a3364c1 commit 845234f

File tree

8 files changed

+110
-21
lines changed

8 files changed

+110
-21
lines changed

src/domain/repository/account.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use super::super::object::account::{Account, AccountId};
2-
use anyhow::Result;
2+
use anyhow;
33

44
pub trait AccountRepository {
5-
fn insert(&self, account: &Account) -> Result<()>;
6-
fn list(&self) -> Result<Vec<Account>>;
7-
fn find_by_id(&self, account_id: &AccountId) -> Result<Account>;
8-
fn delete(&self, account: &Account) -> Result<()>;
5+
fn insert(&self, account: &Account) -> anyhow::Result<()>;
6+
fn list(&self) -> anyhow::Result<Vec<Account>>;
7+
fn find_by_id(&self, account_id: &AccountId) -> anyhow::Result<Account>;
8+
fn update(&self, account: &Account) -> anyhow::Result<()>;
9+
fn delete(&self, account: &Account) -> anyhow::Result<()>;
910
}

src/infrastructures/repository/account.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::super::database::models::{AccountEntity, NewAccountEntity};
22
use crate::domain::object::account::{Account, AccountId};
33
use crate::domain::repository::account::AccountRepository;
4-
use anyhow::Result;
4+
use anyhow;
55
use chrono::NaiveDateTime;
66
use diesel::prelude::*;
77
use diesel::r2d2::{ConnectionManager, Pool};
@@ -57,7 +57,7 @@ pub struct AccountRepositoryImpl {
5757
}
5858

5959
impl AccountRepository for AccountRepositoryImpl {
60-
fn insert(&self, account: &Account) -> Result<()> {
60+
fn insert(&self, account: &Account) -> anyhow::Result<()> {
6161
use super::super::database::schema::account::dsl;
6262

6363
let entity = NewAccountEntity::from(account);
@@ -69,7 +69,7 @@ impl AccountRepository for AccountRepositoryImpl {
6969
Ok(())
7070
}
7171

72-
fn list(&self) -> Result<Vec<Account>> {
72+
fn list(&self) -> anyhow::Result<Vec<Account>> {
7373
use super::super::database::schema::account::dsl;
7474

7575
let query = dsl::account.into_boxed();
@@ -79,7 +79,7 @@ impl AccountRepository for AccountRepositoryImpl {
7979
Ok(results.into_iter().map(|e| e.of()).collect())
8080
}
8181

82-
fn find_by_id(&self, account_id: &AccountId) -> Result<Account> {
82+
fn find_by_id(&self, account_id: &AccountId) -> anyhow::Result<Account> {
8383
use super::super::database::schema::account::dsl;
8484
use super::super::database::schema::account::id;
8585

@@ -91,7 +91,19 @@ impl AccountRepository for AccountRepositoryImpl {
9191
Ok(entity.of())
9292
}
9393

94-
fn delete(&self, account: &Account) -> Result<()> {
94+
fn update(&self, account: &Account) -> anyhow::Result<()> {
95+
use super::super::database::schema::account::dsl;
96+
97+
let mut conn = self.pool.get()?;
98+
let entity = AccountEntity::from(account);
99+
diesel::update(dsl::account.filter(dsl::id.eq(account.id.get())))
100+
.set(&entity)
101+
.execute(&mut conn)?;
102+
103+
Ok(())
104+
}
105+
106+
fn delete(&self, account: &Account) -> anyhow::Result<()> {
95107
let entity = AccountEntity::from(account);
96108
let mut conn = self.pool.get()?;
97109
diesel::delete(&entity).execute(&mut conn)?;

src/server/handler/account.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::super::response::account::{AccountDto, AccountListResopnse};
33
use crate::domain::object::account::AccountId;
44
use crate::server::connection::RequestContext;
55
use crate::usecase;
6-
use actix_web::{delete, get, post, web, web::Json, HttpResponse, Responder};
6+
use actix_web::{delete, get, post, put, web, web::Json, HttpResponse, Responder};
77

88
#[post("/accounts")]
99
async fn post_account(
@@ -42,6 +42,21 @@ async fn get_account(
4242
}
4343
}
4444

45+
#[put("/accounts/{id}")]
46+
async fn put_account(
47+
data: web::Data<RequestContext>,
48+
request: Json<AccountRequest>,
49+
path_params: web::Path<(i64,)>,
50+
) -> impl Responder {
51+
let account_id = AccountId::new(path_params.into_inner().0);
52+
match usecase::account::put_account(&mut data.account_repository(), &request, &account_id) {
53+
Ok(_) => HttpResponse::Ok().finish(),
54+
Err(err) => {
55+
HttpResponse::InternalServerError().json(format!("Internal Server Error {}", err))
56+
}
57+
}
58+
}
59+
4560
#[delete("/accounts/{id}")]
4661
async fn delete_account(
4762
data: web::Data<RequestContext>,

src/server/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod connection;
22
mod handler;
3-
mod request;
3+
pub mod request;
44
mod response;
55
pub mod router;

src/server/request/account.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use crate::domain::object::account::Account;
1+
use crate::domain::object::account::{Account, AccountId};
22
use chrono::NaiveDateTime;
33
use serde::Deserialize;
44

55
#[derive(Debug, Default, Deserialize)]
66
pub struct AccountRequest {
7-
username: String,
8-
grade: i32,
9-
expiration_date: NaiveDateTime,
7+
pub username: String,
8+
pub grade: i32,
9+
pub expiration_date: NaiveDateTime,
1010
}
1111

1212
#[derive(Debug, Default, Deserialize)]
@@ -22,4 +22,13 @@ impl AccountRequest {
2222
self.expiration_date.to_owned(),
2323
)
2424
}
25+
pub fn model(&self, account_id: AccountId, created_at: NaiveDateTime) -> Account {
26+
Account {
27+
id: account_id,
28+
username: self.username.to_owned(),
29+
grade: self.grade.to_owned(),
30+
expiration_date: self.expiration_date.to_owned(),
31+
created_at,
32+
}
33+
}
2534
}

src/server/router.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub async fn run() -> std::io::Result<()> {
1111
.service(handler::account::post_account)
1212
.service(handler::account::get_accounts)
1313
.service(handler::account::get_account)
14+
.service(handler::account::put_account)
1415
.service(handler::account::delete_account)
1516
.service(handler::card::post_card)
1617
.service(handler::card::get_cards)

src/tests/mock_account_repository.rs

+11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ impl AccountRepository for MockAccountRepository {
2929
None => Err(anyhow::anyhow!("Account not found")),
3030
}
3131
}
32+
33+
fn update(&self, account: &Account) -> anyhow::Result<()> {
34+
let _ = &self
35+
.pool
36+
.borrow_mut()
37+
.entry(account.id.get())
38+
.or_insert_with(|| account.clone());
39+
40+
Ok(())
41+
}
42+
3243
fn delete(&self, account: &Account) -> anyhow::Result<()> {
3344
let _ = &self.pool.borrow_mut().remove(&account.id.get());
3445
Ok(())

src/usecase/account.rs

+45-5
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
11
use crate::domain::object::account::{Account, AccountId};
22
use crate::domain::repository::account::AccountRepository;
3-
use anyhow::Result;
3+
use crate::server::request::account::AccountRequest;
4+
use actix_web::web::Json;
5+
use anyhow;
46

5-
pub fn post_account(repository: &mut impl AccountRepository, account: &Account) -> Result<()> {
7+
pub fn post_account(
8+
repository: &mut impl AccountRepository,
9+
account: &Account,
10+
) -> anyhow::Result<()> {
611
repository.insert(account)
712
}
813

9-
pub fn get_account_list(repository: &mut impl AccountRepository) -> Result<Vec<Account>> {
14+
pub fn get_account_list(repository: &mut impl AccountRepository) -> anyhow::Result<Vec<Account>> {
1015
repository.list()
1116
}
1217

1318
pub fn get_account(
1419
repository: &mut impl AccountRepository,
1520
account_id: &AccountId,
16-
) -> Result<Account> {
21+
) -> anyhow::Result<Account> {
1722
repository.find_by_id(account_id)
1823
}
1924

25+
pub fn put_account(
26+
repository: &mut impl AccountRepository,
27+
request: &Json<AccountRequest>,
28+
account_id: &AccountId,
29+
) -> anyhow::Result<()> {
30+
let account = repository.find_by_id(account_id)?;
31+
let updated_account = request.model(account.id, account.created_at);
32+
repository.update(&updated_account)
33+
}
34+
2035
pub fn delete_account(
2136
repository: &mut impl AccountRepository,
2237
account_id: &AccountId,
23-
) -> Result<()> {
38+
) -> anyhow::Result<()> {
2439
let account = repository.find_by_id(account_id)?;
2540
repository.delete(&account)
2641
}
@@ -136,6 +151,31 @@ mod tests {
136151
assert!(result.is_err());
137152
}
138153

154+
#[test]
155+
fn success_put_account() {
156+
let mut repository = MockAccountRepository {
157+
pool: RefCell::new(HashMap::new()),
158+
};
159+
160+
let test_account = Account {
161+
id: AccountId::new(1),
162+
username: "test_user".to_string(),
163+
grade: 4,
164+
expiration_date: Local::now().naive_local() + Duration::hours(1),
165+
created_at: Local::now().naive_local(),
166+
};
167+
168+
let update_account = AccountRequest {
169+
username: "update_user".to_string(),
170+
grade: 3,
171+
expiration_date: Local::now().naive_local() + Duration::hours(2),
172+
};
173+
174+
let _ = repository.insert(&test_account);
175+
let result = put_account(&mut repository, &Json(update_account), &test_account.id);
176+
assert!(result.is_ok());
177+
}
178+
139179
#[test]
140180
fn success_delete_account() {
141181
let mut repository = MockAccountRepository {

0 commit comments

Comments
 (0)