Skip to content

Commit

Permalink
make more refactoring for 2022
Browse files Browse the repository at this point in the history
  • Loading branch information
booscaaa committed Jan 27, 2022
1 parent 20dfc5e commit 835bc3d
Show file tree
Hide file tree
Showing 30 changed files with 797 additions and 302 deletions.
41 changes: 3 additions & 38 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.14.x, 1.15.x]
go-version: [1.17.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -15,47 +15,12 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test ./... -coverprofile=coverage.txt -covermode=atomic
run: go mod tidy && go test ./... -coverprofile=coverage.txt -covermode=atomic

- name: Upload coverage report
uses: codecov/codecov-action@v1.0.2
with:
token: 49f83277-ece6-46e9-bae5-a1e2ba7530bb
file: ./coverage.txt
flags: unittests
name: codecov-umbrella

test-cache:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.15.x
- name: Checkout code
uses: actions/checkout@v2
- uses: actions/cache@v2
with:
# In order:
# * Module download cache
# * Build cache (Linux)
# * Build cache (Mac)
# * Build cache (Windows)
path: |
~/go/pkg/mod
~/.cache/go-build
~/Library/Caches/go-build
%LocalAppData%\go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Test
run: go test ./... -coverprofile=coverage.txt -covermode=atomic

- name: Upload coverage report
uses: codecov/codecov-action@v1.0.2
with:
token: 49f83277-ece6-46e9-bae5-a1e2ba7530bb
file: ./coverage.txt
flags: unittests
name: codecov-umbrella
name: codecov-umbrella
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
go.sum
cover.out
docs
cover.html
docs

tmp
config.json
goapp
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<h1 align="center">Clean architecture and Level 3 of REST</h1>
<p align="center">An application of studies on the implementation of clean architecture with golang with a plus of REST level 3 implementations.</p>
<p align="center">
<a href="https://github.com/booscaaa/golang-clean-arch-hateoas-example/releases/latest"><img alt="Release" src="https://img.shields.io/github/v/release/booscaaa/golang-clean-arch-hateoas-example.svg?style=for-the-badge"></a>
<a href="https://github.com/booscaaa/github.com/booscaaa/golang-clean-arch-hateoas-example/releases/latest"><img alt="Release" src="https://img.shields.io/github/v/release/booscaaa/github.com/booscaaa/golang-clean-arch-hateoas-example.svg?style=for-the-badge"></a>
<a href="/LICENSE"><img alt="Software License" src="https://img.shields.io/badge/license-MIT-red.svg?style=for-the-badge"></a>
<a href="https://github.com/booscaaa/golang-clean-arch-hateoas-example/actions?query=workflow%3ADeploy"><img alt="Build status" src="https://img.shields.io/github/workflow/status/booscaaa/golang-clean-arch-hateoas-example/Deploy?style=for-the-badge"></a>
<a href="https://codecov.io/gh/booscaaa/golang-clean-arch-hateoas-example"><img alt="Coverage" src="https://img.shields.io/codecov/c/github/booscaaa/golang-clean-arch-hateoas-example/master.svg?style=for-the-badge"></a>
<a href="https://github.com/booscaaa/github.com/booscaaa/golang-clean-arch-hateoas-example/actions?query=workflow%3ADeploy"><img alt="Build status" src="https://img.shields.io/github/workflow/status/booscaaa/github.com/booscaaa/golang-clean-arch-hateoas-example/Deploy?style=for-the-badge"></a>
<a href="https://codecov.io/gh/booscaaa/github.com/booscaaa/golang-clean-arch-hateoas-example"><img alt="Coverage" src="https://img.shields.io/codecov/c/github/booscaaa/github.com/booscaaa/golang-clean-arch-hateoas-example/master.svg?style=for-the-badge"></a>
<a href="https://todo-list-hateoas.herokuapp.com/doc/index.html"><img alt="Swagger docs" src="https://img.shields.io/badge/api docs-swagger-purple?style=for-the-badge"></a>
</p>
</p>
Expand Down Expand Up @@ -47,9 +47,9 @@ To run this project in the development mode, you'll need to have a basic environ
**Cloning the Repository**

```bash
$ git clone https://github.com/booscaaa/golang-clean-arch-hateoas-example
$ git clone https://github.com/booscaaa/github.com/booscaaa/golang-clean-arch-hateoas-example

$ cd golang-clean-arch-hateoas-example
$ cd github.com/booscaaa/golang-clean-arch-hateoas-example
```

<br>
Expand Down Expand Up @@ -143,4 +143,4 @@ Thank you!

## License

This project is licensed under the MIT License - see the [LICENSE.md](https://github.com/booscaaa/golang-clean-arch-hateoas-example/blob/master/LICENSE) file for details
This project is licensed under the MIT License - see the [LICENSE.md](https://github.com/booscaaa/github.com/booscaaa/golang-clean-arch-hateoas-example/blob/master/LICENSE) file for details
15 changes: 8 additions & 7 deletions adapter/http/endpoint/item_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package endpoint
import (
"encoding/json"
"fmt"
"golang-clean-arch-hateoas-example/adapter/http/util"
"golang-clean-arch-hateoas-example/core/domain"
"net/http"
"strconv"

"github.com/booscaaa/golang-clean-arch-hateoas-example/adapter/http/util"
"github.com/booscaaa/golang-clean-arch-hateoas-example/core/domain"

"github.com/gorilla/mux"
)

Expand Down Expand Up @@ -150,19 +151,19 @@ func (i Item) GetItemByID(response http.ResponseWriter, request *http.Request) {
}

// FetchItems goDoc
// @Summary Fetch items by sigla
// @Description Fetch items by sigla
// @Summary Fetch items by initials
// @Description Fetch items by initials
// @Tags item
// @Accept json
// @Produce json
// @Param sigla query string true "vnb"
// @Param initials query string true "vnb"
// @Success 200 {object} domain.Item
// @Security ApiKeyAuth
// @Router /item [get]
func (i Item) FetchItems(response http.ResponseWriter, request *http.Request) {
sigla := request.FormValue("sigla")
initials := request.FormValue("initials")

items, err := i.Fetch(sigla)
items, err := i.Fetch(initials)

if err != nil {
response.WriteHeader(500)
Expand Down
15 changes: 8 additions & 7 deletions adapter/http/http.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package http

import (
"golang-clean-arch-hateoas-example/adapter/http/endpoint"
"golang-clean-arch-hateoas-example/adapter/http/util"
"golang-clean-arch-hateoas-example/adapter/postgres/item_repository"
"golang-clean-arch-hateoas-example/core/item"
"net/http"

_ "golang-clean-arch-hateoas-example/docs"
"github.com/booscaaa/golang-clean-arch-hateoas-example/adapter/http/endpoint"
"github.com/booscaaa/golang-clean-arch-hateoas-example/adapter/http/util"
"github.com/booscaaa/golang-clean-arch-hateoas-example/adapter/postgres/item_repository"
"github.com/booscaaa/golang-clean-arch-hateoas-example/core/item"

_ "github.com/booscaaa/golang-clean-arch-hateoas-example/docs"

"github.com/gorilla/mux"
"github.com/jackc/pgx/v4/pgxpool"
Expand All @@ -21,7 +22,7 @@ func GenerateRoutes(conn *pgxpool.Pool) *mux.Router {
cors := util.Cors
jsonApi.Use(cors)

itemRepository := item_repository.ItemRepositoryImpl(conn)
itemRepository := item_repository.NewItemRepository(conn)

itemUseCase := endpoint.Item{
ItemUsecase: item.ItemUseCaseImpl(itemRepository),
Expand All @@ -34,7 +35,7 @@ func GenerateRoutes(conn *pgxpool.Pool) *mux.Router {
jsonApi.Handle("/item/{id}", http.HandlerFunc(itemUseCase.GetItemByID)).Methods("GET", "OPTIONS").Name("/item")
jsonApi.Handle("/item/{id}", http.HandlerFunc(itemUseCase.DeleteItem)).Methods("DELETE", "OPTIONS").Name("/item")
jsonApi.Handle("/item", http.HandlerFunc(itemUseCase.FetchItems)).Queries(
"sigla", "{sigla}",
"initials", "{initials}",
).Methods("GET", "OPTIONS")

return r
Expand Down
15 changes: 14 additions & 1 deletion adapter/postgres/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ import (
"fmt"
"os"

"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
_ "github.com/lib/pq"
"github.com/spf13/viper"
)

type PoolInterface interface {
Close()
Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(pgx.QueryFuncRow) error) (pgconn.CommandTag, error)
SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
Begin(ctx context.Context) (pgx.Tx, error)
BeginFunc(ctx context.Context, f func(pgx.Tx) error) error
BeginTxFunc(ctx context.Context, txOptions pgx.TxOptions, f func(pgx.Tx) error) error
}

func GetConnection(context context.Context) *pgxpool.Pool {
databaseUrl := viper.GetString("database.url")
conn, err := pgxpool.Connect(context, databaseUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@ package item_repository
import (
"context"
"fmt"
"golang-clean-arch-hateoas-example/core/domain"

"github.com/booscaaa/golang-clean-arch-hateoas-example/core/domain"

"github.com/jackc/pgx/v4"
)

func (db *itemRepository) Create(name, description, date, sigla string) (*domain.Item, error) {
func (db *itemRepository) Create(date, description, name, initials string) (*domain.Item, error) {
var idA int
var nameA string
var descriptionA string
var dateA string
var siglaA string
var initialsA string

ctx := context.Background()

err := db.database.QueryRow(
ctx,
"INSERT INTO item (nome, descricao, data, sigla) VALUES ($1, $2, to_timestamp($3, 'YYYY-MM-DD HH24:MI:SS'), $4) returning *;",
name, description, date, sigla,
"INSERT INTO item (name, description, date, initials) VALUES ($1, $2, to_timestamp($3, 'YYYY-MM-DD HH24:MI:SS'), $4) returning *;",
name, description, date, initials,
).Scan(
&idA,
&nameA,
&descriptionA,
&dateA,
&siglaA,
&initialsA,
)

if err == pgx.ErrNoRows {
Expand All @@ -37,7 +38,7 @@ func (db *itemRepository) Create(name, description, date, sigla string) (*domain
return nil, err
}

item, err := domain.NewItem(idA, nameA, descriptionA, dateA, siglaA)
item, err := domain.NewItem(idA, nameA, descriptionA, dateA, initialsA)

if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 835bc3d

Please sign in to comment.