Exemplo n.º 1
0
func (c *Topic) Edit() {
	id, _ := c.Request.Int64("id")
	topic := model.TopicModel.FindById(id)
	if topic == nil {
		panic("topic not found")
	}

	if c.Request.Method == "POST" {
		form := new(model.TopicForm)
		form.LoadData(c.Request)
		if l := len(form.Title); l == 0 || l > 255 {
			form.Message = "title length must between 1 - 255"
			goto RENDER
		}

		if len(form.Content) == 0 {
			form.Message = "content is empty"
			goto RENDER
		}

		topic.Title = form.Title
		topic.Content = form.Content
		topic.State = form.State
		topic.UpdatedAt = time.Now()

		if !model.TopicModel.Save(topic) {
			form.Message = "could not save to db"
		}
	}

RENDER:
	c.Render("admin/topic/edit", topic)
}
Exemplo n.º 2
0
func (c *Topic) New() {
	form := new(model.TopicForm)
	if c.Request.Method == "POST" {
		form.LoadData(c.Request)
		if l := len(form.Title); l == 0 || l > 255 {
			form.Message = "title length must between 1 - 255"
			goto RENDER
		}

		if len(form.Content) == 0 {
			form.Message = "content is empty"
			goto RENDER
		}

		now := time.Now()
		topic := new(model.Topic)
		topic.Title = form.Title
		topic.Content = form.Content
		topic.State = form.State
		topic.UpdatedAt = now
		topic.CreatedAt = now

		if model.TopicModel.Save(topic) {
			c.Redirect(fmt.Sprintf("/topic/%d", topic.Id), 302)
		}

		form.Message = "could not save to db"
	}

RENDER:
	c.Render("admin/topic/new", form)
}