Skip to content

Simple ๐‘๐ž๐ฌ๐ญ ๐€๐ฉ๐ข project implemented CRUD with ๐ฌ๐ช๐ฅ๐ข๐ญ๐ž3 and using ๐‘ฎ๐’Š๐’ framework

Notifications You must be signed in to change notification settings

bappasahabapi/Go-Event-Booking-Rest-Api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

34 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Output:

Event-Api

Tech Stack:

  • Backend: Golang +Gin Framework
  • Database: sqlite
  • Frontend: React (upcomming...)

Features:

  • Basic CRUD
  • Hash Password
  • JWT Authentication
  • Input Validation
  • MVC
  • Authorization User
  • Middleware Interation

Basic:

  • If err == nil, it means: "everything went fine"
  • If err != nil, it means: "something went wrong", and you can check err.Error() to see what happened.
  • So, nil is just Goโ€™s way of saying โ€œthere was no error.โ€ You check for non-nil to handle problems.
Event-Booking-app/
โ”‚
โ”œโ”€โ”€ routes/             `[working on routes]`
โ”‚   โ””โ”€โ”€ item-routes.go
โ”‚
โ”œโ”€โ”€ controllers/ `[working on request and response]`
โ”‚   โ””โ”€โ”€ item-controller.go 
โ”‚
โ”œโ”€โ”€ models/            `[working on type struct and interface]`
โ”‚   โ””โ”€โ”€ item-model.go
โ”‚
โ”œโ”€โ”€ services/        `[working on database query]`
โ”‚   โ””โ”€โ”€ item-services.go
โ”‚
โ”œโ”€โ”€ config/
โ”‚   โ””โ”€โ”€ db.go
โ”‚
โ”œโ”€โ”€ main.go
โ”‚
โ””โ”€โ”€ go.mod


All Install Packages:

  • go mod init bappa.com/rest
  • go get -u github.com/gin-gonic/gin
  • go get github.com/mattn/go-sqlite3
  • go get github.com/fatih/color for color the console
  • go install github.com/gravityblast/fresh@latest for auto restart
  • go get golang.org/x/crypto bcrypt for hash password
  • go get -u github.com/golang-jwt/jwt/v5 JWT

Run the server

- go run . || go run main.go || fresh

  • fresh for auto restart

Steps:

โœ… Events:

  • 01-project initialized with server and gin framework

  • go mod init bappa.com/rest

  • go get -u github.com/gin-gonic/gin

  • 02-Add and Test Create and Get event

  • [X]03-Initilazation of SQL Database

-https://github.com/mattn/go-sqlite3 - go get github.com/mattn/go-sqlite3

๐Ÿ”‹ Database Connection Part

var DB *sql.DB
 
func InitDB() {
    var err error
    DB, err = sql.Open("sqlite3", "api.db")
 
    if err != nil {
        panic("Could not connect to database.")
    }
 
    DB.SetMaxOpenConns(10)
    DB.SetMaxIdleConns(5)
 
    createTables()
}
  • 04-Insert & Read Data from Database

Preparing Statements vs Directly Executing Queries (Prepare() vs Exec()/Query())

We started sending SQL commands to the SQLite database.

And we did this by following different approaches:

  • DB.Exec() (when we created the tables)

  • Prepare() + stmt.Exec() (when we inserted data into the database)

  • DB.Query() (when we fetched data from the database)

Using Prepare() is 100% optional! You could send all your commands directly via Exec() or Query().

The difference between those two methods then just is

  • whether you're fetching data from the database (=> use Query()) or your manipulating the database / data in the database (=> use Exec()).

But what's the advantage of using Prepare()?

Prepare() prepares a SQL statement - this can lead to better performance if the same statement is executed multiple times (potentially with different data for its placeholders).

This is only true, if the prepared statement is not closed (stmt.Close()) in between those executions. In that case, there wouldn't be any advantages.

And, indeed, in this application, we are calling stmt.Close() directly after calling stmt.Exec(). So here, it really wouldn't matter which approach you're using.

๐Ÿ’ก But in order to show you the different ways of using the sql package,I decided to also include this preparation approach in this project

  • 05-Beautify console and json data ๐Ÿ”†

  • go get github.com/fatih/color

  • use like this

import ("github.com/fatih/color")

color.Cyan("๐Ÿ”‹ ๐Ÿš€ Server running at http://localhost:8080")

  • 06-Add git ignore files

  • 07-update readme

  • [X]08-Get event by Id done

  • [X]09-Refctoring code and route for scalability and maintalibility

  • 10-Update route added


โœ… Users: 13-Commit

  • Create routes for the users.
  • First create the users table and make a connection with events table.
  • Write the route, controller,service,model for signup user.
  • 14. Create Hash Password signup user.
  • 15. valided user with finished the login functionality
  • 16. Generate JWT
  • 17. Adding Token Varification [complex part]

โœ… Middleware : 20-Commit :

  • 18. Now implement route protection
  • 19. Retrieving & Storing User and Event IDs
  • 22. Adding Authorization To Restrict Users From Editing & Deleting

โœ… Registration Route : 23-Commit :

  • 23. Registering Users for events

About

Simple ๐‘๐ž๐ฌ๐ญ ๐€๐ฉ๐ข project implemented CRUD with ๐ฌ๐ช๐ฅ๐ข๐ญ๐ž3 and using ๐‘ฎ๐’Š๐’ framework

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages