-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #25.
- Loading branch information
Showing
10 changed files
with
438 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |