partial repository layer added

This commit is contained in:
Fran Jurmanović
2022-09-27 00:33:44 +02:00
parent 13ce0735d0
commit 82e97fc97f
73 changed files with 2686 additions and 1216 deletions

129
pkg/controller/user.go Normal file
View File

@@ -0,0 +1,129 @@
package controller
import (
"net/http"
"wallet-api/pkg/middleware"
"wallet-api/pkg/model"
"wallet-api/pkg/service"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin"
)
type UserController struct {
service *service.UserService
}
/*
NewUserController
Initializes UserController.
Args:
*services.UserService: User service
*gin.RouterGroup: Gin Router Group
Returns:
*UserController: Controller for "auth" interactions
*/
func NewUserController(rs *service.UserService, routeGroups *common.RouteGroups) *UserController {
rc := &UserController{
service: rs,
}
routeGroups.Auth.POST("login", rc.PostLogin)
routeGroups.Auth.POST("register", rc.PostRegister)
routeGroups.Auth.DELETE("deactivate", middleware.Auth, rc.Delete)
routeGroups.Auth.GET("check-token", rc.CheckToken)
return rc
}
/*
PostLogin
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (POST /auth/login).
func (rc *UserController) PostLogin(c *gin.Context) {
body := new(model.Login)
if err := c.ShouldBind(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
returnedUser, exceptionReturn := rc.service.Login(c, body)
if exceptionReturn.Message != "" {
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
return
} else {
c.JSON(200, returnedUser)
}
}
/*
PostRegister
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (POST /auth/register).
func (rc *UserController) PostRegister(c *gin.Context) {
body := new(model.User)
body.Init()
body.IsActive = true
if err := c.ShouldBind(body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
returnedUser, exceptionReturn := rc.service.Create(c, body)
if exceptionReturn.Message != "" {
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
return
} else {
c.JSON(200, returnedUser.Payload())
}
}
/*
Delete
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (DELETE /auth/deactivate).
func (rc *UserController) Delete(c *gin.Context) {
auth := new(model.Auth)
authGet := c.MustGet("auth")
auth.Id = authGet.(*model.Auth).Id
mr, er := rc.service.Deactivate(c, auth)
if er.Message != "" {
c.JSON(er.StatusCode, er)
return
} else {
c.JSON(200, mr)
}
}
/*
CheckToken
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (GET /auth/check-token).
func (rc *UserController) CheckToken(c *gin.Context) {
token, _ := c.GetQuery("token")
re := new(model.CheckToken)
_, err := middleware.CheckToken(token)
if err != nil {
re.Valid = false
c.AbortWithStatusJSON(400, re)
return
}
re.Valid = true
c.JSON(200, re)
}