diff --git a/backend/alembic/README b/backend/alembic/README new file mode 100644 index 0000000..98e4f9c --- /dev/null +++ b/backend/alembic/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/backend/alembic/README.md b/backend/alembic/README.md deleted file mode 100644 index 8498672..0000000 --- a/backend/alembic/README.md +++ /dev/null @@ -1,25 +0,0 @@ -# Alembic - -Alembic allows for our database migrations to be tracked in a version control system. - -To create a new migration run: - -```bash -alembic revision --autogenerate -m 'Describe change here' -``` - -It's best practice to review the script post revision creation: `alembic/versions` - -To apply the migration: - -```bash -alembic upgrade head -``` - -Now you can re-check using `alembic check` - -If we need to rollback use: - -```bash -alembic downgrade -i -``` diff --git a/backend/alembic/env.py b/backend/alembic/env.py index 2a48bbb..157c456 100644 --- a/backend/alembic/env.py +++ b/backend/alembic/env.py @@ -1,10 +1,12 @@ from logging.config import fileConfig -from app.database import Base + from sqlalchemy import engine_from_config from sqlalchemy import pool from alembic import context +from app.database import Base + # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config @@ -16,6 +18,7 @@ if config.config_file_name is not None: # add your model's MetaData object here # for 'autogenerate' support +# from myapp import mymodel target_metadata = Base.metadata # other values from the config, defined by the needs of env.py, diff --git a/backend/alembic/versions/1331953dbdf0_.py b/backend/alembic/versions/1331953dbdf0_.py deleted file mode 100644 index 54a8001..0000000 --- a/backend/alembic/versions/1331953dbdf0_.py +++ /dev/null @@ -1,46 +0,0 @@ -"""empty message - -Revision ID: 1331953dbdf0 -Revises: f6c8aa750e08 -Create Date: 2025-06-22 14:23:39.931696 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -# revision identifiers, used by Alembic. -revision: str = '1331953dbdf0' -down_revision: Union[str, None] = 'f6c8aa750e08' -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - """Upgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_users_email'), table_name='users') - op.drop_index(op.f('ix_users_id'), table_name='users') - op.drop_index(op.f('ix_users_username'), table_name='users') - op.drop_table('users') - # ### end Alembic commands ### - - -def downgrade() -> None: - """Downgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('users', - sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False), - sa.Column('username', sa.VARCHAR(), autoincrement=False, nullable=False), - sa.Column('email', sa.VARCHAR(), autoincrement=False, nullable=False), - sa.Column('hashed_password', sa.VARCHAR(), autoincrement=False, nullable=False), - sa.Column('permissions', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=False), - sa.Column('subscriber', sa.BOOLEAN(), autoincrement=False, nullable=False), - sa.PrimaryKeyConstraint('id', name=op.f('users_pkey')) - ) - op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True) - op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False) - op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True) - # ### end Alembic commands ### diff --git a/backend/alembic/versions/273690fd257d_.py b/backend/alembic/versions/273690fd257d_.py deleted file mode 100644 index 4363820..0000000 --- a/backend/alembic/versions/273690fd257d_.py +++ /dev/null @@ -1,32 +0,0 @@ -"""empty message - -Revision ID: 273690fd257d -Revises: aaeb70eb4cdb -Create Date: 2025-06-21 09:08:04.102896 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision: str = '273690fd257d' -down_revision: Union[str, None] = 'aaeb70eb4cdb' -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - """Upgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - pass - # ### end Alembic commands ### - - -def downgrade() -> None: - """Downgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - pass - # ### end Alembic commands ### diff --git a/backend/alembic/versions/3b78fe6cf60f_.py b/backend/alembic/versions/3b78fe6cf60f_.py deleted file mode 100644 index 9ea0420..0000000 --- a/backend/alembic/versions/3b78fe6cf60f_.py +++ /dev/null @@ -1,32 +0,0 @@ -"""empty message - -Revision ID: 3b78fe6cf60f -Revises: 1331953dbdf0 -Create Date: 2025-06-22 14:24:52.883175 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision: str = '3b78fe6cf60f' -down_revision: Union[str, None] = '1331953dbdf0' -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - """Upgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - pass - # ### end Alembic commands ### - - -def downgrade() -> None: - """Downgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - pass - # ### end Alembic commands ### diff --git a/backend/alembic/versions/4a1e42eba1cf_.py b/backend/alembic/versions/4a1e42eba1cf_.py deleted file mode 100644 index 9a3946d..0000000 --- a/backend/alembic/versions/4a1e42eba1cf_.py +++ /dev/null @@ -1,58 +0,0 @@ -"""empty message - -Revision ID: 4a1e42eba1cf -Revises: b9dcd098debd -Create Date: 2025-06-21 08:55:29.395985 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -# revision identifiers, used by Alembic. -revision: str = '4a1e42eba1cf' -down_revision: Union[str, None] = 'b9dcd098debd' -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - """Upgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_users_email'), table_name='users') - op.drop_index(op.f('ix_users_id'), table_name='users') - op.drop_index(op.f('ix_users_username'), table_name='users') - op.drop_table('users') - op.drop_index(op.f('ix_items_id'), table_name='items') - op.drop_index(op.f('ix_items_name'), table_name='items') - op.drop_table('items') - # ### end Alembic commands ### - - -def downgrade() -> None: - """Downgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('items', - sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False), - sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=True), - sa.Column('description', sa.VARCHAR(), autoincrement=False, nullable=True), - sa.Column('body', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=False), - sa.PrimaryKeyConstraint('id', name=op.f('items_pkey')) - ) - op.create_index(op.f('ix_items_name'), 'items', ['name'], unique=False) - op.create_index(op.f('ix_items_id'), 'items', ['id'], unique=False) - op.create_table('users', - sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False), - sa.Column('username', sa.VARCHAR(), autoincrement=False, nullable=False), - sa.Column('email', sa.VARCHAR(), autoincrement=False, nullable=False), - sa.Column('hashed_password', sa.VARCHAR(), autoincrement=False, nullable=False), - sa.Column('permissions', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=False), - sa.Column('subscriber', sa.BOOLEAN(), autoincrement=False, nullable=False), - sa.PrimaryKeyConstraint('id', name=op.f('users_pkey')) - ) - op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True) - op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False) - op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True) - # ### end Alembic commands ### diff --git a/backend/alembic/versions/a3ac646e53a8_init.py b/backend/alembic/versions/a3ac646e53a8_init.py deleted file mode 100644 index 10a722d..0000000 --- a/backend/alembic/versions/a3ac646e53a8_init.py +++ /dev/null @@ -1,41 +0,0 @@ -"""init - -Revision ID: a3ac646e53a8 -Revises: -Create Date: 2025-06-04 21:36:22.283823 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision: str = 'a3ac646e53a8' -down_revision: Union[str, None] = None -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - """Upgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_items_id'), table_name='items') - op.drop_index(op.f('ix_items_name'), table_name='items') - op.drop_table('items') - # ### end Alembic commands ### - - -def downgrade() -> None: - """Downgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('items', - sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False), - sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=True), - sa.Column('description', sa.VARCHAR(), autoincrement=False, nullable=True), - sa.PrimaryKeyConstraint('id', name=op.f('items_pkey')) - ) - op.create_index(op.f('ix_items_name'), 'items', ['name'], unique=False) - op.create_index(op.f('ix_items_id'), 'items', ['id'], unique=False) - # ### end Alembic commands ### diff --git a/backend/alembic/versions/aaeb70eb4cdb_.py b/backend/alembic/versions/aaeb70eb4cdb_.py deleted file mode 100644 index b210a5c..0000000 --- a/backend/alembic/versions/aaeb70eb4cdb_.py +++ /dev/null @@ -1,32 +0,0 @@ -"""empty message - -Revision ID: aaeb70eb4cdb -Revises: d4894b788937 -Create Date: 2025-06-21 09:06:10.983927 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision: str = 'aaeb70eb4cdb' -down_revision: Union[str, None] = 'd4894b788937' -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - """Upgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - pass - # ### end Alembic commands ### - - -def downgrade() -> None: - """Downgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - pass - # ### end Alembic commands ### diff --git a/backend/alembic/versions/b9dcd098debd_1_1_0.py b/backend/alembic/versions/b9dcd098debd_1_1_0.py deleted file mode 100644 index e42395e..0000000 --- a/backend/alembic/versions/b9dcd098debd_1_1_0.py +++ /dev/null @@ -1,58 +0,0 @@ -"""1.1.0 - -Revision ID: b9dcd098debd -Revises: a3ac646e53a8 -Create Date: 2025-06-14 09:22:14.878105 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -# revision identifiers, used by Alembic. -revision: str = 'b9dcd098debd' -down_revision: Union[str, None] = 'a3ac646e53a8' -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - """Upgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_users_email'), table_name='users') - op.drop_index(op.f('ix_users_id'), table_name='users') - op.drop_index(op.f('ix_users_username'), table_name='users') - op.drop_table('users') - op.drop_index(op.f('ix_items_id'), table_name='items') - op.drop_index(op.f('ix_items_name'), table_name='items') - op.drop_table('items') - # ### end Alembic commands ### - - -def downgrade() -> None: - """Downgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('items', - sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False), - sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=True), - sa.Column('description', sa.VARCHAR(), autoincrement=False, nullable=True), - sa.Column('body', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=False), - sa.PrimaryKeyConstraint('id', name=op.f('items_pkey')) - ) - op.create_index(op.f('ix_items_name'), 'items', ['name'], unique=False) - op.create_index(op.f('ix_items_id'), 'items', ['id'], unique=False) - op.create_table('users', - sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False), - sa.Column('username', sa.VARCHAR(), autoincrement=False, nullable=False), - sa.Column('email', sa.VARCHAR(), autoincrement=False, nullable=False), - sa.Column('hashed_password', sa.VARCHAR(), autoincrement=False, nullable=False), - sa.Column('permissions', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=False), - sa.Column('subscriber', sa.BOOLEAN(), autoincrement=False, nullable=False), - sa.PrimaryKeyConstraint('id', name=op.f('users_pkey')) - ) - op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True) - op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False) - op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True) - # ### end Alembic commands ### diff --git a/backend/alembic/versions/c9b28e38d00c_add_author_name_to_blogs.py b/backend/alembic/versions/c9b28e38d00c_add_author_name_to_blogs.py new file mode 100644 index 0000000..5c29427 --- /dev/null +++ b/backend/alembic/versions/c9b28e38d00c_add_author_name_to_blogs.py @@ -0,0 +1,71 @@ +"""Add author_name to blogs + +Revision ID: c9b28e38d00c +Revises: +Create Date: 2025-06-24 20:51:56.034469 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision: str = 'c9b28e38d00c' +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + """Upgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_users_email'), table_name='users') + op.drop_index(op.f('ix_users_id'), table_name='users') + op.drop_index(op.f('ix_users_username'), table_name='users') + op.drop_table('users') + op.drop_index(op.f('ix_blogs_author_id'), table_name='blogs') + op.drop_index(op.f('ix_blogs_id'), table_name='blogs') + op.drop_index(op.f('ix_blogs_title'), table_name='blogs') + op.drop_table('blogs') + # ### end Alembic commands ### + + +def downgrade() -> None: + """Downgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('blogs', + sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False), + sa.Column('title', sa.VARCHAR(), autoincrement=False, nullable=True), + sa.Column('author_id', sa.INTEGER(), autoincrement=False, nullable=False), + sa.Column('description', sa.VARCHAR(), autoincrement=False, nullable=True), + sa.Column('body', sa.VARCHAR(), autoincrement=False, nullable=False), + sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=False), + sa.Column('updated_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True), + sa.Column('published_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True), + sa.Column('word_count', sa.INTEGER(), autoincrement=False, nullable=True), + sa.Column('version', sa.INTEGER(), autoincrement=False, nullable=True), + sa.Column('read_time', sa.INTEGER(), autoincrement=False, nullable=True), + sa.Column('language', sa.VARCHAR(), autoincrement=False, nullable=True), + sa.Column('tags', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=True), + sa.Column('view_count', sa.INTEGER(), autoincrement=False, nullable=False), + sa.Column('like_count', sa.INTEGER(), autoincrement=False, nullable=False), + sa.PrimaryKeyConstraint('id', name=op.f('blogs_pkey')) + ) + op.create_index(op.f('ix_blogs_title'), 'blogs', ['title'], unique=False) + op.create_index(op.f('ix_blogs_id'), 'blogs', ['id'], unique=False) + op.create_index(op.f('ix_blogs_author_id'), 'blogs', ['author_id'], unique=False) + op.create_table('users', + sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False), + sa.Column('username', sa.VARCHAR(), autoincrement=False, nullable=False), + sa.Column('email', sa.VARCHAR(), autoincrement=False, nullable=False), + sa.Column('hashed_password', sa.VARCHAR(), autoincrement=False, nullable=False), + sa.Column('permissions', postgresql.JSON(astext_type=sa.Text()), autoincrement=False, nullable=False), + sa.Column('subscriber', sa.BOOLEAN(), autoincrement=False, nullable=False), + sa.PrimaryKeyConstraint('id', name=op.f('users_pkey')) + ) + op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True) + op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False) + op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True) + # ### end Alembic commands ### diff --git a/backend/alembic/versions/d4894b788937_.py b/backend/alembic/versions/d4894b788937_.py deleted file mode 100644 index 44a68db..0000000 --- a/backend/alembic/versions/d4894b788937_.py +++ /dev/null @@ -1,28 +0,0 @@ -"""empty message - -Revision ID: d4894b788937 -Revises: 4a1e42eba1cf -Create Date: 2025-06-21 08:56:04.467650 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision: str = 'd4894b788937' -down_revision: Union[str, None] = '4a1e42eba1cf' -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - """Upgrade schema.""" - pass - - -def downgrade() -> None: - """Downgrade schema.""" - pass diff --git a/backend/alembic/versions/f6c8aa750e08_.py b/backend/alembic/versions/f6c8aa750e08_.py deleted file mode 100644 index 7d6388f..0000000 --- a/backend/alembic/versions/f6c8aa750e08_.py +++ /dev/null @@ -1,104 +0,0 @@ -"""empty message - -Revision ID: f6c8aa750e08 -Revises: 273690fd257d -Create Date: 2025-06-22 14:14:03.560792 - -""" - -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -# revision identifiers, used by Alembic. -revision: str = "f6c8aa750e08" -down_revision: Union[str, None] = "273690fd257d" -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - """Upgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f("ix_blogs_author_id"), table_name="blogs") - op.drop_index(op.f("ix_blogs_id"), table_name="blogs") - op.drop_index(op.f("ix_blogs_title"), table_name="blogs") - op.drop_table("blogs") - # ### end Alembic commands ### - - -def downgrade() -> None: - """Downgrade schema.""" - # ### commands auto generated by Alembic - please adjust! ### - op.create_table( - "blogs", - sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False), - sa.Column("title", sa.VARCHAR(), autoincrement=False, nullable=True), - sa.Column("author_id", sa.INTEGER(), autoincrement=False, nullable=True), - sa.Column("description", sa.VARCHAR(), autoincrement=False, nullable=True), - sa.Column( - "body", - postgresql.JSON(astext_type=sa.Text()), - autoincrement=False, - nullable=False, - ), - sa.Column( - "created_at", - postgresql.TIMESTAMP(timezone=True), - server_default=sa.text("now()"), - autoincrement=False, - nullable=False, - ), - sa.Column( - "updated_at", - postgresql.TIMESTAMP(timezone=True), - autoincrement=False, - nullable=True, - ), - sa.Column( - "published_at", - postgresql.TIMESTAMP(timezone=True), - autoincrement=False, - nullable=True, - ), - sa.Column("word_count", sa.INTEGER(), autoincrement=False, nullable=True), - sa.Column("version", sa.INTEGER(), autoincrement=False, nullable=True), - sa.Column("read_time", sa.INTEGER(), autoincrement=False, nullable=True), - sa.Column("language", sa.VARCHAR(), autoincrement=False, nullable=True), - sa.Column( - "tags", - postgresql.JSON(astext_type=sa.Text()), - autoincrement=False, - nullable=True, - ), - sa.Column("view_count", sa.INTEGER(), autoincrement=False, nullable=False), - sa.Column("like_count", sa.INTEGER(), autoincrement=False, nullable=False), - sa.ForeignKeyConstraint( - ["author_id"], ["users.id"], name=op.f("blogs_author_id_fkey") - ), - sa.PrimaryKeyConstraint("id", name=op.f("blogs_pkey")), - ) - op.create_index(op.f("ix_blogs_title"), "blogs", ["title"], unique=False) - op.create_index(op.f("ix_blogs_id"), "blogs", ["id"], unique=False) - op.create_index(op.f("ix_blogs_author_id"), "blogs", ["author_id"], unique=False) - op.create_table( - "users", - sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False), - sa.Column("username", sa.VARCHAR(), autoincrement=False, nullable=False), - sa.Column("email", sa.VARCHAR(), autoincrement=False, nullable=False), - sa.Column("hashed_password", sa.VARCHAR(), autoincrement=False, nullable=False), - sa.Column( - "permissions", - postgresql.JSON(astext_type=sa.Text()), - autoincrement=False, - nullable=False, - ), - sa.Column("subscriber", sa.BOOLEAN(), autoincrement=False, nullable=False), - sa.PrimaryKeyConstraint("id", name=op.f("users_pkey")), - ) - op.create_index(op.f("ix_users_username"), "users", ["username"], unique=True) - op.create_index(op.f("ix_users_id"), "users", ["id"], unique=False) - op.create_index(op.f("ix_users_email"), "users", ["email"], unique=True) - # ### end Alembic commands ### diff --git a/backend/app/models.py b/backend/app/models.py index d31ad7d..8ae55b0 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -16,6 +16,7 @@ class Blog(Base): id = Column(Integer, primary_key=True, index=True) title = Column(String, index=True) author_id = Column(Integer, nullable=False, index=True) + author_name = Column(String, nullable=False, index=True) description = Column(String, nullable=True) body = Column(String, nullable=False) created_at = Column( diff --git a/backend/app/schemas.py b/backend/app/schemas.py index 0bcbb8f..4ae8c38 100644 --- a/backend/app/schemas.py +++ b/backend/app/schemas.py @@ -5,6 +5,7 @@ from pydantic import AwareDatetime, BaseModel, EmailStr class BlogBase(BaseModel): title: str author_id: int + author_name: str description: Optional[str] = None body: str created_at: AwareDatetime @@ -30,6 +31,7 @@ class BlogUpdate(BaseModel): title: Optional[str] = None description: Optional[str] = None + author_name: Optional[str] = None body: Optional[str] = None created_at: Optional[str] = None updated_at: Optional[str] = None diff --git a/frontend/Dockerfile b/frontend/Dockerfile index edc8ccf..4321151 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -1,18 +1,14 @@ # Build stage FROM node:20 AS build - WORKDIR /app - -COPY package.json package-lock.json ./ +COPY package*.json ./ RUN npm install - COPY . . - RUN npm run build # Production stage FROM nginx:alpine - +COPY nginx.conf /etc/nginx/nginx.conf COPY --from=build /app/dist /usr/share/nginx/html EXPOSE 80 diff --git a/frontend/nginx.conf b/frontend/nginx.conf new file mode 100644 index 0000000..3f8a88c --- /dev/null +++ b/frontend/nginx.conf @@ -0,0 +1,28 @@ +worker_processes 1; + +events { worker_connections 1024; } + +http { + include mime.types; + default_type application/octet-stream; + sendfile on; + keepalive_timeout 65; + + server { + listen 80; + server_name _; + + root /usr/share/nginx/html; + index index.html; + + location / { + # try to serve file directly, otherwise fallback to index.html + try_files $uri $uri/ /index.html; + } + + # optional: block .git, .env, etc + location ~ /\.(?!well-known).* { + deny all; + } + } +} diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 929020b..10af657 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -16,6 +16,7 @@ import { useForm } from "react-hook-form"; import { BlogViewer } from "./utils/BlogViewer"; import { RequireAdmin } from "./utils/RouteGuard"; import { AdminPage } from "./utils/AdminPage"; +import { EditBlog } from "./utils/EditBlog"; import Unauthorized from "./utils/UnauthorizedPage"; import { FaGithub, @@ -63,6 +64,7 @@ const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ const logout = () => { localStorage.removeItem("token"); + localStorage.removeItem("user_id"); setIsAuthenticated(false); navigate("/signin"); }; @@ -106,7 +108,10 @@ function BlogPage() { className="block p-4 border rounded hover:bg-gray-100 dark:hover:bg-gray-800" >
By {blog.author_id}
++ {blog.description} +
+By {blog.author_name}
))} @@ -117,10 +122,9 @@ function BlogPage() { // Page for creating a blog post function CreateBlog() { const [fileName, setFileName] = useStateLoading...
; - } + if (!blog) returnLoading…
; + + const isAuthor = me === String(blog.author_id); + + const handleDelete = async () => { + if (!window.confirm(`Are you sure you want to delete ${blog.title}`)) + return; + + try { + const res = await fetch(`${API_URL}/blogs/${slug}`, { + method: "DELETE", + headers: { Accept: "application/json" }, + }); + if (!res.ok) throw new Error(await res.text()); + navigate("/"); + } catch (err) { + console.error("Delete failed:", err); + alert("Could not delete post. See console for details."); + } + }; return (By User {blog.author_id}
-- {blog.description} -
+By {blog.author_name}
Loading…
; + + async function handleSubmit(e: FormEvent) { + e.preventDefault(); + const updatedBlog = { + title, + description, + body, + author_name: authorName, // from the form field + updated_at: new Date().toISOString(), + word_count: countWords(body), + version: blog ? blog.version + 1 : 1, + }; + + const res = await fetch(`${API_URL}/blogs/${slug}`, { + method: "PUT", // or "PATCH" if your API prefers + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(updatedBlog), + }); + if (res.ok) { + navigate(`/blogs/${slug}`); // go back to the viewer + } else { + console.error("Update failed:", await res.text()); + alert("Could not update the post. See console for details."); + } + } + + return ( +