Skip to content

Commit

Permalink
refactor: clean architecture (#11)
Browse files Browse the repository at this point in the history
changed all folder structures and logic in the code
  • Loading branch information
Yuefii authored Jul 29, 2024
1 parent cd122b5 commit 6f1d5bb
Show file tree
Hide file tree
Showing 33 changed files with 225 additions and 481 deletions.
27 changes: 0 additions & 27 deletions cmd/ngodeyuk/main.go

This file was deleted.

29 changes: 29 additions & 0 deletions cmd/web/main.go
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"))
}
2 changes: 1 addition & 1 deletion config/database.go → database/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package database

import (
"log"
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.22.4

require github.com/gin-gonic/gin v1.10.0

require github.com/golang-jwt/jwt/v5 v5.2.1 // indirect

require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBEx
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down
33 changes: 0 additions & 33 deletions internal/courses/handlers/course_handler.go

This file was deleted.

16 changes: 0 additions & 16 deletions internal/courses/router/course_router.go

This file was deleted.

29 changes: 0 additions & 29 deletions internal/courses/services/course_service.go

This file was deleted.

9 changes: 2 additions & 7 deletions pkg/dto/user_dto.go → internal/domain/dtos/user_dto.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dto
package dtos

type RegisterDTO struct {
Name string `json:"name" binding:"required"`
Name string `json:"name" binding:"required"`
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}
Expand All @@ -10,8 +10,3 @@ type LoginDTO struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}

type ChangePasswordDTO struct {
OldPassword string `json:"old_password" binding:"required"`
NewPassword string `json:"new_password" binding:"required"`
}
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.
41 changes: 41 additions & 0 deletions internal/domain/repositories/user_repository.go
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
}
15 changes: 0 additions & 15 deletions internal/hello/handlers/hello.go

This file was deleted.

11 changes: 0 additions & 11 deletions internal/hello/router/hello_router.go

This file was deleted.

7 changes: 0 additions & 7 deletions internal/hello/services/hello.go

This file was deleted.

61 changes: 61 additions & 0 deletions internal/infrastructure/handlers/user_handler.go
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,
})
}
19 changes: 19 additions & 0 deletions internal/infrastructure/routes/routes.go
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)
}
Loading

0 comments on commit 6f1d5bb

Please sign in to comment.