Ejemplo n.º 1
0
func (c *ExerciseAction) Edit() error {
	if c.Method() == "GET" {
		if c.Id > 0 {
			has, err := c.Orm.Id(c.Id).Get(&c.Exercise)
			if err != nil {
				return err
			}
			if has {
				if c.Exercise.Creator.Id != c.GetLoginUserId() {
					return c.Go("root")
				}
				recentExercises := make([]Question, 0)
				err := c.Orm.Desc("created").Limit(5).Find(&recentExercises)
				if err == nil {
					return c.Render("exercise/edit.html", &xweb.T{
						"exercises": &recentExercises,
					})
				}
				return err
			}
		}

		return errors.New("参数错误")
	} else if c.Method() == "POST" {
		_, err := c.Orm.Id(c.Exercise.Id).Update(&c.Exercise)
		if err == nil {
			return c.Render("exercise/editok.html")
		}
		return err
	}
	return xweb.NotSupported()
}
Ejemplo n.º 2
0
func (c *ExerciseAction) Sub() error {
	if c.Method() == "GET" {
		return c.Render("exercise/sub.html")
	} else if c.Method() == "POST" {
		if c.Answer.Id == 0 {
			session := c.Orm.NewSession()
			defer session.Close()
			c.Answer.Creator.Id = c.GetLoginUserId()
			err := session.Begin()
			if err == nil {
				_, err = session.Insert(&c.Answer)
				if err == nil {
					user := new(User)
					has, err := session.Id(c.GetLoginUserId()).Get(user)
					if err == nil {
						if has {
							_, err = session.Table(user).Id(c.GetLoginUserId()).Update(map[string]interface{}{"num_exercises": user.NumExercises + 1})
						} else {
							err = errors.New("user is not exist.")
						}
					}
				}
				if err == nil {
					question := new(Question)
					has, err := session.Id(c.Id).Get(question)
					if err == nil {
						if has {
							_, err = session.Table(question).Id(c.Id).Update(map[string]interface{}{"num_answers": question.NumAnswers + 1})
						} else {
							err = errors.New("question is not exist.")
						}
					}
				}
			}
			if err == nil {
				err = session.Commit()
			} else {
				session.Rollback()
			}

			if err == nil {
				return c.Render("exercise/subok.html")
			}
			return err
		} else {
			_, err := c.Orm.Id(c.Answer.Id).Update(&c.Answer)
			if err == nil {
				return c.Render("exercise/subok.html")
			}
			return err
		}
	}
	return xweb.NotSupported()
}
Ejemplo n.º 3
0
func (c *QuestionAction) Answer() error {
	if c.Method() == "GET" {
		return c.Render("question/answer.html")
	} else if c.Method() == "POST" {
		_, err := c.Orm.Insert(&c.TheAnswer)
		if err == nil {
			return c.Render("question/answerok.html")
		}
		return err
	}
	return xweb.NotSupported()
}
Ejemplo n.º 4
0
func (c *QuestionAction) Ask() error {
	if c.Method() == "GET" {
		return c.Render("question/ask.html")
	} else if c.Method() == "POST" {
		c.TheQuestion.LastUpdated = time.Now()
		_, err := c.Orm.Insert(&c.TheQuestion)
		if err == nil {
			return c.Render("question/askok.html")
		}
		return err
	}
	return xweb.NotSupported()
}
Ejemplo n.º 5
0
func (c *HomeAction) Register() error {
	if c.Method() == "GET" {
		return c.Render("register.html")
	} else if c.Method() == "POST" {
		if len(c.User.LoginName) <= 0 {
			return c.Go("register?message=登录名不能为空")
		}
		if len(c.User.Email) <= 0 {
			return c.Go("register?message=email地址不能为空")
		}
		isEmail, err := regexp.MatchString("\\w*@\\w*.\\w*", c.User.Email)
		if err != nil {
			return err
		}
		if !isEmail {
			return c.Go("register?message=email地址不正确")
		}
		if len(c.User.Password) <= 0 {
			return c.Go("register?message=密码不能为空")
		}
		if c.RePassword != c.User.Password {
			return c.Go("register?message=两次密码不匹配")
		}
		u := &User{}
		has, err := c.Orm.Sql("select * from user where login_name=? or email =?",
			c.User.LoginName, c.User.Email).Get(u)
		if has {
			return c.Go("register?message=登录名或者email地址重复")
		}
		if err != nil {
			return err
		}

		c.User.EncodePasswd()
		c.User.BuildAvatar()
		_, err = c.Orm.Insert(&c.User)
		if err == nil {
			return c.Render("registerok.html")
		}
		return err
	}
	return xweb.NotSupported()
}
Ejemplo n.º 6
0
func (c *UserAction) ChangePass() error {
	if c.Method() == "GET" {
		return c.Render("user/pass.html")
	} else if c.Method() == "POST" {
		if c.Password != c.Repassword {
			c.Message = "两次输入密码不匹配"
			return c.Render("user/pass.html")
		}

		user := &User{Password: c.Password}
		err := user.EncodePasswd()
		if err == nil {
			_, err = c.Orm.Id(c.GetLoginUserId()).Update(user)
			if err == nil {
				return c.Render("user/passok.html")
			}
		}
		return err
	}
	return xweb.NotSupported()
}
Ejemplo n.º 7
0
func (c *HomeAction) Login() error {
	if c.Method() == "GET" {
		return c.Render("login.html")
	} else if c.Method() == "POST" {
		if len(c.User.LoginName) <= 0 {
			return c.Go("login?message=登录名不能为空")
		}
		c.User.EncodePasswd()
		has, err := c.Orm.Get(&c.User)
		if err == nil {
			if has {
				session := c.Session()
				session.Set(USER_ID_TAG, c.User.Id)
				session.Set(USER_NAME_TAG, c.User.UserName)
				session.Set(USER_AVATAR_TAG, c.User.Avatar)
				return c.Go("root")
			}
			return c.Go("login?message=账号或密码错误")
		}
		return err
	}
	return xweb.NotSupported()
}
Ejemplo n.º 8
0
func (c *ExerciseAction) Add() error {
	if c.Method() == "GET" {
		recentExercises := make([]Question, 0)
		err := c.Orm.Desc("created").Limit(5).Find(&recentExercises)
		if err == nil {
			return c.Render("exercise/add.html", &xweb.T{
				"exercises": &recentExercises,
			})
		}
		return err
	} else if c.Method() == "POST" {
		c.Exercise.Creator.Id = c.GetLoginUserId()
		c.Exercise.Type = EXERCISE_MODULE
		session := c.Orm.NewSession()
		defer session.Close()
		err := session.Begin()
		if err == nil {
			_, err = session.Insert(&c.Exercise)
			if err == nil {
				_, err = session.Exec("update user set num_questions=num_questions+1 where id = ?", c.GetLoginUserId())
				session.Engine.ClearCacheBean(new(User), c.GetLoginUserId())
				if err == nil {
					err = session.Commit()
				}
				if err == nil {
					return c.Render("exercise/addok.html")
				}
			}
		}
		if err != nil {
			session.Rollback()
		}
		return err
	}
	return xweb.NotSupported()
}