internal/server and internal/db: if query fails return status with error

This commit is contained in:
nicksherron 2020-02-11 20:27:43 -05:00
parent 1bcb4147fa
commit 980a975b63
2 changed files with 15 additions and 11 deletions

View file

@ -222,7 +222,7 @@ func (cmd Command) commandInsert() int64 {
return inserted
}
func (cmd Command) commandGet() []Query {
func (cmd Command) commandGet() ([]Query, error) {
var results []Query
var rows *sql.Rows
var err error
@ -436,22 +436,22 @@ func (cmd Command) commandGet() []Query {
}
if err != nil {
log.Println(err)
return []Query{}, nil
}
defer rows.Close()
for rows.Next() {
var result Query
err = rows.Scan(&result.Command, &result.Uuid, &result.Created)
if err != nil {
log.Println(err)
return []Query{}, nil
}
results = append(results, result)
}
return results
return results, nil
}
func (cmd Command) commandGetUUID() Query {
func (cmd Command) commandGetUUID() (Query, error) {
var result Query
err := db.QueryRow(`
SELECT "command","path", "created" , "uuid", "exit_status", "system_name"
@ -460,9 +460,9 @@ func (cmd Command) commandGetUUID() Query {
AND "user_id" = $2`, cmd.Uuid, cmd.User.ID).Scan(&result.Command, &result.Path, &result.Created, &result.Uuid,
&result.ExitStatus, &result.SystemName)
if err != nil {
log.Println(err)
return Query{}, err
}
return result
return result, nil
}
func (cmd Command) commandDelete() int64 {

View file

@ -276,15 +276,19 @@ func Run() {
command.Query = c.Query("query")
command.SystemName = c.Query("systemName")
result := command.commandGet()
if len(result) == 0 {
c.JSON(http.StatusOK, gin.H{})
result, err := command.commandGet()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.IndentedJSON(http.StatusOK, result)
} else {
command.Uuid = c.Param("path")
result := command.commandGetUUID()
result, err := command.commandGetUUID()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
result.Username = user.Username
c.IndentedJSON(http.StatusOK, result)
}