diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/CaitsCurates.iml b/.idea/CaitsCurates.iml new file mode 100644 index 0000000..25ed3f6 --- /dev/null +++ b/.idea/CaitsCurates.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..3cbe25d --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + postgresql + true + org.postgresql.Driver + jdbc:postgresql://localhost:5432/CaitsDB + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6e86672 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8f75e0a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sqlDataSources.xml b/.idea/sqlDataSources.xml new file mode 100644 index 0000000..93eebd0 --- /dev/null +++ b/.idea/sqlDataSources.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..8d5050f --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/api/Dockerfile b/api/Dockerfile new file mode 100644 index 0000000..8df0652 --- /dev/null +++ b/api/Dockerfile @@ -0,0 +1,13 @@ +FROM golang:1.21.1 + + +RUN mkdir /app + +ADD . /app + +WORKDIR /app + +RUN go build -o main ./src/main.go + +EXPOSE 8080 +CMD [ "/app/main" ] \ No newline at end of file diff --git a/api/docker-compose.yml b/api/docker-compose.yml new file mode 100644 index 0000000..0e8a093 --- /dev/null +++ b/api/docker-compose.yml @@ -0,0 +1,21 @@ +version: '3.8' + +services: + db: + image: postgres + restart: always + environment: + POSTGRES_PASSWORD: pwd + POSTGRES_USER: user + POSTGRES_DB: CaitsDB + ports: + - "5432:5432" + myapi: + build: . + ports: + - "8080:8080" + environment: + - PORT=8080 + - DATABASE_URL=postgres://user:pwd@db:5432/CaitsDB + depends_on: + - db \ No newline at end of file diff --git a/api/go.mod b/api/go.mod index 20f1ab1..6310dcc 100644 --- a/api/go.mod +++ b/api/go.mod @@ -1,11 +1,11 @@ -module generate/bootcamp +module CaitsCurates/backend -go 1.19 +go 1.21.1 require ( github.com/gin-contrib/cors v1.4.0 github.com/gin-gonic/gin v1.9.1 - github.com/huandu/go-assert v1.1.6 + github.com/go-playground/assert/v2 v2.2.0 github.com/jackc/pgx v3.6.2+incompatible github.com/lib/pq v1.10.9 ) @@ -14,7 +14,6 @@ require ( github.com/bytedance/sonic v1.9.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/cockroachdb/apd v1.1.0 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect diff --git a/api/go.sum b/api/go.sum index f6c065f..dd7000b 100644 --- a/api/go.sum +++ b/api/go.sum @@ -21,6 +21,7 @@ github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= @@ -39,8 +40,6 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/huandu/go-assert v1.1.6 h1:oaAfYxq9KNDi9qswn/6aE0EydfxSa+tWZC1KabNitYs= -github.com/huandu/go-assert v1.1.6/go.mod h1:JuIfbmYG9ykwvuxoJ3V8TB5QP+3+ajIA54Y44TmkMxs= github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 h1:vr3AYkKovP8uR8AvSGGUK1IDqRa5lAAvEkZG1LKaCRc= github.com/jackc/fake v0.0.0-20150926172116-812a484cc733/go.mod h1:WrMFNQdiFJ80sQsxDoMokWK1W5TQtxBFNpzWTD84ibQ= github.com/jackc/pgx v3.6.2+incompatible h1:2zP5OD7kiyR3xzRYMhOcXVvkDZsImVXfj+yIyTQf3/o= diff --git a/api/src/controller/controller.go b/api/src/controller/controller.go index 7b93de8..a2c4f8e 100644 --- a/api/src/controller/controller.go +++ b/api/src/controller/controller.go @@ -1,8 +1,8 @@ package controller import ( + "CaitsCurates/backend/src/model" "fmt" - "generate/bootcamp/src/model" "net/http" "strconv" @@ -22,39 +22,41 @@ func (pg *PgController) Serve() *gin.Engine { r := gin.Default() r.Use(cors.Default()) - r.GET("/v1/books/:bookId", func(c *gin.Context) { - id := c.Param("bookId") + r.GET("/gifts/:giftId", func(c *gin.Context) { + id := c.Param("giftId") intId, err := strconv.Atoi(id) if err != nil { panic(err) } - c.JSON(http.StatusOK, pg.Book(int64(intId))) + c.JSON(http.StatusOK, pg.GetExampleGift(int64(intId))) }) - r.GET("/v1/books/", func(c *gin.Context) { - books, err := pg.AllBooks() + r.GET("/v1/gifts/", func(c *gin.Context) { + books, err := pg.AllExampleGifts() if err != nil { c.JSON(http.StatusInternalServerError, "Oops") } c.JSON(http.StatusOK, books) }) - r.POST("/v1/addBook", func(c *gin.Context) { - var book model.Book - - if err := c.BindJSON(&book); err != nil { - c.JSON(http.StatusBadRequest, "Failed to unmarshal book") + r.POST("/v1/addGift", func(c *gin.Context) { + var eg model.ExampleGift + fmt.Print(eg.GiftId) + fmt.Print("HHHHH\n\n\nHHHHH\n\n\nHHHH") + if err := c.BindJSON(&eg); err != nil { + c.JSON(http.StatusBadRequest, "Failed to unmarshal book") fmt.Print(err) + return } - insertedBook, err := pg.AddBook(book) + insertedGift, err := pg.AddExampleGift(eg) if err != nil { - c.JSON(http.StatusBadRequest, "Failed to add a book") + c.JSON(http.StatusBadRequest, eg.GiftId) panic(err) } - c.JSON(http.StatusOK, insertedBook) + c.JSON(http.StatusOK, insertedGift) }) return r diff --git a/api/src/main.go b/api/src/main.go index 22536b0..71ebf43 100644 --- a/api/src/main.go +++ b/api/src/main.go @@ -1,9 +1,10 @@ package main + import ( + "CaitsCurates/backend/src/controller" + "CaitsCurates/backend/src/model" "fmt" - "generate/bootcamp/src/controller" - "generate/bootcamp/src/model" "os" "github.com/jackc/pgx" @@ -14,8 +15,8 @@ func main() { cfg := pgx.ConnConfig{ User: "postgres", - Database: "backendbootcamp", - Password: "password", + Database: db_url, + Password: "pwd", Host: "127.0.0.1", Port: 5432, } @@ -24,6 +25,7 @@ func main() { cfg, err = pgx.ParseConnectionString(db_url) if err != nil { + print(db_url) panic(err) } } @@ -45,3 +47,4 @@ func main() { c.Serve().Run(":8080") } + diff --git a/api/src/migrations/init.sql b/api/src/migrations/init.sql index bdb6ef6..fb036e2 100644 --- a/api/src/migrations/init.sql +++ b/api/src/migrations/init.sql @@ -1,30 +1,8 @@ -CREATE TABLE IF NOT EXISTS students ( - nuid integer PRIMARY KEY, - name varchar NOT NULL -); +DROP TABLE IF EXISTS examplegifts; -CREATE TABLE IF NOT EXISTS books ( - book_id integer PRIMARY KEY, - title varchar NOT NULL, - author varchar NOT NULL -); -CREATE TABLE IF NOT EXISTS checked_out_books ( - checkout_id serial PRIMARY KEY, - book_id integer NOT NULL REFERENCES books (book_id), - nuid integer NOT NULL REFERENCES students (nuid), - expected_return_date timestamp with time zone NOT NULL -); - -CREATE TABLE IF NOT EXISTS holds ( - hold_id serial PRIMARY KEY, - book_id integer NOT NULL REFERENCES books (book_id), - nuid integer NOT NULL REFERENCES students (nuid), - hold_creation_date timestamp with time zone NOT NULL -); - -CREATE TABLE IF NOT EXISTS liked_books ( - like_id serial PRIMARY KEY, - book_id integer NOT NULL REFERENCES books (book_id), - nuid integer NOT NULL REFERENCES students (nuid) -); +CREATE TABLE examplegifts ( + gift_id integer PRIMARY KEY, + name varchar NOT NULL, + price integer NOT NULL +); \ No newline at end of file diff --git a/api/src/model/model.go b/api/src/model/model.go index fd1deca..35eac53 100644 --- a/api/src/model/model.go +++ b/api/src/model/model.go @@ -9,13 +9,13 @@ type PgModel struct { } type Model interface { - Book(int64) Book - AllBooks() ([]Book, error) - AddBook(Book) (Book, error) + GetExampleGift(int64) ExampleGift + AllExampleGifts() ([]ExampleGift, error) + AddExampleGift(ExampleGift) (ExampleGift, error) } -func (m *PgModel) Book(id int64) Book { - book, err := GetBookFromDB(m.Conn, id) +func (m *PgModel) GetExampleGift(id int64) ExampleGift { + book, err := GetExampleGiftFromDB(m.Conn, id) if err != nil { panic(err) @@ -24,21 +24,21 @@ func (m *PgModel) Book(id int64) Book { return book } -func (m *PgModel) AddBook(book Book) (Book, error) { - b, err := WriteBookToDb(m.Conn, book) +func (m *PgModel) AddExampleGift(eg ExampleGift) (ExampleGift, error) { + b, err := WriteExampleGiftToDb(m.Conn, eg) if err != nil { - return Book{}, err + return ExampleGift{}, err } return b, nil } -func (m *PgModel) AllBooks() ([]Book, error) { - books, err := GetAllBooksFromDB(m.Conn) +func (m *PgModel) AllExampleGifts() ([]ExampleGift, error) { + books, err := GetAllExampleGiftsFromDB(m.Conn) if err != nil { - return []Book{}, err + return []ExampleGift{}, err } return books, nil } diff --git a/api/src/model/transactions.go b/api/src/model/transactions.go index a26b675..26be297 100644 --- a/api/src/model/transactions.go +++ b/api/src/model/transactions.go @@ -7,53 +7,53 @@ import ( "github.com/jackc/pgx" ) -func WriteBookToDb(pool *pgx.Conn, book Book) (Book, error) { - bookIdStr := strconv.FormatInt(book.BookId, 10) +func WriteExampleGiftToDb(pool *pgx.Conn, eg ExampleGift) (ExampleGift, error) { + giftIdStr := strconv.FormatInt(eg.GiftId, 10) - err := pool.QueryRow(fmt.Sprintf("INSERT INTO books (book_id, title, author) VALUES ('%s','%s', '%s') RETURNING book_id;", bookIdStr, book.Title, book.Author)).Scan(&book.BookId) + err := pool.QueryRow(fmt.Sprintf("INSERT INTO examplegifts (gift_id, name, price) VALUES ('%s','%s', '%d') RETURNING gift_id;", giftIdStr, eg.Name, eg.Price)).Scan(&eg.GiftId) if err != nil { - return Book{}, err + return ExampleGift{}, err } - return book, nil + return eg, nil } -func GetBookFromDB(pool *pgx.Conn, book_id int64) (Book, error) { - book := Book{ - BookId: book_id, +func GetExampleGiftFromDB(pool *pgx.Conn, giftId int64) (ExampleGift, error) { + eg := ExampleGift{ + GiftId: giftId, } - var bid int - err := pool.QueryRow(fmt.Sprintf("SELECT book_id, title, author FROM books where book_id = '%d';", book_id)).Scan(&bid, &book.Title, &book.Author) + var gid int + err := pool.QueryRow(fmt.Sprintf("SELECT gift_id, name, price FROM examplegifts where gift_id = '%d';", giftId)).Scan(&gid, &eg.Name, &eg.Price) if err != nil { panic(err) } - return book, nil + return eg, nil } -func GetAllBooksFromDB(pool *pgx.Conn) ([]Book, error) { - rows, err := pool.Query("SELECT book_id, title, author FROM books;") +func GetAllExampleGiftsFromDB(pool *pgx.Conn) ([]ExampleGift, error) { + rows, err := pool.Query("SELECT gift_id, name, price FROM examplegifts;") if err != nil { panic(err) } - results := []Book{} + results := []ExampleGift{} defer rows.Close() for rows.Next() { - book := Book{} - err := rows.Scan(&book.BookId, &book.Title, &book.Author) + eg := ExampleGift{} + err := rows.Scan(&eg.GiftId, &eg.Name, &eg.Price) if err != nil { panic(err) } - results = append(results, book) + results = append(results, eg) } return results, nil diff --git a/api/src/model/types.go b/api/src/model/types.go index 6ff0b2a..4470799 100644 --- a/api/src/model/types.go +++ b/api/src/model/types.go @@ -1,12 +1,8 @@ package model -type Book struct { - BookId int64 `json:"id" db:"book_id"` - Title string `json:"title" db:"title"` - Author string `json:"author" db:"author"` +type ExampleGift struct { + GiftId int64 `json:"id" db:"gift_id"` + Name string `json:"name" db:"name"` + Price int `json:"price" db:"price"` } -type Student struct { - NUID int64 `json:"nuid" db:"nuid"` - Name string `json:"name" db:"name"` -} diff --git a/api/tests/api_test.go b/api/tests/api_test.go index 4345a6f..8079b89 100644 --- a/api/tests/api_test.go +++ b/api/tests/api_test.go @@ -1,16 +1,16 @@ package tests import ( + c "CaitsCurates/backend/src/controller" + "CaitsCurates/backend/src/model" "encoding/json" "fmt" - c "generate/bootcamp/src/controller" - "generate/bootcamp/src/model" + "github.com/go-playground/assert/v2" "net/http" "net/http/httptest" "os" "testing" - "github.com/huandu/go-assert" "github.com/jackc/pgx" ) @@ -18,9 +18,9 @@ func TestGetBooks(t *testing.T) { db_url, exists := os.LookupEnv("DATABASE_URL") cfg := pgx.ConnConfig{ - User: "postgres", - Database: "backendbootcamp", - Password: "password", + User: "user", + Database: "CaitsDB", + Password: "pwd", Host: "127.0.0.1", Port: 5432, } @@ -50,22 +50,22 @@ func TestGetBooks(t *testing.T) { w := httptest.NewRecorder() - req, _ := http.NewRequest("GET", "/v1/books/1738", nil) + req, _ := http.NewRequest("GET", "/gifts/1", nil) router.ServeHTTP(w, req) assert.Equal(t, 200, w.Code) - var books model.Book + var gift model.ExampleGift - if e := json.Unmarshal(w.Body.Bytes(), &books); e != nil { + if e := json.Unmarshal(w.Body.Bytes(), &gift); e != nil { panic(err) } - test_book := model.Book{ - BookId: 1738, - Title: "The Lightning Thief", - Author: "Rick Riordan", + testBook := model.ExampleGift{ + GiftId: 1, + Name: "nice sweater", + Price: 50, } - assert.Equal(t, test_book, books) + assert.Equal(t, testBook, gift) } diff --git a/api/tests/db_test.go b/api/tests/db_test.go index 88471f0..70ef379 100644 --- a/api/tests/db_test.go +++ b/api/tests/db_test.go @@ -10,9 +10,9 @@ import ( func TestDBConnection(t *testing.T) { dbURL := fmt.Sprintf("postgres://%s:%s@localhost:5432/%s?sslmode=disable", - "postgres", + "user", "pwd", - "bootcamp", + "CaitsDB", ) db, err := sql.Open("postgres", dbURL) diff --git a/scripts/init_db.sh b/scripts/init_db.sh deleted file mode 100755 index e8ac8d8..0000000 --- a/scripts/init_db.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env bash -set -x -set -eo pipefail - -if ! [ -x "$(command -v psql)" ]; then - echo >&2 "Error: psql is not installed." - exit 1 -fi - -DB_USER="${POSTGRES_USER:=postgres}" -DB_PASSWORD="${POSTGRES_PASSWORD:=password}" -DB_NAME="${POSTGRES_DB:=backendbootcamp}" -DB_PORT="${POSTGRES_PORT:=5432}" -DB_HOST="${POSTGRES_HOST:=127.0.0.1}" - -if [[ -z "${SKIP_DOCKER}" ]] -then - RUNNING_POSTGRES_CONTAINER=$(docker ps --filter 'name=postgres' --format '{{.ID}}') - if [[ -n $RUNNING_POSTGRES_CONTAINER ]]; then - echo >&2 "there is a postgres container already running, kill it with" - echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}" - exit 1 - fi - docker run \ - -e POSTGRES_USER=${DB_USER} \ - -e POSTGRES_PASSWORD=${DB_PASSWORD} \ - -e POSTGRES_DB=${DB_NAME} \ - -p "${DB_PORT}":5432 \ - -d \ - --name "postgres_$(date '+%s')" \ - postgres -N 1000 -fi - -until PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do - >&2 echo "Postgres is still unavailable - sleeping" - sleep 1 -done - ->&2 echo "Postgres is up and running on port ${DB_PORT} !" \ No newline at end of file