Skip to content

Commit

Permalink
feat: add ORM layer (#27)
Browse files Browse the repository at this point in the history
Fix #25.
  • Loading branch information
bfabio authored Jun 22, 2022
1 parent b85cc6e commit ad63fef
Show file tree
Hide file tree
Showing 10 changed files with 438 additions and 18 deletions.
12 changes: 12 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ linters-settings:
# Fiber takes care of unwrapping the error
- func (*github.com/gofiber/fiber/v2.Ctx)

# Defaults
- .Errorf(
- errors.New(
- errors.Unwrap(
- .Wrap(
- .Wrapf(
- .WithMessage(
- .WithMessagef(
- .WithStack(

linters:
enable-all: true

Expand All @@ -57,6 +67,8 @@ linters:

# Not terribly useful and ends up in too much boilerplate
- exhaustruct
# False positives (https://github.com/daixiang0/gci/issues/54)
- gci

# Run only fast linters from enabled linters set (first run won't be fast)
# Default: false
Expand Down
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# This is for local development only.
# See Dockerfile.goreleaser for the image published on release.
#

ARG GO_VERSION=1.18

FROM golang:${GO_VERSION} as build

WORKDIR /go/src

COPY . .

RUN go build

FROM alpine:3

COPY --from=build /go/src/developers-italia-api /usr/local/bin/developers-italia-api

ENTRYPOINT ["/usr/local/bin/developers-italia-api"]
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3.8'
services:
api:
image: italia/developers-italia-api
build:
context: .
dockerfile: Dockerfile
environment:
DATABASE_DSN: host=localhost user=postgres password=postgres dbname=postgres port=5432

db:
image: postgres:14.3-alpine
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- '5432:5432'
22 changes: 21 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@ module github.com/italia/developers-italia-api

go 1.18

require (
github.com/gofiber/fiber/v2 v2.34.1
gorm.io/driver/postgres v1.3.7
gorm.io/gorm v1.23.6
)

require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/gofiber/fiber/v2 v2.34.1 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.12.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.11.0 // indirect
github.com/jackc/pgx/v4 v4.16.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/compress v1.15.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.7.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.37.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
186 changes: 186 additions & 0 deletions go.sum

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions internal/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package database

import (
"fmt"

"github.com/italia/developers-italia-api/internal/models"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

//nolint:gochecknoglobals // gorm suggests to do this in the examples
var Database *gorm.DB

func Init(dsn string) error {
var err error

Database, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
PrepareStmt: true,
})

if err != nil {
return fmt.Errorf("can't open database: %w", err)
}

if err = Database.AutoMigrate(&models.Publisher{}); err != nil {
return fmt.Errorf("can't migrate database: %w", err)
}

return nil
}
99 changes: 99 additions & 0 deletions internal/handlers/publishers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package handlers

import (
"errors"

"github.com/gofiber/fiber/v2"
"gorm.io/gorm"

db "github.com/italia/developers-italia-api/internal/database"
"github.com/italia/developers-italia-api/internal/models"
)

func GetPublishers(ctx *fiber.Ctx) error {
var publishers []models.Publisher

db.Database.Find(&publishers)

return ctx.JSON(&publishers)
}

func GetPublisher(ctx *fiber.Ctx) error {
var publisher models.Publisher

if err := db.Database.First(&publisher, ctx.Params("id")).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return ctx.Status(fiber.StatusNotFound).JSON(fiber.Map{
"message": err.Error(),
})
}

return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})
}

return ctx.JSON(&publisher)
}

func PostPublisher(ctx *fiber.Ctx) error {
var publisher models.Publisher

if err := ctx.BodyParser(&publisher); err != nil {
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": err.Error(),
})
}

if err := db.Database.Create(&publisher).Error; err != nil {
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})
}

return ctx.JSON(&publisher)
}

func PatchPublisher(ctx *fiber.Ctx) error {
var publisher models.Publisher

if err := ctx.BodyParser(&publisher); err != nil {
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": err.Error(),
})
}

query := db.Database.First(models.Publisher{}, ctx.Params("id"))

if err := query.Updates(&publisher).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return ctx.Status(fiber.StatusNotFound).JSON(fiber.Map{
"message": err.Error(),
})
}

return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})
}

return ctx.JSON(&publisher)
}

func DeletePublisher(ctx *fiber.Ctx) error {
var publisher models.Publisher

if err := db.Database.Delete(&publisher, ctx.Params("id")).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return ctx.Status(fiber.StatusNotFound).JSON(fiber.Map{
"message": err.Error(),
})
}

return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})
}

return ctx.JSON(&publisher)
}
17 changes: 0 additions & 17 deletions internal/main.go

This file was deleted.

22 changes: 22 additions & 0 deletions internal/models/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package models

import "gorm.io/gorm"

type Bundle struct {
gorm.Model
Name string
}

type Log struct {
gorm.Model
}

type Publisher struct {
gorm.Model
Name string `json:"name"`
}

type Software struct {
gorm.Model
Name string
}
30 changes: 30 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"log"
"os"

"github.com/gofiber/fiber/v2"
"github.com/italia/developers-italia-api/internal/database"
"github.com/italia/developers-italia-api/internal/handlers"
)

func main() {
if err := database.Init(os.Getenv("DATABASE_DSN")); err != nil {
log.Fatal(err)
}

app := fiber.New(fiber.Config{
Prefork: true,
})

app.Get("/publishers", handlers.GetPublishers)
app.Get("/publishers/:id", handlers.GetPublisher)
app.Post("/publishers", handlers.PostPublisher)
app.Patch("/publishers/:id", handlers.PatchPublisher)
app.Delete("/publishers/:id", handlers.DeletePublisher)

if err := app.Listen(":3000"); err != nil {
log.Fatal(err)
}
}

0 comments on commit ad63fef

Please sign in to comment.