Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
milwad-dev committed May 12, 2024
1 parent 960859d commit 0fd577c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/handlers/label_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ func (db *DBHandler) StoreLabel(w http.ResponseWriter, r *http.Request) {
data := make(map[string]string)
data["message"] = "The label store successfully."

utils.JsonResponse(w, data)
utils.JsonResponse(w, data, 200)
}
53 changes: 49 additions & 4 deletions internal/handlers/task_handler.go
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)
}
2 changes: 1 addition & 1 deletion internal/handlers/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ func (db *DBHandler) GetLatestUsers(w http.ResponseWriter, r *http.Request) {
users = append(users, user)
}

utils.JsonResponse(w, users)
utils.JsonResponse(w, users, 200)
}
4 changes: 2 additions & 2 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type User struct {
Email string `json:"email"`
Phone string `json:"phone"`
Password string `json:"password"`
EmailVerifiedAt time.Time `json:"emailVerified_at"`
PhoneVerifiedAt time.Time `json:"phoneVerified_at"`
EmailVerifiedAt time.Time `json:"emailVerified_at,omitempty"`
PhoneVerifiedAt time.Time `json:"phoneVerified_at,omitempty"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
Expand Down
8 changes: 7 additions & 1 deletion internal/utils/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import (
)

// JsonResponse => set json header and return json response
func JsonResponse(w http.ResponseWriter, data any) {
func JsonResponse(w http.ResponseWriter, data any, statusCode int) {
// Set headers
w.Header().Set("Content-Type", "application/json")

// Set status code of response
w.WriteHeader(statusCode)

// Return json response
json.NewEncoder(w).Encode(data)
}

0 comments on commit 0fd577c

Please sign in to comment.