Skip to content

Commit

Permalink
Feature/repo layer source (#17)
Browse files Browse the repository at this point in the history
* adds DB client layer
* adds repository layer
* adds unit tests for repository layer

---------

Signed-off-by: Amit Singh <[email protected]>
  • Loading branch information
semmet95 authored Dec 28, 2024
1 parent 6aca912 commit 6eb66e8
Show file tree
Hide file tree
Showing 14 changed files with 292 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:

- name: Golangci-lint
uses: golangci/[email protected]
with:
args: --timeout=5m

- name: Unit Tests
run: make unit-tests
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# generated files
source-score
**coverage/
coverage.out
test_database.db

# ide
.vscode/
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ build: codegen
go build

unit-tests:
go run github.com/onsi/ginkgo/v2/ginkgo run --skip-package=acceptance ./...
go run github.com/onsi/ginkgo/v2/ginkgo run --skip-package=acceptance --cover --coverprofile=coverage.out ./...

acceptance-tests: build
chmod +x ./source-score
( \
./source-score & BG_PID=$$!; \
trap "echo 'terminating the app'; kill $$BG_PID" EXIT; \
echo "app running with PID $$BG_PID"; \
go run github.com/onsi/ginkgo/v2/ginkgo run acceptance/...; \
go run github.com/onsi/ginkgo/v2/ginkgo run --cover --coverprofile=coverage.out acceptance/...; \
)

tests: unit-tests acceptance-tests

start: codegen
go run main.go

Expand Down
9 changes: 5 additions & 4 deletions api/source-score.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/CreateSource'
$ref: '#/components/schemas/SourceInput'
responses:
201:
description: source created
Expand Down Expand Up @@ -66,7 +66,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/CreateSource'
$ref: '#/components/schemas/SourceInput'
responses:
200:
description: source updated
Expand Down Expand Up @@ -100,7 +100,7 @@ components:
type: string
example: pong

CreateSource:
SourceInput:
type: object
required:
- name
Expand Down Expand Up @@ -133,6 +133,7 @@ components:
- summary
- tags
- uri
- uriDigest
properties:
name:
type: string
Expand All @@ -159,4 +160,4 @@ components:
uri:
type: string
x-oapi-codegen-extra-tags:
binding: required
binding: required
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ require (
)

require (
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
)

Expand All @@ -40,7 +43,6 @@ require (
github.com/alexkohler/nakedret/v2 v2.0.5 // indirect
github.com/alexkohler/prealloc v1.0.0 // indirect
github.com/alingse/asasalint v0.0.11 // indirect
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/ashanbrown/forbidigo v1.6.0 // indirect
github.com/ashanbrown/makezero v1.1.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down Expand Up @@ -107,7 +109,6 @@ require (
github.com/golangci/unconvert v0.0.0-20240309020433-c5143eacb3ed // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gordonklaus/ineffassign v0.1.0 // indirect
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
github.com/gostaticanalysis/comment v1.4.2 // indirect
Expand Down Expand Up @@ -240,6 +241,7 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/postgres v1.5.11
gorm.io/driver/sqlite v1.5.7
gorm.io/gorm v1.25.12
honnef.co/go/tools v0.5.1 // indirect
mvdan.cc/gofumpt v0.7.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mgechev/revive v1.5.0 h1:oaSmjA7rP8+HyoRuCgC531VHwnLH1AlJdjj+1AnQceQ=
Expand Down Expand Up @@ -1075,6 +1077,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=
gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I=
gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewRouter() *router {
}

func (r *router) CreateSource(ctx *gin.Context) {
body := CreateSource{}
body := SourceInput{}
// using BindJson method to serialize body with struct
if err := ctx.BindJSON(&body); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
Expand Down
32 changes: 16 additions & 16 deletions pkg/api/server.gen.go

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

2 changes: 1 addition & 1 deletion pkg/conf/conf_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (

func TestConf(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Conf Test Suite")
RunSpecs(t, "Conf Unit Test Suite")
}
42 changes: 33 additions & 9 deletions pkg/db/cnpg/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cnpg

import (
"context"
"log"

"gorm.io/driver/postgres"
Expand All @@ -11,14 +12,37 @@ type Client struct {
DB *gorm.DB
}

func NewClient(dbURL string, config *gorm.Config) *Client {
client := new(Client)
db, err := gorm.Open(postgres.Open(dbURL), config)
if err != nil {
log.Fatalf("failed to open connection with database:%s :: %s", dbURL, err)
}
func NewClient(ctx context.Context, dbURL string, config *gorm.Config) *Client {
client := new(Client)
DB, err := gorm.Open(postgres.Open(dbURL), config)
if err != nil {
log.Fatalf("failed to open connection with database:%s :: %s", dbURL, err)
}

client.DB = db
client.DB = DB

return client
}
return client
}

func (client *Client) SetAutoMigration(ctx context.Context, allModels []interface{}) {
err := client.DB.AutoMigrate(allModels...)
if err != nil {
log.Fatalf("failed enable auto migration for all models :: %s", err)
}
}

func (client *Client) Create(ctx context.Context, record interface{}) *gorm.DB {
return client.DB.Create(record)
}

func (client *Client) Delete(ctx context.Context, record interface{}) *gorm.DB {
return client.DB.Delete(record)
}

func (client *Client) FindFirst(ctx context.Context, record interface{}) *gorm.DB {
return client.DB.First(record)
}

func (client *Client) Update(ctx context.Context, record interface{}) *gorm.DB {
return client.DB.Save(record)
}
81 changes: 74 additions & 7 deletions pkg/domain/source/source_repository.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,82 @@
package source

type sourceRepository struct {
client interface{}
import (
"context"
"crypto/sha256"
"encoding/hex"
"log"

"source-score/pkg/api"
"source-score/pkg/db/cnpg"
)

type SourceRepository struct {
client *cnpg.Client
}

func NewSourceRepository(client interface{}) *sourceRepository {
return &sourceRepository{
func NewSourceRepository(ctx context.Context, client *cnpg.Client) *SourceRepository {
return &SourceRepository{
client: client,
}
}

func (sr *sourceRepository) GetSourceByDigest(uriDigest string) {

}
func (sr *SourceRepository) DeleteSourceByUriDigest(ctx context.Context, source *api.Source) error {
result := sr.client.Delete(ctx, source)
log.Printf("%d rows affected\n", result.RowsAffected)

return result.Error
}

func (sr *SourceRepository) GetSourceByUriDigest(ctx context.Context, uriDigest string) (*api.Source, error) {
source := &api.Source{}
source.UriDigest = uriDigest
result := sr.client.FindFirst(ctx, source)

if result.Error != nil {
return nil, result.Error
}

return source, nil
}

func (sr *SourceRepository) PutSource(ctx context.Context, sourceInput *api.SourceInput) error {
hash := sha256.New()
_, err := hash.Write([]byte(sourceInput.Uri))
if err != nil {
return err
}

uriDigest := hex.EncodeToString(hash.Sum(nil))
source := &api.Source{
Name: sourceInput.Name,
Summary: sourceInput.Summary,
Tags: sourceInput.Tags,
Uri: sourceInput.Uri,
UriDigest: uriDigest,
}

result := sr.client.Create(ctx, source)
log.Printf("%d rows affected\n", result.RowsAffected)

return result.Error
}

// Updates source model fields except for `uri` and `uriDigest`
func (sr *SourceRepository) UpdateSourceByUriDigest(ctx context.Context, sourceInput *api.SourceInput, uriDigest string) error {
source := &api.Source{}
source.UriDigest = uriDigest

result := sr.client.FindFirst(ctx, source)
if result.Error != nil {
return result.Error
}

source.Name = sourceInput.Name
source.Summary = sourceInput.Summary
source.Tags = sourceInput.Tags

result = sr.client.Update(ctx, source)
log.Printf("%d rows affected\n", result.RowsAffected)

return result.Error
}
Loading

0 comments on commit 6eb66e8

Please sign in to comment.