first commit
This commit is contained in:
67
internal/controllers/auth.go
Normal file
67
internal/controllers/auth.go
Normal file
@ -0,0 +1,67 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"studioj/boilerplate_go/internal/models"
|
||||
"studioj/boilerplate_go/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AuthController interface {
|
||||
Register(*gin.Context)
|
||||
Login(*gin.Context)
|
||||
}
|
||||
|
||||
type authController struct {
|
||||
service services.AuthService
|
||||
}
|
||||
|
||||
func NewAuthController(service services.AuthService) AuthController {
|
||||
return &authController{
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
func (controller *authController) Register(c *gin.Context) {
|
||||
var params models.RegisterRequest
|
||||
if c.BindJSON(¶ms) != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"})
|
||||
return
|
||||
}
|
||||
|
||||
user, err := controller.service.Register(¶ms)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
token, err := controller.service.CreateToken(user.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"user": user, "token": token.Token, "refresh_token": token.RefreshToken})
|
||||
}
|
||||
|
||||
func (controller *authController) Login(c *gin.Context) {
|
||||
var params models.LoginRequest
|
||||
if c.BindJSON(¶ms) != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"})
|
||||
return
|
||||
}
|
||||
|
||||
user, err := controller.service.Login(¶ms)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
token, err := controller.service.CreateToken(user.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"user": user, "token": token.Token, "refresh_token": token.RefreshToken})
|
||||
}
|
59
internal/controllers/token.go
Normal file
59
internal/controllers/token.go
Normal file
@ -0,0 +1,59 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"studioj/boilerplate_go/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type TokenController interface {
|
||||
Find(*gin.Context)
|
||||
}
|
||||
|
||||
type tokenController struct {
|
||||
service services.TokenService
|
||||
}
|
||||
|
||||
func NewTokenController(service services.TokenService) TokenController {
|
||||
return &tokenController{
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
func (controller *tokenController) Find(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
user_id := c.GetString("user_id")
|
||||
|
||||
if user_id != id {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Wrong user"})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := controller.service.Find(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
func (controller *tokenController) List(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
user_id := c.GetString("user_id")
|
||||
|
||||
if user_id != id {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Wrong user"})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := controller.service.Find(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
46
internal/controllers/user.go
Normal file
46
internal/controllers/user.go
Normal file
@ -0,0 +1,46 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"studioj/boilerplate_go/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type UserController interface {
|
||||
Find(*gin.Context)
|
||||
}
|
||||
|
||||
type userController struct {
|
||||
service services.UserService
|
||||
tokenService services.TokenService
|
||||
}
|
||||
|
||||
func NewUserController(service services.UserService, tokenService services.TokenService) UserController {
|
||||
return &userController{
|
||||
service: service,
|
||||
tokenService: tokenService,
|
||||
}
|
||||
}
|
||||
|
||||
func (controller *userController) Find(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
user_id := c.GetString("sub")
|
||||
fmt.Println("id", id)
|
||||
fmt.Println("user_id", user_id)
|
||||
|
||||
if user_id != id {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Wrong user"})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := controller.service.FindByID(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
Reference in New Issue
Block a user