Add a model for likes
parent
b81b9f90ec
commit
7b5f0f1704
@ -0,0 +1,2 @@
|
|||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
DROP TABLE likes;
|
@ -0,0 +1,7 @@
|
|||||||
|
-- Your SQL goes here
|
||||||
|
CREATE TABLE likes (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL,
|
||||||
|
post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE NOT NULL,
|
||||||
|
creation_date TIMESTAMP NOT NULL DEFAULT now()
|
||||||
|
)
|
@ -0,0 +1,29 @@
|
|||||||
|
use chrono;
|
||||||
|
use diesel::{PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods};
|
||||||
|
|
||||||
|
use schema::likes;
|
||||||
|
|
||||||
|
#[derive(Queryable)]
|
||||||
|
pub struct Like {
|
||||||
|
id: i32,
|
||||||
|
user_id: i32,
|
||||||
|
post_id: i32,
|
||||||
|
creation_date: chrono::NaiveDateTime
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Insertable)]
|
||||||
|
#[table_name = "likes"]
|
||||||
|
pub struct NewLike {
|
||||||
|
user_id: i32,
|
||||||
|
post_id: i32
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Like {
|
||||||
|
pub fn get(conn: &PgConnection, id: i32) -> Option<Like> {
|
||||||
|
likes::table.filter(likes::id.eq(id))
|
||||||
|
.limit(1)
|
||||||
|
.load::<Like>(conn)
|
||||||
|
.expect("Error loading like by ID")
|
||||||
|
.into_iter().nth(0)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue