bigest part of the api done
This commit is contained in:
57
controllers/authentication/auth.controller.go
Normal file
57
controllers/authentication/auth.controller.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package authentication
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"spahl.ddns.net/jasper/wok-able-backend/auth"
|
||||
"spahl.ddns.net/jasper/wok-able-backend/models"
|
||||
)
|
||||
|
||||
func Register(c *gin.Context) {
|
||||
var user models.User
|
||||
if err := c.BindJSON(&user); err != nil || user.Email == "" || user.Username == "" {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := models.DB.Create(&user).Save(&user).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
token, err := auth.GenerateJWT(user.ID, user.Username, user.Email)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.SetCookie("token", token, 2*60*60, "", "", false, true)
|
||||
c.Status(http.StatusCreated)
|
||||
}
|
||||
|
||||
func Login(c *gin.Context) {
|
||||
var authentication models.Auth
|
||||
if err := c.BindJSON(&authentication); err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var user models.User
|
||||
models.DB.Where("username = @name OR email = @name", sql.Named("name", authentication.Username)).First(&user)
|
||||
if user.Email == "" {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Username or Password is incorrect"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := auth.CheckPassword(authentication.Password, user.Salt, user.PasswordHash); err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Username or Password is incorrect"})
|
||||
return
|
||||
}
|
||||
|
||||
token, _ := auth.GenerateJWT(user.ID, user.Username, user.Email)
|
||||
c.SetCookie("token", token, 2*60*60, "", "", false, true)
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
func Logout(c *gin.Context) {
|
||||
c.SetCookie("token", "", -1, "", "", false, true)
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/")
|
||||
}
|
||||
9
controllers/authentication/setup.go
Normal file
9
controllers/authentication/setup.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package authentication
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func Setup(r *gin.RouterGroup) {
|
||||
r.GET("/logout", Logout)
|
||||
r.POST("/login", Login)
|
||||
r.POST("/register", Register)
|
||||
}
|
||||
15
controllers/setup.go
Normal file
15
controllers/setup.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
auth "spahl.ddns.net/jasper/wok-able-backend/controllers/authentication"
|
||||
v1 "spahl.ddns.net/jasper/wok-able-backend/controllers/v1"
|
||||
)
|
||||
|
||||
func Setup(c *gin.Engine) {
|
||||
api := c.Group("/api")
|
||||
{
|
||||
v1.Setup(api.Group("/v1"))
|
||||
auth.Setup(api.Group("/auth"))
|
||||
}
|
||||
}
|
||||
74
controllers/v1/card/card.controller.go
Normal file
74
controllers/v1/card/card.controller.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package card
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"spahl.ddns.net/jasper/wok-able-backend/auth"
|
||||
"spahl.ddns.net/jasper/wok-able-backend/models"
|
||||
)
|
||||
|
||||
func getCardById(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var card models.Card
|
||||
err = models.DB.Scopes(auth.UserScope(c)).First(&card, id).Error
|
||||
if err != nil {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "Card not found"})
|
||||
return
|
||||
}
|
||||
c.IndentedJSON(http.StatusOK, card.ToDto())
|
||||
}
|
||||
|
||||
func createCard(c *gin.Context) {
|
||||
var card models.Card
|
||||
if err := c.BindJSON(&card); err != nil {
|
||||
return
|
||||
}
|
||||
card.UserID = c.GetUint("user_id")
|
||||
|
||||
if models.DB.Create(&card).Save(&card).Error != nil {
|
||||
return
|
||||
}
|
||||
c.IndentedJSON(http.StatusCreated, card.ToDto())
|
||||
}
|
||||
|
||||
func updateCard(c *gin.Context) {
|
||||
var cardDto models.CardDto
|
||||
if err := c.BindJSON(&cardDto); err != nil {
|
||||
return
|
||||
}
|
||||
var card models.Card
|
||||
if err := models.DB.Scopes(auth.UserScope(c)).First(&card, cardDto.ID).Error; err != nil {
|
||||
c.IndentedJSON(http.StatusNotFound, err.Error())
|
||||
}
|
||||
card.Front = cardDto.Front
|
||||
card.Back = cardDto.Back
|
||||
card.Hint = cardDto.Hint
|
||||
card.CardDeckID = cardDto.CardDeckID
|
||||
|
||||
if err := models.DB.Scopes(auth.UserScope(c)).Save(&card).Error; err != nil {
|
||||
c.IndentedJSON(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
c.IndentedJSON(http.StatusAccepted, card.ToDto())
|
||||
}
|
||||
|
||||
func deleteCard(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
err = models.DB.Scopes(auth.UserScope(c)).Delete(&models.Card{}, id).Error
|
||||
if err != nil {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusAccepted)
|
||||
}
|
||||
10
controllers/v1/card/setup.go
Normal file
10
controllers/v1/card/setup.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package card
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func Setup(r *gin.RouterGroup) {
|
||||
r.GET("/:id", getCardById)
|
||||
r.POST("/", createCard)
|
||||
r.PUT("/", updateCard)
|
||||
r.DELETE("/:id", deleteCard)
|
||||
}
|
||||
49
controllers/v1/carddeck/carddeck.controller.go
Normal file
49
controllers/v1/carddeck/carddeck.controller.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package carddeck
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"spahl.ddns.net/jasper/wok-able-backend/auth"
|
||||
"spahl.ddns.net/jasper/wok-able-backend/models"
|
||||
)
|
||||
|
||||
func getCardDecks(c *gin.Context) {
|
||||
var cardDecks []models.CardDeck
|
||||
models.DB.Scopes(auth.UserScope(c)).Preload("Cards").Find(&cardDecks)
|
||||
dto := []models.CardDeckDto{}
|
||||
for _, deck := range cardDecks {
|
||||
dto = append(dto, deck.ToDto())
|
||||
}
|
||||
c.IndentedJSON(http.StatusOK, dto)
|
||||
}
|
||||
|
||||
func getCardDeckById(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var cardDeck models.CardDeck
|
||||
err = models.DB.Scopes(auth.UserScope(c)).Preload("Cards").First(&cardDeck, id).Error
|
||||
if err != nil {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "Card Deck not found"})
|
||||
return
|
||||
}
|
||||
c.IndentedJSON(http.StatusOK, cardDeck.ToDto())
|
||||
}
|
||||
|
||||
func createCardDeck(c *gin.Context) {
|
||||
var cardDeck models.CardDeck
|
||||
if err := c.BindJSON(&cardDeck); err != nil {
|
||||
return
|
||||
}
|
||||
cardDeck.UserID = c.GetUint("user_id")
|
||||
|
||||
if models.DB.Scopes(auth.UserScope(c)).Create(&cardDeck).Save(&cardDeck).Error != nil {
|
||||
return
|
||||
}
|
||||
c.IndentedJSON(http.StatusCreated, cardDeck)
|
||||
}
|
||||
9
controllers/v1/carddeck/setup.go
Normal file
9
controllers/v1/carddeck/setup.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package carddeck
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func Setup(r *gin.RouterGroup) {
|
||||
r.GET("/", getCardDecks)
|
||||
r.GET("/:id", getCardDeckById)
|
||||
r.POST("/", createCardDeck)
|
||||
}
|
||||
14
controllers/v1/setup.go
Normal file
14
controllers/v1/setup.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"spahl.ddns.net/jasper/wok-able-backend/auth"
|
||||
"spahl.ddns.net/jasper/wok-able-backend/controllers/v1/card"
|
||||
"spahl.ddns.net/jasper/wok-able-backend/controllers/v1/carddeck"
|
||||
)
|
||||
|
||||
func Setup(r *gin.RouterGroup) {
|
||||
r.Use(auth.GetUser())
|
||||
carddeck.Setup(r.Group("/carddeck"))
|
||||
card.Setup(r.Group("/card"))
|
||||
}
|
||||
Reference in New Issue
Block a user