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
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
);
INSERT INTO "role"(id, name) VALUES (0, 'Builder') ON CONFLICT(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"(id, name) VALUES (10, 'Normal') ON CONFLICT(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 (0, 'Builder') ON CONFLICT(role_id) DO UPDATE SET "name" = 'Builder';
INSERT INTO "role"(role_id, name) VALUES (1, 'Admin') ON CONFLICT(role_id) DO UPDATE SET "name" = 'Admin';
INSERT INTO "role"(role_id, name) VALUES (10, 'Normal') ON CONFLICT(role_id) DO UPDATE SET "name" = 'Normal';
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,
gender BOOLEAN 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()
);

View file

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

View file

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

View file

@ -1,5 +1,5 @@
-- Add up migration script here
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
);
);

View file

@ -1,7 +1,8 @@
-- Add up migration script here
CREATE TABLE IF NOT EXISTS "post_interaction"(
interaction_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW(),
post_creation_time TIMESTAMPTZ NOT NULL REFERENCES "post"(creation_time),
user_id BIGSERIAL NOT NULL REFERENCES "user"(user_id),
interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id)
post_id BIGINT NOT NULL REFERENCES "post"(post_id),
user_id BIGINT NOT NULL REFERENCES "user"(user_id),
interaction_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
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
CREATE TABLE IF NOT EXISTS "comment_interaction"(
interaction_time TIMESTAMPTZ PRIMARY KEY NOT NULL UNIQUE DEFAULT NOW(),
comment_creation_time TIMESTAMPTZ NOT NULL REFERENCES "comment"(creation_time),
user_id BIGSERIAL NOT NULL REFERENCES "user"(user_id),
interaction_id BIGSERIAL NOT NULL REFERENCES "interaction"(id)
comment_id BIGINT NOT NULL REFERENCES "comment"(comment_id),
user_id BIGINT NOT NULL REFERENCES "user"(user_id),
interaction_time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
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
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
);
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
CREATE TABLE IF NOT EXISTS "user_contact"(
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,
PRIMARY KEY (user_id, contact_id),
UNIQUE (contact_id, contact_value)

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -5,41 +5,43 @@ use crate::database::comment_interaction;
#[derive(Debug, Serialize, Deserialize)]
pub struct CommentInteraction {
pub interaction_time: DateTime<Utc>,
pub comment_creation_time: DateTime<Utc>,
pub interaction_id: i64,
pub comment_id: i64,
pub user_id: i64,
pub interaction_time: DateTime<Utc>,
pub interaction_id: i64,
}
impl CommentInteraction {
pub async fn create(
comment_creation_time: &DateTime<Utc>,
comment_id: &i64,
user_id: &i64,
interaction_id: &i64,
) -> 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> {
comment_interaction::read(interaction_time).await
pub async fn read(comment_id: &i64, user_id: &i64) -> Result<CommentInteraction, sqlx::Error> {
comment_interaction::read(comment_id, user_id).await
}
pub async fn update(
interaction_time: &DateTime<Utc>,
comment_id: &i64,
user_id: &i64,
interaction_id: &i64,
) -> 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(
interaction_time: &DateTime<Utc>,
comment_id: &i64,
user_id: &i64,
) -> 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(
comment_creation_time: &DateTime<Utc>,
comment_id: &i64,
) -> 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)]
pub struct Contact {
pub id: i64,
pub contact_id: i64,
pub name: String,
}
@ -13,16 +13,16 @@ impl Contact {
contact::create(name).await
}
pub async fn read(id: &i64) -> Result<Contact, sqlx::Error> {
contact::read(id).await
pub async fn read(contact_id: &i64) -> Result<Contact, sqlx::Error> {
contact::read(contact_id).await
}
pub async fn update(id: &i64, name: &String) -> Result<Contact, sqlx::Error> {
contact::update(id, name).await
pub async fn update(contact_id: &i64, name: &String) -> Result<Contact, sqlx::Error> {
contact::update(contact_id, name).await
}
pub async fn delete(id: &i64) -> Result<Contact, sqlx::Error> {
contact::delete(id).await
pub async fn delete(contact_id: &i64) -> Result<Contact, sqlx::Error> {
contact::delete(contact_id).await
}
pub async fn read_all() -> Result<Vec<Contact>, sqlx::Error> {

View file

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

View file

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

View file

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

View file

@ -3,13 +3,6 @@ use serde::{Deserialize, Serialize};
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)]
pub struct User {
pub user_id: i64,

View file

@ -1,3 +1,4 @@
pub mod comment;
pub mod contact;
pub mod interaction;
pub mod login;
@ -19,6 +20,7 @@ pub fn route() -> Router {
.nest("/user_contacts", user_contact::route())
.nest("/interactions", interaction::route())
.nest("/posts", post::route())
.nest("/comments", comment::route())
.route_layer(axum::middleware::from_fn(
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)]
struct UpdateContact {
id: i64,
name: String,
}
pub fn route() -> Router {
Router::new()
.route("/", post(create))
.route("/", patch(update))
.route("/{id}", delete(delete_))
.route("/{contact_id}", patch(update))
.route("/{contact_id}", delete(delete_))
}
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 {
match Contact::update(&update_contact.id, &update_contact.name).await {
async fn update(
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))),
Err(err_val) => (
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 {
match Contact::delete(&id).await {
async fn delete_(Path(contact_id): Path<i64>) -> impl IntoResponse {
match Contact::delete(&contact_id).await {
Ok(contact) => (StatusCode::NO_CONTENT, Json(serde_json::json!(contact))),
Err(err_val) => (
StatusCode::BAD_REQUEST,

View file

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

View file

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

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

View file

@ -1,41 +1,60 @@
use std::sync::Arc;
use axum::{
extract::Path,
http::StatusCode,
response::IntoResponse,
routing::{delete, get, patch, post},
Json, Router,
Extension, Json, Router,
};
use chrono::{DateTime, Utc};
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)]
struct CreateComment {
post_creation_time: DateTime<Utc>,
user_id: i64,
post_id: i64,
comment: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct UpdateComment {
creation_time: DateTime<Utc>,
comment: String,
}
pub fn route() -> Router {
Router::new()
.route("/", post(create))
.route("/{creation_time}", get(read))
.route("/", patch(update))
.route("/{creation_time}", delete(delete_))
.route("/posts/{post_creation_time}", get(read_all_for_post))
.route(
"/",
post(create).route_layer(axum::middleware::from_fn(
by_authorization_token_then_insert,
)),
)
.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(
&create_comment.post_creation_time,
&create_comment.user_id,
&user.user_id,
&create_comment.post_id,
&create_comment.comment,
)
.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 {
match Comment::read(&creation_time).await {
async fn read(Path(comment_id): Path<i64>) -> impl IntoResponse {
match Comment::read(&comment_id).await {
Ok(comment) => (StatusCode::OK, Json(serde_json::json!(comment))),
Err(err_val) => (
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 {
match Comment::update(&update_comment.creation_time, &update_comment.comment).await {
async fn update(
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))),
Err(err_val) => (
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 {
match Comment::delete(&creation_time).await {
async fn delete_(
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))),
Err(err_val) => (
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 {
match Comment::read_all_for_post(&post_creation_time).await {
async fn read_all_for_post(Path(comment_id): Path<i64>) -> impl IntoResponse {
match Comment::read_all_for_post(&comment_id).await {
Ok(comments) => (StatusCode::OK, Json(serde_json::json!(comments))),
Err(err_val) => (
StatusCode::BAD_REQUEST,

View file

@ -1,48 +1,48 @@
use std::sync::Arc;
use axum::{
extract::Path,
http::StatusCode,
response::IntoResponse,
routing::{delete, get, patch, post},
Json, Router,
routing::{delete, get, post},
Extension, Json, Router,
};
use chrono::{DateTime, Utc};
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)]
struct CreateCommentInteraction {
pub comment_creation_time: DateTime<Utc>,
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 {
Router::new()
.route("/", post(create))
.route("/{interaction_time}", get(read))
.route("/", patch(update))
.route("/{interaction_time}", delete(delete_))
.route(
"/comments/{comment_creation_time}",
get(read_all_for_comment),
"/comments/{comment_id}",
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(
Extension(user): Extension<Arc<User>>,
Path(comment_id): Path<i64>,
Json(create_comment_interaction): Json<CreateCommentInteraction>,
) -> impl IntoResponse {
match CommentInteraction::create(
&create_comment_interaction.comment_creation_time,
&create_comment_interaction.user_id,
&comment_id,
&user.user_id,
&create_comment_interaction.interaction_id,
)
.await
@ -58,38 +58,11 @@ async fn create(
}
}
async fn read(Path(interaction_time): Path<DateTime<Utc>>) -> impl IntoResponse {
match CommentInteraction::read(&interaction_time).await {
Ok(comment_interaction) => (StatusCode::OK, Json(serde_json::json!(comment_interaction))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
Json(serde_json::json!(err_val.to_string())),
),
}
}
async fn update(
Json(update_comment_interaction): Json<UpdateCommentInteraction>,
async fn delete_(
Extension(user): Extension<Arc<User>>,
Path(comment_id): Path<i64>,
) -> impl IntoResponse {
match CommentInteraction::update(
&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 {
match CommentInteraction::delete(&comment_id, &user.user_id).await {
Ok(comment_interaction) => (
StatusCode::NO_CONTENT,
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(
Path(comment_creation_time): Path<DateTime<Utc>>,
) -> impl IntoResponse {
match CommentInteraction::read_all_for_comment(&comment_creation_time).await {
async fn read_all_for_comment(Path(comment_id): Path<i64>) -> impl IntoResponse {
match CommentInteraction::read_all_for_comment(&comment_id).await {
Ok(comment_interactions) => (
StatusCode::OK,
Json(serde_json::json!(comment_interactions)),

View file

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

View file

@ -1,43 +1,48 @@
use std::sync::Arc;
use axum::{
extract::Path,
http::StatusCode,
response::IntoResponse,
routing::{delete, get, patch, post},
Json, Router,
routing::{delete, get, post},
Extension, Json, Router,
};
use chrono::{DateTime, Utc};
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)]
struct CreatePostInteraction {
pub post_creation_time: DateTime<Utc>,
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 {
Router::new()
.route("/", post(create))
.route("/{interaction_time}", get(read))
.route("/", patch(update))
.route("/{interaction_time}", delete(delete_))
.route("/posts/{post_creation_time}", get(read_all_for_post))
.route(
"/posts/{post_id}",
post(create).route_layer(axum::middleware::from_fn(
by_authorization_token_then_insert,
)),
)
.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(
&create_post_interaction.post_creation_time,
&create_post_interaction.user_id,
&post_id,
&user.user_id,
&create_post_interaction.interaction_id,
)
.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 {
match PostInteraction::read(&interaction_time).await {
Ok(post_interaction) => (StatusCode::OK, Json(serde_json::json!(post_interaction))),
Err(err_val) => (
StatusCode::BAD_REQUEST,
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 {
async fn delete_(
Extension(user): Extension<Arc<User>>,
Path(post_id): Path<i64>,
) -> impl IntoResponse {
match PostInteraction::delete(&post_id, &user.user_id).await {
Ok(post_interaction) => (
StatusCode::NO_CONTENT,
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 {
match PostInteraction::read_all_for_post(&post_creation_time).await {
async fn read_all_for_post(Path(post_id): Path<i64>) -> impl IntoResponse {
match PostInteraction::read_all_for_post(&post_id).await {
Ok(post_interactions) => (StatusCode::OK, Json(serde_json::json!(post_interactions))),
Err(err_val) => (
StatusCode::BAD_REQUEST,

View file

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