Skip to content

Commit 0883fb7

Browse files
author
Udacity
committed
Add starter code.
1 parent 02fd784 commit 0883fb7

File tree

4 files changed

+160
-2
lines changed

4 files changed

+160
-2
lines changed

starter/README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1-
# Purpose of this Folder
1+
# Inventory Management (starter project)
22

3-
This folder should contain the scaffolded project files to get a student started on their project. This repo will be added to the Classroom for students to use, so please do not have any solutions in this folder.
3+
This project is the starting point for building your Inventory Management API using [Go](https://go.dev/), the [Gin](https://github.com/gin-gonic/gin) framework, and [GORM](https://gorm.io/index.html) for database operations. You can use this starter code to help seed the database, but _its usage is completely optional_ for this project (you may wish to seed the database using your own code).
4+
5+
## Instructions
6+
7+
1. Clone this project to your local machine, then navigate to the **/starter** directory.
8+
9+
2. Set up a PostgreSQL database on your local machine.
10+
11+
3. In **main.go**, create a `dsn` variable and add your PostgreSQL connection string.
12+
13+
```
14+
// TODO: Add your PostgreSQL connection string here (dsn) to connect to your database
15+
```
16+
17+
4. Define the `Item` struct in **main.go**. This struct represents the inventory items stored in the database.
18+
19+
```
20+
// TODO: Create a struct for Item here
21+
```
22+
23+
5. Before running the server, ensure all dependencies are properly managed.
24+
25+
```
26+
go mod tidy
27+
```
28+
29+
6. Run the server.
30+
31+
```
32+
go run main.go
33+
```
34+
35+
The server will connect to your PostgreSQL database and seed it with 20 sample items. Be sure to check the database to confirm that the items have been seeded successfully.

starter/go.mod

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module inventory_management
2+
3+
go 1.23.2
4+
5+
require (
6+
github.com/google/uuid v1.6.0
7+
gorm.io/driver/postgres v1.5.9
8+
gorm.io/gorm v1.25.12
9+
)
10+
11+
require (
12+
github.com/jackc/pgpassfile v1.0.0 // indirect
13+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
14+
github.com/jackc/pgx/v5 v5.5.5 // indirect
15+
github.com/jackc/puddle/v2 v2.2.1 // indirect
16+
github.com/jinzhu/inflection v1.0.0 // indirect
17+
github.com/jinzhu/now v1.1.5 // indirect
18+
golang.org/x/crypto v0.17.0 // indirect
19+
golang.org/x/sync v0.1.0 // indirect
20+
golang.org/x/text v0.14.0 // indirect
21+
)

starter/go.sum

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
5+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
6+
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
7+
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
8+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
9+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
10+
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
11+
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
12+
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
13+
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
14+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
15+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
16+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
17+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
18+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
19+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
20+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
21+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
22+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
23+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
24+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
25+
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
26+
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
27+
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
28+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
29+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
30+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
31+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
32+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
33+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
34+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
35+
gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8=
36+
gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
37+
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
38+
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=

starter/main.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/google/uuid"
7+
"gorm.io/driver/postgres"
8+
"gorm.io/gorm"
9+
)
10+
11+
// TODO: Create a struct for Item here
12+
13+
var DB *gorm.DB
14+
15+
// initDatabase initializes the database connection
16+
func initDatabase() {
17+
var err error
18+
// TODO: Add your PostgreSQL connection string here (dsn) to connect to your database
19+
20+
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
21+
if err != nil {
22+
log.Fatal("Failed to connect to the database!", err)
23+
}
24+
25+
DB.AutoMigrate(&Item{})
26+
seedDatabase()
27+
}
28+
29+
// seedDatabase will seed the database with 20 default items
30+
func seedDatabase() {
31+
var count int64
32+
DB.Model(&Item{}).Count(&count)
33+
if count == 0 {
34+
items := []Item{
35+
{ID: uuid.New().String(), Name: "Laptop", Stock: 10, Price: 999.99},
36+
{ID: uuid.New().String(), Name: "Smartphone", Stock: 20, Price: 699.99},
37+
{ID: uuid.New().String(), Name: "Headphones", Stock: 15, Price: 199.99},
38+
{ID: uuid.New().String(), Name: "Keyboard", Stock: 25, Price: 89.99},
39+
{ID: uuid.New().String(), Name: "Mouse", Stock: 30, Price: 49.99},
40+
{ID: uuid.New().String(), Name: "Monitor", Stock: 12, Price: 299.99},
41+
{ID: uuid.New().String(), Name: "Webcam", Stock: 18, Price: 79.99},
42+
{ID: uuid.New().String(), Name: "Printer", Stock: 7, Price: 149.99},
43+
{ID: uuid.New().String(), Name: "Tablet", Stock: 5, Price: 399.99},
44+
{ID: uuid.New().String(), Name: "Smartwatch", Stock: 14, Price: 249.99},
45+
{ID: uuid.New().String(), Name: "External Hard Drive", Stock: 8, Price: 119.99},
46+
{ID: uuid.New().String(), Name: "USB Flash Drive", Stock: 50, Price: 19.99},
47+
{ID: uuid.New().String(), Name: "Router", Stock: 6, Price: 89.99},
48+
{ID: uuid.New().String(), Name: "Projector", Stock: 3, Price: 499.99},
49+
{ID: uuid.New().String(), Name: "Bluetooth Speaker", Stock: 22, Price: 129.99},
50+
{ID: uuid.New().String(), Name: "Gaming Console", Stock: 11, Price: 499.99},
51+
{ID: uuid.New().String(), Name: "Camera", Stock: 4, Price: 599.99},
52+
{ID: uuid.New().String(), Name: "Fitness Tracker", Stock: 16, Price: 99.99},
53+
{ID: uuid.New().String(), Name: "Drone", Stock: 2, Price: 899.99},
54+
{ID: uuid.New().String(), Name: "VR Headset", Stock: 9, Price: 399.99},
55+
}
56+
57+
DB.Create(&items)
58+
log.Println("Database seeded with 20 sample items.")
59+
} else {
60+
log.Println("Database already contains data, skipping seeding.")
61+
}
62+
}
63+
64+
func main() {
65+
initDatabase()
66+
log.Println("Server successfully connected to the database and seeded data.")
67+
}

0 commit comments

Comments
 (0)