-
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
Showing
75 changed files
with
9,534 additions
and
19 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
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 |
---|---|---|
@@ -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(¶ms) | ||
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)) | ||
} |
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 |
---|---|---|
@@ -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(¶ms) | ||
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{}{}) | ||
} |
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 |
---|---|---|
@@ -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(¶ms) | ||
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)) | ||
|
||
} |
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 |
---|---|---|
@@ -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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.