Git Product home page Git Product logo

blog-go's Introduction

Go Blog

Built in Go 1.20

The app uses:

Getting started

  1. Clone the repository
  2. Go to the project's root directory
  3. Rename app.env.sample to app.env and replace the values
  4. Run in your terminal:
    • docker-compose up --build to run the containers
  5. Now everything should be ready and server running on SERVER_ADDRESS specified in app.env

Testing

  1. Run the postgres container (docker-compose up)
  2. Run in your terminal:
    • make test to run all tests or
    • make test_coverage p={PATH} - to get the coverage in the HTML format - where {PATH} is the path to the target directory for which you want to generate test coverage. The {PATH} should be replaced with the actual path you want to use. For example ./api or
    • use standard go test commands (e.g. go test -v ./api)

API endpoints

Users

  • /users - handles POST requests to create users.
  • /users/login - handles POST requests to log in users.

Tokens/Session

  • /tokens/renew - handles POST requests to renew the access tokens.

Category

  • /category - handles POST requests to create categories
  • /category/{name} - handles DELETE requests to delete a category

Posts

  • /posts - handles POST requests to create posts.
  • /posts/{id} - handles DELETE requests to delete a post.
  • /posts/id/{id} and /posts/title/{slug} - handles GET requests to get post details.
  • /posts/all - handles GET requests to list all posts. Query params: page, page_size.
  • /posts/author - handles GET requests to list posts created by author with given name (that username or email contain given string). Query params: page, page_size, author.
  • /posts/category - handles GET requests to list posts from the given category. Query params: page, page_size, category_id.
  • /posts/tags - handles GET requests to list posts with given tags. Query params: page, page_size, tag_ids where tag_ids is comma-separated int format (e.g. &tag_ids=1,2,3).
  • /posts/{id} - handles PATCH requests to update the post.

Comments

  • /comments - handles POST requests to create a comment.
  • /comments/{id} - handles DELETE requests to delete a comment.
  • /comments/{id} - handles PATCH requests to update a comment.
  • /comments/{post_id} - handles GET requests to list comments of a post. Query params: page and page_size.

Documentation

API

The API (HTTP gateway) documentation can be found at this swaggerhub page and (after running the server) at http://localhost:8080/docs/

Database

The database's schema and intricate details can be found on dedicated webpage, which provides a comprehensive overview of the data structure, tables, relationships, and other essential information. To explore the database further, please visit this dbdocs.io webpage. Password: bloggopassword

blog-go's People

Contributors

aalug avatar

Stargazers

 avatar

Watchers

 avatar  avatar

blog-go's Issues

Add validation for create and login user

Add validation for create and login user requests.
Add validation for every field in the request.
This will improve the way the request errors are displayed.

Create `create user` API

Create create user API.
It should handle POST requests and create users.
Without hashing passwords for now.

Implement create and login user in gRPC

Implement create and login user functions in gRPC.
Most of the functions will be the same as it was in the gin app.
Main difference are errors and data validation.

Add list posts by author API

Add list posts by author API.
Create a function to handle GET requests and return posts created by an author with the given name (that username or email contains a given string).
It should accept three query parameters - page, page_size, and author.
Test the function.

Add create post API

Add create post API.
It should:

  • validate the date
  • get or create the category with the name that was sent in the request
  • get the authenticated user (to set as an author)
  • create a new post
  • add tags (sent in the request) to the created post
  • send the formatted response with all post details, tags, category name, and author name

Add list all posts API

Add a list all posts API.
Create a function to handle GET requests and return posts.
It should accept two query parameters - page and page_size.
Test the function.

Add update post API

Add update post API.
The function should handle PATCH requests.
It should get the id of the post from the URI, and new post details from json.
Only the author of the post should be allowed to edit it.

Add get post by title API

Add get post by title API.
It should handle the /posts/title/{slug} endpoint and return post details - post object with tag list and author and category as names, not ids.
Change url of get post by id to /posts/id/{id} so it does not conflick with this endpoint.

Create CRUD for the post_tags table

