Skip to content

Commit

Permalink
renaming of project structure and layout of data package
Browse files Browse the repository at this point in the history
  • Loading branch information
ardan-bkennedy committed Jun 16, 2020
1 parent 5039338 commit 45588c0
Show file tree
Hide file tree
Showing 54 changed files with 760 additions and 805 deletions.
6 changes: 3 additions & 3 deletions .gitignore
@@ -1,7 +1,7 @@
# Binaries
cmd/sales-admin/sales-admin
cmd/sales-api/sales-api
cmd/sidecar/metrics/metrics
app/sales-admin/sales-admin
app/sales-api/sales-api
app/sidecar/metrics/metrics

# Mac
.DS_Store
Expand Down
3 changes: 3 additions & 0 deletions app/sales-admin/commands/doc.go
@@ -0,0 +1,3 @@
// Package commands contains the functionality for the set of commands
// currently supported by the CLI tooling.
package commands
Expand Up @@ -7,9 +7,9 @@ import (
"io/ioutil"
"time"

"github.com/ardanlabs/service/internal/auth"
"github.com/ardanlabs/service/internal/data"
"github.com/ardanlabs/service/internal/platform/database"
"github.com/ardanlabs/service/business/auth"
"github.com/ardanlabs/service/business/data/user"
"github.com/ardanlabs/service/foundation/database"
"github.com/dgrijalva/jwt-go"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -38,7 +38,7 @@ func GenToken(cfg database.Config, id string, privateKeyFile string, algorithm s
},
Roles: []string{auth.RoleAdmin},
}
user, err := data.Retrieve.User.One(ctx, claims, db, id)
user, err := user.One(ctx, claims, db, id)
if err != nil {
return errors.Wrap(err, "retrieve user")
}
Expand Down
File renamed without changes.
Expand Up @@ -3,8 +3,8 @@ package commands
import (
"fmt"

"github.com/ardanlabs/service/internal/data"
"github.com/ardanlabs/service/internal/platform/database"
"github.com/ardanlabs/service/business/data"
"github.com/ardanlabs/service/foundation/database"
"github.com/pkg/errors"
)

Expand Down
Expand Up @@ -3,8 +3,8 @@ package commands
import (
"fmt"

"github.com/ardanlabs/service/internal/data"
"github.com/ardanlabs/service/internal/platform/database"
"github.com/ardanlabs/service/business/data"
"github.com/ardanlabs/service/foundation/database"
"github.com/pkg/errors"
)

Expand Down
Expand Up @@ -5,9 +5,10 @@ import (
"fmt"
"time"

"github.com/ardanlabs/service/internal/auth"
"github.com/ardanlabs/service/internal/data"
"github.com/ardanlabs/service/internal/platform/database"
"github.com/ardanlabs/service/business/auth"
"github.com/ardanlabs/service/business/data"
"github.com/ardanlabs/service/business/data/user"
"github.com/ardanlabs/service/foundation/database"
"github.com/pkg/errors"
)

Expand All @@ -33,7 +34,7 @@ func UserAdd(cfg database.Config, email, password string) error {
PasswordConfirm: password,
Roles: []string{auth.RoleAdmin, auth.RoleUser},
}
u, err := data.Create.User(ctx, db, nu, time.Now())
u, err := user.Create(ctx, db, nu, time.Now())
if err != nil {
return errors.Wrap(err, "create user")
}
Expand Down
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"time"

"github.com/ardanlabs/service/internal/data"
"github.com/ardanlabs/service/internal/platform/database"
"github.com/ardanlabs/service/business/data/user"
"github.com/ardanlabs/service/foundation/database"
"github.com/pkg/errors"
)

Expand All @@ -22,7 +22,7 @@ func Users(cfg database.Config) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

users, err := data.Retrieve.User.List(ctx, db)
users, err := user.List(ctx, db)
if err != nil {
return errors.Wrap(err, "retrieve users")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/sales-admin/main.go → app/sales-admin/main.go
Expand Up @@ -8,8 +8,8 @@ import (
"os"

"github.com/ardanlabs/conf"
"github.com/ardanlabs/service/cmd/sales-admin/internal/commands"
"github.com/ardanlabs/service/internal/platform/database"
"github.com/ardanlabs/service/app/sales-admin/commands"
"github.com/ardanlabs/service/foundation/database"
"github.com/pkg/errors"
)

Expand Down
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"net/http"

"github.com/ardanlabs/service/internal/platform/database"
"github.com/ardanlabs/service/internal/platform/web"
"github.com/ardanlabs/service/foundation/database"
"github.com/ardanlabs/service/foundation/web"
"github.com/jmoiron/sqlx"
"go.opentelemetry.io/otel/api/global"
)
Expand Down
@@ -1,13 +1,15 @@
// Package handlers contains the full set of handler functions and routes
// supported by the web api.
package handlers

