Skip to content

Commit

Permalink
Initial Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
matherg committed Sep 11, 2023
1 parent c1a5b32 commit b8c4a7a
Show file tree
Hide file tree
Showing 21 changed files with 179 additions and 143 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/CaitsCurates.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/sqlDataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
21 changes: 21 additions & 0 deletions api/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 3 additions & 4 deletions api/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
30 changes: 16 additions & 14 deletions api/src/controller/controller.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package controller

import (
"CaitsCurates/backend/src/model"
"fmt"
"generate/bootcamp/src/model"
"net/http"
"strconv"

Expand All @@ -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
Expand Down
11 changes: 7 additions & 4 deletions api/src/main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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,
}
Expand All @@ -24,6 +25,7 @@ func main() {
cfg, err = pgx.ParseConnectionString(db_url)

if err != nil {
print(db_url)
panic(err)
}
}
Expand All @@ -45,3 +47,4 @@ func main() {

c.Serve().Run(":8080")
}

34 changes: 6 additions & 28 deletions api/src/migrations/init.sql
Original file line number Diff line number Diff line change
@@ -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
);
22 changes: 11 additions & 11 deletions api/src/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Loading

0 comments on commit b8c4a7a

Please sign in to comment.