feat(api): add return for all user templates
BREAKING: Template api will return different variables in template object
This commit is contained in:
@@ -34,7 +34,7 @@ func Create(c *gin.Context) {
|
||||
// Get user id
|
||||
user, err := jwt.GetUser(c)
|
||||
if err != nil {
|
||||
res.Error(c, err.Error(), http.StatusInternalServerError)
|
||||
res.NeedsToLogin(c)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -61,6 +61,21 @@ func Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
func Get(c *gin.Context) {
|
||||
// Get user from context
|
||||
user, err := jwt.GetUser(c)
|
||||
if err != nil {
|
||||
res.NeedsToLogin(c)
|
||||
return
|
||||
}
|
||||
|
||||
// Get all user templates
|
||||
templates, err := template.Get("user_id = $1", user.Id)
|
||||
if err != nil {
|
||||
res.Error(c, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
res.Success(c, templates)
|
||||
}
|
||||
|
||||
func Update(c *gin.Context) {
|
||||
|
||||
@@ -7,11 +7,11 @@ import (
|
||||
)
|
||||
|
||||
type Template struct {
|
||||
ID int
|
||||
UserID int
|
||||
Name string
|
||||
Template string
|
||||
CreatedAt time.Time
|
||||
ID int `json:"id"`
|
||||
UserID int `json:"user_id"`
|
||||
Name string `json:"name"`
|
||||
Template string `json:"template"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func Create(name string, template string, userId float64) error {
|
||||
@@ -30,18 +30,23 @@ func Create(name string, template string, userId float64) error {
|
||||
// * will follow the pointer to its value
|
||||
// If user id is 0, then we will search only by name
|
||||
func FindByName(name string, userId float64) ([]Template, error) {
|
||||
templates, err := Get(`"name" = $1 AND user_id = $2`, name, userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return templates, nil
|
||||
}
|
||||
|
||||
func Get(where string, args ...any) ([]Template, error) {
|
||||
// Create timeout context
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Build query and execute
|
||||
query := `SELECT * FROM templates WHERE "name" = $1`
|
||||
args := []any{name}
|
||||
|
||||
// Do we need to search by user id?
|
||||
if userId > 0 {
|
||||
query += " AND user_id = $2"
|
||||
args = append(args, userId)
|
||||
query := `SELECT * FROM templates`
|
||||
if where != "" {
|
||||
query += " WHERE " + where
|
||||
}
|
||||
|
||||
rows, err := db.Pool.Query(ctx, query, args...)
|
||||
@@ -65,6 +70,5 @@ func FindByName(name string, userId float64) ([]Template, error) {
|
||||
results = append(results, t)
|
||||
}
|
||||
|
||||
// Give pointer back
|
||||
return results, nil
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
func SetupRoutes() *gin.Engine {
|
||||
r := gin.Default()
|
||||
|
||||
// Guest routes (Register, Login, check auth)
|
||||
// Guest routes (Register, Login)
|
||||
r.POST("/register", user.Register)
|
||||
r.POST("/login", user.Login)
|
||||
|
||||
@@ -24,7 +24,7 @@ func SetupRoutes() *gin.Engine {
|
||||
|
||||
// Template routes (REST FUCKING GOOOOO)
|
||||
templates := auth.Group("/templates")
|
||||
// GET (Gets all templates)
|
||||
templates.GET("", template.Get)
|
||||
templates.POST("", template.Create)
|
||||
// PUT (Edit)
|
||||
// DELETE (Delete)
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
}
|
||||
|
||||
:8080 {
|
||||
encode
|
||||
reverse_proxy frontend:3000
|
||||
|
||||
handle_path /api* {
|
||||
|
||||
Reference in New Issue
Block a user