changing field names
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
|||||||
"adam-french.co.uk/backend/models"
|
"adam-french.co.uk/backend/models"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (store *Store) AuthMiddlewear(ctx *gin.Context) {
|
func (store *Store) AuthMiddlewear(ctx *gin.Context) {
|
||||||
@@ -48,7 +47,7 @@ func (store *Store) CheckToken(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
userID := uint(userIDF)
|
userID := uint(userIDF)
|
||||||
|
|
||||||
user := models.User{Model: gorm.Model{ID: userID}}
|
user := models.User{ID: userID}
|
||||||
tx := store.DB.First(&user)
|
tx := store.DB.First(&user)
|
||||||
if tx.Error != nil {
|
if tx.Error != nil {
|
||||||
ctx.JSON(http.StatusNotFound, tx.Error.Error())
|
ctx.JSON(http.StatusNotFound, tx.Error.Error())
|
||||||
@@ -80,8 +79,8 @@ func (store *Store) RefreshToken(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
userID := uint(userIDF)
|
userID := uint(userIDF)
|
||||||
|
|
||||||
user := models.User{}
|
user := models.User{ID: userID}
|
||||||
tx := store.DB.First(&user, userID)
|
tx := store.DB.First(&user)
|
||||||
if tx.Error != nil {
|
if tx.Error != nil {
|
||||||
ctx.JSON(http.StatusNotFound, tx.Error.Error())
|
ctx.JSON(http.StatusNotFound, tx.Error.Error())
|
||||||
removeCookies(ctx)
|
removeCookies(ctx)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"adam-french.co.uk/backend/models"
|
"adam-french.co.uk/backend/models"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -23,9 +24,16 @@ func (store *Store) GetPosts(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (store *Store) GetPost(ctx *gin.Context) {
|
func (store *Store) GetPost(ctx *gin.Context) {
|
||||||
postID := ctx.Param("id")
|
postIDStr := ctx.Param("id")
|
||||||
var post models.Post
|
|
||||||
if err := store.DB.First(&post, postID).Error; err != nil {
|
postID, err := strconv.ParseUint(postIDStr, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
ctx.JSON(http.StatusBadRequest, "invalid id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
post := models.Post{ID: uint(postID)}
|
||||||
|
if err := store.DB.First(&post).Error; err != nil {
|
||||||
ctx.JSON(http.StatusNotFound, err.Error())
|
ctx.JSON(http.StatusNotFound, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type Post struct {
|
type Post struct {
|
||||||
gorm.Model // includes ID, CreatedAt, UpdatedAt, DeletedAt
|
ID uint `gorm:"primarykey" json:"id"`
|
||||||
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
|
DeletedAt sql.NullTime `gorm:"index" json:"deletedAt"`
|
||||||
Title string `gorm:"not null" json:"title"`
|
Title string `gorm:"not null" json:"title"`
|
||||||
AuthorID uint `json:"-"`
|
AuthorID uint `json:"-"`
|
||||||
Author *User `gorm:"foreignKey:AuthorID" json:"author"`
|
Author *User `gorm:"foreignKey:AuthorID" json:"author"`
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
gorm.Model // includes ID, CreatedAt, UpdatedAt, DeletedAt
|
ID uint `gorm:"primarykey" json:"id"`
|
||||||
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
|
DeletedAt sql.NullTime `gorm:"index" json:"deletedAt"`
|
||||||
Username string `gorm:"uniqueIndex" json:"username"`
|
Username string `gorm:"uniqueIndex" json:"username"`
|
||||||
Password []byte `json:"-"`
|
Password []byte `json:"-"`
|
||||||
Admin bool `json:"admin"`
|
Admin bool `json:"admin"`
|
||||||
|
|||||||
Reference in New Issue
Block a user