Skip to content

Commit

Permalink
Add starter code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Udacity committed Oct 10, 2024
1 parent 02fd784 commit 0883fb7
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 2 deletions.
36 changes: 34 additions & 2 deletions starter/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
# Purpose of this Folder
# Inventory Management (starter project)

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.
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).

## Instructions

1. Clone this project to your local machine, then navigate to the **/starter** directory.

2. Set up a PostgreSQL database on your local machine.

3. In **main.go**, create a `dsn` variable and add your PostgreSQL connection string.

```
// TODO: Add your PostgreSQL connection string here (dsn) to connect to your database
```

4. Define the `Item` struct in **main.go**. This struct represents the inventory items stored in the database.

```
// TODO: Create a struct for Item here
```

5. Before running the server, ensure all dependencies are properly managed.

```
go mod tidy
```

6. Run the server.

```
go run main.go
```

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.
21 changes: 21 additions & 0 deletions starter/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module inventory_management

go 1.23.2

require (
github.com/google/uuid v1.6.0
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.12
)

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

import (
"log"

"github.com/google/uuid"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

// TODO: Create a struct for Item here

var DB *gorm.DB

// initDatabase initializes the database connection
func initDatabase() {
var err error
// TODO: Add your PostgreSQL connection string here (dsn) to connect to your database

DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal("Failed to connect to the database!", err)
}

DB.AutoMigrate(&Item{})
seedDatabase()
}

// seedDatabase will seed the database with 20 default items
func seedDatabase() {
var count int64
DB.Model(&Item{}).Count(&count)
if count == 0 {
items := []Item{
{ID: uuid.New().String(), Name: "Laptop", Stock: 10, Price: 999.99},
{ID: uuid.New().String(), Name: "Smartphone", Stock: 20, Price: 699.99},
{ID: uuid.New().String(), Name: "Headphones", Stock: 15, Price: 199.99},
{ID: uuid.New().String(), Name: "Keyboard", Stock: 25, Price: 89.99},
{ID: uuid.New().String(), Name: "Mouse", Stock: 30, Price: 49.99},
{ID: uuid.New().String(), Name: "Monitor", Stock: 12, Price: 299.99},
{ID: uuid.New().String(), Name: "Webcam", Stock: 18, Price: 79.99},
{ID: uuid.New().String(), Name: "Printer", Stock: 7, Price: 149.99},
{ID: uuid.New().String(), Name: "Tablet", Stock: 5, Price: 399.99},
{ID: uuid.New().String(), Name: "Smartwatch", Stock: 14, Price: 249.99},
{ID: uuid.New().String(), Name: "External Hard Drive", Stock: 8, Price: 119.99},
{ID: uuid.New().String(), Name: "USB Flash Drive", Stock: 50, Price: 19.99},
{ID: uuid.New().String(), Name: "Router", Stock: 6, Price: 89.99},
{ID: uuid.New().String(), Name: "Projector", Stock: 3, Price: 499.99},
{ID: uuid.New().String(), Name: "Bluetooth Speaker", Stock: 22, Price: 129.99},
{ID: uuid.New().String(), Name: "Gaming Console", Stock: 11, Price: 499.99},
{ID: uuid.New().String(), Name: "Camera", Stock: 4, Price: 599.99},
{ID: uuid.New().String(), Name: "Fitness Tracker", Stock: 16, Price: 99.99},
{ID: uuid.New().String(), Name: "Drone", Stock: 2, Price: 899.99},
{ID: uuid.New().String(), Name: "VR Headset", Stock: 9, Price: 399.99},
}

DB.Create(&items)
log.Println("Database seeded with 20 sample items.")
} else {
log.Println("Database already contains data, skipping seeding.")
}
}

func main() {
initDatabase()
log.Println("Server successfully connected to the database and seeded data.")
}

0 comments on commit 0883fb7

Please sign in to comment.