Exemple #1
0
func ApiProblems(w http.ResponseWriter, r *http.Request) {
	problems, err := models.GetAllProblems()
	if err != nil {
		log.WithFields(log.Fields{
			"error": err.Error(),
		}).Warn("Database error")

		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	JSONResponse(w, problems, http.StatusOK)
}
Exemple #2
0
func Problems(w http.ResponseWriter, r *http.Request) {
	problems, err := models.GetAllProblems()
	if err != nil {
		log.WithFields(log.Fields{
			"error": err.Error(),
		}).Warn("Database error")

		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	var problemsSolvedIds []int
	id, err := session.GetId(r)
	if err == nil {
		problemsSolvedIds, err = models.GetSolvedProblemsIdById(id)
		if err != nil {
			log.WithFields(log.Fields{
				"error": err.Error(),
			}).Warn("Database error")

			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}

	if problemsSolvedIds == nil {
		problemsSolvedIds = []int{}
	}

	params := struct {
		IsLogged          bool
		Problems          []models.Problem
		ProblemsSolvedIds []int
	}{IsLogged: session.IsLogged(r), Problems: problems, ProblemsSolvedIds: problemsSolvedIds}

	err = problemsTemplates.ExecuteTemplate(w, "problems.html", params)
	if err != nil {
		log.WithFields(log.Fields{
			"error": err.Error(),
		}).Warn("Template Error")
	}
}