finneh4249 / T2A2-API-Application

0 stars 0 forks source link

Ethan Cornwill T2A2 API Application

SocialAPI

Problem and Solution

Problem: The difficulty for developers to create social media features within their applications.

Solution: My API provides a comprehensive set of endpoints and functionalities to enable developers to easily integrate social media features into their applications. This includes user profiles, post creation and management, commenting and liking, following and unfollowing, and messaging.

Task Allocation and Tracking

Project Planning

This project utilised GitHub Projects to manage the project. Between September 8th and 15th, the project was created, and initial planning was completed.

While working on the project I realised that some features that I was initially planning to create would be far too complex and outside of the scope of the project, as such, I decided instead to focus more on other parts of the project to enhance their functionality.

timeline main_board

Milestones

milestone1 milstones

Milestone 1: Planning

issue_2

Milestone 2: User Management

issue_3

Milestone 3: Post Management

issue_4 issue_12

Milestone 4: Comments and Notifications

issue_5 issue_6

Milestone 5: Following and Friends

issue_7

Milestone 6: Search and Analytics (Not Implemented)

issue_8 issue_9

Milestone 7: Testing and Deployment (Deployment other than Local Not Implemented)

issue_10 issue_10_2 issue_11 issue_13

Third-Party Services, Packages, and Dependencies

Database System Benefits and Drawbacks

Benefits of PostgreSQL:

Drawbacks of PostgreSQL:

ORM Features and Functionalities

SQLAlchemy:

Entity Relationship Diagram (ERD)

Intial ERD

ERD

The ERD illustrates the draft of the relationships between entities in the database. During the development process, the ERD was modified to reflect changes in the entities, and relationships.

Final ERD

ERD

Implemented Models and Relationships

Models:

Relationships:

How Relationships Aid Implementation:

API Endpoints

Users

Authentication


- **Example Response:**

