示例#1
0
文件: home.go 项目: niko3oo/lencha
func Home(w http.ResponseWriter, r *http.Request) {

	params := struct {
		IsLogged bool
	}{IsLogged: session.IsLogged(r)}

	err := homeTemplates.ExecuteTemplate(w, "index.html", params)
	if err != nil {
		log.WithFields(log.Fields{
			"error": err.Error(),
		}).Warn("Template Error")
	}
}
示例#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")
	}
}
示例#3
0
func Problem(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	problemName := vars["problem"]

	problem, err := models.GetProblemByName(problemName)
	if err != nil {
		http.Error(w, "Not found", http.StatusInternalServerError)
		return
	}

	params := struct {
		IsLogged        bool
		Problem         models.Problem
		DescriptionHTML template.HTML
	}{IsLogged: session.IsLogged(r), Problem: problem, DescriptionHTML: template.HTML(problem.Description)}

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