adding new tables
This commit is contained in:
40
backend/handlers/handle_activity.go
Normal file
40
backend/handlers/handle_activity.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"adam-french.co.uk/backend/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type CreateActivityInput struct {
|
||||
Type string `json:"type" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Link *string `json:"link"`
|
||||
}
|
||||
|
||||
func (store *Store) GetActivity(ctx *gin.Context) {
|
||||
var activitys []models.Activity
|
||||
if err := store.DB.Order("Created_At DESC").Find(&activitys).Error; err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, activitys)
|
||||
}
|
||||
|
||||
func (store *Store) CreateActivity(ctx *gin.Context) {
|
||||
var input CreateActivityInput
|
||||
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
activity := models.Activity{Type: input.Type, Name: input.Name, Link: input.Link}
|
||||
tx := store.DB.Create(&activity)
|
||||
if tx.Error != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, tx.Error.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusCreated, activity)
|
||||
}
|
||||
40
backend/handlers/handle_favorites.go
Normal file
40
backend/handlers/handle_favorites.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"adam-french.co.uk/backend/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type CreateFavoriteInput struct {
|
||||
Type string `json:"type" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Link *string `json:"link"`
|
||||
}
|
||||
|
||||
func (store *Store) GetFavorites(ctx *gin.Context) {
|
||||
var favorites []models.Favorite
|
||||
if err := store.DB.Order("Created_At DESC").Find(&favorites).Error; err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, favorites)
|
||||
}
|
||||
|
||||
func (store *Store) CreateFavorite(ctx *gin.Context) {
|
||||
var input CreateFavoriteInput
|
||||
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
favorite := models.Favorite{Type: input.Type, Name: input.Name, Link: input.Link}
|
||||
tx := store.DB.Create(&favorite)
|
||||
if tx.Error != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, tx.Error.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusCreated, favorite)
|
||||
}
|
||||
Reference in New Issue
Block a user