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