Skip to content

Commit

Permalink
Merge pull request #7 from jvmistica/feat/import-records
Browse files Browse the repository at this point in the history
feat: implement import functionality
  • Loading branch information
jvmistica committed Jun 2, 2023
2 parents 8507992 + e29c3b7 commit 2186f3b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
34 changes: 31 additions & 3 deletions pkg/record/bot.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package record

import (
"encoding/csv"
"fmt"
"net/http"

"github.com/go-telegram-bot-api/telegram-bot-api"
)

var (
currentMsg string
prevMsg string
msg tgbotapi.MessageConfig
currentMsg string
currentFile [][]string
prevMsg string
msg tgbotapi.MessageConfig
)

// Listen listens to the messages send to the bot and sends the appropriate response
Expand All @@ -19,6 +22,23 @@ func (r *RecordDB) Listen(updates tgbotapi.UpdatesChannel, bot *tgbotapi.BotAPI)
continue
}

if update.Message.Document != nil {
url, _ := bot.GetFileDirectURL(update.Message.Document.FileID)
// handle error

file, _ := http.Get(url)
// handle error
defer file.Body.Close()

csvReader := csv.NewReader(file.Body)
data, _ := csvReader.ReadAll()
// handle error

currentFile = data
msg = r.processMessage(prevMsg, update.Message.Chat.ID)

}

// Read texts sent to the bot
if update.Message.Text != "" {
currentMsg = update.Message.Text
Expand Down Expand Up @@ -57,6 +77,14 @@ func (r *RecordDB) processMessage(prevMsg string, chatID int64) tgbotapi.Message
return tgbotapi.NewMessage(chatID, result)
}

if prevMsg == "/importitems" {
result, err := r.Import(currentFile)
if err != nil {
return tgbotapi.NewMessage(chatID, fmt.Sprintf("%s", err))
}
return tgbotapi.NewMessage(chatID, result)
}

cmds, err := r.CheckCommand(currentMsg)
if err != nil {
return tgbotapi.NewMessage(chatID, fmt.Sprintf("%s", err))
Expand Down
11 changes: 6 additions & 5 deletions pkg/record/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (

// defaultResponse contains default responses when parameters are not given
var defaultResponse map[string]string = map[string]string{
"/start": ResponseStart,
"/showitem": ResponseShow,
"/additem": ResponseAdd,
"/updateitem": ResponseUpdate,
"/deleteitem": ResponseDelete,
"/start": ResponseStart,
"/showitem": ResponseShow,
"/additem": ResponseAdd,
"/updateitem": ResponseUpdate,
"/deleteitem": ResponseDelete,
"/importitems": ResponseImport,
}

// CheckCommand checks if the command is valid and
Expand Down
23 changes: 4 additions & 19 deletions pkg/record/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,29 +188,14 @@ func (r *RecordDB) Import(records [][]string) (string, error) {

// ImportRecords imports a list of records into the "item" table
func (r *RecordDB) ImportRecords(records [][]string) (string, error) {
for _, row := range records {
amount, err := strconv.ParseFloat(row[2], 32)
for i := 1; i < len(records); i++ {
row := records[i]
price, err := strconv.ParseFloat(row[2], 32)
if err != nil {
return "", err
}

calories, err := strconv.Atoi(row[4])
if err != nil {
return "", err
}

price, err := strconv.ParseFloat(row[6], 32)
if err != nil {
return "", err
}

expiration, err := time.Parse(defaultTimeFormat, row[8])
if err != nil {
return "", err
}

rec := Item{Name: row[0], Description: row[1], Amount: float32(amount), Unit: row[3],
Calories: uint16(calories), Category: row[5], Price: float32(price), Currency: row[7], Expiration: expiration}
rec := Item{Name: row[0], Description: row[1], Price: float32(price)}
if err := r.DB.Create(&rec); err.Error != nil {
return "", err.Error
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/record/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ var (
"/showitem - show an item's details\n" +
"/additem - add an item\n" +
"/updateitem - update an item\n" +
"/deleteitem - delete an item\n"
"/deleteitem - delete an item\n" +
"/importitems - import items from a CSV file\n"

ResponseInvalid = "That's not a valid command. Here's a list of valid commands:\n\n" +
listItems +
Expand Down Expand Up @@ -56,4 +57,6 @@ var (
ResponseItemNotExist = "Item \"<item>\" does not exist in the inventory."
ResponseNoMatchFilter = "There are no items matching that filter."
ResponseNoItems = "There are no items in your inventory."

ResponseImport = "Please attach the CSV file."
)

0 comments on commit 2186f3b

Please sign in to comment.