-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
960859d
commit 0fd577c
Showing
5 changed files
with
60 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,70 @@ | ||
package handlers | ||
|
||
import ( | ||
"github.com/milwad-dev/do-it/internal/models" | ||
"github.com/milwad-dev/do-it/internal/utils" | ||
"net/http" | ||
) | ||
|
||
func (db *DBHandler) GetLatestTasks(w http.ResponseWriter, r *http.Request) { | ||
data := make(map[string]string) | ||
data := make(map[string]interface{}) | ||
|
||
query := "SELECT * FROM tasks ORDER BY created_at DESC" | ||
query := `SELECT | ||
tasks.id, | ||
tasks.title, | ||
tasks.description, | ||
tasks.status, | ||
tasks.user_id, | ||
tasks.label_id, | ||
tasks.completed_at, | ||
tasks.created_at, | ||
users.id, | ||
users.name, | ||
users.email, | ||
users.created_at | ||
FROM tasks | ||
INNER JOIN users ON tasks.user_id = users.id | ||
` | ||
rows, err := db.Query(query) | ||
if err != nil { | ||
data["message"] = err.Error() | ||
utils.JsonResponse(w, data) | ||
data["status"] = "error" | ||
|
||
utils.JsonResponse(w, data, http.StatusInternalServerError) | ||
|
||
return | ||
} | ||
|
||
var tasks []models.Task | ||
|
||
for rows.Next() { | ||
var task models.Task | ||
var user models.User | ||
|
||
rows.Scan( | ||
&task.ID, | ||
&task.Title, | ||
&task.Description, | ||
&task.Status, | ||
&task.UserId, | ||
&task.LabelId, | ||
&task.CompletedAt, | ||
&task.CreatedAt, | ||
|
||
&user.ID, | ||
&user.Name, | ||
&user.Email, | ||
&user.CreatedAt, | ||
) | ||
|
||
task.User = user | ||
|
||
tasks = append(tasks, task) | ||
} | ||
|
||
utils.JsonResponse(w, data) | ||
data["data"] = tasks | ||
data["status"] = "success" | ||
|
||
utils.JsonResponse(w, data, 200) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters