// /contest/save
func (this *ContestController) SaveContest() {
	if !this.isAdmin() {
		return
	}
	contest := models.Contest{}
	contest.Name = this.GetString("name")
	contest.Description = this.GetString("description")
	_, _ = contest.Create()
	this.Redirect("/contest/"+contest.Name+"/addProblem", 302)
}
// /contest/:name/:pid/submit
func (this *ContestController) submit() {
	if !this.isLoggedIn() {
		this.Redirect("/user/login", 302)
		return
	}
	contestName := this.Ctx.Input.Param(":name")
	pid, err := strconv.Atoi(this.Ctx.Input.Param(":pid"))
	if err != nil {
		log.Println(err)
		this.Redirect("/", 302)
		return
	}
	contest := models.Contest{}
	contest.Name = contestName
	if err := contest.GetByName(); !err {
		this.Redirect("/", 302)
		return
	}
	currTime := time.Now()
	if contest.EndTime.Before(currTime) {
		this.Redirect("/contest/"+contestName, 302)
		return
	}
	uid := this.GetSession("id")
	code := this.GetString("code")
	lang := this.GetString("language")

	problemLog := models.Problemlogs{}
	problemLog.Uid = uid.(int)
	problemLog.Pid = pid
	problemLog.GetByPidUid()

	output := models.SubmitUpdateScore(uid.(int), pid, code, lang)

	contestLog := models.Contestlogs{}
	contestLog.Uid = uid.(int)
	contestLog.Cid = contest.Id
	status := contestLog.GetByUidCid()
	if !status {
		contestLog.Points = output.Score
		contestLog.Add()
	} else {
		if output.Score > problemLog.Points {
			contestLog.Points += output.Score - problemLog.Points
			contestLog.Update()
		}
	}

	js, _ := json.Marshal(output)
	this.Data["json"] = string(js)
	this.ServeJson()

}
//Serve the contest page
// /contest/:name
func (this *ContestController) Contest() {

	contestName := this.Ctx.Input.Param(":name")
	contest := models.Contest{}
	contest.Name = contestName
	if err := contest.GetByName(); !err {
		this.Redirect("/", 302)
		return
	}
	currTime := time.Now()
	if contest.EndTime.Before(currTime) {
		this.Data["elapsed"] = true
	}
}