Skip to content

Commit

Permalink
Merge pull request #7 from itishrishikesh/auth
Browse files Browse the repository at this point in the history
Added feed follow handlers and Aggreation logic for Posts
  • Loading branch information
itishrishikesh authored Dec 11, 2023
2 parents 8030851 + 8068e34 commit eb5bd39
Show file tree
Hide file tree
Showing 15 changed files with 519 additions and 13 deletions.
28 changes: 28 additions & 0 deletions feed_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"time"

"github.com/go-chi/chi"
"github.com/google/uuid"
"github.com/itishrishikesh/gofeed/constants"
"github.com/itishrishikesh/gofeed/internal/database"
Expand Down Expand Up @@ -75,3 +76,30 @@ func (config *apiConfig) createFeedFollowHandler(writer http.ResponseWriter, req
}
respondWithJSON(writer, constants.HTTP_CREATED, databaseFeedFollowToFeedFollow(feedFollow))
}

func (config *apiConfig) getFeedFollowsHandler(writer http.ResponseWriter, request *http.Request, user database.User) {
feedFollows, err := config.DB.GetFeedFollows(request.Context(), user.ID)
if err != nil {
respondWithError(writer, constants.HTTP_BAD_REQUEST, fmt.Sprintf("E#1OWRVV - Couldn't get feed follows %v", err))
return
}
respondWithJSON(writer, constants.HTTP_CREATED, databaseFeedFollowToFeedFollows(feedFollows))
}

func (config *apiConfig) deleteFeedHandler(writer http.ResponseWriter, request *http.Request, user database.User) {
feedFollowId := chi.URLParam(request, "feedFollowId")
feedFollowUUID, err := uuid.Parse(feedFollowId)
if err != nil {
respondWithError(writer, constants.HTTP_BAD_REQUEST, fmt.Sprintf("E#1OWSD0 - Feed follow ID isn't correct %v", err))
return
}
err = config.DB.DeleteFeedFollow(request.Context(), database.DeleteFeedFollowParams{
ID: feedFollowUUID,
UserID: user.ID,
})
if err != nil {
respondWithError(writer, constants.HTTP_BAD_REQUEST, fmt.Sprintf("E#1OWSG3 - Couldn't delete feed follow %v", err))
return
}
respondWithJSON(writer, constants.HTTP_SUCCESS, struct{}{})
}
66 changes: 64 additions & 2 deletions internal/database/feeds.sql.go

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

47 changes: 47 additions & 0 deletions internal/database/feeds_follow.sql.go

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

25 changes: 19 additions & 6 deletions internal/database/models.go

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

110 changes: 110 additions & 0 deletions internal/database/posts.sql.go

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

11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net/http"
"os"
"time"

"github.com/go-chi/chi"
"github.com/go-chi/cors"
Expand All @@ -19,7 +20,6 @@ type apiConfig struct {
}

func main() {

godotenv.Load()

portNumber := os.Getenv("PORT")
Expand All @@ -37,12 +37,14 @@ func main() {
log.Fatal("E#1ORHEU - Failed to open a connection to db")
}

queries := database.New(conn)
db := database.New(conn)

apiCfg := apiConfig{
DB: queries,
DB: db,
}

go startScraping(db, 10, time.Minute)

router := chi.NewRouter()

log.Println("I#1OLYVV - Server starting on port number", portNumber)
Expand All @@ -64,6 +66,9 @@ func main() {
v1Router.Post("/feeds", apiCfg.authMiddleware(apiCfg.createFeedHandler))
v1Router.Get("/feeds", apiCfg.getFeedsHandler)
v1Router.Post("/feedfollow", apiCfg.authMiddleware(apiCfg.createFeedFollowHandler))
v1Router.Get("/feedfollow", apiCfg.authMiddleware(apiCfg.getFeedFollowsHandler))
v1Router.Delete("/feedfollow/{feedFollowId}", apiCfg.authMiddleware(apiCfg.deleteFeedHandler))
v1Router.Get("/posts", apiCfg.authMiddleware(apiCfg.getPostsForUserHandler))
router.Mount("/v1", v1Router)

server := &http.Server{
Expand Down
Loading

0 comments on commit eb5bd39

Please sign in to comment.