-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adds DB client layer * adds repository layer * adds unit tests for repository layer --------- Signed-off-by: Amit Singh <[email protected]>
- Loading branch information
Showing
14 changed files
with
292 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ jobs: | |
|
||
- name: Golangci-lint | ||
uses: golangci/[email protected] | ||
with: | ||
args: --timeout=5m | ||
|
||
- name: Unit Tests | ||
run: make unit-tests | ||
|
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# generated files | ||
source-score | ||
**coverage/ | ||
coverage.out | ||
test_database.db | ||
|
||
# ide | ||
.vscode/ | ||
|
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -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 | ||
} |
Oops, something went wrong.