```json
{
  "message": "Email confirmed successfully"
}
curl -X http://localhost:5000/auth/reset-password/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTcyNzQxNTEzOCwianRpIjoiN2I4YzdhYjgtYjNiZS00YzMxLWJjZjQtODdkZTJjNzBkZjVmIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6NSwibmJmIjoxNzI3NDE1MTM4LCJjc3JmIjoiMTI4ZDMyYzItNDc5Ni00MTYxLTk2ZGEtODAzYmZkZmMxMjQzIiwiZXhwIjoxNzI3NDE2MDM4fQ.RDPX_RIAJ7MoM3F103Q8cYS_8v_6SNMbaw9c3GI_9yY
{
  "message": "Password reset successful",
  "user": {
    "id": 1,
    "username": "john_doe",
    "email": "john@example.com",
    "bio": "I like trainZ",
    "is_admin": false,
    "is_confirmed": true,
    "created_at": "2022-02-22T20:30:00.000000"
  }
}

Posts

Feed

curl http://localhost:5000/feed -H "Authorization: Bearer <your_token>"
[
  {
    "id": 3,
    "title": "Superman Test Post",
    "content": "This is a big good juju test post on the ethanc account!\nI like trains!",
    "likes_count": 0,
    "comments_count": 0,
    "created_at": "2024-09-24T18:29:50.058701",
    "updated_at": "2024-09-24T18:29:58.624499",
    "author": {
      "id": 4,
      "username": "ethanc"
    },
    "likes": [],
    "comments": []
  },
  {
    "id": 2,
    "title": "Another Post",
    "content": "This is my second post. Created by the User",
    "likes_count": 1,
    "comments_count": 1,
    "created_at": "2024-09-24T05:00:18.568652",
    "updated_at": null,
    "author": {
      "id": 2,
      "username": "user"
    },
    "likes": [
      {
        "id": 1,
        "user_id": 1
      }
    ],
    "comments": [
      {
        "id": 1,
        "user_id": 1,
        "content": "This is a comment on the second post.",
        "created_at": "2024-09-24T05:00:18.569052",
        "updated_at": null
      }
    ]
  },
  {
    "id": 1,
    "title": "Hello, World!",
    "content": "This is my first post. Created by the Admin!",
    "likes_count": 2,
    "comments_count": 2,
    "created_at": "2024-09-24T05:00:18.568466",
    "updated_at": null,
    "author": {
      "id": 1,
      "username": "admin"
    },
    "likes": [
      {
        "id": 2,
        "user_id": 2
      },
      {
        "id": 3,
        "user_id": 4
      }
    ],
    "comments": [
      {
        "id": 2,
        "user_id": 2,
        "content": "This is a comment on the first post.",
        "created_at": "2024-09-24T05:00:18.569164",
        "updated_at": null
      },
      {
        "id": 4,
        "user_id": 4,
        "content": "This is a big good juju test comment on the first post!",
        "created_at": "2024-09-24T18:21:22.664342",
        "updated_at": null
      }
    ]
  }
]
curl http://localhost:5000/feed/following -H "Authorization: Bearer <your_token>"
[
  {
    "id": 3,
    "title": "Superman Test Post",
    "content": "This is a big good juju test post on the ethanc account!\nI like trains!",
    "likes_count": 0,
    "comments_count": 0,
    "created_at": "2024-09-24T18:29:50.058701",
    "updated_at": "2024-09-24T18:29:58.624499",
    "author": {
      "id": 4,
      "username": "ethanc"
    },
    "likes": [],
    "comments": []
  },
  {
    "id": 2,
    "title": "Another Post",
    "content": "This is my second post. Created by the User",
    "likes_count": 1,
    "comments_count": 1,
    "created_at": "2024-09-24T05:00:18.568652",
    "updated_at": null,
    "author": {
      "id": 2,
      "username": "user"
    },
    "likes": [
      {
        "id": 1,
        "user_id": 1
      }
    ],
    "comments": [
      {
        "id": 1,
        "user_id": 1,
        "content": "This is a comment on the second post.",
        "created_at": "2024-09-24T05:00:18.569052",
        "updated_at": null
      }
    ]
  },
  {
    "id": 1,
    "title": "Hello, World!",
    "content": "This is my first post. Created by the Admin!",
    "likes_count": 2,
    "comments_count": 2,
    "created_at": "2024-09-24T05:00:18.568466",
    "updated_at": null,
    "author": {
      "id": 1,
      "username": "admin"
    },
    "likes": [
      {
        "id": 2,
        "user_id": 2
      },
      {
        "id": 3,
        "user_id": 4
      }
    ],
    "comments": [
      {
        "id": 2,
        "user_id": 2,
        "content": "This is a comment on the first post.",
        "created_at": "2024-09-24T05:00:18.569164",
        "updated_at": null
      },
      {
        "id": 4,
        "user_id": 4,
        "content": "This is a big good juju test comment on the first post!",
        "created_at": "2024-09-24T18:21:22.664342",
        "updated_at": null
      }
    ]
  }
]

Comments

Likes

{
  "message": "Post unliked successfully"
}

Follows

Remember to replace http://localhost:5000 with the actual URL of your API.

Prerequisites

Installation

  1. Install PostgreSQL:

    • Windows: Download and install PostgreSQL from https://www.postgresql.org/download/. Follow the installation instructions.
    • macOS: Use Homebrew to install PostgreSQL:
      brew install postgresql
    • Linux: Consult your distribution's package manager for installation instructions.
  2. Create a PostgreSQL user and database:

    • Open the PostgreSQL command-line interface (psql).
    • Create a new user:
      CREATE USER your_username WITH PASSWORD 'your_password';
    • Create a new database:
      CREATE DATABASE social_media_api;
    • Grant privileges to the user:
      GRANT ALL PRIVILEGES ON DATABASE social_media_api TO your_username;
    • Connect to the database:
       \connect social_media_api
    • Grant privileges to the user with the public schema:
    GRANT USAGE ON SCHEMA public TO your_username;
  3. Clone the repository:

    git clone https://github.com/finneh4249/t2a2-api-application.git
  4. Create a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\bin\activate.ps1
  5. Install dependencies:

    pip install -r requirements.txt
  6. Configure database settings: Update the .env.example file with your database connection details, including the username, password, database name, and host. Rename the .env.example file to .env.
  7. Create the database: In order to create the database, use the following commands:
  flask cli db_create

This command will create all the tables in the database, and seed it with default values.

  1. Run the application:
flask run
  1. Access API endpoints: Use your preferred HTTP client (e.g., Postman, curl, Insomnia, etc) to interact with the API endpoints.

Additional Information

Error Handling

The API implements robust error handling to provide informative feedback to clients in case of exceptions or unexpected situations. When an error occurs, the API returns a JSON response with a descriptive error message and an appropriate HTTP status code.

@user_controller.route('/users/<user_id>')
def get_user(user_id):
  user = User.query.get(user_id)
  if user is None:
    return {"message": "User not found"}, 404

In this example, the code attempts to retrieve the user with the specified user_id. If the user is not found, a 404 Not Found error is raised. Other errors that may commonly occur during the execution of the code are handled in the same way, for example, a 401 Unauthorized error is returned if the user is not the owner of the requested resource.

With the exception of a Marshamallow Validation Error, if any other exception occurs, a generic 500 Internal Server Error is returned with an error message.

CLI Commands

The CLI commands are provided for convenience. You can use them to create or drop the database, and create a new user, or admin user.

flask cli db_create # Creates all tables in the database.
flask cli db_drop # Drops all tables in the database.
flask cli create_user <username> <email> <password> <bio> [--admin] # Creates a user, use the --admin flag to create an admin user.
flask cli delete_user <username> # Deletes the selected user from the database.