Example #1
0
func (this *TopicController) Post() {
	if !checkAccount(this.Ctx) {
		this.Redirect(("/login"), 302)
		return
	}

	title := this.Input().Get("title")
	content := this.Input().Get("content")
	tid := this.Input().Get("tid")
	category := this.Input().Get("category")

	var err error
	if len(tid) == 0 {
		err = models.AddTopic(title, category, content)
	} else {
		err = models.UpdateTopic(tid, title, category, content)
	}

	if err != nil {
		beego.Error(err)
	}

	this.Redirect("/topic", 302)
}
Example #2
0
func (t *TopicController) Post() {
	if !checkAccount(t.Ctx) {
		t.TplNames = "login.html"
		return
	}
	op := t.Input().Get("op")
	switch op {
	case "add":
		title := t.Input().Get("title")
		content := t.Input().Get("content")
		category := t.Input().Get("category")
		_, fh, ferr := t.GetFile("attachment")
		if ferr != nil {
			beego.Error(ferr)
		}
		var attachment string
		if fh != nil {
			attachment = fh.Filename
			beego.Info(attachment)
			ferr = t.SaveToFile("attachment", path.Join("attachment", attachment))
			if ferr != nil {
				beego.Error(ferr)
			}
		}
		err := models.AddTopic(title, content, category, attachment)
		if err != nil {
			beego.Error(err)
		}
		t.Redirect("/topic", 301)
		return
	case "edit":
		return
	case "editsubmit":
		id := t.Input().Get("id")
		title := t.Input().Get("title")
		content := t.Input().Get("content")
		category := t.Input().Get("category")
		_, fh, ferr := t.GetFile("attachment")
		if ferr != nil {
			beego.Error(ferr)
		}
		var attachment string
		if fh != nil {
			attachment = fh.Filename
			fmt.Println("#############################################", attachment)
			beego.Info(attachment)
			ferr = t.SaveToFile("attachment", path.Join("attachment", attachment))
			if ferr != nil {
				beego.Error(ferr)
			}
		}
		err := models.UpdateTopic(id, title, content, category, attachment)
		if err != nil {
			beego.Error(err)
		}
		t.Redirect("/topic", 301)
		return
	default:
		t.Data["IsTopic"] = true
		t.TplNames = "topic.html"
		var err error
		t.Data["Topics"], err = models.GetAllTopic()
		if err != nil {
			beego.Error(nil)
		}
	}
}