-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed all folder structures and logic in the code
- Loading branch information
Showing
33 changed files
with
225 additions
and
481 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/joho/godotenv" | ||
|
||
"ngodeyuk-core/database" | ||
"ngodeyuk-core/internal/infrastructure/routes" | ||
) | ||
|
||
func main() { | ||
route := gin.Default() | ||
err := godotenv.Load() | ||
if err != nil { | ||
log.Fatalf("error load .env file: %v", err) | ||
} | ||
|
||
db, err := database.InitDB() | ||
if err != nil { | ||
panic("failed to connect database.") | ||
} | ||
routes.UserRoutes(route, db) | ||
|
||
fmt.Println("Server is running at http://localhost:2000") | ||
log.Fatal(route.Run(":2000")) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package config | ||
package database | ||
|
||
import ( | ||
"log" | ||
|
File renamed without changes.
File renamed without changes.
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package repositories | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
|
||
"ngodeyuk-core/internal/domain/models" | ||
) | ||
|
||
type UserRepository interface { | ||
Create(user *models.User) error | ||
FindByUsername(username string) (*models.User, error) | ||
Update(user *models.User) error | ||
} | ||
|
||
type userRepository struct { | ||
db *gorm.DB | ||
} | ||
|
||
func NewUserRepository(db *gorm.DB) UserRepository { | ||
return &userRepository{db} | ||
} | ||
|
||
func (repository *userRepository) Create(user *models.User) error { | ||
return repository.db.Create(user).Error | ||
} | ||
|
||
func (repository *userRepository) FindByUsername(username string) (*models.User, error) { | ||
var user models.User | ||
err := repository.db.Where("username = ?", username).First(&user).Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &user, nil | ||
} | ||
|
||
func (repository *userRepository) Update(user *models.User) error { | ||
if err := repository.db.Save(user).Error; err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
"ngodeyuk-core/internal/domain/dtos" | ||
"ngodeyuk-core/internal/services" | ||
) | ||
|
||
type UserHandler interface { | ||
Register(c *gin.Context) | ||
Login(c *gin.Context) | ||
} | ||
|
||
type userHandler struct { | ||
service services.UserService | ||
} | ||
|
||
func NewUserHandler(service services.UserService) UserHandler { | ||
return &userHandler{service} | ||
} | ||
|
||
func (handler *userHandler) Register(ctx *gin.Context) { | ||
var input dtos.RegisterDTO | ||
if err := ctx.ShouldBindJSON(&input); err != nil { | ||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
err := handler.service.Register(&input) | ||
if err != nil { | ||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
ctx.JSON(http.StatusCreated, gin.H{ | ||
"data": gin.H{ | ||
"name": input.Name, | ||
"username": input.Username, | ||
}, | ||
}) | ||
} | ||
|
||
func (handler *userHandler) Login(ctx *gin.Context) { | ||
var input dtos.LoginDTO | ||
if err := ctx.ShouldBindJSON(&input); err != nil { | ||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
token, err := handler.service.Login(&input) | ||
if err != nil { | ||
ctx.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, gin.H{ | ||
"token": token, | ||
}) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package routes | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"gorm.io/gorm" | ||
|
||
"ngodeyuk-core/internal/domain/repositories" | ||
"ngodeyuk-core/internal/infrastructure/handlers" | ||
"ngodeyuk-core/internal/services" | ||
) | ||
|
||
func UserRoutes(route *gin.Engine, db *gorm.DB) { | ||
repository := repositories.NewUserRepository(db) | ||
service := services.NewUserService(repository) | ||
handler := handlers.NewUserHandler(service) | ||
|
||
route.POST("auth/register", handler.Register) | ||
route.POST("auth/login", handler.Login) | ||
} |
Oops, something went wrong.