package handlers import ( "net/http" "adam-french.co.uk/backend/models" "github.com/gin-gonic/gin" "golang.org/x/crypto/bcrypt" ) type UserCredentials struct { Username string `json:"username" binding:"required"` Password string `json:"password" binding:"required"` } func (store *Store) CreateUser(ctx *gin.Context) { var input UserCredentials if err := ctx.ShouldBindBodyWithJSON(&input); err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } hashedPassword, err := bcrypt.GenerateFromPassword([]byte(input.Password), bcrypt.DefaultCost) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } user := models.User{Username: input.Username, Password: hashedPassword} store.DB.Create(&user) // Generate JWT token ctx.JSON(http.StatusOK, gin.H{"data": user}) } func (store *Store) LoginUser(ctx *gin.Context) { var input UserCredentials if err := ctx.ShouldBindBodyWithJSON(&input); err != nil { ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } user := models.User{Username: input.Username} if err := store.DB.Where("username = ?", input.Username).First(&user).Error; err != nil { ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) return } if err := bcrypt.CompareHashAndPassword(user.Password, []byte(input.Password)); err != nil { ctx.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) return } // Generate JWT token ctx.JSON(http.StatusAccepted, gin.H{"data": user}) } func (store *Store) GetUser(ctx *gin.Context) { } func (store *Store) GetUsers(ctx *gin.Context) { var users []models.User if err := store.DB.Find(&users).Error; err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } ctx.JSON(http.StatusOK, gin.H{"data": models.Post{}}) } func (store *Store) UpdateUser(c *gin.Context) { }