DEV Community

Practical Rust Web Development - CI Travis

Werner Echezuría on July 24, 2019

Tools like Travis can be really helpful to figure it out the stability of our software, specially when we have a huge codebase that requires a lot ...
Collapse
 
laynath242 profile image
LayNath242 • Edited

hello teacher.i have user table and role table.

select fullname, roles.title from users inner join roles on roles.id = users.role_id;

how to write this in rust.
thank you.

Collapse
 
aka_dude profile image
Andrew Andersen • Edited

Probably you need this diesel.rs

Collapse
 
laynath242 profile image
LayNath242

thank you, but i'm so beginner i still don't understand.

Thread Thread
 
aka_dude profile image
Andrew Andersen

Do you know Rust?

Thread Thread
 
laynath242 profile image
LayNath242

know but not much.

Collapse
 
deciduously profile image
Ben Lovy

Check out include_sql. Diesel is a full featured ORM. Not all projects need it, you can of course write SQL directly.

Collapse
 
laynath242 profile image
LayNath242

thank you.

Collapse
 
laynath242 profile image
LayNath242 • Edited

Hello. I do this and got all data. but i only need only username and title. How to do this.Thank you.

[derive(Serialize, Deserialize)]

pub struct UserRoleList(pub Vec<(Role, User)>);

impl UserRoleList {
pub fn list(connection: &PgConnection) -> Self {
use crate::schema::{roles,users};
use diesel::RunQueryDsl;
use diesel::QueryDsl;

    let result = roles::table.inner_join(users::table)
                .load::<(Role, User)>(connection)
                .expect("Error loading ");
        UserRoleList(result)
}

}

Collapse
 
werner profile image
Werner Echezuría

Hi, like SQL you can use a select clause, for example: github.com/practical-rust-web-deve...

Collapse
 
laynath242 profile image
LayNath242

thank you

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
werner profile image
Werner Echezuría

Hi, you need to register an user, for example:

➜  mystore git:(master) curl -i --request POST \                                                                                          <<<
  --url http://localhost:8088/register \
  --header 'content-type: application/json' \
  --data '{
        "email": "name@domain.com",
        "company": "My Company",
        "password":"password",
        "password_confirmation": "password"
}'
HTTP/1.1 200 OK
content-length: 92
content-type: application/json
date: Tue, 06 Aug 2019 12:38:34 GMT

{"email":"name@domain.com","company":"My Company","created_at":"2019-08-06T09:38:41.833136"}%                                                 

Then you need to login:

➜  mystore git:(master) curl -i --request POST \                                                                                          <<<
  --url http://localhost:8088/auth \
  --header 'content-type: application/json' \
  --data '{
        "email": "name@domain.com",
        "password":"password"
}'
HTTP/1.1 200 OK
content-length: 92
content-type: application/json
x-csrf-token: 41130a33c627781f7dd0f81d95a33ca4223b8d85b322386793aacb97aa13833115b85913951dafceead03516e870f682d077e300c0ceb030371984d9133ca002991aec8fad22c391
set-cookie: mystorejwt=4LKhublDMvdz0/tU/lZVLQAwj8pSqNLsRl8heiOM8LMyH3pekc7PSdEit09j4eXYFCMcLt+2h+Wxf2U1aoDN2/8J+3hVt/61z3EXTBnGNe/oig3qh0Ftz2mt6OBXjwot5EjogsXtFEbje4TSMby0zE3yy+xOLhejEseBdABz0F6YhNls8zPw2UA7bH+RZ6Fa471Ph83IU0XVhqHDcgz9uQAtlgTs/BnX6EorDK2xTmIDgiWRWr6DCNB005sf76rT4emFbV6DlORe95s8Hh702Q==; HttpOnly; Path=/; Domain=localhost; Max-Age=86400
date: Tue, 06 Aug 2019 12:40:04 GMT

{"email":"name@domain.com","company":"My Company","created_at":"2019-08-06T09:38:41.833136"}%                                                 

You copy the cookie and csrf token and pass them through the request:

curl -v --cookie "mystorejwt=4LKhublDMvdz0/tU/lZVLQAwj8pSqNLsRl8heiOM8LMyH3pekc7PSdEit09j4eXYFCMcLt+2h+Wxf2U1aoDN2/8J+3hVt/61z3EXTBnGNe/oig3qh0Ftz2mt6OBXjwot5EjogsXtFEbje4TSMby0zE3yy+xOLhejEseBdABz0F6YhNls8zPw2UA7bH+RZ6Fa471Ph83IU0XVhqHDcgz9uQAtlgTs/BnX6EorDK2xTmIDgiWRWr6DCNB005sf76rT4emFbV6DlORe95s8Hh702Q==; Path=/; Domain=localhost; Max-Age=86400" --header "x-csrf-token: 41130a33c627781f7dd0f81d95a33ca4223b8d85b322386793aacb97aa13833115b85913951dafceead03516e870f682d077e300c0ceb030371984d9133ca002991aec8fad22c391" -H "Content-Type: application/json"  http://localhost:8088/products\?search\="products"