Exemple #1
0
func AddTerminal(obj Terminal) (int64, error) {
	var id int64
	var profileId int64
	var carrierId int64
	var err error
	orm := GetOrm()
	if err = orm.Begin(); err == nil {
		obj.CreateOn = time.Now()
		obj.ModifyOn = time.Now()
		obj.OfflineOn = time.Now()
		//profile
		if obj.TerminalProfile == nil {
			obj.TerminalProfile = &TerminalProfile{
				CreateOn: time.Now(),
				ModifyOn: time.Now(),
			}
		} else {
			obj.TerminalProfile.CreateOn = time.Now()
			obj.TerminalProfile.ModifyOn = time.Now()
		}

		if profileId, err = orm.Insert(obj.TerminalProfile); err == nil {
			obj.TerminalProfile.Id = profileId
		} else {
			orm.Rollback()
			return 0, err
		}
		//profile end

		//carrier
		if obj.TerminalCarrier == nil {
			obj.TerminalCarrier = &TerminalCarrier{
				CreateOn: time.Now(),
				ModifyOn: time.Now(),
			}
		} else {
			obj.TerminalCarrier.CreateOn = time.Now()
			obj.TerminalCarrier.ModifyOn = time.Now()
		}

		if carrierId, err = orm.Insert(obj.TerminalCarrier); err == nil {
			obj.TerminalCarrier.Id = carrierId
		} else {
			orm.Rollback()
			return 0, err
		}
		//carrier end

		if id, err = orm.Insert(&obj); err == nil {
			orm.Commit()
		} else {
			orm.Rollback()
			return 0, err
		}
	} else {
		return 0, err
	}

	return id, nil
}
Exemple #2
0
func AddUser(obj User) (int64, error) {
	var id int64
	var profileId int64
	var err error
	orm := GetOrm()
	if err = orm.Begin(); err == nil {
		obj.CreateOn = time.Now()
		obj.ModifyOn = time.Now()
		if obj.UserProfile == nil {
			obj.UserProfile = &UserProfile{
				CreateOn: time.Now(),
				ModifyOn: time.Now(),
			}
		} else {
			obj.UserProfile.CreateOn = time.Now()
			obj.UserProfile.ModifyOn = time.Now()
		}
		if profileId, err = orm.Insert(obj.UserProfile); err == nil {
			obj.UserProfile.Id = profileId
			if id, err = orm.Insert(&obj); err == nil {
				orm.Commit()
			} else {
				orm.Rollback()
				return 0, err
			}
		} else {
			orm.Rollback()
			return 0, err
		}
	} else {
		return 0, err
	}

	return id, nil
}
Exemple #3
0
/*
tid: 文章id
content:评论内容
*/
func AddComment(tid, content string) error {
	titleId, err := strconv.ParseInt(tid, 10, 64)
	if err != nil {
		return err
	}
	orm := orm.NewOrm()
	comment := &Comment{
		Tid:       titleId,
		Content:   content,
		ReplyTime: CurrentTime(),
	}
	_, err = orm.Insert(comment)
	if err != nil {
		return err
	}
	// 文章评论数增一
	topic := &Topic{
		Id: titleId,
	}
	if orm.Read(topic) == nil {
		topic.ReplyCount++
		topic.LastReplyTime = CurrentTime()
		// TODO set current user id
		// topic.ReplyLastUserId = current_user_id
		orm.Update(topic)
	}
	return err
}
func Save(article *entities.Article) error {
	var err error
	orm := orm.NewOrm()
	fmt.Printf("****", article)
	_, err = orm.Insert(article)
	return err
}
Exemple #5
0
func AddMessage(obj Message) (int64, error) {
	orm := GetOrm()
	id, err := orm.Insert(&obj)
	if err != nil {
		return -1, err
	}
	return id, nil
}
Exemple #6
0
func AddLog(obj Log) (int64, error) {
	obj.LogOn = time.Now()
	orm := GetOrm()
	id, err := orm.Insert(&obj)
	if err != nil {
		return -1, err
	}
	return id, nil
}
func SaveUser(user *entities.User) error {
	orm := orm.NewOrm()

	err := CheckNewUser(user)

	if nil != err {
		return err
	}

	user.CreatedAt = time.Now()

	id, err := orm.Insert(user)
	if nil == err {
		return err
	}

	user.Id = id
	return nil
}
Exemple #8
0
func AddCategory(title string) error {
	orm := orm.NewOrm()
	category := &Category{
		Title:      title,
		CreateTime: CurrentTime(),
		TopicTime:  CurrentTime(),
	}
	// query if duplicated
	err := orm.QueryTable("category").Filter("title", title).One(category)
	if err == nil {
		return err
	}

	// insert new one
	_, err = orm.Insert(category)
	if err != nil {
		return err
	}
	return nil
}
Exemple #9
0
func AddTopic(title, content, tags, cid, attachName string) error {
	orm := orm.NewOrm()
	categoryId, err := strconv.ParseInt(cid, 10, 64)
	if err != nil {
		return err
	}
	category := &Category{Id: categoryId}
	err = orm.QueryTable("category").Filter("id", categoryId).One(category)
	if err != nil {
		return err
	}
	// java scala linux => $java#$scala#$linux#
	tagArray := strings.Split(strings.Trim(tags, " "), " ")
	tags = "$" + strings.Join(tagArray, "#$") + "#"

	topic := &Topic{
		Title:         title,
		Content:       content,
		Tags:          tags,
		Attachment:    attachName,
		CreateTime:    CurrentTime(),
		UpdateTime:    CurrentTime(),
		LastReplyTime: CurrentTime(),
		Category:      category.Title,
	}
	// add one topic
	_, err = orm.Insert(topic)
	if err != nil {
		return err
	}
	// update category's count
	category.TopicCount++
	category.TopicTime = CurrentTime()
	_, err = orm.Update(category)
	if err != nil {
		return err
	}
	// insert or update tag
	InsertOrUpdateTags(tagArray)
	return err
}
func Save(article *entities.Article) error {
	var err error
	orm := orm.NewOrm()
	_, err = orm.Insert(article)
	return err
}
Exemple #11
0
func AddTag(name string) error {
	orm := orm.NewOrm()
	tag := &Tag{Name: name, Count: 1}
	_, err := orm.Insert(tag)
	return err
}