Skip to content

Commit

Permalink
feat: better organization;more testing on database layer
Browse files Browse the repository at this point in the history
  • Loading branch information
SomethingSexy committed Aug 18, 2024
1 parent 4796691 commit b996794
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 86 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ See <https://github.com/qdm12/godevcontainer/tree/master>

## DB

### Go Definitions

`sqlc generate`

### Migration Scripts

<https://atlasgo.io/versioned/intro>
Expand Down Expand Up @@ -35,3 +39,8 @@ atlas migrate diff initial \
--dev-url "postgres://postgres:postgres@db:5432/chronicle?sslmode=disable" \
--format '{{ sql . " " }}'
```

atlas migrate diff add_commits \
--to file://schema.sql \
--dev-url "postgres://postgres:postgres@db:5432/chronicle?sslmode=disable" \
--format '{{ sql . " " }}'
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package http
package game

import (
"errors"
"net/http"

"github.com/SomethingSexy/chronicle/internal/chronicle/core/game/domain"
"github.com/SomethingSexy/chronicle/internal/chronicle/core/domain"
)

type GameRequest struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package http
package game

import (
"net/http"

"github.com/SomethingSexy/chronicle/internal/chronicle/core/game/application/command"
"github.com/SomethingSexy/chronicle/internal/chronicle/core/game/port"
"github.com/SomethingSexy/chronicle/internal/chronicle/core/application/command"
"github.com/SomethingSexy/chronicle/internal/chronicle/port"
"github.com/SomethingSexy/chronicle/internal/common"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)

func NewHttpServer(commands port.GameCommands, queries port.GameQueries) GameHttpServer {
func NewGameHttpServer(commands port.ChronicleCommands, queries port.GameQueries) GameHttpServer {
return GameHttpServer{
commands: commands,
queries: queries,
}
}

type GameHttpServer struct {
commands port.GameCommands
commands port.ChronicleCommands
queries port.GameQueries
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package adapter
package http

import (
"net/http"

"github.com/SomethingSexy/chronicle/internal/chronicle/port"
"github.com/SomethingSexy/chronicle/internal/common"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/render"
Expand All @@ -13,10 +13,10 @@ import (
var ContentTypeJsonApi = "application/vnd.api+json"

type HttpServer struct {
app port.Service
app common.Service
}

func NewHttpServer(application port.Service) HttpServer {
func NewHttpServer(application common.Service) HttpServer {
return HttpServer{
app: application,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Modify "game" table
ALTER TABLE "public"."game" ADD COLUMN "game_id" uuid NOT NULL;
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
h1:CEf8zGVDRoCvO8j5wNVaiUdVZPS797qdvMjjBfH0mx8=
h1:T/g5EnXrva1cg191drI+udZPZFrrJvyKHVTqzi2C+lQ=
20240731025606_initial.sql h1:/w26l6f+1wdv92vWPNgQL7T0nF+ldS2hLbYKhwij0rQ=
20240801202257_add_commits.sql h1:kubjKMXhbD0PHZx7BQ7BbdmhUHzSD4yxIcVRAR5hXW8=
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ ORDER BY name;

-- name: CreateGame :one
INSERT INTO game (
name, type
game_id, name, type
) VALUES (
$1, $2
$1, $2, $3
)
RETURNING *;

Expand Down

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

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

23 changes: 22 additions & 1 deletion internal/chronicle/adapter/persistence/postgres/sqlc/schema.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
-- Represents an overall game
CREATE TABLE game (
id BIGSERIAL PRIMARY KEY,
game_id uuid NOT NULL,
name text NOT NULL,
type text NOT NULL
);
);

-- Represents the game world in general
-- This may or may not be necessary
CREATE TABLE world (
id BIGSERIAL PRIMARY KEY,
world_id uuid NOT NULL,
game_id BIGSERIAL NOT NULL REFERENCES game(id)
);

CREATE TABLE location (
ud BIGSERIAL PRIMARY KEY,
location_id uuid NOT NULL,
world_ID BIGSERIAL NOT NULL REFERENCES world(id),
type text NOT NULL,
name text NOT NULL,
path ltree
);

create index location_path_idx on location using gist (path);
16 changes: 16 additions & 0 deletions internal/chronicle/core/application/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package application

import (
"github.com/SomethingSexy/chronicle/internal/chronicle/core/application/command"
"github.com/SomethingSexy/chronicle/internal/chronicle/port"
)

func NewApplication() port.ChronicleApplication {
commands := port.ChronicleCommands{
CreateGame: command.NewCreateGameCommand(),
}

return port.ChronicleApplication{
Commands: commands,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"log"

"github.com/SomethingSexy/chronicle/internal/chronicle/core/game/domain"
"github.com/SomethingSexy/chronicle/internal/chronicle/core/domain"
"github.com/SomethingSexy/chronicle/internal/common"
)

Expand Down
File renamed without changes.
18 changes: 0 additions & 18 deletions internal/chronicle/core/game/application/application.go

This file was deleted.

19 changes: 0 additions & 19 deletions internal/chronicle/core/game/port/service.go

This file was deleted.

7 changes: 0 additions & 7 deletions internal/chronicle/port/server.go

This file was deleted.

17 changes: 13 additions & 4 deletions internal/chronicle/port/service.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package port

import "github.com/go-chi/chi/v5"
import (
"github.com/SomethingSexy/chronicle/internal/chronicle/core/application/command"
)

// This should represent an overall service application
type Service interface {
Routes() []chi.Router
type ChronicleApplication struct {
Commands ChronicleCommands
Queries GameQueries
}

type ChronicleCommands struct {
CreateGame command.CreateGameHander
}

type GameQueries struct {
}
18 changes: 10 additions & 8 deletions internal/chronicle/service/application.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package service

import (
"github.com/SomethingSexy/chronicle/internal/chronicle/adapter"
gameApplication "github.com/SomethingSexy/chronicle/internal/chronicle/core/game/application"
gamePort "github.com/SomethingSexy/chronicle/internal/chronicle/core/game/port"
"github.com/SomethingSexy/chronicle/internal/chronicle/adapter/http"
"github.com/SomethingSexy/chronicle/internal/chronicle/adapter/http/game"
gameApplication "github.com/SomethingSexy/chronicle/internal/chronicle/core/application"
"github.com/SomethingSexy/chronicle/internal/chronicle/port"
"github.com/go-chi/chi/v5"
)

func NewService() {
game := gameApplication.NewApplication()

service := ChronicleService{
GameApplication: game,
ChronicleApplication: game,
}

httpServer := adapter.NewHttpServer(service)
httpServer := http.NewHttpServer(service)

httpServer.Start()
}

type ChronicleService struct {
GameApplication gamePort.GameApplication
ChronicleApplication port.ChronicleApplication
}

func (c ChronicleService) Routes() []chi.Router {
gameHttpServer := c.GameApplication.Server.Routes()
return []chi.Router{gameHttpServer}
gameHttpServer := game.NewGameHttpServer(c.ChronicleApplication.Commands, port.GameQueries{})
routes := gameHttpServer.Routes()
return []chi.Router{routes}
}

// type Application struct {
Expand Down
8 changes: 8 additions & 0 deletions internal/common/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package common

import "github.com/go-chi/chi/v5"

// This should represent an overall service application
type Service interface {
Routes() []chi.Router
}

0 comments on commit b996794

Please sign in to comment.