feat: admin routing part 5

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2025-01-28 22:38:14 +03:00
parent c3156aeb41
commit 6d3b6a4e79
35 changed files with 385 additions and 401 deletions

View file

@ -1,10 +1,10 @@
-- Add up migration script here -- Add up migration script here
CREATE TABLE IF NOT EXISTS "role"( CREATE TABLE IF NOT EXISTS "role"(
id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, role_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE,
name VARCHAR(16) NOT NULL UNIQUE name VARCHAR(16) NOT NULL UNIQUE
); );
INSERT INTO "role"(id, name) VALUES (0, 'Builder') ON CONFLICT(id) DO UPDATE SET "name" = 'Builder'; INSERT INTO "role"(role_id, name) VALUES (0, 'Builder') ON CONFLICT(role_id) DO UPDATE SET "name" = 'Builder';
INSERT INTO "role"(id, name) VALUES (1, 'Admin') ON CONFLICT(id) DO UPDATE SET "name" = 'Admin'; INSERT INTO "role"(role_id, name) VALUES (1, 'Admin') ON CONFLICT(role_id) DO UPDATE SET "name" = 'Admin';
INSERT INTO "role"(id, name) VALUES (10, 'Normal') ON CONFLICT(id) DO UPDATE SET "name" = 'Normal'; INSERT INTO "role"(role_id, name) VALUES (10, 'Normal') ON CONFLICT(role_id) DO UPDATE SET "name" = 'Normal';
INSERT INTO "role"(id, name) VALUES (-1, 'Banned') ON CONFLICT(id) DO UPDATE SET "name" = 'Banned'; INSERT INTO "role"(role_id, name) VALUES (-1, 'Banned') ON CONFLICT(role_id) DO UPDATE SET "name" = 'Banned';

View file

@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS "user"(
surname VARCHAR(256) NOT NULL, surname VARCHAR(256) NOT NULL,
gender BOOLEAN NOT NULL, gender BOOLEAN NOT NULL,
birth_date DATE NOT NULL, birth_date DATE NOT NULL,
role_id BIGINT NOT NULL REFERENCES "role" DEFAULT 10, role_id BIGINT NOT NULL REFERENCES "role"(role_id) DEFAULT 10,
creation_time TIMESTAMPTZ NOT NULL DEFAULT NOW() creation_time TIMESTAMPTZ NOT NULL DEFAULT NOW()
); );

View file

@ -1,7 +1,7 @@
-- Add up migration script here -- Add up migration script here
CREATE TABLE IF NOT EXISTS "post"( CREATE TABLE IF NOT EXISTS "post"(
user_id BIGSERIAL NOT NULL REFERENCES "user"(user_id), post_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE,
creation_time TIMESTAMPTZ UNIQUE NOT NULL DEFAULT NOW(), user_id BIGINT NOT NULL REFERENCES "user"(user_id),
post VARCHAR(8192) NOT NULL UNIQUE, creation_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY(user_id, creation_time) post TEXT NOT NULL UNIQUE
); );

View file

@ -1,7 +1,8 @@
-- Add up migration script here -- Add up migration script here
CREATE TABLE IF NOT EXISTS "comment"( CREATE TABLE IF NOT EXISTS "comment"(
creation_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW(), comment_id BIGSERIAL NOT NULL PRIMARY KEY,
post_creation_time TIMESTAMPTZ NOT NULL REFERENCES "post"(creation_time), user_id BIGINT NOT NULL REFERENCES "user"(user_id),
user_id BIGSERIAL NOT NULL REFERENCES "user"(user_id), post_id BIGINT NOT NULL REFERENCES "post"(post_id),
comment VARCHAR(8192) NOT NULL creation_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
comment TEXT NOT NULL
); );

View file

@ -1,5 +1,5 @@
-- Add up migration script here -- Add up migration script here
CREATE TABLE IF NOT EXISTS "interaction"( CREATE TABLE IF NOT EXISTS "interaction"(
id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, interaction_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE,
name VARCHAR(32) UNIQUE NOT NULL name VARCHAR(32) UNIQUE NOT NULL
); );

View file

@ -1,7 +1,8 @@
-- Add up migration script here -- Add up migration script here
CREATE TABLE IF NOT EXISTS "post_interaction"( CREATE TABLE IF NOT EXISTS "post_interaction"(
interaction_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW(), post_id BIGINT NOT NULL REFERENCES "post"(post_id),
post_creation_time TIMESTAMPTZ NOT NULL REFERENCES "post"(creation_time), user_id BIGINT NOT NULL REFERENCES "user"(user_id),
user_id BIGSERIAL NOT NULL REFERENCES "user"(user_id), interaction_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id) interaction_id BIGINT NOT NULL REFERENCES "interaction"(interaction_id),
PRIMARY KEY(post_id, user_id)
); );

View file

@ -1,7 +1,8 @@
-- Add up migration script here -- Add up migration script here
CREATE TABLE IF NOT EXISTS "comment_interaction"( CREATE TABLE IF NOT EXISTS "comment_interaction"(
interaction_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW(), comment_id BIGINT NOT NULL REFERENCES "comment"(comment_id),
comment_creation_time TIMESTAMPTZ NOT NULL REFERENCES "comment"(creation_time), user_id BIGINT NOT NULL REFERENCES "user"(user_id),
user_id BIGSERIAL NOT NULL REFERENCES "user"(user_id), interaction_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id) interaction_id BIGINT NOT NULL REFERENCES "interaction"(interaction_id),
PRIMARY KEY(comment_id, user_id)
); );

View file

@ -1,7 +1,7 @@
-- Add up migration script here -- Add up migration script here
CREATE TABLE IF NOT EXISTS "contact"( CREATE TABLE IF NOT EXISTS "contact"(
id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, contact_id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE,
name VARCHAR(32) NOT NULL UNIQUE name VARCHAR(32) NOT NULL UNIQUE
); );
INSERT INTO "contact"(id, name) VALUES (0, 'Email') ON CONFLICT(id) DO UPDATE SET "name" = 'Email'; INSERT INTO "contact"(contact_id, name) VALUES (0, 'Email') ON CONFLICT(contact_id) DO UPDATE SET "name" = 'Email';

View file

