socialpredict icon indicating copy to clipboard operation
socialpredict copied to clipboard

Functionalize Market Extraction from MarketDetailsHandler, use elsewhere in codebase

Open pwdel opened this issue 1 year ago • 0 comments

Turn market extraction portion of MarketDetailsHandler(w http.ResponseWriter, r *http.Request) into a function, find other locations in codebase where this is being used and enforce usage.

Ensure we're constructing a public response to keep data secure.

	// Use database connection
	db := util.GetDB()

	var market models.Market
	// Fetch the Market without preloading the Creator
	result := db.Where("ID = ?", marketId).First(&market)
	if result.Error != nil {
		if errors.Is(result.Error, gorm.ErrRecordNotFound) {
			http.Error(w, "Market not found", http.StatusNotFound)
		} else {
			http.Error(w, "Error fetching market", http.StatusInternalServerError)
		}
		return
	}

	// Construct ResponseMarket from the market model
	responseMarket := PublicResponseMarket{
		ID:                      market.ID,
		QuestionTitle:           market.QuestionTitle,
		Description:             market.Description,
		OutcomeType:             market.OutcomeType,
		ResolutionDateTime:      market.ResolutionDateTime,
		FinalResolutionDateTime: market.FinalResolutionDateTime,
		UTCOffset:               market.UTCOffset,
		IsResolved:              market.IsResolved,
		ResolutionResult:        market.ResolutionResult,
		InitialProbability:      market.InitialProbability,
		CreatorUsername:         market.CreatorUsername,
	}

pwdel avatar Feb 05 '24 19:02 pwdel