memos/store/query.go

101 lines
2.8 KiB
Go
Raw Normal View History

2021-12-09 17:02:57 +03:00
package store
import (
2021-12-12 09:39:28 +03:00
"memos/utils"
2021-12-09 17:02:57 +03:00
"strings"
)
type Query struct {
Id string `json:"id"`
UserId string `json:"userId"`
Title string `json:"title"`
Querystring string `json:"querystring"`
PinnedAt string `json:"pinnedAt"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
}
func CreateNewQuery(title string, querystring string, userId string) (Query, error) {
2021-12-12 09:39:28 +03:00
nowDateTimeStr := utils.GetNowDateTimeStr()
2021-12-09 17:02:57 +03:00
newQuery := Query{
2021-12-12 09:39:28 +03:00
Id: utils.GenUUID(),
2021-12-09 17:02:57 +03:00
Title: title,
Querystring: querystring,
UserId: userId,
PinnedAt: "",
CreatedAt: nowDateTimeStr,
UpdatedAt: nowDateTimeStr,
}
2021-12-10 08:41:17 +03:00
sqlQuery := `INSERT INTO queries (id, title, querystring, user_id, pinned_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)`
_, err := DB.Exec(sqlQuery, newQuery.Id, newQuery.Title, newQuery.Querystring, newQuery.UserId, newQuery.PinnedAt, newQuery.CreatedAt, newQuery.UpdatedAt)
2021-12-09 17:02:57 +03:00
2021-12-15 05:55:17 +03:00
return newQuery, FormatDBError(err)
2021-12-09 17:02:57 +03:00
}
type QueryPatch struct {
Title *string
Querystring *string
PinnedAt *string
}
func UpdateQuery(id string, queryPatch *QueryPatch) (Query, error) {
query, _ := GetQueryById(id)
set, args := []string{}, []interface{}{}
if v := queryPatch.Title; v != nil {
query.Title = *v
set, args = append(set, "title=?"), append(args, *v)
}
if v := queryPatch.Querystring; v != nil {
query.Querystring = *v
set, args = append(set, "querystring=?"), append(args, *v)
}
if v := queryPatch.PinnedAt; v != nil {
query.PinnedAt = *v
set, args = append(set, "pinned_at=?"), append(args, *v)
}
2021-12-12 09:39:28 +03:00
set, args = append(set, "updated_at=?"), append(args, utils.GetNowDateTimeStr())
2021-12-09 17:02:57 +03:00
args = append(args, id)
sqlQuery := `UPDATE queries SET ` + strings.Join(set, ",") + ` WHERE id=?`
_, err := DB.Exec(sqlQuery, args...)
2021-12-15 05:55:17 +03:00
return query, FormatDBError(err)
2021-12-09 17:02:57 +03:00
}
func DeleteQuery(queryId string) error {
2021-12-09 17:02:57 +03:00
query := `DELETE FROM queries WHERE id=?`
_, err := DB.Exec(query, queryId)
2021-12-15 05:55:17 +03:00
return FormatDBError(err)
2021-12-09 17:02:57 +03:00
}
func GetQueryById(queryId string) (Query, error) {
2021-12-10 08:41:17 +03:00
sqlQuery := `SELECT id, title, querystring, pinned_at, created_at, updated_at FROM queries WHERE id=?`
2021-12-09 17:02:57 +03:00
query := Query{}
2021-12-10 08:41:17 +03:00
err := DB.QueryRow(sqlQuery, queryId).Scan(&query.Id, &query.Title, &query.Querystring, &query.PinnedAt, &query.CreatedAt, &query.UpdatedAt)
2021-12-15 05:55:17 +03:00
return query, FormatDBError(err)
2021-12-09 17:02:57 +03:00
}
func GetQueriesByUserId(userId string) ([]Query, error) {
2021-12-10 08:41:17 +03:00
query := `SELECT id, title, querystring, pinned_at, created_at, updated_at FROM queries WHERE user_id=?`
2021-12-09 17:02:57 +03:00
rows, _ := DB.Query(query, userId)
defer rows.Close()
queries := []Query{}
for rows.Next() {
query := Query{}
2021-12-10 08:41:17 +03:00
rows.Scan(&query.Id, &query.Title, &query.Querystring, &query.PinnedAt, &query.CreatedAt, &query.UpdatedAt)
2021-12-09 17:02:57 +03:00
queries = append(queries, query)
}
2021-12-10 08:41:17 +03:00
if err := rows.Err(); err != nil {
2021-12-15 05:55:17 +03:00
return nil, FormatDBError(err)
2021-12-10 08:41:17 +03:00
}
2021-12-09 17:02:57 +03:00
2021-12-10 08:41:17 +03:00
return queries, nil
2021-12-09 17:02:57 +03:00
}