fcc-learn-golang-assets icon indicating copy to clipboard operation
fcc-learn-golang-assets copied to clipboard

params.FeedID in handler_feed_follows.go is null on windows

Open onionkight opened this issue 7 months ago • 0 comments

I parse feed_id directly as uuid.UUID type in the JSON structure parameters. Although the uuid.UUID type can usually be correctly decoded by JSON tags, in some cases, Go's JSON decoder may not correctly decode the incoming string directly into uuid.UUID.

func (cfg *apiConfig) handlerFeedFollowCreate(w http.ResponseWriter, r *http.Request, user database.User) {
	type parameters struct {
		FeedID string `json:"feed_id"`
	}

	decoder := json.NewDecoder(r.Body)
	params := parameters{}
	err := decoder.Decode(&params)
	if err != nil {
		respondWithError(w, http.StatusBadRequest, "Invalid JSON format")
		return
	}

	feedUUID, err := uuid.Parse(params.FeedID)
	if err != nil {
		respondWithError(w, http.StatusBadRequest, "Invalid UUID format")
		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:    feedUUID,
	})
	if err != nil {
		respondWithError(w, http.StatusInternalServerError, "Couldn't create feed follow")
		return
	}

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

onionkight avatar Aug 03 '24 15:08 onionkight