Exemplo n.º 1
0
// @router /create [post]
func (c *MetaController) Create() {
	var (
		types = strings.TrimSpace(c.GetString("type")) // tag:标签  category:分类 keyword:关键字  vote: 赞
		value = strings.TrimSpace(c.GetString("value"))
		meta  models.Meta
	)

	res := &common.MetaCreateResponse{}
	defer func() {
		c.Data["json"] = res
		c.ServeJSON()
	}()

	if types == "" || value == "" {
		res.Code = 1
		res.Msg = "type or value not empty"
		return
	}

	meta.Value = value
	meta.Type = types
	id, _ := c.GetInt64("id")
	if id > 0 {
		meta.Id = id
		if meta.Read() == nil {
			meta.Update("Type", "Value")
		} else {
			meta.Add()
		}
	} else {
		meta.Add()
	}
	res.Code = 0
	res.Msg = "success"
	res.MetaId = meta.Id
	return
}
Exemplo n.º 2
0
// @router /create [post]
func (c *PostController) Create() {
	var (
		id       int64
		title    = strings.TrimSpace(c.GetString("title"))
		subTitle = c.GetString("subTitle")
		content  = c.GetString("content")
		desc     = strings.TrimSpace(c.GetString("description"))
		tags     = strings.TrimSpace(c.GetString("tags"))
		types    int64
		status   int64
		coverId  int64
		post     models.Post
	)
	res := &common.PostCreateResponse{}
	defer func() {
		c.Data["json"] = res
		c.ServeJSON()
	}()

	// filter
	if title == "" {
		res.Code = 1
		res.Msg = "no title"
		return
	}
	o := orm.NewOrm()
	id, _ = c.GetInt64("id")
	status, _ = c.GetInt64("status")
	coverId, _ = c.GetInt64("coverId")
	types, _ = c.GetInt64("type")
	// deal tags
	var addtags []string
	if tags != "" {
		tagarr := strings.Split(tags, ",")
		for _, v := range tagarr {
			if tag := strings.TrimSpace(v); tag != "" {
				exists := false
				for _, vv := range addtags {
					if vv == tag {
						exists = true
						break
					}
				}
				if !exists {
					addtags = append(addtags, tag)
				}
			}
		}
	}

	// TODO check post is exits
	if id > 0 {
		post.Id = id
		if post.Read() != nil {
			fmt.Println("post not exist, will create one")
			post.Id = 0
		}
	}
	post.UserId = c.userid
	post.Status = status
	post.Title = title
	post.SubTitle = subTitle
	post.CoverId = coverId
	post.Description = desc
	post.Content = content
	post.Type = types
	if id < 1 { // add
		if _, err := post.Add(); err == nil {
			res.Code = 0
			res.Msg = "create post success"
			res.PostId = post.Id
		} else {
			res.Code = 2
			res.Msg = "create post error"
			return
		}
	} else { // update
		if post.Update("createdate", "updatedate", "title",
			"content", "subtitle", "description",
			"status", "userid", "type", "coverId") == nil {
			res.Code = 0
			res.Msg = "update post success"
			res.PostId = post.Id
		} else {
			res.Code = 2
			res.Msg = "update post error"
			return
		}
	}

	// update or create type meta
	if created, id, err := o.ReadOrCreate(&models.Postmeta{MetaId: types, PostId: post.Id}, "MetaId", "PostId"); err == nil {
		if created {
			fmt.Println("New Insert an object. Id:", id)
		} else {
			fmt.Println("Get an object. Id:", id)
		}
	}

	// TODO update post meta of tags
	if len(addtags) > 0 {
		for _, v := range addtags {
			meta := models.Meta{Value: v, Type: "tag"}
			if meta.Read("Value") != nil {
				meta.Count = 1
				meta.Add()
			} else {
				meta.Count++
				meta.Update("Count")
			}
			postMeta := models.Postmeta{MetaId: meta.Id, PostId: post.Id}
			// created-> is new
			// id-> the id
			// err-> error
			if created, id, err := o.ReadOrCreate(&postMeta, "MetaId", "PostId"); err == nil {
				if created {
					fmt.Println("New Insert an object. Id:", id)
				} else {
					fmt.Println("Get an object. Id:", id)
				}
			}
		}
	}
	return
}