Skip to content

Commit

Permalink
Add Feed Follows
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyyy09 committed Aug 3, 2023
1 parent 0792fb6 commit c3c3058
Show file tree
Hide file tree
Showing 75 changed files with 9,534 additions and 19 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ go 1.20
require (
github.com/go-chi/chi v1.5.4
github.com/go-chi/cors v1.2.1
github.com/google/uuid v1.3.0
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs=
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg=
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
48 changes: 48 additions & 0 deletions handler_feed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"encoding/json"
"net/http"
"time"
"github.com/google/uuid"
)

func (cfg *apiConfig) handlerFeedCreate(w http.ResponseWriter, r *http.Request, user database.User) {
type parameters struct {
Name string `json:"name"`
URL string `json:"url"`
}
decoder := json.NewDecoder(r.Body)
params := parameters{}
err := decoder.Decode(&params)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters")
return
}

feed, err := cfg.DB.CreateFeed(r.Context(), database.CreateFeedParams{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
UserID: user.ID,
Name: params.Name,
Url: params.URL,
})
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't create feed")
return
}


respondWithJSON(w, http.StatusOK, databaseFeedToFeed(feed))
}

func (cfg *apiConfig) handlerGetFeeds(w http.ResponseWriter, r *http.Request) {
feeds, err := cfg.DB.GetFeeds(r.Context())
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't get feeds")
return
}

respondWithJSON(w, http.StatusOK, databaseFeedsToFeeds(feeds))
}
66 changes: 66 additions & 0 deletions handler_feed_follows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"encoding/json"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/google/uuid"
)

func (cfg *apiConfig) handlerFeedFollowsGet(w http.ResponseWriter, r *http.Request, user database.User) {
feedFollows, err := cfg.DB.GetFeedFollowsForUser(r.Context(), user.ID)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't create feed follow")
return
}

respondWithJSON(w, http.StatusOK, databaseFeedFollowsToFeedFollows(feedFollows))
}

func (cfg *apiConfig) handlerFeedFollowCreate(w http.ResponseWriter, r *http.Request, user database.User) {
type parameters struct {
FeedID uuid.UUID
}
decoder := json.NewDecoder(r.Body)
params := parameters{}
err := decoder.Decode(&params)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters")
return
}

feedFollow, err := cfg.DB.CreateFeedFollow(r.Context(), database.CreateFeedFollowParams{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
UserID: user.ID,
FeedID: params.FeedID,
})
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't create feed follow")
return
}

respondWithJSON(w, http.StatusOK, databaseFeedFollowToFeedFollow(feedFollow))
}

func (cfg *apiConfig) handlerFeedFollowDelete(w http.ResponseWriter, r *http.Request, user database.User) {
feedFollowIDStr := chi.URLParam(r, "feedFollowID")
feedFollowID, err := uuid.Parse(feedFollowIDStr)
if err != nil {
respondWithError(w, http.StatusBadRequest, "Invalid feed follow ID")
return
}

err = cfg.DB.DeleteFeedFollow(r.Context(), database.DeleteFeedFollowParams{
UserID: user.ID,
ID: feedFollowID,
})
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't create feed follow")
return
}

respondWithJSON(w, http.StatusOK, struct{}{})
}
43 changes: 43 additions & 0 deletions handler_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"encoding/json"
"net/http"
"time"
"github.com/google/uuid"
)

func(apiCfg *apiConfig) handlerReadiness(w http.ResponseWriter, r *http.Request) {
type parameters struct {
Name string `json:"name"`
}

decoder := json.NewDecoder(r.Body)

params:= parameters{}
err:=decoder.Decode(&params)
if err!=nil{
respondWithError(w, http.StatusBadRequest, "Invalid request payload")
return
}

user,err:=apiCfg.DB.CreateUser(r.Context(), database.CreateUserParams{
ID:uuid.New(),
CreatedAt:time.Now().UTC(),
UpdatedAt:time.Now().UTC(),
Name:params.Name,
})

if err!=nil{
respondWithError(w, http.StatusInternalServerError, "Internal server error")
return
}

respondWithJSON(w, http.StatusOK, map[string]string{"status": "ok"})
}

func (apiCfg *apiConfig) handlerGetUser(w http.ResponseWriter, r *http.Request, user database.User) {

respondWithJSON(w, http.StatusOK, databaseUserToUser(user))

}
23 changes: 23 additions & 0 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package auth

import (
"errors"
"net/http"
"strings"
)

var ErrNoAuthHeaderIncluded = errors.New("no authorization header included")

// GetAPIKey -
func GetAPIKey(headers http.Header) (string, error) {
authHeader := headers.Get("Authorization")
if authHeader == "" {
return "", ErrNoAuthHeaderIncluded
}
splitAuth := strings.Split(authHeader, " ")
if len(splitAuth) < 2 || splitAuth[0] != "ApiKey" {
return "", errors.New("malformed authorization header")
}

return splitAuth[1], nil
}
31 changes: 31 additions & 0 deletions internal/database/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions internal/database/feed_follows.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c3c3058

Please sign in to comment.