Esempio n. 1
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
}
Esempio n. 2
0
func DeleteComment(cid, tid string) error {
	commentId, err := strconv.ParseInt(cid, 10, 64)
	if err != nil {
		return err
	}
	orm := orm.NewOrm()
	comment := &Comment{Id: commentId}
	_, err = orm.Delete(comment)
	if err != nil {
		return err
	}
	topicId, err := strconv.ParseInt(tid, 10, 64)
	if err != nil {
		return err
	}
	comments := make([]*Comment, 0)
	_, err = orm.QueryTable("comment").Filter("tid", topicId).OrderBy("-replyTime").All(&comments)
	if err != nil {
		return err
	}
	topic := &Topic{Id: topicId}
	if orm.Read(topic) == nil && len(comments) > 0 {
		topic.ReplyCount--
		topic.LastReplyTime = comments[0].ReplyTime
		// TODO set last user id
		// topic.ReplyLastUserId = comments[0].UserId
		orm.Update(topic)
	}
	return err
}
Esempio n. 3
0
func UpdateUserProfile(id int64, update *UserProfile) (*UserProfile, error) {
	if obj, err := GetUserProfile(id); err == nil {
		if update.Identity != "" {
			obj.Identity = update.Identity
		}
		if update.RealName != "" {
			obj.RealName = update.RealName
		}
		if update.Gender != 0 {
			obj.Gender = update.Gender
		}
		if update.Email != "" {
			obj.Email = update.Email
		}
		if update.Address != "" {
			obj.Address = update.Address
		}
		if update.Phone != "" {
			obj.Phone = update.Phone
		}
		if update.SimNumber != "" {
			obj.SimNumber = update.SimNumber
		}
		obj.ModifyOn = time.Now()

		orm := GetOrm()
		_, err := orm.Update(obj)
		if err != nil {
			return nil, err
		}
		return obj, nil
	} else {
		return nil, err
	}
}
Esempio n. 4
0
func UpdateTerminalCarrier(id int64, update *TerminalCarrier) (*TerminalCarrier, error) {
	if obj, err := GetTerminalCarrier(id); err == nil {
		if update.LicensePlateNumber != "" {
			obj.LicensePlateNumber = update.LicensePlateNumber
		}
		if update.VehicleIdentificationNumber != "" {
			obj.VehicleIdentificationNumber = update.VehicleIdentificationNumber
		}
		if update.CarrierType != "" {
			obj.CarrierType = update.CarrierType
		}
		if update.Brand != "" {
			obj.Brand = update.Brand
		}
		if update.Color != "" {
			obj.Color = update.Color
		}
		if update.Picture != "" {
			obj.Picture = update.Picture
		}
		obj.ModifyOn = time.Now()
		orm := GetOrm()
		_, err := orm.Update(obj)
		if err != nil {
			return nil, err
		}
		return obj, nil
	} else {
		return nil, err
	}
}
Esempio n. 5
0
func UpdateUser(id int64, update *User) (*User, error) {
	if obj, err := GetUser(id); err == nil {
		if update.UserName != "" {
			obj.UserName = update.UserName
		}
		if update.Password != "" {
			obj.Password = update.Password
		}
		if update.UserProfile != nil && obj.UserProfile != nil {
			if profile, err := UpdateUserProfile(id, update.UserProfile); err == nil {
				obj.UserProfile = profile
			}
		}
		obj.ModifyOn = time.Now()

		orm := GetOrm()
		_, err := orm.Update(obj)
		if err != nil {
			return nil, err
		}
		return obj, nil
	} else {
		return nil, err
	}
}
Esempio n. 6
0
func IncreaseTagCount(name string) error {
	orm := orm.NewOrm()
	tag := &Tag{Name: name}
	// 没有主键,无法使用Read方法
	err := orm.QueryTable("tag").Filter("name", name).One(tag)
	if err != nil {
		return err
	}
	tag.Count++
	_, err = orm.Update(tag)
	return err
}
Esempio n. 7
0
func UpdateMessage(id int64, update *Message) (*Message, error) {
	if obj, err := GetMessage(id); err == nil {
		if update.FeedBack != "" {
			obj.FeedBack = update.FeedBack
			obj.FeedBackOn = time.Now()
		}
		orm := GetOrm()
		_, err := orm.Update(obj)
		if err != nil {
			return nil, err
		}
		return obj, nil
	} else {
		return nil, err
	}
}
Esempio n. 8
0
func GetTopic(id string) (*Topic, error) {
	tid, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		return nil, err
	}
	orm := orm.NewOrm()

	topic := &Topic{
		Id: tid,
	}
	err = orm.QueryTable("topic").Filter("id", tid).One(topic)
	if err != nil {
		return nil, err
	}
	// update views
	topic.Views++
	orm.Update(topic)
	topic.Tags = strings.Replace(strings.Replace(topic.Tags, "#", " ", -1), "$", "", -1)
	return topic, nil
}
Esempio n. 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
}
Esempio n. 10
0
func DecreaseTagCount(name string) error {
	orm := orm.NewOrm()
	querySelector := orm.QueryTable("tag").Filter("name", name)
	count, err := querySelector.Count()
	if err != nil {
		return err
	}
	if count > 0 {
		tag := &Tag{Name: name}
		err = querySelector.One(tag)
		if err != nil {
			return err
		}
		tag.Count--
		if tag.Count > 0 {
			_, err = orm.Update(tag)
		} else {
			_, err = orm.Delete(tag)
		}
		return err
	}
	return nil
}
Esempio n. 11
0
func UpdateGroupProfile(id int64, update *GroupProfile) (*GroupProfile, error) {
	if obj, err := GetGroupProfile(id); err == nil {
		if update.GroupRealName != "" {
			obj.GroupRealName = update.GroupRealName
		}
		if update.ContactName != "" {
			obj.ContactName = update.ContactName
		}
		if update.ContactPhone != "" {
			obj.ContactPhone = update.ContactPhone
		}
		obj.ModifyOn = time.Now()

		orm := GetOrm()
		_, err := orm.Update(obj)
		if err != nil {
			return nil, err
		}
		return obj, nil
	} else {
		return nil, err
	}
}
Esempio n. 12
0
func UpdateTerminalProfile(id int64, update *TerminalProfile) (*TerminalProfile, error) {
	if obj, err := GetTerminalProfile(id); err == nil {
		if update.TerminalSn != "" {
			obj.TerminalSn = update.TerminalSn
		}
		if update.Tmsisdn != "" {
			obj.Tmsisdn = update.Tmsisdn
		}
		if update.Pmsisdn != "" {
			obj.Pmsisdn = update.Pmsisdn
		}
		if update.Imsi != "" {
			obj.Imsi = update.Imsi
		}
		if update.Imei != "" {
			obj.Imei = update.Imei
		}
		if update.ProductCode != "" {
			obj.ProductCode = update.ProductCode
		}
		if update.IsActivated != 0 {
			obj.IsActivated = update.IsActivated
		}
		if update.Mileage != 0 {
			obj.Mileage = update.Mileage
		}
		obj.ModifyOn = time.Now()
		orm := GetOrm()
		_, err := orm.Update(obj)
		if err != nil {
			return nil, err
		}
		return obj, nil
	} else {
		return nil, err
	}
}
Esempio n. 13
0
func UpdateTerminal(id int64, update *Terminal) (*Terminal, error) {
	if obj, err := GetTerminal(id); err == nil {
		if update.TerminalSn != "" {
			obj.TerminalSn = update.TerminalSn
		}
		if update.Password != "" {
			obj.Password = update.Password
		}
		if update.User != nil {
			obj.User.Id = update.User.Id
		}
		if update.Group != nil {
			obj.Group.Id = update.Group.Id
		}
		if update.TerminalProfile != nil && obj.TerminalProfile != nil {
			if profile, err := UpdateTerminalProfile(id, update.TerminalProfile); err == nil {
				obj.TerminalProfile = profile
			}
		}
		if update.TerminalCarrier != nil && obj.TerminalCarrier != nil {
			if carrier, err := UpdateTerminalCarrier(id, update.TerminalCarrier); err == nil {
				obj.TerminalCarrier = carrier
			}
		}
		obj.ModifyOn = time.Now()

		orm := GetOrm()
		_, err := orm.Update(obj)
		if err != nil {
			return nil, err
		}
		return obj, nil
	} else {
		return nil, err
	}
}
Esempio n. 14
0
func UpdateTopic(id, title, content, tags, cid, attachName string, attached bool) error {
	tid, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		return err
	}
	categoryId, err := strconv.ParseInt(cid, 10, 64)
	if err != nil {
		return err
	}

	orm := orm.NewOrm()
	category := &Category{Id: categoryId}
	err = orm.QueryTable("category").Filter("id", categoryId).One(category)
	if err != nil {
		return err
	}
	topic := &Topic{Id: tid}
	tagArray := strings.Split(strings.Trim(tags, " "), " ")
	tags = "$" + strings.Join(tagArray, "#$") + "#"
	var oldTags string
	var oldTitle string
	var oldAttachName string
	// valida exist and modify
	err = orm.Read(topic)
	if err != nil {
		return err
	} else {
		oldTitle = topic.Category
		oldTags = topic.Tags
		oldAttachName = topic.Attachment

		topic.Title = title
		topic.Content = content
		topic.Tags = tags
		if attached {
			topic.Attachment = attachName
		}
		topic.UpdateTime = CurrentTime()
		topic.Category = category.Title
		_, err = orm.Update(topic)
		if err != nil {
			return err
		}
	}

	// delete old file
	if attached {
		oldAttachName = path.Join("attachment", oldAttachName)
		if _, err := os.Stat(oldAttachName); err == nil {
			os.Remove(oldAttachName)
		}
	}
	// update category's topicCount
	if oldTitle != category.Title {
		// new category plus one
		category.TopicCount++
		_, err = orm.Update(category)
		if err != nil {
			return err
		}
		// TODO 如果旧的分类被删除,则报错,错误信息是:<QuerySeter> no row found
		count, err := orm.QueryTable("category").Filter("title", oldTitle).Count()
		if err != nil {
			return err
		}
		// 分类存在,则分类数目减一
		if count == 1 {
			oldCategory := &Category{Title: oldTitle}
			err = orm.QueryTable("category").Filter("title", oldTitle).One(oldCategory)
			if err != nil {
				return err
			}
			oldCategory.TopicCount--
			if oldCategory.TopicCount >= 0 {
				_, err = orm.Update(oldCategory)
				if err != nil {
					return err
				}
			}
		}
	}
	oldTags = strings.Replace(strings.Replace(oldTags, "#", " ", -1), "$", "", -1)
	oldTagArray := strings.Split(strings.Trim(oldTags, " "), " ")
	return ModifyTags(commons.Diff(tagArray, oldTagArray))
}