diff --git a/chirpy b/chirpy deleted file mode 100755 index 1367632..0000000 Binary files a/chirpy and /dev/null differ diff --git a/internal/database/chirps.sql.go b/internal/database/chirps.sql.go index 5777ba2..3a50a66 100644 --- a/internal/database/chirps.sql.go +++ b/internal/database/chirps.sql.go @@ -101,3 +101,38 @@ func (q *Queries) GetChirpById(ctx context.Context, id uuid.UUID) (Chirp, error) ) return i, err } + +const getChirpsByAuthor = `-- name: GetChirpsByAuthor :many +SELECT id, created_at, updated_at, body, user_id FROM chirps +WHERE user_id = $1 +ORDER BY created_at ASC +` + +func (q *Queries) GetChirpsByAuthor(ctx context.Context, userID uuid.UUID) ([]Chirp, error) { + rows, err := q.db.QueryContext(ctx, getChirpsByAuthor, userID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Chirp + for rows.Next() { + var i Chirp + if err := rows.Scan( + &i.ID, + &i.CreatedAt, + &i.UpdatedAt, + &i.Body, + &i.UserID, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/main.go b/main.go index 5b29abf..a8adc96 100644 --- a/main.go +++ b/main.go @@ -289,8 +289,53 @@ func (s *server) HandleCreateUser(w http.ResponseWriter, r *http.Request) { }) } -func (s *server) HandleGetAllChirps(w http.ResponseWriter, r *http.Request) { - existingChirps, err := s.database.GetAllChirps(r.Context()) +const ( + Ascending = "asc" + Descending = "desc" +) + +func (s *server) HandleGetChirps(w http.ResponseWriter, r *http.Request) { + authorId := r.URL.Query().Get("author_id") + sortDir := r.URL.Query().Get("sort") + + hasAuthorId := strings.TrimSpace(authorId) != "" + hasSort := strings.TrimSpace(sortDir) != "" + + validAuthorId, err := uuid.Parse(authorId) + + if hasAuthorId && err != nil { + writeJsonResponse(w, http.StatusBadRequest, apiError{ + Err: "Author id must be a valid uuid", + }) + return + } + + if hasSort && sortDir != Ascending && sortDir != Descending { + writeJsonResponse(w, http.StatusBadRequest, apiError{ + Err: fmt.Sprintf("Sort direction must be either '%s' or '%s'", Ascending, Descending), + }) + return + } + + if !hasSort { + sortDir = "asc" + } + + var existingChirps []database.Chirp + + if hasAuthorId { + existingChirps, err = s.database.GetChirpsByAuthor(r.Context(), validAuthorId) + } else { + existingChirps, err = s.database.GetAllChirps(r.Context()) + } + + slices.SortFunc(existingChirps, func(a, b database.Chirp) int { + if sortDir == Descending { + return b.CreatedAt.Compare(a.CreatedAt) + } + + return a.CreatedAt.Compare(b.CreatedAt) + }) if err != nil { writeJsonResponse(w, http.StatusInternalServerError, apiError{ @@ -800,7 +845,7 @@ func main() { mux.HandleFunc("PUT /api/users", server.HandleUpdateUser) mux.HandleFunc("POST /api/polka/webhooks", server.HandleWebhooks) - mux.HandleFunc("GET /api/chirps", server.HandleGetAllChirps) + mux.HandleFunc("GET /api/chirps", server.HandleGetChirps) mux.HandleFunc("GET /api/chirps/{id}", server.HandleGetChirp) mux.HandleFunc("POST /api/chirps", server.HandleCreateChirp) mux.HandleFunc("DELETE /api/chirps/{id}", server.HandleDeleteChirp) diff --git a/sql/queries/chirps.sql b/sql/queries/chirps.sql index 4a0e0c3..4ad6f01 100644 --- a/sql/queries/chirps.sql +++ b/sql/queries/chirps.sql @@ -19,3 +19,8 @@ WHERE id = $1; -- name: DeleteChirpById :exec DELETE FROM chirps WHERE id = $1; + +-- name: GetChirpsByAuthor :many +SELECT id, created_at, updated_at, body, user_id FROM chirps +WHERE user_id = $1 +ORDER BY created_at ASC;