Skip to content

Commit

Permalink
Project initial commit / sheet api handler and model
Browse files Browse the repository at this point in the history
  • Loading branch information
root authored and root committed Jan 19, 2021
1 parent bdf6077 commit 11112f4
Show file tree
Hide file tree
Showing 32 changed files with 154 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
main
/BE/main
4 changes: 2 additions & 2 deletions .env → BE/.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ POSTGRES_PASSWORD=password
POSTGRES_USER=postgres
POSTGRES_PORT=5432
POSTGRES_HOST=pg
POSTGRES_DB=boilerplate
POSTGRES_DB=simplesheet
TEST_DB_HOST=localhost
TEST_DB_NAME=boilerplatetest
TEST_DB_NAME=simplesheettest
API_PORT=8080
File renamed without changes.
35 changes: 35 additions & 0 deletions BE/cmd/api/handlers/createsheet/createsheet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package createsheet

import (
"encoding/json"
"fmt"
"net/http"

"github.com/simplesheet/BE/cmd/api/models"
"github.com/simplesheet/BE/pkg/application"
"github.com/simplesheet/BE/pkg/middleware"
"github.com/julienschmidt/httprouter"
)

func createSheet(app *application.Application) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
defer r.Body.Close()

sheet := &models.Sheet{}
json.NewDecoder(r.Body).Decode(sheet)

if err := sheet.Create(r.Context(), app); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Oops")
return
}

w.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(sheet)
w.Write(response)
}
}

func Do(app *application.Application) httprouter.Handle {
return middleware.Chain(createSheet(app), middleware.LogRequest)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"fmt"
"net/http"

"github.com/boilerplate/cmd/api/models"
"github.com/boilerplate/pkg/application"
"github.com/boilerplate/pkg/middleware"
"github.com/simplesheet/BE/cmd/api/models"
"github.com/simplesheet/BE/pkg/application"
"github.com/simplesheet/BE/pkg/middleware"
"github.com/julienschmidt/httprouter"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"fmt"
"net/http"

"github.com/boilerplate/cmd/api/models"
"github.com/boilerplate/pkg/application"
"github.com/boilerplate/pkg/middleware"
"github.com/simplesheet/BE/cmd/api/models"
"github.com/simplesheet/BE/pkg/application"
"github.com/simplesheet/BE/pkg/middleware"
"github.com/julienschmidt/httprouter"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"
"strconv"

"github.com/boilerplate/cmd/api/models"
"github.com/simplesheet/BE/cmd/api/models"
"github.com/julienschmidt/httprouter"
)

Expand Down
10 changes: 5 additions & 5 deletions cmd/api/main.go → BE/cmd/api/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import (
"github.com/boilerplate/cmd/api/router"
"github.com/boilerplate/pkg/application"
"github.com/boilerplate/pkg/exithandler"
"github.com/boilerplate/pkg/logger"
"github.com/boilerplate/pkg/server"
"github.com/simplesheet/BE/cmd/api/router"
"github.com/simplesheet/BE/pkg/application"
"github.com/simplesheet/BE/pkg/exithandler"
"github.com/simplesheet/BE/pkg/logger"
"github.com/simplesheet/BE/pkg/server"
"github.com/joho/godotenv"
)

Expand Down
File renamed without changes.
58 changes: 58 additions & 0 deletions BE/cmd/api/models/sheet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package models;

import (
"context"
"github.com/simplesheet/BE/pkg/application"
)

type Sheet struct {
ID int `json:"id"`
Field string `json:"field"`
Value string `json:"value"`
}

func (s *Sheet) Create(ctx context.Context, app *application.Application) error {
stmt := `
INSERT INTO sheets (
field,
value
)
VALUES ($1, $2)
RETURNING id
`
err := app.DB.Client.QueryRowContext(
ctx,
stmt,
s.Field,
s.Value,
).Scan(&s.ID)

if err != nil {
return err
}

return nil
}

func (s *Sheet) GetByID(ctx context.Context, app *application.Application) error {
stmt := `
SELECT *
FROM sheets
WHERE id = $1
`
err := app.DB.Client.QueryRowContext(
ctx,
stmt,
s.ID,
).Scan(
&s.ID,
s.Field,
s.Value,
)

if err != nil {
return err
}

return nil
}
5 changes: 2 additions & 3 deletions cmd/api/models/user.go → BE/cmd/api/models/user.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package models
package models;

import (
"context"

"github.com/boilerplate/pkg/application"
"github.com/simplesheet/BE/pkg/application"
)