Create functions to perform CRUD operations for the post_tags table.
There should only be functions to:

  • add existing tag to the post
  • remove the tag from the post

To do it - use sqlc
Test created functions.

Extract metadata from context

Extract metadata from context.
The metadata needed right now is:

  • user agent
  • client IP

Both of those are required to create a session in the database.

Add refresh tokens

Add handling of refresh tokens.
Create migrations with new sessions table.
Add a function to handle POST requests to renew access tokens.
Test the function.

Add delete category API

Add delete category API. It should handle DELETE request to the /category/:name endpoint.
Test the API.

Add delete comment API

Add delete comment API.
Add a function to handle DELETE requests to delete a comment.
Only the author of the comment should be allowed to delete it.
Test the function.

Implement update user functions in gRPC

Implement update user functions in gRPC.
Create an SQL query code and generate the UpdateUser function with sqlc.
Using this newly created function, implement functionality to update users through gRPC and HTTP.

Add the wait for db command

Add the wait for db command to fix the problem with the app container executing migration commands before the Postgres container is up.

Add update comment API

Add update comment API.
Add a function to handle PATCH requests to update a comment.
Only the author of the comment should be allowed to update it.
Test the function.

Add list categories API

Add list categories API. It should handle GET requests to the /category endpoint.
It should accept query params:

  • page (page number), based on which it will call the database function with Offset: (request.Page - 1) * request.PageSize,
  • page_size - Limit: request.PageSize

Test the API.

Create auth middleware

Create auth middleware that will check/validate the authorization header and token.
Add the create category route to protected routes.
Test the middleware.

Add HTTP gateway

Add HTTP gateway.
Use grpc-gateway.
For now, there will be only two endpoints working with this.
The rest will be implemented later.

Implement removing multiple tags from a post

Implement removing multiple tags from a post.
Create SQL queries / generate with sqlc go functions to be able to pass as an argument to the remove tags from post function a slice of strings - tag names, and int - a post id.
This should delete all post_tags tables (responsible for many-to-many relationships) and check are each tag used by another post. If the tag is unused by any post (it has no connection to any post_tags table) it should be deleted.
Test the function.

Create CRUD for users

Create functions to perform CRUD operations for the users table.
To do it - use sqlc
Test created functions.

Add delete post API

Add delete post API.
It should handle deleting posts but only by post authors.
Test the API.

Implement adding multiple tags to a post

Implement adding multiple tags to a post.
Create SQL queries / generate with sqlc go functions to be able to pass as an argument to the add tags to post function a slice of strings - tag names, and int - a post id.
This should get (if exist) or create tags based on given names and return tag ids.
After this function should create post_tags tables to connect all tags to the post.

Add create comment API

Add create comment API.
It should handle POST requests and create/add a comment to an existing post.
As a post author, the authenticated user should be set.
Test the function.

Implement hashing passwords

Implement hashing passwords with bcrypt.
Create functions to:

  • hash password
  • check password (compare string passed as a password in a POST request to the hashed value in the database)

Add get post by id API

Add get post by id API.
It should handle the /posts/{id} endpoint and return post details - post object with tag list and author and category as names, not ids

Test update post API

Test update post API.
It was not tested before because of the issue with the library.
Take another approach to test the function.

Add list posts by tags API

Add list posts by tags API.
Create a function to handle GET requests and return posts with given tags.
It should accept three query parameters - page, page_size, and tag_ids.
tag_ids should be in a comma-separated int format (1,2,3)
Test the function.

Create init migrations

Create migrations init_schema for the project.
It should contain the following tables:

  • users
  • categories
  • posts
  • tags
  • comments
    Additionally, the many-to-many relationship between posts and tags will require a post_tags table.

Add update category API

Add update category API.
It should handle PATCH requests and update the names of categories.
Test the API.

Add list posts by category API

Add list posts by category API.
Create a function to handle GET requests and return posts from the given category.
It should accept three query parameters - page, page_size, and category_id.
Test the function.

Add list comments API

Add list comments for post API.
Add a function to handle GET requests to list comments of a given post.
The post ID should be sent in URI and page and page_size in query params.
Test the function.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.