You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

43 lines
1.3 KiB

package tokens
import (
"time"
"git.devices.local/mawas/golang-api-skeleton/lib/cache"
"git.devices.local/mawas/golang-api-skeleton/lib/common"
"git.devices.local/mawas/golang-api-skeleton/models"
"git.devices.local/mawas/golang-api-skeleton/repositories"
"git.devices.local/mawas/golang-api-skeleton/services"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func Create(c *gin.Context) {
// var requestBody models.Token
db := c.MustGet("db").(*gorm.DB)
cc := c.MustGet("cache").(cache.Cache)
// if err := c.ShouldBindBodyWith(&requestBody, binding.JSON); err != nil {
// return
// }
tokenRepo := repositories.NewTokenRepository(db, "01F5FSJXDHWT4HK93B9NB8V5G4", "test", cc)
tokenService := services.NewTokenService(tokenRepo)
userID, _ := common.StringToGUID("01F5G1W6WJCKQ4PPRS36RYGQ08")
t := &models.Token{
ExpiresAt: time.Now().Add(24 * time.Hour),
UserID: userID,
Active: true,
}
token, _ := tokenService.Create(t)
c.JSON(200, token)
}
func Read(c *gin.Context) {
db := c.MustGet("db").(*gorm.DB)
cc := c.MustGet("cache").(cache.Cache)
repo := repositories.NewTokenRepository(db, "01F5FSJXDHWT4HK93B9NB8V5G4", "test", cc)
service := services.NewTokenService(repo)
t := c.Param("token")
token, _ := service.ReadByKey(t)
c.JSON(200, token)
}