func (self *respondtohabitController) Post(sn *session.Session) {
	habit := new(model.Habit)
	ret_val := returnVal{}
	if !sn.IsLogin {
		ret_val.Status = Fail
		ret_val.Content = RequireLogin
	} else {
		if err := form2Struct(sn.Request.Form, habit); err != nil {
			ret_val.Status = Fail
			ret_val.Content = err.Error()
		} else {
			habit.FriendId = sn.Cache.Values.User_id
			if habit.Approved {
				if err := habit.Accept(); err != nil {
					ret_val.Status = Fail
					ret_val.Content = err.Error()
				} else {
					ret_val.Status = Success
					ret_val.Content = habit.Content
				}
			} else {
				if err := habit.Decline(); err != nil {
					ret_val.Status = Fail
					ret_val.Content = err.Error()
				} else {
					ret_val.Status = Success
				}
			}
		}
	}
	self.Controller.Respond(sn.ResponseWriter, "json", ret_val, nil)
}
Esempio n. 2
0
func (self *newhabitController) Post(sn *session.Session) {
	habit := new(model.Habit)
	ret_val := returnVal{}
	if err := form2Struct(sn.Request.Form, habit); err != nil {
		ret_val.Status = Fail
		ret_val.Content = err.Error()
	} else {
		habit.UserId = sn.Cache.Values.User_id
		if habit.FriendId == 0 {
			habit.FriendId = habit.UserId
			habit.Approved = true
		}
		if err := habit.Create(); err != nil {
			ret_val.Status = Fail
			ret_val.Content = err.Error()
		} else {
			ret_val.Status = Success
			ret_val.Content = fmt.Sprintf("%d:%s:%d", habit.Id, habit.Content, habit.Progress)
		}
	}
	self.Controller.Respond(sn.ResponseWriter, "json", ret_val, nil)
}