示例#1
0
func (cmd selectItem) ExecuteWaiting(message tgbotapi.Message) error {
	cmd.ReleaseWaiting(message.From.ID)

	user := database.User{}
	err := user.Load(message.From.ID)
	if err != nil && err != sql.ErrNoRows {
		return err
	} else if err == sql.ErrNoRows {
		if err = user.Init(message.From); err != nil {
			return err
		}
	}

	var selection database.Selection
	err = database.DB.Get(&selection, `
		select
			*
		from
			selection
		where
			chat_id = $1 and
			active = 1
	`, message.Chat.ID)
	if err != nil {
		return err
	}

	var item database.SelectionItem
	err = database.DB.Get(&item, `
		select
			*
		from
			selection_item
		where
			item = $1 and
			selection_id = $2
	`, message.Text, selection.ID)
	if err != nil {
		return err
	}

	_, err = database.DB.Exec(`
		delete from
			selection_vote
		where
			user_id = $1 and
			selection_id = $2
	`, user.ID, selection.ID)
	if err != nil {
		return err
	}

	_, err = database.NewSelectionVote(user.ID, selection.ID, item.ID)
	if err != nil {
		return err
	}

	return cmd.QuickReply(message, "Added selection!")
}
示例#2
0
func (cmd createSelection) ExecuteWaiting(message tgbotapi.Message) error {
	cmd.ReleaseWaiting(message.From.ID)

	user := database.User{}
	err := user.Load(message.From.ID)
	if err != nil && err != sql.ErrNoRows {
		return err
	} else if err == sql.ErrNoRows {
		if err = user.Init(message.From); err != nil {
			return err
		}
	}

	items := strings.Split(message.Text, "\n")

	selection, err := database.NewSelection(user.ID, message.Chat.ID)
	if err != nil {
		return err
	}

	num := 0

	for _, item := range items {
		if item[0] == '!' {
			_, err = database.DB.Exec(`
				update selection
					set title = $1
					where id = $2
			`, item[1:], selection.ID)
			if err != nil {
				return err
			}

			continue
		}

		_, err := database.NewSelectionItem(selection.ID, item)
		if err != nil {
			return err
		}

		num++
	}

	return cmd.QuickReply(message, "Added "+strconv.Itoa(num)+" items to selection!")
}