Exemplo n.º 1
0
// Function to make a fresh request to the Stack Exchange API to return questions relating to set of ID's
// Initiates parameters required to make the request.
// The returning data is then sent back to the webui handler to be parsed into the page
func GetQuestions(ctx context.Context, ids []int) (*stackongo.Questions, error) {
	params := make(stackongo.Params)
	params.Pagesize(100)
	params.Sort("creation")
	params.AddVectorized("tagged", tags)

	questions, err := dataCollect.GetQuestionsByIDs(session, ids, appInfo, params)
	if err != nil {
		return nil, errors.New("Error collection new question by id\t" + err.Error())
	}
	return questions, nil
}
Exemplo n.º 2
0
// Remove any questions in db that does not exist in StackOverflow
func RemoveDeletedQuestions(db *sql.DB, ctx context.Context) error {
	defer UpdateTableTimes(db, ctx, "questions")

	// Get questions from db
	ids := []int{}
	rows, err := db.Query("SELECT question_id FROM questions")
	if err != nil {
		return err
	}
	var id int
	defer rows.Close()
	for rows.Next() {
		err := rows.Scan(&id)
		if err != nil {
			return err
		}
		ids = append(ids, id)
	}

	// Get the questions from StackExchange
	params := make(stackongo.Params)
	params.Pagesize(100)
	params.Sort("creation")

	questions, err := dataCollect.GetQuestionsByIDs(session, ids, appInfo, params)
	if err != nil {
		return err
	}

	// If the number of questions equal, no questions have been deleted
	if len(questions.Items) == len(ids) {
		return nil
	}

	// Find the questions not in SE
	// Add to deletedQns
	deletedQns := make([]int, 0, len(ids)-len(questions.Items))
	for _, id := range ids {
		deleted := true
		for _, question := range questions.Items {
			if question.Question_id == id {
				deleted = false
				break
			}
		}
		if deleted {
			deletedQns = append(deletedQns, id)
		}
	}

	// Remove deletedQns from db
	query := "DELETE FROM questions WHERE "
	for i, id := range deletedQns {
		query += "question_id=" + strconv.Itoa(id)
		if i < len(deletedQns)-1 {
			query += " OR "
		}
	}
	_, err = db.Exec(query)
	if err != nil {
		return err
	}

	// Remove tags associated with deleted questions
	_, err = db.Exec("DELETE FROM question_tag WHERE question_id NOT IN (SELECT questions.question_id FROM questions)")
	if err != nil {
		return err
	}

	return nil
}