This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package repository | |
func DB() *gorm.DB { | |
username:=os.Getenv("DB_USERNAME") | |
password:=os.Getenv("DB_PASSWORD") | |
dbname:=os.Getenv("DB_NAME") | |
db, err := gorm.Open("mysql", username+":"+password+"@/"+dbname+"?charset=utf8&parseTime=True&loc=Local") | |
if err != nil { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model | |
import ( | |
"github.com/jinzhu/gorm" | |
) | |
type Order struct { | |
gorm.Model | |
User User `gorm:"foreignkey:UserID"` | |
Product Product `gorm:"foreignkey:ProductID"` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package repository | |
import ( | |
"mini-ecommerce/model" | |
"github.com/jinzhu/gorm" | |
) | |
type UserRepository interface { | |
AddUser(model.User) (model.User, error) | |
GetUser(int) (model.User, error) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (db *userRepository) GetUser(id int) (user model.User, err error) { | |
return user, db.connection.First(&user, id).Error | |
} | |
func (db *userRepository) DeleteUser(user model.User) (model.User, error) { | |
if err := db.connection.First(&user, user.ID).Error; err != nil { | |
return user, err | |
} | |
return user, db.connection.Delete(&user).Error | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package handler | |
type UserHandler interface { | |
AddUser(*gin.Context) | |
GetUser(*gin.Context) | |
GetAllUser(*gin.Context) | |
SignInUser(*gin.Context) | |
UpdateUser(*gin.Context) | |
DeleteUser(*gin.Context) | |
GetProductOrdered(*gin.Context) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func hashPassword(pass *string) { | |
bytePass := []byte(*pass) | |
hPass, _ := bcrypt.GenerateFromPassword(bytePass, bcrypt.DefaultCost) | |
*pass = string(hPass) | |
} | |
func comparePassword(dbPass, pass string) bool { | |
return bcrypt.CompareHashAndPassword([]byte(dbPass), []byte(pass)) == nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package handler | |
func GenerateToken(userid uint) string { | |
claims := jwt.MapClaims{ | |
"exp": time.Now().Add(time.Hour * 3).Unix(), | |
"iat": time.Now().Unix(), | |
"userID": userid, | |
} | |
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package middleware | |
func AuthorizeJWT() gin.HandlerFunc { | |
return func(ctx *gin.Context) { | |
const BearerSchema string = "Bearer " | |
authHeader := ctx.GetHeader("Authorization") | |
if authHeader == "" { | |
ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{ | |
"error": "No Authorization header found"}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package route | |
func RunAPI(address string) error { | |
userHandler := handler.NewUserHandler() | |
productHandler := handler.NewProductHandler() | |
orderHandler := handler.NewOrderHandler() | |
r := gin.Default() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
func loadenv() { | |
if err := godotenv.Load(); err != nil { | |
log.Fatal("Error loading .env file") | |
} | |
} | |
func main() { | |
fmt.Println("Main Application Starts") |
OlderNewer