first commit

This commit is contained in:
kandrusyak
2023-07-14 14:54:41 +03:00
commit 7b03922374
29 changed files with 839 additions and 0 deletions

105
pkg/api/apiCall.go Normal file
View File

@@ -0,0 +1,105 @@
package api
import (
"bytes"
"encoding/json"
"io"
"net/http"
)
func PostRequest(url string, model any, header http.Header) ([]byte, error) {
uJson, err := json.Marshal(model)
if err != nil {
return nil, err
}
client := http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewReader(uJson))
req.Header = header
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return data, nil
}
func GetRequest(url string, header http.Header, args ...string) ([]byte, error, *http.Response) {
client := http.Client{}
req, err := http.NewRequest("GET", url, nil)
if len(args) > 0 {
req.URL.RawQuery = args[0]
}
req.Header = header
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err, nil
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err, nil
}
return data, nil, resp
}
func PatchRequest(url string, model any, header http.Header) ([]byte, error) {
uJson, err := json.Marshal(model)
if err != nil {
return nil, err
}
client := http.Client{}
req, err := http.NewRequest("PATCH", url, bytes.NewReader(uJson))
req.Header = header
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return data, nil
}
func DeleteRequest(url string, header http.Header) ([]byte, error) {
client := http.Client{}
req, err := http.NewRequest("DELETE", url, nil)
req.Header = header
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return data, nil
}

27
pkg/utils/query.go Normal file
View File

@@ -0,0 +1,27 @@
package utils
import (
"github.com/labstack/echo/v4"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
func ApplySort(tx *gorm.DB, column string, isDesc bool) {
tx.Order(clause.OrderByColumn{
Column: clause.Column{Name: column},
Desc: isDesc,
})
}
func ApplyFilters(tx *gorm.DB, column string, value string) {
if column != "" && value != "" {
tx.Where(column + " LIKE '%" + value + "%'")
}
}
func ApplyIdFilter(c echo.Context, tx *gorm.DB) {
id := c.Param("id")
if id != "" {
tx.Where("id = ?", id)
}
}

52
pkg/utils/url.go Normal file
View File

@@ -0,0 +1,52 @@
package utils
import (
"net/url"
"strconv"
"strings"
)
func GetQueryParam(query url.Values, key string, defaultVal string) string {
data := query.Get(key)
if data == "" {
return defaultVal
}
return data
}
func GetPaginationParams(query url.Values) (int, int) {
offset := GetQueryParam(query, "offset", "0")
limit := GetQueryParam(query, "limit", "10")
limitInt, err := strconv.Atoi(limit)
if err != nil {
limitInt = 10
}
offsetInt, err := strconv.Atoi(offset)
if err != nil {
offsetInt = 0
}
return limitInt, offsetInt
}
func GetFilterParams(query url.Values) (string, bool, string, string) {
sort := GetQueryParam(query, "sort", "id:asc")
filter := GetQueryParam(query, "filter", ":")
filters := strings.Split(filter, ":")
sorts := strings.Split(sort, ":")
isDesc := false
if sorts[1] == "desc" {
isDesc = true
}
return sorts[0], isDesc, filters[0], filters[1]
}