import (
"log"
"net/http"
"os"

"github.com/ardanlabs/service/internal/auth" // Import is removed in final PR
"github.com/ardanlabs/service/internal/mid"
"github.com/ardanlabs/service/internal/platform/web"
"github.com/ardanlabs/service/business/auth" // Import is removed in final PR
"github.com/ardanlabs/service/business/mid"
"github.com/ardanlabs/service/foundation/web"
"github.com/jmoiron/sqlx"
)

Expand All @@ -25,7 +27,7 @@ func API(build string, shutdown chan os.Signal, log *log.Logger, db *sqlx.DB, au
app.Handle(http.MethodGet, "/v1/health", c.health)

// Register user management and authentication endpoints.
u := user{
u := userHandlers{
db: db,
authenticator: authenticator,
}
Expand All @@ -39,7 +41,7 @@ func API(build string, shutdown chan os.Signal, log *log.Logger, db *sqlx.DB, au
app.Handle(http.MethodGet, "/v1/users/token", u.token)

// Register product and sale endpoints.
p := product{
p := productHandlers{
db: db,
}
app.Handle(http.MethodGet, "/v1/products", p.list, mid.Authenticate(authenticator))
Expand Down
Expand Up @@ -4,36 +4,37 @@ import (
"context"
"net/http"

"github.com/ardanlabs/service/internal/auth"
"github.com/ardanlabs/service/internal/data"
"github.com/ardanlabs/service/internal/platform/web"
"github.com/ardanlabs/service/business/auth"
"github.com/ardanlabs/service/business/data"
"github.com/ardanlabs/service/business/data/product"
"github.com/ardanlabs/service/foundation/web"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/api/global"
)

type product struct {
type productHandlers struct {
db *sqlx.DB
}

func (p *product) list(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (h *productHandlers) list(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ctx, span := global.Tracer("service").Start(ctx, "handlers.product.list")
defer span.End()

products, err := data.Retrieve.Product.List(ctx, p.db)
products, err := product.List(ctx, h.db)
if err != nil {
return err
}

return web.Respond(ctx, w, products, http.StatusOK)
}

func (p *product) retrieve(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (h *productHandlers) retrieve(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ctx, span := global.Tracer("service").Start(ctx, "handlers.product.retrieve")
defer span.End()

params := web.Params(r)
prod, err := data.Retrieve.Product.One(ctx, p.db, params["id"])
prod, err := product.One(ctx, h.db, params["id"])
if err != nil {
switch err {
case data.ErrInvalidID:
Expand All @@ -48,7 +49,7 @@ func (p *product) retrieve(ctx context.Context, w http.ResponseWriter, r *http.R
return web.Respond(ctx, w, prod, http.StatusOK)
}

func (p *product) create(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (h *productHandlers) create(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ctx, span := global.Tracer("service").Start(ctx, "handlers.product.create")
defer span.End()

Expand All @@ -67,15 +68,15 @@ func (p *product) create(ctx context.Context, w http.ResponseWriter, r *http.Req
return errors.Wrap(err, "decoding new product")
}

prod, err := data.Create.Product(ctx, p.db, claims, np, v.Now)
prod, err := product.Create(ctx, h.db, claims, np, v.Now)
if err != nil {
return errors.Wrapf(err, "creating new product: %+v", np)
}

return web.Respond(ctx, w, prod, http.StatusCreated)
}

func (p *product) update(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (h *productHandlers) update(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ctx, span := global.Tracer("service").Start(ctx, "handlers.product.update")
defer span.End()

Expand All @@ -95,7 +96,7 @@ func (p *product) update(ctx context.Context, w http.ResponseWriter, r *http.Req
}

params := web.Params(r)
if err := data.Update.Product(ctx, p.db, claims, params["id"], up, v.Now); err != nil {
if err := product.Update(ctx, h.db, claims, params["id"], up, v.Now); err != nil {
switch err {
case data.ErrInvalidID:
return web.NewRequestError(err, http.StatusBadRequest)
Expand All @@ -111,12 +112,12 @@ func (p *product) update(ctx context.Context, w http.ResponseWriter, r *http.Req
return web.Respond(ctx, w, nil, http.StatusNoContent)
}

func (p *product) delete(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (h *productHandlers) delete(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ctx, span := global.Tracer("service").Start(ctx, "handlers.product.delete")
defer span.End()

params := web.Params(r)
if err := data.Delete.Product(ctx, p.db, params["id"]); err != nil {
if err := product.Delete(ctx, h.db, params["id"]); err != nil {
switch err {
case data.ErrInvalidID:
return web.NewRequestError(err, http.StatusBadRequest)
Expand Down
Expand Up @@ -4,32 +4,33 @@ import (
"context"
"net/http"

"github.com/ardanlabs/service/internal/auth"
"github.com/ardanlabs/service/internal/data"
"github.com/ardanlabs/service/internal/platform/web"
"github.com/ardanlabs/service/business/auth"
"github.com/ardanlabs/service/business/data"
"github.com/ardanlabs/service/business/data/user"
"github.com/ardanlabs/service/foundation/web"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/api/global"
)

type user struct {
type userHandlers struct {
db *sqlx.DB
authenticator *auth.Authenticator
}

func (u *user) list(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (h *userHandlers) list(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ctx, span := global.Tracer("service").Start(ctx, "handlers.user.list")
defer span.End()

users, err := data.Retrieve.User.List(ctx, u.db)
users, err := user.List(ctx, h.db)
if err != nil {
return err
}

return web.Respond(ctx, w, users, http.StatusOK)
}

func (u *user) retrieve(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (h *userHandlers) retrieve(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ctx, span := global.Tracer("service").Start(ctx, "handlers.user.retrieve")
defer span.End()

Expand All @@ -39,7 +40,7 @@ func (u *user) retrieve(ctx context.Context, w http.ResponseWriter, r *http.Requ
}

params := web.Params(r)
usr, err := data.Retrieve.User.One(ctx, claims, u.db, params["id"])
usr, err := user.One(ctx, claims, h.db, params["id"])
if err != nil {
switch err {
case data.ErrInvalidID:
Expand All @@ -56,7 +57,7 @@ func (u *user) retrieve(ctx context.Context, w http.ResponseWriter, r *http.Requ
return web.Respond(ctx, w, usr, http.StatusOK)
}

func (u *user) create(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (h *userHandlers) create(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ctx, span := global.Tracer("service").Start(ctx, "handlers.user.create")
defer span.End()

Expand All @@ -70,15 +71,15 @@ func (u *user) create(ctx context.Context, w http.ResponseWriter, r *http.Reques
return errors.Wrap(err, "")
}

usr, err := data.Create.User(ctx, u.db, nu, v.Now)
usr, err := user.Create(ctx, h.db, nu, v.Now)
if err != nil {
return errors.Wrapf(err, "User: %+v", &usr)
}

return web.Respond(ctx, w, usr, http.StatusCreated)
}

func (u *user) update(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (h *userHandlers) update(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ctx, span := global.Tracer("service").Start(ctx, "handlers.user.update")
defer span.End()

Expand All @@ -98,7 +99,7 @@ func (u *user) update(ctx context.Context, w http.ResponseWriter, r *http.Reques
}

params := web.Params(r)
err := data.Update.User(ctx, claims, u.db, params["id"], upd, v.Now)
err := user.Update(ctx, claims, h.db, params["id"], upd, v.Now)
if err != nil {
switch err {
case data.ErrInvalidID:
Expand All @@ -115,12 +116,12 @@ func (u *user) update(ctx context.Context, w http.ResponseWriter, r *http.Reques
return web.Respond(ctx, w, nil, http.StatusNoContent)
}

func (u *user) delete(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (h *userHandlers) delete(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ctx, span := global.Tracer("service").Start(ctx, "handlers.user.delete")
defer span.End()

params := web.Params(r)
err := data.Delete.User(ctx, u.db, params["id"])
err := user.Delete(ctx, h.db, params["id"])
if err != nil {
switch err {
case data.ErrInvalidID:
Expand All @@ -137,7 +138,7 @@ func (u *user) delete(ctx context.Context, w http.ResponseWriter, r *http.Reques
return web.Respond(ctx, w, nil, http.StatusNoContent)
}

func (u *user) token(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (h *userHandlers) token(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ctx, span := global.Tracer("service").Start(ctx, "handlers.user.token")
defer span.End()

Expand All @@ -152,7 +153,7 @@ func (u *user) token(ctx context.Context, w http.ResponseWriter, r *http.Request
return web.NewRequestError(err, http.StatusUnauthorized)
}

claims, err := data.Authenticate(ctx, u.db, v.Now, email, pass)
claims, err := user.Authenticate(ctx, h.db, v.Now, email, pass)
if err != nil {
switch err {
case data.ErrAuthenticationFailure:
Expand All @@ -165,7 +166,7 @@ func (u *user) token(ctx context.Context, w http.ResponseWriter, r *http.Request
var tkn struct {
Token string `json:"token"`
}
tkn.Token, err = u.authenticator.GenerateToken(claims)
tkn.Token, err = h.authenticator.GenerateToken(claims)
if err != nil {
return errors.Wrap(err, "generating token")
}
Expand Down

0 comments on commit 45588c0

Please sign in to comment.