Skip to content

Commit 20601c5

Browse files
committed
Separated models to their own files
1 parent 3ec3eef commit 20601c5

File tree

9 files changed

+52
-28
lines changed

9 files changed

+52
-28
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
![GO](https://github.com/lundjrl/chef/actions/workflows/go.yml/badge.svg)
44

5-
65
<div>
76
<a href="https://github.com/lundjrl/chef">
87
<img alt="Screenshot of application home view." src="assets/home.png" width="400px">
@@ -22,12 +21,16 @@
2221

2322
## Screenshots
2423

24+
### Manage Your Home Inventory
25+
2526
<div>
2627
<a href="https://github.com/lundjrl/chef">
2728
<img alt="Screenshot of application inventory view." src="assets/inventory.png" width="400px">
2829
</a>
2930
</div>
3031

32+
### Edit Your Grocery List
33+
3134
## Running Tests
3235

3336
- `go test ./shared/tests -v`

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func newModel() mainModel {
2525
{Title: "Count", Width: 24},
2626
}
2727

28-
var items []db.GroceryItem
28+
var items []db.InventoryItem
2929
result := db.DBConn.Find(&items)
3030

3131
if result.Error != nil {
@@ -100,7 +100,7 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
100100
case "enter":
101101
if m.State == shared.InputView {
102102
item := m.TextInput.Value()
103-
db.CreateGroceryItem(item)
103+
db.CreateInventoryItem(item)
104104
rows := m.Table.Rows()
105105
id := len(m.Table.Rows()) + 1
106106
row := []string{fmt.Sprint(id), item, fmt.Sprint(1)}
@@ -115,7 +115,7 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
115115
id := selected[0]
116116
count := 2 // count should be a dynamic string
117117

118-
db.UpdateGroceryItem(id, count)
118+
db.UpdateInventoryItem(id, count)
119119
case "tab":
120120
switch m.State {
121121
case shared.TableView:

run

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/zsh
22

3-
go build && go run main.go
3+
go build && go run main.go init

shared/database/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ func InitDatabaseConnection() {
1717
}
1818
fmt.Println("Database connection started")
1919

20+
DBConn.AutoMigrate(&InventoryItem{})
2021
DBConn.AutoMigrate(&GroceryItem{})
21-
DBConn.AutoMigrate(&CheckboxItem{})
2222

2323
fmt.Println("Database Migrated")
2424
}

shared/database/grocery.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package database
2+
3+
import (
4+
"gorm.io/gorm"
5+
)
6+
7+
type GroceryItem struct {
8+
gorm.Model
9+
Name string `json:"name"`
10+
Checked bool `json:"checked"`
11+
}
12+
13+
func GetGroceryItems() []GroceryItem {
14+
var items []GroceryItem
15+
result := DBConn.Find(&items)
16+
17+
if result.Error != nil {
18+
panic(result.Error)
19+
}
20+
21+
return items
22+
}
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,24 @@ import (
88
"gorm.io/gorm"
99
)
1010

11-
type GroceryItem struct {
11+
type InventoryItem struct {
1212
gorm.Model
1313
Name string `json:"name"`
1414
Count int `json:"count"`
1515
}
1616

17-
type CheckboxItem struct {
18-
gorm.Model
19-
Name string `json:"name"`
20-
Checked bool `json:"checked"`
17+
func GetInventoryItems() []InventoryItem {
18+
var items []InventoryItem
19+
result := DBConn.Find(&items)
20+
21+
if result.Error != nil {
22+
panic(result.Error)
23+
}
24+
25+
return items
2126
}
2227

23-
func GetGroceryItemByName(itemName string) (string, error) {
28+
func GetInventoryItemByName(itemName string) (string, error) {
2429
name := strings.ToLower(itemName)
2530

2631
if len(name) <= 0 {
@@ -29,20 +34,20 @@ func GetGroceryItemByName(itemName string) (string, error) {
2934

3035
db := DBConn
3136

32-
var item GroceryItem
37+
var item InventoryItem
3338
result := db.Find(&item, "Name = ?", name)
3439
return result.Name(), result.Error
3540
}
3641

37-
func CreateGroceryItem(itemName string) (string, error) {
42+
func CreateInventoryItem(itemName string) (string, error) {
3843
name := strings.ToLower(itemName)
3944

4045
if len(name) <= 0 {
4146
return "", errors.New("please type a grocery item")
4247
}
4348

4449
db := DBConn
45-
item := new(GroceryItem)
50+
item := new(InventoryItem)
4651
item.Name = name
4752
item.Count = 1
4853

@@ -51,10 +56,10 @@ func CreateGroceryItem(itemName string) (string, error) {
5156
return "", result.Error
5257
}
5358

54-
func UpdateGroceryItem(id string, count int) (string, error) {
59+
func UpdateInventoryItem(id string, count int) (string, error) {
5560
db := DBConn
5661

57-
var item GroceryItem
62+
var item InventoryItem
5863
db.First(&item, "Id")
5964

6065
item.Count = count
@@ -63,15 +68,15 @@ func UpdateGroceryItem(id string, count int) (string, error) {
6368
return "Item updated.", result.Error
6469
}
6570

66-
func DeleteGroceryItem(itemName string) (string, error) {
71+
func DeleteInventoryItem(itemName string) (string, error) {
6772
name := strings.ToLower(itemName)
6873
db := DBConn
6974

7075
if len(name) <= 0 {
7176
return "", errors.New("please type a grocery item")
7277
}
7378

74-
var item GroceryItem
79+
var item InventoryItem
7580
db.First(&item, "Name = ?", name)
7681

7782
if item.Name == "" {

shared/table/table.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,7 @@ func Main() (tea.Model, error) {
5555
{Title: "Count", Width: 10},
5656
}
5757

58-
var items []db.GroceryItem
59-
result := db.DBConn.Find(&items)
60-
61-
if result.Error != nil {
62-
panic(result.Error)
63-
}
64-
58+
items := db.GetInventoryItems()
6559
tableRows := []table.Row{}
6660

6761
for _, item := range items {

shared/tests/app.db

-24 KB
Binary file not shown.

shared/tests/model_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
model "github.com/lundjrl/chef/shared/database"
77
)
88

9-
func TestGetGroceryItemByName(t *testing.T) {
9+
func TestGetInventoryItemByName(t *testing.T) {
1010
model.InitDatabaseConnection()
1111

12-
val, err := model.GetGroceryItemByName("avocado")
12+
val, err := model.GetInventoryItemByName("avocado")
1313

1414
if err != nil {
1515
t.Log(err)

0 commit comments

Comments
 (0)