Skip to content

Commit

Permalink
editing/creating newspaper and using it on publish
Browse files Browse the repository at this point in the history
  • Loading branch information
PXNX committed Nov 19, 2023
1 parent a2fe65d commit 6114c80
Show file tree
Hide file tree
Showing 17 changed files with 380 additions and 61 deletions.
1 change: 1 addition & 0 deletions migrations/20231118165958_schema2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CREATE TABLE
id BIGSERIAL PRIMARY KEY,
avatar text NOT NULL,
name varchar(40) NOT NULL,
background text,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Expand Down
6 changes: 6 additions & 0 deletions migrations/20231119000241_schema2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Add migration script here
-- Add migration script here
-- Add migration script here

alter table public.newspapers
add "background" text;
88 changes: 88 additions & 0 deletions migrations/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
CREATE TABLE
users (
id BIGSERIAL PRIMARY KEY,
email text NOT NULL UNIQUE,
avatar text,
name varchar(40),
skill_0 smallint default 0,
skill_1 smallint default 0,
skill_2 smallint default 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE
newspapers (
id BIGSERIAL PRIMARY KEY,
avatar text NOT NULL,
name varchar(40) NOT NULL,
background text,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TYPE newspaper_ranks AS ENUM (
'author','editor', 'owner');
CREATE TABLE
journalists (
user_id bigint,
newspaper_id bigint,
rank newspaper_ranks,
primary key (user_id, newspaper_id),
CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users (id),
CONSTRAINT fk_newspaper_id FOREIGN KEY ( newspaper_id) REFERENCES newspapers (id)
);

CREATE TABLE
articles (
id BIGSERIAL PRIMARY KEY,
title varchar(40) not null,
content text NOT NULL,
author_id bigint NOT NULL,
newspaper_id bigint,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_author_id FOREIGN KEY (author_id) REFERENCES users (id),
CONSTRAINT fk_newspaper_id FOREIGN KEY (newspaper_id) REFERENCES newspapers (id)
);

CREATE TABLE
upvotes (
user_id bigint,
article_id bigint,
primary key (user_id, article_id),
CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users (id),
CONSTRAINT fk_article_id FOREIGN KEY (article_id) REFERENCES articles (id)
);

CREATE TABLE
vouchers (
id BIGSERIAL PRIMARY KEY,
code varchar(20) not null UNIQUE,
reward smallint not null
);

CREATE TABLE
used_vouchers (
user_id bigint,
voucher_id bigint,
primary key (user_id, voucher_id),
CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users (id),
CONSTRAINT fk_voucher_id FOREIGN KEY (voucher_id) REFERENCES vouchers (id)
);

CREATE TABLE
user_sessions (
id BIGSERIAL PRIMARY KEY,
user_id bigint NOT NULL,
session_token_p1 text NOT NULL,
session_token_p2 text NOT NULL,
created_at bigint NOT NULL,
expires_at bigint NOT NULL,
CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users (id)
);

CREATE TABLE
oauth2_state_storage (
id bigSERIAL PRIMARY KEY,
csrf_state text NOT NULL,
pkce_code_verifier text NOT NULL,
return_url text NOT NULL
);
1 change: 1 addition & 0 deletions public/icons/account.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions public/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -11457,6 +11457,20 @@ details.collapse summary::-webkit-details-marker {
background-image: url('https://victorypark.com.ua/wp-content/uploads/2023/04/banner_azov.jpeg') !important;
}

.bg-gradient-to-br {
background-image: linear-gradient(to bottom right, var(--tw-gradient-stops)) !important;
}

.from-primary {
--tw-gradient-from: var(--fallback-p,oklch(var(--p)/1)) var(--tw-gradient-from-position) !important;
--tw-gradient-to: var(--fallback-p,oklch(var(--p)/0)) var(--tw-gradient-to-position) !important;
--tw-gradient-stops: var(--tw-gradient-from), var(--tw-gradient-to) !important;
}

.to-secondary {
--tw-gradient-to: var(--fallback-s,oklch(var(--s)/1)) var(--tw-gradient-to-position) !important;
}

.decoration-slice {
-webkit-box-decoration-break: slice !important;
box-decoration-break: slice !important;
Expand Down Expand Up @@ -11550,6 +11564,14 @@ details.collapse summary::-webkit-details-marker {
fill: currentColor !important;
}

.fill-orange-600 {
fill: #ea580c !important;
}

.stroke-neutral-content {
stroke: var(--fallback-nc,oklch(var(--nc)/1)) !important;
}

.stroke-primary {
stroke: var(--fallback-p,oklch(var(--p)/1)) !important;
}
Expand Down
62 changes: 58 additions & 4 deletions src/routes/article.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async fn articles(
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct Createarticle {
struct CreateArticle {
article_title: String,
article_content: String,
publisher: Option<i64>,
Expand All @@ -104,7 +104,7 @@ async fn publish_article(
Extension(user_data): Extension<Option<UserData>>,

State(db_pool): State<PgPool>,
Form(input): Form<Createarticle>,
Form(input): Form<CreateArticle>,
) -> Result<impl IntoResponse, AppError> {
let user_id = user_data.unwrap().id;

Expand Down Expand Up @@ -160,6 +160,59 @@ async fn create_article(
Ok(Html(content))
}

async fn edit_article(
Extension(user_data): Extension<Option<UserData>>,
Path(article_id): Path<i64>,
State(db_pool): State<PgPool>,
State(env): State<Environment<'static>>,
) -> Result<impl IntoResponse, AppError> {
let tmpl = env.get_template("article/edit.html")?;
let user_id = user_data.unwrap().id;

let article = query!(
r#"SELECT content
FROM
articles where id = $1;"#,
&article_id
)
.fetch_one(&db_pool)
.await?;

let content = tmpl.render(
context!(user_id => user_id, article_content=>article.content
),
)?;
Ok(Html(content))
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct EditArticle {
article_content: String,
}

async fn save_article(
Extension(user_data): Extension<Option<UserData>>,
Path(article_id): Path<i64>,
State(db_pool): State<PgPool>,
Form(input): Form<EditArticle>,
) -> Result<impl IntoResponse, AppError> {
let user_id = user_data.unwrap().id;

//todo: check if user is editor/owner

let query = query!(
r#"UPDATE articles SET content = $1
where id = $2;"#,
input.article_content,
&article_id
)
.execute(&db_pool)
.await?;

Ok(Redirect::to(format!("/a/{}", article_id).as_str()))
}

async fn article(
Extension(user_data): Extension<Option<UserData>>,
State(db_pool): State<PgPool>,
Expand Down Expand Up @@ -258,6 +311,7 @@ pub fn article_router() -> Router<AppState> {
Router::new()
.route("/", get(articles))
.route("/:id", get(article))
.route("/edit", get(create_article).post(publish_article))
.route("/upvote/:id", put(upvote_article).delete(remove_upvote))
.route("/create", get(create_article).post(publish_article))
.route("/:id/edit", get(edit_article).post(save_article))
.route("/:id/upvote", put(upvote_article).delete(remove_upvote))
}
97 changes: 95 additions & 2 deletions src/routes/newspaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,110 @@ async fn newspaper(

async fn newspaper_settings(
Extension(user_data): Extension<Option<UserData>>,
Path(newspaper_id): Path<i64>,
State(db_pool): State<PgPool>,
State(env): State<Environment<'static>>,
) -> Result<impl IntoResponse, AppError> {
let tmpl = env.get_template("newspaper/settings.html")?;

let content = tmpl.render(context!(user_id => user_data.unwrap().id))?;
let user_id = user_data.unwrap().id;

let newspaper = query!(
r#"SELECT name, avatar, background FROM newspapers WHERE id=$1;"#,
&newspaper_id
)
.fetch_one(&db_pool)
.await?;

let content = tmpl.render(context!(user_id => user_id,
newspaper_name => newspaper.name,
newspaper_avatar =>newspaper.avatar,
newspaper_background =>newspaper.background,
newspaper_id => newspaper_id
))?;

Ok(Html(content))
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct CreateNewspaper {
newspaper_name: String,
newspaper_avatar: String,
}

async fn publish_newspaper(
Extension(user_data): Extension<Option<UserData>>,

State(db_pool): State<PgPool>,
Form(input): Form<CreateNewspaper>,
) -> Result<impl IntoResponse, AppError> {
let user_id = user_data.unwrap().id;

let newspaper = query!(
r#"INSERT INTO newspapers (name,avatar)
VALUES ($1,$2) returning id;
"#,
input.newspaper_name,
input.newspaper_avatar,
)
.fetch_one(&db_pool)
.await?;

query!(
r#"INSERT INTO journalists (newspaper_id,user_id,rank)
VALUES ($1,$2,'owner');
"#,
&newspaper.id,
user_id,
)
.execute(&db_pool)
.await?;

Ok(Redirect::to(format!("/n/{}", newspaper.id).as_str()))
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct Newspaper {
newspaper_name: String,
newspaper_id: i64,
}

async fn create_newspaper(
Extension(user_data): Extension<Option<UserData>>,
State(db_pool): State<PgPool>,
State(env): State<Environment<'static>>,
) -> Result<impl IntoResponse, AppError> {
let tmpl = env.get_template("newspaper/create.html")?;
let user_id = user_data.unwrap().id;

let newspapers: Vec<Newspaper> = query!(
r#"SELECT
newspapers.name ,
newspapers.id
FROM
journalists
LEFT OUTER JOIN newspapers ON (newspaper_id =newspapers.id) where user_id = $1;"#,
&user_id
)
.fetch_all(&db_pool)
.await?
.iter()
.map(|n| Newspaper {
newspaper_name: n.name.to_owned(),
newspaper_id: n.id,
})
.collect();

let content = tmpl.render(context!(user_id => user_id, newspapers => newspapers
))?;
Ok(Html(content))
}

pub fn newspaper_router() -> Router<AppState> {
Router::new()
// .route("/", get(newspapers))
.route("/create", get(create_newspaper).post(publish_newspaper))
.route("/:id", get(newspaper))
.route("/settings", get(newspaper_settings))
.route("/:id/settings", get(newspaper_settings))
}
2 changes: 1 addition & 1 deletion templates/article/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<h3 class="font-bold text-lg">Publish article?</h3>

<form id="publish_article" hx-ext="debug" class="form-control w-full max-w-sm space-y-4"
hx-post="/a/edit">
hx-post="/a/create">


<div>
Expand Down
Loading

0 comments on commit 6114c80

Please sign in to comment.