Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ coverage/
liquibase.properties
liquibase_libs/

.junie
.junie

coverage.out
14 changes: 7 additions & 7 deletions internal/flow/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ import (
"time"
)

type Handler interface {
CreateFlow(w http.ResponseWriter, r *http.Request)
GetFlowById(w http.ResponseWriter, r *http.Request)
GetFlowsByProduct(w http.ResponseWriter, r *http.Request)
}

func NewHandler(dbConn db.DBTX) Handler {
queries := db.New(dbConn)
service := &service{
service := &postgresService{
queries: queries,
}
return &flowHandler{
flowService: service,
}
}

type flowService interface {
CreateFlow(ctx context.Context, req createFlowRequest, id int) (db.Flow, error)
GetFlowById(ctx context.Context, id int) (db.Flow, error)
GetFlowsByProduct(ctx context.Context, id int) ([]db.Flow, error)
}

type flowHandler struct {
flowService flowService
}
Expand Down
6 changes: 3 additions & 3 deletions internal/flow/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestCreateFlow(t *testing.T) {
if tt.mockSetup != nil {
tt.mockSetup(mock)
}
h := &flowHandler{flowService: &service{queries: mock}}
h := &flowHandler{flowService: &postgresService{queries: mock}}

var body []byte
if s, ok := tt.requestBody.(string); ok {
Expand Down Expand Up @@ -260,7 +260,7 @@ func TestGetFlowById(t *testing.T) {
if tt.mockSetup != nil {
tt.mockSetup(mock)
}
h := &flowHandler{flowService: &service{queries: mock}}
h := &flowHandler{flowService: &postgresService{queries: mock}}

req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/flows/1", nil)
req.SetPathValue("id", tt.pathID)
Expand Down Expand Up @@ -367,7 +367,7 @@ func TestGetFlowsByProduct(t *testing.T) {
if tt.mockSetup != nil {
tt.mockSetup(mock)
}
h := &flowHandler{flowService: &service{queries: mock}}
h := &flowHandler{flowService: &postgresService{queries: mock}}

req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/products/1/flows", nil)
req.SetPathValue("id", tt.pathID)
Expand Down
9 changes: 0 additions & 9 deletions internal/flow/interface.go

This file was deleted.

14 changes: 10 additions & 4 deletions internal/flow/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ import (
"github.com/jackc/pgx/v5"
)

type service struct {
type flowService interface {
CreateFlow(ctx context.Context, req createFlowRequest, id int) (db.Flow, error)
GetFlowById(ctx context.Context, id int) (db.Flow, error)
GetFlowsByProduct(ctx context.Context, id int) ([]db.Flow, error)
}

type postgresService struct {
queries db.Querier
}

func (s *service) CreateFlow(ctx context.Context, req createFlowRequest, id int) (db.Flow, error) {
func (s *postgresService) CreateFlow(ctx context.Context, req createFlowRequest, id int) (db.Flow, error) {

flow, err := s.queries.CreateFlow(ctx, req.ToParams(id))
if err != nil {
Expand All @@ -26,7 +32,7 @@ func (s *service) CreateFlow(ctx context.Context, req createFlowRequest, id int)
return flow, nil
}

func (s *service) GetFlowById(ctx context.Context, id int) (db.Flow, error) {
func (s *postgresService) GetFlowById(ctx context.Context, id int) (db.Flow, error) {
flow, err := s.queries.GetFlow(ctx, id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
Expand All @@ -37,7 +43,7 @@ func (s *service) GetFlowById(ctx context.Context, id int) (db.Flow, error) {
return flow, nil
}

func (s *service) GetFlowsByProduct(ctx context.Context, id int) ([]db.Flow, error) {
func (s *postgresService) GetFlowsByProduct(ctx context.Context, id int) ([]db.Flow, error) {
_, err := s.queries.GetProductById(ctx, id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
Expand Down
6 changes: 3 additions & 3 deletions internal/flow/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestService_CreateFlow(t *testing.T) {
if tt.mockSetup != nil {
tt.mockSetup(mock)
}
s := &service{queries: mock}
s := &postgresService{queries: mock}

flow, err := s.CreateFlow(context.Background(), tt.req, tt.productID)

Expand Down Expand Up @@ -126,7 +126,7 @@ func TestService_GetFlowById(t *testing.T) {
if tt.mockSetup != nil {
tt.mockSetup(mock)
}
s := &service{queries: mock}
s := &postgresService{queries: mock}

flow, err := s.GetFlowById(context.Background(), tt.id)

Expand Down Expand Up @@ -238,7 +238,7 @@ func TestService_GetFlowsByProduct(t *testing.T) {
if tt.mockSetup != nil {
tt.mockSetup(mock)
}
s := &service{queries: mock}
s := &postgresService{queries: mock}

flows, err := s.GetFlowsByProduct(context.Background(), tt.productID)

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.

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.

42 changes: 24 additions & 18 deletions internal/platform/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,27 @@ import (
"errors"
"net/http"
"products/internal"
"products/internal/platform/db"
"time"

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

func NewPlatformHandler(db DBTX) Handler {
queries := &Queries{
db: db,
}
type Handler interface {
CreatePlatform(w http.ResponseWriter, r *http.Request)
GetPlatforms(w http.ResponseWriter, r *http.Request)
GetPlatform(w http.ResponseWriter, r *http.Request)
UpdatePlatform(w http.ResponseWriter, r *http.Request)
DeletePlatform(w http.ResponseWriter, r *http.Request)
}

func NewPlatformHandler(dbConn db.DBTX) Handler {
queries := db.New(dbConn)
return &platformHandler{
queries: queries,
service: postgresService{db: queries},
}
}

type platformHandler struct {
queries Querier
service platformService
}

func (h *platformHandler) CreatePlatform(w http.ResponseWriter, r *http.Request) {
Expand All @@ -37,12 +42,13 @@ func (h *platformHandler) CreatePlatform(w http.ResponseWriter, r *http.Request)
}
contextWithTimeOut, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
if err := h.queries.CreatePlatform(contextWithTimeOut, req.ToParams()); err != nil {
platform, err := h.service.CreatePlatform(contextWithTimeOut, req)
if err != nil {
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Failed to create platform"}, http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusCreated)
internal.WriteJSONResponse(w, r, http.StatusCreated, platform)
}

func (h *platformHandler) UpdatePlatform(w http.ResponseWriter, r *http.Request) {
Expand All @@ -68,9 +74,9 @@ func (h *platformHandler) UpdatePlatform(w http.ResponseWriter, r *http.Request)
}
contextWithTimeOut, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
_, err := h.queries.UpdatePlatform(contextWithTimeOut, req.ToParams(id))
_, err := h.service.UpdatePlatform(contextWithTimeOut, req, id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
if errors.Is(err, internal.NotFoundError{}) {
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Platform not found"}, http.StatusNotFound)
return
}
Expand All @@ -89,9 +95,9 @@ func (h *platformHandler) DeletePlatform(w http.ResponseWriter, r *http.Request)
}
contextWithTimeOut, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
_, err := h.queries.DeletePlatform(contextWithTimeOut, id)
_, err := h.service.DeletePlatform(contextWithTimeOut, id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
if errors.Is(err, internal.NotFoundError{}) {
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Platform not found"}, http.StatusNotFound)
return
}
Expand All @@ -107,13 +113,13 @@ func (h *platformHandler) DeletePlatform(w http.ResponseWriter, r *http.Request)
func (h *platformHandler) GetPlatforms(w http.ResponseWriter, r *http.Request) {
contextWithTimeOut, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
platforms, err := h.queries.GetPlatforms(contextWithTimeOut)
platforms, err := h.service.GetPlatforms(contextWithTimeOut)
if err != nil {
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Failed to fetch platforms"}, http.StatusInternalServerError)
return
}
if platforms == nil {
platforms = []Platform{}
platforms = []db.Platform{}
}
internal.WriteJSONResponse(w, r, http.StatusOK, platforms)
}
Expand All @@ -126,9 +132,9 @@ func (h *platformHandler) GetPlatform(w http.ResponseWriter, r *http.Request) {
}
contextWithTimeOut, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
platform, err := h.queries.GetPlatform(contextWithTimeOut, id)
platform, err := h.service.GetPlatform(contextWithTimeOut, id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
if errors.Is(err, internal.NotFoundError{}) {
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Platform not found"}, http.StatusNotFound)
return
}
Expand Down
Loading
Loading