54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.javil.eu/jacob1123/budgeteer/postgres"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func (h *Handler) autocompleteCategories(c *gin.Context) {
|
|
budgetID := c.Param("budgetid")
|
|
budgetUUID, err := uuid.Parse(budgetID)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"budgetid missing from URL"})
|
|
return
|
|
}
|
|
|
|
query := c.Request.URL.Query().Get("s")
|
|
searchParams := postgres.SearchCategoriesParams{
|
|
BudgetID: budgetUUID,
|
|
Search: "%" + query + "%",
|
|
}
|
|
categories, err := h.Service.SearchCategories(c.Request.Context(), searchParams)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, categories)
|
|
}
|
|
|
|
func (h *Handler) autocompletePayee(c *gin.Context) {
|
|
budgetID := c.Param("budgetid")
|
|
budgetUUID, err := uuid.Parse(budgetID)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, ErrorResponse{"budgetid missing from URL"})
|
|
return
|
|
}
|
|
|
|
query := c.Request.URL.Query().Get("s")
|
|
searchParams := postgres.SearchPayeesParams{
|
|
BudgetID: budgetUUID,
|
|
Search: query + "%",
|
|
}
|
|
payees, err := h.Service.SearchPayees(c.Request.Context(), searchParams)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, payees)
|
|
}
|