type User struct {
Expand Down
17 changes: 17 additions & 0 deletions BE/cmd/api/router/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package router

import (
"github.com/simplesheet/BE/cmd/api/handlers/createuser"
"github.com/simplesheet/BE/cmd/api/handlers/getuser"
"github.com/simplesheet/BE/cmd/api/handlers/createsheet"
"github.com/simplesheet/BE/pkg/application"
"github.com/julienschmidt/httprouter"
)

func Get(app *application.Application) *httprouter.Router {
mux := httprouter.New()
mux.GET("/users/:id", getuser.Do(app))
mux.POST("/users", createuser.Do(app))
mux.POST("/sheets", createsheet.Do(app))
return mux
}
6 changes: 3 additions & 3 deletions cmd/dbmigrate/main.go → BE/cmd/dbmigrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import (
"log"

"github.com/boilerplate/pkg/config"
"github.com/simplesheet/BE/pkg/config"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/file"
"github.com/joho/godotenv"
)

Expand Down
1 change: 1 addition & 0 deletions BE/db/migrations/20210118103259_create_sheet.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE public.sheets
6 changes: 6 additions & 0 deletions BE/db/migrations/20210118103259_create_sheet.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS public.sheets
(
id SERIAL PRIMARY KEY,
field VARCHAR(100) NOT NULL,
value VARCHAR(100) NOT NULL
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
DROP DATABASE IF EXISTS boilerplatetest;
CREATE DATABASE boilerplatetest;
DROP DATABASE IF EXISTS simplesheettest;
CREATE DATABASE simplesheettest;
EOSQL
16 changes: 8 additions & 8 deletions docker-compose.yml → BE/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
version: "3.7"

volumes:
boilerplatevolume:
name: boilerplate-volume
simple-sheet-volume:
name: simple-sheet-volume

networks:
boilerplatenetwork:
name: boilerplate-network
simple-sheet-network:
name: simple-sheet-network

services:
pg:
Expand All @@ -17,11 +17,11 @@ services:
ports:
- "${POSTGRES_PORT}:${POSTGRES_PORT}"
volumes:
- boilerplatevolume:/var/lib/postgresql/data
- simple-sheet-volume:/var/lib/postgresql/data
- ./db/scripts:/docker-entrypoint-initdb.d/
networks:
- boilerplatenetwork
boilerplate_api:
- simple-sheet-network
simple-sheet_api:
build:
context: .
dockerfile: Dockerfile.dev
Expand All @@ -36,7 +36,7 @@ services:
ports:
- "${API_PORT}:${API_PORT}"
networks:
- boilerplatenetwork
- simple-sheet-network
env_file:
- .env
entrypoint: ["/bin/bash", "./scripts/entrypoint.dev.sh"]
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package application

import (
"github.com/boilerplate/pkg/config"
"github.com/boilerplate/pkg/db"
"github.com/simplesheet/BE/pkg/config"
"github.com/simplesheet/BE/pkg/db"
)

// Application holds commonly used app wide data, for ease of DI
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os/signal"
"syscall"

"github.com/boilerplate/pkg/logger"
"github.com/simplesheet/BE/pkg/logger"
)

// Init accepts a callback function that will be invoked when program exits
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/middleware/logging.go → BE/pkg/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package middleware
import (
"net/http"

"github.com/boilerplate/pkg/logger"
"github.com/simplesheet/BE/pkg/logger"
"github.com/julienschmidt/httprouter"
)

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion scripts/entrypoint.dev.sh → BE/scripts/entrypoint.dev.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ echo "#################### migrating dev db"
go run cmd/dbmigrate/main.go

echo "#################### migrating test db"
go run cmd/dbmigrate/main.go -dbname=boilerplatetest
go run cmd/dbmigrate/main.go -dbname=simplesheettest

echo "#################### downloading CompileDaemon"
# disable go modules to avoid this package from getting into go.mod
Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
### Golang API boilerplate
### Golang API (psql) and React FE simple sheet for "instead of Excell" money management.

Step by step instructions can be found in:
- [Part 1](https://hackernoon.com/how-to-create-golang-rest-api-project-layout-configuration-part-1-am733yi7)
- [Part 2](https://hackernoon.com/how-to-create-golang-rest-api-project-layout-configuration-part-2-wh2z3y5z)
- [Part 3](https://hackernoon.com/how-to-create-golang-rest-api-project-layout-configuration-part-3-pr453ylt)
Based on go-lang api boilerplate.
15 changes: 0 additions & 15 deletions cmd/api/router/router.go

This file was deleted.

0 comments on commit 11112f4

Please sign in to comment.