@ -1,7 +1,7 @@
-- Add up migration script here -- Add up migration script here
CREATE TABLE IF NOT EXISTS "user_contact"( CREATE TABLE IF NOT EXISTS "user_contact"(
user_id BIGSERIAL NOT NULL REFERENCES "user"(user_id), user_id BIGSERIAL NOT NULL REFERENCES "user"(user_id),
contact_id BIGSERIAL NOT NULL REFERENCES "contact"(id), contact_id BIGSERIAL NOT NULL REFERENCES "contact"(contact_id),
contact_value VARCHAR(256) NOT NULL, contact_value VARCHAR(256) NOT NULL,
PRIMARY KEY (user_id, contact_id), PRIMARY KEY (user_id, contact_id),
UNIQUE (contact_id, contact_value) UNIQUE (contact_id, contact_value)

View file

@ -1,80 +1,79 @@
use chrono::{DateTime, Utc};
use crate::feature::comment::Comment; use crate::feature::comment::Comment;
use super::DATABASE_CONNECTIONS; use super::DATABASE_CONNECTIONS;
pub async fn create( pub async fn create(
post_creation_time: &DateTime<Utc>,
user_id: &i64, user_id: &i64,
post_id: &i64,
comment: &String, comment: &String,
) -> Result<Comment, sqlx::Error> { ) -> Result<Comment, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Comment, Comment,
r#" r#"
INSERT INTO "comment"(post_creation_time, user_id, comment) INSERT INTO "comment"(user_id, post_id, comment)
VALUES ($1, $2, $3) VALUES ($1, $2, $3)
RETURNING * RETURNING *
"#, "#,
post_creation_time,
user_id, user_id,
post_id,
comment, comment,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn read(creation_time: &DateTime<Utc>) -> Result<Comment, sqlx::Error> { pub async fn read(comment_id: &i64) -> Result<Comment, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Comment, Comment,
r#" r#"
SELECT * FROM "comment" WHERE "creation_time" = $1 SELECT * FROM "comment" WHERE "comment_id" = $1
"#, "#,
creation_time comment_id,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn update( pub async fn update(
creation_time: &DateTime<Utc>, comment_id: &i64,
user_id: &i64,
comment: &String, comment: &String,
) -> Result<Comment, sqlx::Error> { ) -> Result<Comment, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Comment, Comment,
r#" r#"
UPDATE "comment" SET "comment" = $2 WHERE "creation_time" = $1 UPDATE "comment" SET "comment" = $3 WHERE "comment_id" = $1 AND "user_id" = $2
RETURNING * RETURNING *
"#, "#,
creation_time, comment_id,
user_id,
comment comment
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn delete(creation_time: &DateTime<Utc>) -> Result<Comment, sqlx::Error> { pub async fn delete(comment_id: &i64, user_id: &i64) -> Result<Comment, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Comment, Comment,
r#" r#"
DELETE FROM "comment" WHERE "creation_time" = $1 DELETE FROM "comment" WHERE "comment_id" = $1 AND "user_id" = $2
RETURNING * RETURNING *
"#, "#,
creation_time comment_id,
user_id,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn read_all_for_post( pub async fn read_all_for_post(post_id: &i64) -> Result<Vec<Comment>, sqlx::Error> {
post_creation_time: &DateTime<Utc>,
) -> Result<Vec<Comment>, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Comment, Comment,
r#" r#"
SELECT * FROM "comment" WHERE "post_creation_time" = $1 SELECT * FROM "comment" WHERE "post_id" = $1
"#, "#,
post_creation_time post_id,
) )
.fetch_all(&*DATABASE_CONNECTIONS) .fetch_all(&*DATABASE_CONNECTIONS)
.await .await

View file

@ -1,22 +1,20 @@
use chrono::{DateTime, Utc};
use crate::feature::comment_interaction::CommentInteraction; use crate::feature::comment_interaction::CommentInteraction;
use super::DATABASE_CONNECTIONS; use super::DATABASE_CONNECTIONS;
pub async fn create( pub async fn create(
comment_creation_time: &DateTime<Utc>, comment_id: &i64,
user_id: &i64, user_id: &i64,
interaction_id: &i64, interaction_id: &i64,
) -> Result<CommentInteraction, sqlx::Error> { ) -> Result<CommentInteraction, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
CommentInteraction, CommentInteraction,
r#" r#"
INSERT INTO "comment_interaction"(comment_creation_time, user_id, interaction_id) INSERT INTO "comment_interaction"(comment_id, user_id, interaction_id)
VALUES ($1, $2, $3) VALUES ($1, $2, $3)
RETURNING * RETURNING *
"#, "#,
comment_creation_time, comment_id,
user_id, user_id,
interaction_id, interaction_id,
) )
@ -24,57 +22,61 @@ pub async fn create(
.await .await
} }
pub async fn read(interaction_time: &DateTime<Utc>) -> Result<CommentInteraction, sqlx::Error> { pub async fn read(comment_id: &i64, user_id: &i64) -> Result<CommentInteraction, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
CommentInteraction, CommentInteraction,
r#" r#"
SELECT * FROM "comment_interaction" WHERE "interaction_time" = $1 SELECT * FROM "comment_interaction" WHERE "comment_id" = $1 AND "user_id" = $2
"#, "#,
interaction_time comment_id,
user_id,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn update( pub async fn update(
interaction_time: &DateTime<Utc>, comment_id: &i64,
user_id: &i64,
interaction_id: &i64, interaction_id: &i64,
) -> Result<CommentInteraction, sqlx::Error> { ) -> Result<CommentInteraction, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
CommentInteraction, CommentInteraction,
r#" r#"
UPDATE "comment_interaction" SET "interaction_id" = $2 WHERE "interaction_time" = $1 UPDATE "comment_interaction" SET "interaction_id" = $3 WHERE "comment_id" = $1 AND "user_id" = $2
RETURNING * RETURNING *
"#, "#,
interaction_time, comment_id,
user_id,
interaction_id interaction_id
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn delete(interaction_time: &DateTime<Utc>) -> Result<CommentInteraction, sqlx::Error> { pub async fn delete(comment_id: &i64, user_id: &i64) -> Result<CommentInteraction, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
CommentInteraction, CommentInteraction,
r#" r#"
DELETE FROM "comment_interaction" WHERE "interaction_time" = $1 DELETE FROM "comment_interaction" WHERE "comment_id" = $1 AND "user_id" = $2
RETURNING * RETURNING *
"#, "#,
interaction_time comment_id,
user_id,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn read_all_for_comment( pub async fn read_all_for_comment(
comment_creation_time: &DateTime<Utc>, comment_id: &i64,
) -> Result<Vec<CommentInteraction>, sqlx::Error> { ) -> Result<Vec<CommentInteraction>, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
CommentInteraction, CommentInteraction,
r#" r#"
SELECT * FROM "comment_interaction" WHERE "comment_creation_time" = $1 SELECT * FROM "comment_interaction" WHERE "comment_id" = $1
"#, "#,
comment_creation_time comment_id
) )
.fetch_all(&*DATABASE_CONNECTIONS) .fetch_all(&*DATABASE_CONNECTIONS)
.await .await

View file

@ -16,40 +16,40 @@ pub async fn create(name: &String) -> Result<Contact, sqlx::Error> {
.await .await
} }
pub async fn read(id: &i64) -> Result<Contact, sqlx::Error> { pub async fn read(contact_id: &i64) -> Result<Contact, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Contact, Contact,
r#" r#"
SELECT * FROM "contact" WHERE "id" = $1 SELECT * FROM "contact" WHERE "contact_id" = $1
"#, "#,
id contact_id,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn update(id: &i64, name: &String) -> Result<Contact, sqlx::Error> { pub async fn update(contact_id: &i64, name: &String) -> Result<Contact, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Contact, Contact,
r#" r#"
UPDATE "contact" SET "name" = $2 WHERE "id" = $1 UPDATE "contact" SET "name" = $2 WHERE "contact_id" = $1
RETURNING * RETURNING *
"#, "#,
id, contact_id,
name, name,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn delete(id: &i64) -> Result<Contact, sqlx::Error> { pub async fn delete(contact_id: &i64) -> Result<Contact, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Contact, Contact,
r#" r#"
DELETE FROM "contact" WHERE "id" = $1 DELETE FROM "contact" WHERE "contact_id" = $1
RETURNING * RETURNING *
"#, "#,
id contact_id,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await

View file

@ -16,40 +16,40 @@ pub async fn create(name: &String) -> Result<Interaction, sqlx::Error> {
.await .await
} }
pub async fn read(id: &i64) -> Result<Interaction, sqlx::Error> { pub async fn read(interaction_id: &i64) -> Result<Interaction, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Interaction, Interaction,
r#" r#"
SELECT * FROM "interaction" WHERE "id" = $1 SELECT * FROM "interaction" WHERE "interaction_id" = $1
"#, "#,
id interaction_id,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn update(id: &i64, name: &String) -> Result<Interaction, sqlx::Error> { pub async fn update(interaction_id: &i64, name: &String) -> Result<Interaction, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Interaction, Interaction,
r#" r#"
UPDATE "interaction" SET "name" = $2 WHERE "id" = $1 UPDATE "interaction" SET "name" = $2 WHERE "interaction_id" = $1
RETURNING * RETURNING *
"#, "#,
id, interaction_id,
name name
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn delete(id: &i64) -> Result<Interaction, sqlx::Error> { pub async fn delete(interaction_id: &i64) -> Result<Interaction, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Interaction, Interaction,
r#" r#"
DELETE FROM "interaction" WHERE "id" = $1 DELETE FROM "interaction" WHERE "interaction_id" = $1
RETURNING * RETURNING *
"#, "#,
id interaction_id,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await

View file

@ -1,5 +1,3 @@
use chrono::{DateTime, Utc};
use crate::feature::post::Post; use crate::feature::post::Post;
use super::DATABASE_CONNECTIONS; use super::DATABASE_CONNECTIONS;
@ -13,53 +11,48 @@ pub async fn create(user_id: &i64, post: &String) -> Result<Post, sqlx::Error> {
RETURNING * RETURNING *
"#, "#,
user_id, user_id,
post post,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn read(user_id: &i64, creation_time: &DateTime<Utc>) -> Result<Post, sqlx::Error> { pub async fn read(post_id: &i64) -> Result<Post, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Post, Post,
r#" r#"
SELECT * FROM "post" WHERE "user_id"= $1 AND "creation_time" = $2 SELECT * FROM "post" WHERE "post_id"= $1
"#, "#,
user_id, post_id,
creation_time
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn update( pub async fn update(post_id: &i64, user_id: &i64, post: &String) -> Result<Post, sqlx::Error> {
user_id: &i64,
creation_time: &DateTime<Utc>,
post: &String,
) -> Result<Post, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Post, Post,
r#" r#"
UPDATE "post" SET post = $3 WHERE "user_id" = $1 AND "creation_time" = $2 UPDATE "post" SET post = $3 WHERE "post_id" = $1 AND "user_id" = $2
RETURNING * RETURNING *
"#, "#,
post_id,
user_id, user_id,
creation_time, post,
post
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn delete(user_id: &i64, creation_time: &DateTime<Utc>) -> Result<Post, sqlx::Error> { pub async fn delete(post_id: &i64, user_id: &i64) -> Result<Post, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Post, Post,
r#" r#"
DELETE FROM "post" WHERE "user_id" = $1 AND "creation_time" = $2 DELETE FROM "post" WHERE "post_id" = $1 AND "user_id" = $2
RETURNING * RETURNING *
"#, "#,
post_id,
user_id, user_id,
creation_time
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await

View file

@ -1,22 +1,20 @@
use chrono::{DateTime, Utc};
use crate::feature::post_interaction::PostInteraction; use crate::feature::post_interaction::PostInteraction;
use super::DATABASE_CONNECTIONS; use super::DATABASE_CONNECTIONS;
pub async fn create( pub async fn create(
post_creation_time: &DateTime<Utc>, post_id: &i64,
user_id: &i64, user_id: &i64,
interaction_id: &i64, interaction_id: &i64,
) -> Result<PostInteraction, sqlx::Error> { ) -> Result<PostInteraction, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
PostInteraction, PostInteraction,
r#" r#"
INSERT INTO "post_interaction"(post_creation_time, user_id, interaction_id) INSERT INTO "post_interaction"(post_id, user_id, interaction_id)
VALUES ($1, $2, $3) VALUES ($1, $2, $3)
RETURNING * RETURNING *
"#, "#,
post_creation_time, post_id,
user_id, user_id,
interaction_id, interaction_id,
) )
@ -24,57 +22,59 @@ pub async fn create(
.await .await
} }
pub async fn read(interaction_time: &DateTime<Utc>) -> Result<PostInteraction, sqlx::Error> { pub async fn read(post_id: &i64, user_id: &i64) -> Result<PostInteraction, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
PostInteraction, PostInteraction,
r#" r#"
SELECT * FROM "post_interaction" WHERE "interaction_time" = $1 SELECT * FROM "post_interaction" WHERE "post_id" = $1 AND "user_id" = $2
"#, "#,
interaction_time post_id,
user_id,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn update( pub async fn update(
interaction_time: &DateTime<Utc>, post_id: &i64,
user_id: &i64,
interaction_id: &i64, interaction_id: &i64,
) -> Result<PostInteraction, sqlx::Error> { ) -> Result<PostInteraction, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
PostInteraction, PostInteraction,
r#" r#"
UPDATE "post_interaction" SET "interaction_id" = $2 WHERE "interaction_time" = $1 UPDATE "post_interaction" SET "interaction_id" = $3 WHERE "post_id" = $1 AND "user_id" = $2
RETURNING * RETURNING *
"#, "#,
interaction_time, post_id,
user_id,
interaction_id, interaction_id,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn delete(interaction_time: &DateTime<Utc>) -> Result<PostInteraction, sqlx::Error> { pub async fn delete(post_id: &i64, user_id: &i64) -> Result<PostInteraction, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
PostInteraction, PostInteraction,
r#" r#"
DELETE FROM "post_interaction" WHERE "interaction_time" = $1 DELETE FROM "post_interaction" WHERE "post_id" = $1 AND "user_id" = $2
RETURNING * RETURNING *
"#, "#,
interaction_time post_id,
user_id,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn read_all_for_post( pub async fn read_all_for_post(post_id: &i64) -> Result<Vec<PostInteraction>, sqlx::Error> {
post_creation_time: &DateTime<Utc>,
) -> Result<Vec<PostInteraction>, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
PostInteraction, PostInteraction,
r#" r#"
SELECT * FROM "post_interaction" WHERE "post_creation_time" = $1 SELECT * FROM "post_interaction" WHERE "post_id" = $1
"#, "#,
post_creation_time post_id,
) )
.fetch_all(&*DATABASE_CONNECTIONS) .fetch_all(&*DATABASE_CONNECTIONS)
.await .await

View file

@ -16,40 +16,40 @@ pub async fn create(name: &String) -> Result<Role, sqlx::Error> {
.await .await
} }
pub async fn read(id: &i64) -> Result<Role, sqlx::Error> { pub async fn read(role_id: &i64) -> Result<Role, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Role, Role,
r#" r#"
SELECT * FROM "role" WHERE "id" = $1 SELECT * FROM "role" WHERE "role_id" = $1
"#, "#,
id role_id,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn update(id: &i64, name: &String) -> Result<Role, sqlx::Error> { pub async fn update(role_id: &i64, name: &String) -> Result<Role, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Role, Role,
r#" r#"
UPDATE "role" SET "name" = $2 WHERE "id" = $1 UPDATE "role" SET "name" = $2 WHERE "role_id" = $1
RETURNING * RETURNING *
"#, "#,
id, role_id,
name, name,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await
} }
pub async fn delete(id: &i64) -> Result<Role, sqlx::Error> { pub async fn delete(role_id: &i64) -> Result<Role, sqlx::Error> {
sqlx::query_as!( sqlx::query_as!(
Role, Role,
r#" r#"
DELETE FROM "role" WHERE "id" = $1 DELETE FROM "role" WHERE "role_id" = $1
RETURNING * RETURNING *
"#, "#,
id role_id,
) )
.fetch_one(&*DATABASE_CONNECTIONS) .fetch_one(&*DATABASE_CONNECTIONS)
.await .await

View file

@ -5,39 +5,39 @@ use crate::database::comment;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Comment { pub struct Comment {
pub creation_time: DateTime<Utc>, pub comment_id: i64,
pub post_creation_time: DateTime<Utc>,
pub user_id: i64, pub user_id: i64,
pub post_id: i64,
pub creation_time: DateTime<Utc>,
pub comment: String, pub comment: String,
} }
impl Comment { impl Comment {
pub async fn create( pub async fn create(
post_creation_time: &DateTime<Utc>,
user_id: &i64, user_id: &i64,
post_id: &i64,
comment: &String, comment: &String,
) -> Result<Comment, sqlx::Error> { ) -> Result<Comment, sqlx::Error> {
comment::create(post_creation_time, user_id, comment).await comment::create(user_id, post_id, comment).await
} }
pub async fn read(creation_time: &DateTime<Utc>) -> Result<Comment, sqlx::Error> { pub async fn read(comment_id: &i64) -> Result<Comment, sqlx::Error> {
comment::read(creation_time).await comment::read(comment_id).await
} }
pub async fn update( pub async fn update(
creation_time: &DateTime<Utc>, comment_id: &i64,
user_id: &i64,
comment: &String, comment: &String,
) -> Result<Comment, sqlx::Error> { ) -> Result<Comment, sqlx::Error> {
comment::update(creation_time, comment).await comment::update(comment_id, user_id, comment).await
} }
pub async fn delete(creation_time: &DateTime<Utc>) -> Result<Comment, sqlx::Error> { pub async fn delete(comment_id: &i64, user_id: &i64) -> Result<Comment, sqlx::Error> {
comment::delete(creation_time).await comment::delete(comment_id, user_id).await
} }
pub async fn read_all_for_post( pub async fn read_all_for_post(post_id: &i64) -> Result<Vec<Comment>, sqlx::Error> {
post_creation_time: &DateTime<Utc>, comment::read_all_for_post(post_id).await
) -> Result<Vec<Comment>, sqlx::Error> {
comment::read_all_for_post(post_creation_time).await
} }
} }

View file

@ -5,41 +5,43 @@ use crate::database::comment_interaction;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct CommentInteraction { pub struct CommentInteraction {
pub interaction_time: DateTime<Utc>, pub comment_id: i64,
pub comment_creation_time: DateTime<Utc>,
pub interaction_id: i64,
pub user_id: i64, pub user_id: i64,
pub interaction_time: DateTime<Utc>,
pub interaction_id: i64,
} }
impl CommentInteraction { impl CommentInteraction {
pub async fn create( pub async fn create(
comment_creation_time: &DateTime<Utc>, comment_id: &i64,
user_id: &i64, user_id: &i64,
interaction_id: &i64, interaction_id: &i64,
) -> Result<CommentInteraction, sqlx::Error> { ) -> Result<CommentInteraction, sqlx::Error> {
comment_interaction::create(comment_creation_time, user_id, interaction_id).await comment_interaction::create(comment_id, user_id, interaction_id).await
} }
pub async fn read(interaction_time: &DateTime<Utc>) -> Result<CommentInteraction, sqlx::Error> { pub async fn read(comment_id: &i64, user_id: &i64) -> Result<CommentInteraction, sqlx::Error> {
comment_interaction::read(interaction_time).await comment_interaction::read(comment_id, user_id).await
} }
pub async fn update( pub async fn update(
interaction_time: &DateTime<Utc>, comment_id: &i64,
user_id: &i64,
interaction_id: &i64, interaction_id: &i64,
) -> Result<CommentInteraction, sqlx::Error> { ) -> Result<CommentInteraction, sqlx::Error> {
comment_interaction::update(interaction_time, interaction_id).await comment_interaction::update(comment_id, user_id, interaction_id).await
} }
pub async fn delete( pub async fn delete(
interaction_time: &DateTime<Utc>, comment_id: &i64,
user_id: &i64,
) -> Result<CommentInteraction, sqlx::Error> { ) -> Result<CommentInteraction, sqlx::Error> {
comment_interaction::delete(interaction_time).await comment_interaction::delete(comment_id, user_id).await
} }
pub async fn read_all_for_comment( pub async fn read_all_for_comment(
comment_creation_time: &DateTime<Utc>, comment_id: &i64,
) -> Result<Vec<CommentInteraction>, sqlx::Error> { ) -> Result<Vec<CommentInteraction>, sqlx::Error> {
comment_interaction::read_all_for_comment(comment_creation_time).await comment_interaction::read_all_for_comment(comment_id).await
} }
} }

View file

@ -4,7 +4,7 @@ use crate::database::contact;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Contact { pub struct Contact {
pub id: i64, pub contact_id: i64,
pub name: String, pub name: String,
} }
@ -13,16 +13,16 @@ impl Contact {
contact::create(name).await contact::create(name).await
} }
pub async fn read(id: &i64) -> Result<Contact, sqlx::Error> { pub async fn read(contact_id: &i64) -> Result<Contact, sqlx::Error> {
contact::read(id).await contact::read(contact_id).await
} }
pub async fn update(id: &i64, name: &String) -> Result<Contact, sqlx::Error> { pub async fn update(contact_id: &i64, name: &String) -> Result<Contact, sqlx::Error> {
contact::update(id, name).await contact::update(contact_id, name).await
} }
pub async fn delete(id: &i64) -> Result<Contact, sqlx::Error> { pub async fn delete(contact_id: &i64) -> Result<Contact, sqlx::Error> {
contact::delete(id).await contact::delete(contact_id).await
} }
pub async fn read_all() -> Result<Vec<Contact>, sqlx::Error> { pub async fn read_all() -> Result<Vec<Contact>, sqlx::Error> {

View file

@ -4,7 +4,7 @@ use crate::database::interaction;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Interaction { pub struct Interaction {
pub id: i64, pub interaction_id: i64,
pub name: String, pub name: String,
} }
@ -13,16 +13,16 @@ impl Interaction {
interaction::create(name).await interaction::create(name).await
} }
pub async fn read(id: &i64) -> Result<Interaction, sqlx::Error> { pub async fn read(interaction_id: &i64) -> Result<Interaction, sqlx::Error> {
interaction::read(id).await interaction::read(interaction_id).await
} }
pub async fn update(id: &i64, name: &String) -> Result<Interaction, sqlx::Error> { pub async fn update(interaction_id: &i64, name: &String) -> Result<Interaction, sqlx::Error> {
interaction::update(id, name).await interaction::update(interaction_id, name).await
} }
pub async fn delete(id: &i64) -> Result<Interaction, sqlx::Error> { pub async fn delete(interaction_id: &i64) -> Result<Interaction, sqlx::Error> {
interaction::delete(id).await interaction::delete(interaction_id).await
} }
pub async fn read_all() -> Result<Vec<Interaction>, sqlx::Error> { pub async fn read_all() -> Result<Vec<Interaction>, sqlx::Error> {

View file

@ -5,8 +5,9 @@ use crate::database::post;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Post { pub struct Post {
pub creation_time: DateTime<Utc>, pub post_id: i64,
pub user_id: i64, pub user_id: i64,
pub creation_time: DateTime<Utc>,
pub post: String, pub post: String,
} }
@ -15,20 +16,16 @@ impl Post {
post::create(user_id, post).await post::create(user_id, post).await
} }
pub async fn read(user_id: &i64, creation_time: &DateTime<Utc>) -> Result<Post, sqlx::Error> { pub async fn read(post_id: &i64) -> Result<Post, sqlx::Error> {
post::read(user_id, creation_time).await post::read(post_id).await
} }
pub async fn update( pub async fn update(post_id: &i64, user_id: &i64, post: &String) -> Result<Post, sqlx::Error> {
user_id: &i64, post::update(post_id, user_id, post).await
creation_time: &DateTime<Utc>,
post: &String,
) -> Result<Post, sqlx::Error> {
post::update(user_id, creation_time, post).await
} }
pub async fn delete(user_id: &i64, creation_time: &DateTime<Utc>) -> Result<Post, sqlx::Error> { pub async fn delete(post_id: &i64, user_id: &i64) -> Result<Post, sqlx::Error> {
post::delete(user_id, creation_time).await post::delete(post_id, user_id).await
} }
pub async fn read_all() -> Result<Vec<Post>, sqlx::Error> { pub async fn read_all() -> Result<Vec<Post>, sqlx::Error> {

View file

@ -5,39 +5,38 @@ use crate::database::post_interaction;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct PostInteraction { pub struct PostInteraction {
pub interaction_time: DateTime<Utc>, pub post_id: i64,
pub post_creation_time: DateTime<Utc>,
pub interaction_id: i64,
pub user_id: i64, pub user_id: i64,
pub interaction_time: DateTime<Utc>,
pub interaction_id: i64,
} }
impl PostInteraction { impl PostInteraction {
pub async fn create( pub async fn create(
post_creation_time: &DateTime<Utc>, post_id: &i64,
user_id: &i64, user_id: &i64,
interaction_id: &i64, interaction_id: &i64,
) -> Result<PostInteraction, sqlx::Error> { ) -> Result<PostInteraction, sqlx::Error> {
post_interaction::create(post_creation_time, user_id, interaction_id).await post_interaction::create(post_id, user_id, interaction_id).await
} }
pub async fn read(interaction_time: &DateTime<Utc>) -> Result<PostInteraction, sqlx::Error> { pub async fn read(post_id: &i64, user_id: &i64) -> Result<PostInteraction, sqlx::Error> {
post_interaction::read(interaction_time).await post_interaction::read(post_id, user_id).await
} }
pub async fn update( pub async fn update(
interaction_time: &DateTime<Utc>, post_id: &i64,
user_id: &i64,
interaction_id: &i64, interaction_id: &i64,
) -> Result<PostInteraction, sqlx::Error> { ) -> Result<PostInteraction, sqlx::Error> {
post_interaction::update(interaction_time, interaction_id).await post_interaction::update(post_id, user_id, interaction_id).await
} }
pub async fn delete(interaction_time: &DateTime<Utc>) -> Result<PostInteraction, sqlx::Error> { pub async fn delete(post_id: &i64, user_id: &i64) -> Result<PostInteraction, sqlx::Error> {
post_interaction::delete(interaction_time).await post_interaction::delete(post_id, user_id).await
} }
pub async fn read_all_for_post( pub async fn read_all_for_post(post_id: &i64) -> Result<Vec<PostInteraction>, sqlx::Error> {
post_creation_time: &DateTime<Utc>, post_interaction::read_all_for_post(post_id).await
) -> Result<Vec<PostInteraction>, sqlx::Error> {
post_interaction::read_all_for_post(post_creation_time).await
} }
} }

View file

@ -4,7 +4,7 @@ use crate::database::role;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Role { pub struct Role {
pub id: i64, pub role_id: i64,
pub name: String, pub name: String,
} }
@ -13,16 +13,16 @@ impl Role {
role::create(name).await role::create(name).await
} }
pub async fn read(id: &i64) -> Result<Role, sqlx::Error> { pub async fn read(role_id: &i64) -> Result<Role, sqlx::Error> {
role::read(id).await role::read(role_id).await
} }
pub async fn update(id: &i64, name: &String) -> Result<Role, sqlx::Error> { pub async fn update(role_id: &i64, name: &String) -> Result<Role, sqlx::Error> {
role::update(id, name).await role::update(role_id, name).await
} }
pub async fn delete(id: &i64) -> Result<Role, sqlx::Error> { pub async fn delete(role_id: &i64) -> Result<Role, sqlx::Error> {
role::delete(id).await role::delete(role_id).await
} }
pub async fn read_all() -> Result<Vec<Role>, sqlx::Error> { pub async fn read_all() -> Result<Vec<Role>, sqlx::Error> {

View file

@ -3,13 +3,6 @@ use serde::{Deserialize, Serialize};
use crate::database::user; use crate::database::user;
#[derive(Debug, Serialize, Deserialize)]
pub struct Contact {
pub email: String,
pub phone: String,
pub website: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct User { pub struct User {
pub user_id: i64, pub user_id: i64,

View file

@ -1,3 +1,4 @@
pub mod comment;
pub mod contact; pub mod contact;
pub mod interaction; pub mod interaction;
pub mod login; pub mod login;
@ -19,6 +20,7 @@ pub fn route() -> Router {
.nest("/user_contacts", user_contact::route()) .nest("/user_contacts", user_contact::route())
.nest("/interactions", interaction::route()) .nest("/interactions", interaction::route())
.nest("/posts", post::route()) .nest("/posts", post::route())
.nest("/comments", comment::route())
.route_layer(axum::middleware::from_fn( .route_layer(axum::middleware::from_fn(
builder_or_admin_by_authorization_token, builder_or_admin_by_authorization_token,
)) ))

View file

@ -0,0 +1,25 @@
use axum::{
extract::Path, http::StatusCode, response::IntoResponse, routing::delete, Json, Router,
};
use crate::feature::comment::Comment;
pub fn route() -> Router {
Router::new().route("/{comment_id}", delete(delete_))
}
async fn delete_(Path(comment_id): Path<i64>) -> impl IntoResponse {
match Comment::read(&comment_id).await {
Ok(comment) => match Comment::delete(&comment_id, &comment.user_id).await {
Ok(comment) => (StatusCode::NO_CONTENT, Json(serde_json::json!(comment))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
},
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}

View file

@ -16,15 +16,14 @@ struct CreateContact {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct UpdateContact { struct UpdateContact {
id: i64,
name: String, name: String,
} }
pub fn route() -> Router { pub fn route() -> Router {
Router::new() Router::new()
.route("/", post(create)) .route("/", post(create))
.route("/", patch(update)) .route("/{contact_id}", patch(update))
.route("/{id}", delete(delete_)) .route("/{contact_id}", delete(delete_))
} }
async fn create(Json(create_contact): Json<CreateContact>) -> impl IntoResponse { async fn create(Json(create_contact): Json<CreateContact>) -> impl IntoResponse {
@ -37,8 +36,11 @@ async fn create(Json(create_contact): Json<CreateContact>) -> impl IntoResponse
} }
} }
async fn update(Json(update_contact): Json<UpdateContact>) -> impl IntoResponse { async fn update(
match Contact::update(&update_contact.id, &update_contact.name).await { Path(contact_id): Path<i64>,
Json(update_contact): Json<UpdateContact>,
) -> impl IntoResponse {
match Contact::update(&contact_id, &update_contact.name).await {
Ok(contact) => (StatusCode::ACCEPTED, Json(serde_json::json!(contact))), Ok(contact) => (StatusCode::ACCEPTED, Json(serde_json::json!(contact))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,
@ -47,8 +49,8 @@ async fn update(Json(update_contact): Json<UpdateContact>) -> impl IntoResponse
} }
} }
async fn delete_(Path(id): Path<i64>) -> impl IntoResponse { async fn delete_(Path(contact_id): Path<i64>) -> impl IntoResponse {
match Contact::delete(&id).await { match Contact::delete(&contact_id).await {
Ok(contact) => (StatusCode::NO_CONTENT, Json(serde_json::json!(contact))), Ok(contact) => (StatusCode::NO_CONTENT, Json(serde_json::json!(contact))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,

View file

@ -16,15 +16,14 @@ struct CreateInteraction {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct UpdateInteraction { struct UpdateInteraction {
id: i64,
name: String, name: String,
} }
pub fn route() -> Router { pub fn route() -> Router {
Router::new() Router::new()
.route("/", post(create)) .route("/", post(create))
.route("/", patch(update)) .route("/{interaction_id}", patch(update))
.route("/{id}", delete(delete_)) .route("/{interaction_id}", delete(delete_))
} }
async fn create(Json(create_interaction): Json<CreateInteraction>) -> impl IntoResponse { async fn create(Json(create_interaction): Json<CreateInteraction>) -> impl IntoResponse {
@ -37,8 +36,11 @@ async fn create(Json(create_interaction): Json<CreateInteraction>) -> impl IntoR
} }
} }
async fn update(Json(update_interaction): Json<UpdateInteraction>) -> impl IntoResponse { async fn update(
match Interaction::update(&update_interaction.id, &update_interaction.name).await { Path(interaction_id): Path<i64>,
Json(update_interaction): Json<UpdateInteraction>,
) -> impl IntoResponse {
match Interaction::update(&interaction_id, &update_interaction.name).await {
Ok(interaction) => (StatusCode::ACCEPTED, Json(serde_json::json!(interaction))), Ok(interaction) => (StatusCode::ACCEPTED, Json(serde_json::json!(interaction))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,
@ -47,8 +49,8 @@ async fn update(Json(update_interaction): Json<UpdateInteraction>) -> impl IntoR
} }
} }
async fn delete_(Path(id): Path<i64>) -> impl IntoResponse { async fn delete_(Path(interaction_id): Path<i64>) -> impl IntoResponse {
match Interaction::delete(&id).await { match Interaction::delete(&interaction_id).await {
Ok(interaction) => (StatusCode::NO_CONTENT, Json(serde_json::json!(interaction))), Ok(interaction) => (StatusCode::NO_CONTENT, Json(serde_json::json!(interaction))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,

View file

@ -1,29 +1,22 @@
use std::sync::Arc;
use axum::{ use axum::{
extract::Path, http::StatusCode, response::IntoResponse, routing::delete, Extension, Json, extract::Path, http::StatusCode, response::IntoResponse, routing::delete, Json, Router,
Router,
}; };
use chrono::{DateTime, Utc};
use crate::{ use crate::feature::post::Post;
feature::{post::Post, user::User},
routing::middleware::by_uri_then_insert,
};
pub fn route() -> Router { pub fn route() -> Router {
Router::new().route( Router::new().route("/{post_id}", delete(delete_))
"/users/{user_id}/creation_times/{creation_time}",
delete(delete_).route_layer(axum::middleware::from_fn(by_uri_then_insert)),
)
} }
async fn delete_( async fn delete_(Path(post_id): Path<i64>) -> impl IntoResponse {
Extension(user): Extension<Arc<User>>, match Post::read(&post_id).await {
Path(creation_time): Path<DateTime<Utc>>, Ok(post) => match Post::delete(&post_id, &post.user_id).await {
) -> impl IntoResponse { Ok(post) => (StatusCode::NO_CONTENT, Json(serde_json::json!(post))),
match Post::delete(&user.user_id, &creation_time).await { Err(err_val) => (
Ok(post) => (StatusCode::NO_CONTENT, Json(serde_json::json!(post))), StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
},
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())), Json(serde_json::json!(err_val.to_string())),

View file

@ -11,18 +11,20 @@ use crate::feature::role::Role;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct UpdateRole { struct UpdateRole {
id: i64,
name: String, name: String,
} }
pub fn route() -> Router { pub fn route() -> Router {
Router::new() Router::new()
.route("/", patch(update)) .route("/{role_id}", patch(update))
.route("/{id}", delete(delete_)) .route("/{role_id}", delete(delete_))
} }
async fn update(Json(update_role): Json<UpdateRole>) -> impl IntoResponse { async fn update(
match Role::update(&update_role.id, &update_role.name).await { Path(role_id): Path<i64>,
Json(update_role): Json<UpdateRole>,
) -> impl IntoResponse {
match Role::update(&role_id, &update_role.name).await {
Ok(role) => (StatusCode::ACCEPTED, Json(serde_json::json!(role))), Ok(role) => (StatusCode::ACCEPTED, Json(serde_json::json!(role))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,
@ -31,8 +33,8 @@ async fn update(Json(update_role): Json<UpdateRole>) -> impl IntoResponse {
} }
} }
async fn delete_(Path(id): Path<i64>) -> impl IntoResponse { async fn delete_(Path(role_id): Path<i64>) -> impl IntoResponse {
match Role::delete(&id).await { match Role::delete(&role_id).await {
Ok(role) => (StatusCode::NO_CONTENT, Json(serde_json::json!(role))), Ok(role) => (StatusCode::NO_CONTENT, Json(serde_json::json!(role))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,

View file

@ -1,41 +1,60 @@
use std::sync::Arc;
use axum::{ use axum::{
extract::Path, extract::Path,
http::StatusCode, http::StatusCode,
response::IntoResponse, response::IntoResponse,
routing::{delete, get, patch, post}, routing::{delete, get, patch, post},
Json, Router, Extension, Json, Router,
}; };
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::feature::comment::Comment; use crate::feature::{comment::Comment, user::User};
use super::middleware::by_authorization_token_then_insert;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct CreateComment { struct CreateComment {
post_creation_time: DateTime<Utc>, post_id: i64,
user_id: i64,
comment: String, comment: String,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct UpdateComment { struct UpdateComment {
creation_time: DateTime<Utc>,
comment: String, comment: String,
} }
pub fn route() -> Router { pub fn route() -> Router {
Router::new() Router::new()
.route("/", post(create)) .route(
.route("/{creation_time}", get(read)) "/",
.route("/", patch(update)) post(create).route_layer(axum::middleware::from_fn(
.route("/{creation_time}", delete(delete_)) by_authorization_token_then_insert,
.route("/posts/{post_creation_time}", get(read_all_for_post)) )),
)
.route("/{comment_id}", get(read))
.route(
"/{comment_id}",
patch(update).route_layer(axum::middleware::from_fn(
by_authorization_token_then_insert,
)),
)
.route(
"/{comment_id}",
delete(delete_).route_layer(axum::middleware::from_fn(
by_authorization_token_then_insert,
)),
)
.route("/posts/{post_id}", get(read_all_for_post))
} }
async fn create(Json(create_comment): Json<CreateComment>) -> impl IntoResponse { async fn create(
Extension(user): Extension<Arc<User>>,
Json(create_comment): Json<CreateComment>,
) -> impl IntoResponse {
match Comment::create( match Comment::create(
&create_comment.post_creation_time, &user.user_id,
&create_comment.user_id, &create_comment.post_id,
&create_comment.comment, &create_comment.comment,
) )
.await .await
@ -48,8 +67,8 @@ async fn create(Json(create_comment): Json<CreateComment>) -> impl IntoResponse
} }
} }
async fn read(Path(creation_time): Path<DateTime<Utc>>) -> impl IntoResponse { async fn read(Path(comment_id): Path<i64>) -> impl IntoResponse {
match Comment::read(&creation_time).await { match Comment::read(&comment_id).await {
Ok(comment) => (StatusCode::OK, Json(serde_json::json!(comment))), Ok(comment) => (StatusCode::OK, Json(serde_json::json!(comment))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,
@ -58,8 +77,12 @@ async fn read(Path(creation_time): Path<DateTime<Utc>>) -> impl IntoResponse {
} }
} }
async fn update(Json(update_comment): Json<UpdateComment>) -> impl IntoResponse { async fn update(
match Comment::update(&update_comment.creation_time, &update_comment.comment).await { Extension(user): Extension<Arc<User>>,
Path(comment_id): Path<i64>,
Json(update_comment): Json<UpdateComment>,
) -> impl IntoResponse {
match Comment::update(&comment_id, &user.user_id, &update_comment.comment).await {
Ok(comment) => (StatusCode::ACCEPTED, Json(serde_json::json!(comment))), Ok(comment) => (StatusCode::ACCEPTED, Json(serde_json::json!(comment))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,
@ -68,8 +91,11 @@ async fn update(Json(update_comment): Json<UpdateComment>) -> impl IntoResponse
} }
} }
async fn delete_(Path(creation_time): Path<DateTime<Utc>>) -> impl IntoResponse { async fn delete_(
match Comment::delete(&creation_time).await { Extension(user): Extension<Arc<User>>,
Path(comment_id): Path<i64>,
) -> impl IntoResponse {
match Comment::delete(&comment_id, &user.user_id).await {
Ok(comment) => (StatusCode::NO_CONTENT, Json(serde_json::json!(comment))), Ok(comment) => (StatusCode::NO_CONTENT, Json(serde_json::json!(comment))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,
@ -78,8 +104,8 @@ async fn delete_(Path(creation_time): Path<DateTime<Utc>>) -> impl IntoResponse
} }
} }
async fn read_all_for_post(Path(post_creation_time): Path<DateTime<Utc>>) -> impl IntoResponse { async fn read_all_for_post(Path(comment_id): Path<i64>) -> impl IntoResponse {
match Comment::read_all_for_post(&post_creation_time).await { match Comment::read_all_for_post(&comment_id).await {
Ok(comments) => (StatusCode::OK, Json(serde_json::json!(comments))), Ok(comments) => (StatusCode::OK, Json(serde_json::json!(comments))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,

View file

@ -1,48 +1,48 @@
use std::sync::Arc;
use axum::{ use axum::{
extract::Path, extract::Path,
http::StatusCode, http::StatusCode,
response::IntoResponse, response::IntoResponse,
routing::{delete, get, patch, post}, routing::{delete, get, post},
Json, Router, Extension, Json, Router,
}; };
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::feature::comment_interaction::CommentInteraction; use crate::feature::{comment_interaction::CommentInteraction, user::User};
use super::middleware::by_authorization_token_then_insert;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct CreateCommentInteraction { struct CreateCommentInteraction {
pub comment_creation_time: DateTime<Utc>,
pub interaction_id: i64, pub interaction_id: i64,
pub user_id: i64,
}
#[derive(Debug, Serialize, Deserialize)]
struct UpdateCommentInteraction {
pub interaction_time: DateTime<Utc>,
pub comment_creation_time: DateTime<Utc>,
pub interaction_id: i64,
pub user_id: i64,
} }
pub fn route() -> Router { pub fn route() -> Router {
Router::new() Router::new()
.route("/", post(create))
.route("/{interaction_time}", get(read))
.route("/", patch(update))
.route("/{interaction_time}", delete(delete_))
.route( .route(
"/comments/{comment_creation_time}", "/comments/{comment_id}",
get(read_all_for_comment), post(create).route_layer(axum::middleware::from_fn(
by_authorization_token_then_insert,
)),
) )
.route(
"/comments/{comment_id}",
delete(delete_).route_layer(axum::middleware::from_fn(
by_authorization_token_then_insert,
)),
)
.route("/comments/{comment_id}", get(read_all_for_comment))
} }
async fn create( async fn create(
Extension(user): Extension<Arc<User>>,
Path(comment_id): Path<i64>,
Json(create_comment_interaction): Json<CreateCommentInteraction>, Json(create_comment_interaction): Json<CreateCommentInteraction>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match CommentInteraction::create( match CommentInteraction::create(
&create_comment_interaction.comment_creation_time, &comment_id,
&create_comment_interaction.user_id, &user.user_id,
&create_comment_interaction.interaction_id, &create_comment_interaction.interaction_id,
) )
.await .await
@ -58,38 +58,11 @@ async fn create(
} }
} }
async fn read(Path(interaction_time): Path<DateTime<Utc>>) -> impl IntoResponse { async fn delete_(
match CommentInteraction::read(&interaction_time).await { Extension(user): Extension<Arc<User>>,
Ok(comment_interaction) => (StatusCode::OK, Json(serde_json::json!(comment_interaction))), Path(comment_id): Path<i64>,
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn update(
Json(update_comment_interaction): Json<UpdateCommentInteraction>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match CommentInteraction::update( match CommentInteraction::delete(&comment_id, &user.user_id).await {
&update_comment_interaction.interaction_time,
&update_comment_interaction.interaction_id,
)
.await
{
Ok(comment_interaction) => (
StatusCode::ACCEPTED,
Json(serde_json::json!(comment_interaction)),
),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn delete_(Path(interaction_time): Path<DateTime<Utc>>) -> impl IntoResponse {
match CommentInteraction::delete(&interaction_time).await {
Ok(comment_interaction) => ( Ok(comment_interaction) => (
StatusCode::NO_CONTENT, StatusCode::NO_CONTENT,
Json(serde_json::json!(comment_interaction)), Json(serde_json::json!(comment_interaction)),
@ -101,10 +74,8 @@ async fn delete_(Path(interaction_time): Path<DateTime<Utc>>) -> impl IntoRespon
} }
} }
async fn read_all_for_comment( async fn read_all_for_comment(Path(comment_id): Path<i64>) -> impl IntoResponse {
Path(comment_creation_time): Path<DateTime<Utc>>, match CommentInteraction::read_all_for_comment(&comment_id).await {
) -> impl IntoResponse {
match CommentInteraction::read_all_for_comment(&comment_creation_time).await {
Ok(comment_interactions) => ( Ok(comment_interactions) => (
StatusCode::OK, StatusCode::OK,
Json(serde_json::json!(comment_interactions)), Json(serde_json::json!(comment_interactions)),

View file

@ -7,7 +7,6 @@ use axum::{
routing::{delete, get, patch, post}, routing::{delete, get, patch, post},
Extension, Json, Router, Extension, Json, Router,
}; };
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::feature::{post::Post, user::User}; use crate::feature::{post::Post, user::User};
@ -21,7 +20,6 @@ struct CreatePost {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct UpdatePost { struct UpdatePost {
creation_time: DateTime<Utc>,
post: String, post: String,
} }
@ -33,16 +31,13 @@ pub fn route() -> Router {
by_authorization_token_then_insert, by_authorization_token_then_insert,
)), )),
) )
.route( .route("/{post_id}", get(read))
"/users/{user_id}/creation_times/{creation_time}", .route("/{post_id}", patch(update))
get(read).route_layer(axum::middleware::from_fn(by_uri_then_insert)),
)
.route("/", patch(update))
.route_layer(axum::middleware::from_fn( .route_layer(axum::middleware::from_fn(
by_authorization_token_then_insert, by_authorization_token_then_insert,
)) ))
.route( .route(
"/creation_times/{creation_time}", "/{post_id}",
delete(delete_).route_layer(axum::middleware::from_fn( delete(delete_).route_layer(axum::middleware::from_fn(
by_authorization_token_then_insert, by_authorization_token_then_insert,
)), )),
@ -67,11 +62,8 @@ async fn create(
} }
} }
async fn read( async fn read(Path(post_id): Path<i64>) -> impl IntoResponse {
Extension(user): Extension<Arc<User>>, match Post::read(&post_id).await {
Path(creation_time): Path<DateTime<Utc>>,
) -> impl IntoResponse {
match Post::read(&user.user_id, &creation_time).await {
Ok(post) => (StatusCode::OK, Json(serde_json::json!(post))), Ok(post) => (StatusCode::OK, Json(serde_json::json!(post))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,
@ -82,9 +74,10 @@ async fn read(
async fn update( async fn update(
Extension(user): Extension<Arc<User>>, Extension(user): Extension<Arc<User>>,
Json(update_role): Json<UpdatePost>, Path(post_id): Path<i64>,
Json(update_post): Json<UpdatePost>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match Post::update(&user.user_id, &update_role.creation_time, &update_role.post).await { match Post::update(&post_id, &user.user_id, &update_post.post).await {
Ok(post) => (StatusCode::ACCEPTED, Json(serde_json::json!(post))), Ok(post) => (StatusCode::ACCEPTED, Json(serde_json::json!(post))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,
@ -95,9 +88,9 @@ async fn update(
async fn delete_( async fn delete_(
Extension(user): Extension<Arc<User>>, Extension(user): Extension<Arc<User>>,
Path(creation_time): Path<DateTime<Utc>>, Path(post_id): Path<i64>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match Post::delete(&user.user_id, &creation_time).await { match Post::delete(&post_id, &user.user_id).await {
Ok(post) => (StatusCode::NO_CONTENT, Json(serde_json::json!(post))), Ok(post) => (StatusCode::NO_CONTENT, Json(serde_json::json!(post))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,

View file

@ -1,43 +1,48 @@
use std::sync::Arc;
use axum::{ use axum::{
extract::Path, extract::Path,
http::StatusCode, http::StatusCode,
response::IntoResponse, response::IntoResponse,
routing::{delete, get, patch, post}, routing::{delete, get, post},
Json, Router, Extension, Json, Router,
}; };
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::feature::post_interaction::PostInteraction; use crate::feature::{post_interaction::PostInteraction, user::User};
use super::middleware::by_authorization_token_then_insert;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct CreatePostInteraction { struct CreatePostInteraction {
pub post_creation_time: DateTime<Utc>,
pub interaction_id: i64, pub interaction_id: i64,
pub user_id: i64,
}
#[derive(Debug, Serialize, Deserialize)]
struct UpdatePostInteraction {
pub interaction_time: DateTime<Utc>,
pub post_creation_time: DateTime<Utc>,
pub interaction_id: i64,
pub user_id: i64,
} }
pub fn route() -> Router { pub fn route() -> Router {
Router::new() Router::new()
.route("/", post(create)) .route(
.route("/{interaction_time}", get(read)) "/posts/{post_id}",
.route("/", patch(update)) post(create).route_layer(axum::middleware::from_fn(
.route("/{interaction_time}", delete(delete_)) by_authorization_token_then_insert,
.route("/posts/{post_creation_time}", get(read_all_for_post)) )),
)
.route(
"/posts/{post_id}",
delete(delete_).route_layer(axum::middleware::from_fn(
by_authorization_token_then_insert,
)),
)
.route("/posts/{post_id}", get(read_all_for_post))
} }
async fn create(Json(create_post_interaction): Json<CreatePostInteraction>) -> impl IntoResponse { async fn create(
Extension(user): Extension<Arc<User>>,
Path(post_id): Path<i64>,
Json(create_post_interaction): Json<CreatePostInteraction>,
) -> impl IntoResponse {
match PostInteraction::create( match PostInteraction::create(
&create_post_interaction.post_creation_time, &post_id,
&create_post_interaction.user_id, &user.user_id,
&create_post_interaction.interaction_id, &create_post_interaction.interaction_id,
) )
.await .await
@ -53,36 +58,11 @@ async fn create(Json(create_post_interaction): Json<CreatePostInteraction>) -> i
} }
} }
async fn read(Path(interaction_time): Path<DateTime<Utc>>) -> impl IntoResponse { async fn delete_(
match PostInteraction::read(&interaction_time).await { Extension(user): Extension<Arc<User>>,
Ok(post_interaction) => (StatusCode::OK, Json(serde_json::json!(post_interaction))), Path(post_id): Path<i64>,
Err(err_val) => ( ) -> impl IntoResponse {
StatusCode::BAD_REQUEST, match PostInteraction::delete(&post_id, &user.user_id).await {
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn update(Json(update_post_interaction): Json<UpdatePostInteraction>) -> impl IntoResponse {
match PostInteraction::update(
&update_post_interaction.interaction_time,
&update_post_interaction.interaction_id,
)
.await
{
Ok(post_interaction) => (
StatusCode::ACCEPTED,
Json(serde_json::json!(post_interaction)),
),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn delete_(Path(interaction_time): Path<DateTime<Utc>>) -> impl IntoResponse {
match PostInteraction::delete(&interaction_time).await {
Ok(post_interaction) => ( Ok(post_interaction) => (
StatusCode::NO_CONTENT, StatusCode::NO_CONTENT,
Json(serde_json::json!(post_interaction)), Json(serde_json::json!(post_interaction)),
@ -94,8 +74,8 @@ async fn delete_(Path(interaction_time): Path<DateTime<Utc>>) -> impl IntoRespon
} }
} }
async fn read_all_for_post(Path(post_creation_time): Path<DateTime<Utc>>) -> impl IntoResponse { async fn read_all_for_post(Path(post_id): Path<i64>) -> impl IntoResponse {
match PostInteraction::read_all_for_post(&post_creation_time).await { match PostInteraction::read_all_for_post(&post_id).await {
Ok(post_interactions) => (StatusCode::OK, Json(serde_json::json!(post_interactions))), Ok(post_interactions) => (StatusCode::OK, Json(serde_json::json!(post_interactions))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,

View file

@ -4,12 +4,12 @@ use crate::feature::role::Role;
pub fn route() -> Router { pub fn route() -> Router {
Router::new() Router::new()
.route("/{id}", get(read)) .route("/{role_id}", get(read))
.route("/", get(read_all)) .route("/", get(read_all))
} }
async fn read(Path(id): Path<i64>) -> impl IntoResponse { async fn read(Path(role_id): Path<i64>) -> impl IntoResponse {
match Role::read(&id).await { match Role::read(&role_id).await {
Ok(role) => (StatusCode::OK, Json(serde_json::json!(role))), Ok(role) => (StatusCode::OK, Json(serde_json::json!(role))),
Err(err_val) => ( Err(err_val) => (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,