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
2 changes: 1 addition & 1 deletion api/platform/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"net/http"
"products/internal/db"
db "products/internal/db/platform"
"time"

"github.com/jackc/pgx/v5/pgtype"
Expand Down
2 changes: 1 addition & 1 deletion api/platform/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
"net/http"
"products/internal"
"products/internal/db"
db "products/internal/db/platform"
"time"

"github.com/jackc/pgx/v5"
Expand Down
6 changes: 4 additions & 2 deletions api/platform/platform.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package platformHandler

import "products/internal/db/platform"

type PlatformHandler struct {
queries PlatformQuerier
queries platform.Querier
}

func NewPlatformHandler(q PlatformQuerier) *PlatformHandler {
func NewPlatformHandler(q platform.Querier) *PlatformHandler {
return &PlatformHandler{
queries: q,
}
Expand Down
14 changes: 0 additions & 14 deletions api/platform/platform_querier.go

This file was deleted.

72 changes: 36 additions & 36 deletions api/platform/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"errors"
"net/http"
"net/http/httptest"
"products/internal/db"
"products/internal/db/platform"
"strconv"
"testing"

Expand All @@ -17,14 +17,14 @@ import (

type mockPlatformQuerier struct {
err error
createPlatform func(ctx context.Context, arg db.CreatePlatformParams) error
getPlatforms func(ctx context.Context) ([]db.Platform, error)
getPlatform func(ctx context.Context, id int32) (db.Platform, error)
createPlatform func(ctx context.Context, arg platform.CreatePlatformParams) error
getPlatforms func(ctx context.Context) ([]platform.Platform, error)
getPlatform func(ctx context.Context, id int32) (platform.Platform, error)
deletePlatform func(ctx context.Context, id int32) (int32, error)
updatePlatform func(ctx context.Context, arg db.UpdatePlatformParams) (int32, error)
updatePlatform func(ctx context.Context, arg platform.UpdatePlatformParams) (int32, error)
}

func (m *mockPlatformQuerier) CreatePlatform(ctx context.Context, arg db.CreatePlatformParams) error {
func (m *mockPlatformQuerier) CreatePlatform(ctx context.Context, arg platform.CreatePlatformParams) error {
if m.createPlatform != nil {
return m.createPlatform(ctx, arg)
}
Expand All @@ -38,21 +38,21 @@ func (m *mockPlatformQuerier) DeletePlatform(ctx context.Context, id int32) (int
return -1, m.err
}

func (m *mockPlatformQuerier) GetPlatform(ctx context.Context, id int32) (db.Platform, error) {
func (m *mockPlatformQuerier) GetPlatform(ctx context.Context, id int32) (platform.Platform, error) {
if m.getPlatform != nil {
return m.getPlatform(ctx, id)
}
return db.Platform{}, m.err
return platform.Platform{}, m.err
}

func (m *mockPlatformQuerier) GetPlatforms(ctx context.Context) ([]db.Platform, error) {
func (m *mockPlatformQuerier) GetPlatforms(ctx context.Context) ([]platform.Platform, error) {
if m.getPlatforms != nil {
return m.getPlatforms(ctx)
}
return nil, m.err
}

func (m *mockPlatformQuerier) UpdatePlatform(ctx context.Context, arg db.UpdatePlatformParams) (int32, error) {
func (m *mockPlatformQuerier) UpdatePlatform(ctx context.Context, arg platform.UpdatePlatformParams) (int32, error) {
if m.updatePlatform != nil {
return m.updatePlatform(ctx, arg)
}
Expand Down Expand Up @@ -129,12 +129,12 @@ func TestGetPlatforms(t *testing.T) {
tests := []struct {
name string
dbErr error
platforms []db.Platform
platforms []platform.Platform
expectedStatus int
}{
{
name: "Success",
platforms: []db.Platform{
platforms: []platform.Platform{
{ID: 1, Name: "Platform 1", Description: pgtype.Text{String: "Desc 1", Valid: true}},
{ID: 2, Name: "Platform 2", Description: pgtype.Text{String: "Desc 2", Valid: true}},
},
Expand All @@ -143,7 +143,7 @@ func TestGetPlatforms(t *testing.T) {
},
{
name: "Empty Success",
platforms: []db.Platform{},
platforms: []platform.Platform{},
dbErr: nil,
expectedStatus: http.StatusOK,
},
Expand All @@ -159,7 +159,7 @@ func TestGetPlatforms(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
mDB := &mockPlatformQuerier{
err: tt.dbErr,
getPlatforms: func(ctx context.Context) ([]db.Platform, error) {
getPlatforms: func(ctx context.Context) ([]platform.Platform, error) {
return tt.platforms, tt.dbErr
},
}
Expand All @@ -175,7 +175,7 @@ func TestGetPlatforms(t *testing.T) {
}

if tt.expectedStatus == http.StatusOK {
var got []db.Platform
var got []platform.Platform
err := json.Unmarshal(rr.Body.Bytes(), &got)
if err != nil {
t.Fatalf("failed to unmarshal response: %v", err)
Expand All @@ -192,56 +192,56 @@ func TestGetPlatform(t *testing.T) {
tests := []struct {
name string
id string
dbPlatform db.Platform
dbPlatform platform.Platform
dbErr error
expectedStatus int
}{
{
name: "Success",
id: "1",
dbPlatform: db.Platform{ID: 1, Name: "Platform 1"},
dbPlatform: platform.Platform{ID: 1, Name: "Platform 1"},
dbErr: nil,
expectedStatus: http.StatusOK,
},
{
name: "Invalid ID",
id: "abc",
dbPlatform: db.Platform{},
dbPlatform: platform.Platform{},
dbErr: nil,
expectedStatus: http.StatusBadRequest,
},
{
name: "Invalid ID (Zero)",
id: "0",
dbPlatform: db.Platform{},
dbPlatform: platform.Platform{},
dbErr: nil,
expectedStatus: http.StatusBadRequest,
},
{
name: "Invalid ID (Negative)",
id: "-1",
dbPlatform: db.Platform{},
dbPlatform: platform.Platform{},
dbErr: nil,
expectedStatus: http.StatusBadRequest,
},
{
name: "Invalid ID (Overflow)",
id: "2147483648",
dbPlatform: db.Platform{},
dbPlatform: platform.Platform{},
dbErr: nil,
expectedStatus: http.StatusBadRequest,
},
{
name: "Not Found",
id: "999",
dbPlatform: db.Platform{},
dbPlatform: platform.Platform{},
dbErr: pgx.ErrNoRows,
expectedStatus: http.StatusNotFound,
},
{
name: "DB Error",
id: "1",
dbPlatform: db.Platform{},
dbPlatform: platform.Platform{},
dbErr: errors.New("db error"),
expectedStatus: http.StatusInternalServerError,
},
Expand All @@ -251,7 +251,7 @@ func TestGetPlatform(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
mDB := &mockPlatformQuerier{
err: tt.dbErr,
getPlatform: func(ctx context.Context, id int32) (db.Platform, error) {
getPlatform: func(ctx context.Context, id int32) (platform.Platform, error) {
return tt.dbPlatform, tt.dbErr
},
}
Expand All @@ -268,7 +268,7 @@ func TestGetPlatform(t *testing.T) {
}

if tt.expectedStatus == http.StatusOK {
var got db.Platform
var got platform.Platform
err := json.Unmarshal(rr.Body.Bytes(), &got)
if err != nil {
t.Fatalf("failed to unmarshal response: %v", err)
Expand Down Expand Up @@ -369,7 +369,7 @@ func TestUpdatePlatform(t *testing.T) {
{
name: "Success",
pathID: "1",
requestBody: db.Platform{
requestBody: platform.Platform{
ID: 1,
Name: "Updated Platform",
Description: pgtype.Text{String: "Updated Description", Valid: true},
Expand All @@ -380,7 +380,7 @@ func TestUpdatePlatform(t *testing.T) {
{
name: "Invalid Path ID",
pathID: "abc",
requestBody: db.Platform{
requestBody: platform.Platform{
ID: 1,
Name: "Updated Platform",
},
Expand All @@ -390,7 +390,7 @@ func TestUpdatePlatform(t *testing.T) {
{
name: "Invalid Path ID (Zero)",
pathID: "0",
requestBody: db.Platform{
requestBody: platform.Platform{
ID: 0,
Name: "Updated Platform",
},
Expand All @@ -400,7 +400,7 @@ func TestUpdatePlatform(t *testing.T) {
{
name: "Invalid Path ID (Negative)",
pathID: "-1",
requestBody: db.Platform{
requestBody: platform.Platform{
ID: -1,
Name: "Updated Platform",
},
Expand All @@ -410,7 +410,7 @@ func TestUpdatePlatform(t *testing.T) {
{
name: "Invalid Path ID (Overflow)",
pathID: "2147483648",
requestBody: db.Platform{
requestBody: platform.Platform{
ID: 1,
Name: "Updated Platform",
},
Expand All @@ -420,7 +420,7 @@ func TestUpdatePlatform(t *testing.T) {
{
name: "ID Mismatch",
pathID: "2",
requestBody: db.Platform{
requestBody: platform.Platform{
ID: 1,
Name: "Updated Platform",
},
Expand All @@ -430,7 +430,7 @@ func TestUpdatePlatform(t *testing.T) {
{
name: "Missing Name",
pathID: "1",
requestBody: db.Platform{
requestBody: platform.Platform{
ID: 1,
Name: "",
},
Expand All @@ -447,7 +447,7 @@ func TestUpdatePlatform(t *testing.T) {
{
name: "Platform Not Found",
pathID: "999",
requestBody: db.Platform{
requestBody: platform.Platform{
ID: 999,
Name: "Non-existent",
},
Expand All @@ -457,7 +457,7 @@ func TestUpdatePlatform(t *testing.T) {
{
name: "DB Error",
pathID: "1",
requestBody: db.Platform{
requestBody: platform.Platform{
ID: 1,
Name: "Test Platform",
},
Expand All @@ -470,9 +470,9 @@ func TestUpdatePlatform(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
mDB := &mockPlatformQuerier{
err: tt.dbErr,
updatePlatform: func(ctx context.Context, arg db.UpdatePlatformParams) (int32, error) {
updatePlatform: func(ctx context.Context, arg platform.UpdatePlatformParams) (int32, error) {
if tt.name == "Success" {
expectedBody := tt.requestBody.(db.Platform)
expectedBody := tt.requestBody.(platform.Platform)
if arg.ID != expectedBody.ID {
t.Errorf("expected ID %d, got %d", expectedBody.ID, arg.ID)
}
Expand Down
2 changes: 1 addition & 1 deletion api/platform/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
"net/http"
"products/internal"
"products/internal/db"
db "products/internal/db/platform"
"time"

"github.com/jackc/pgx/v5"
Expand Down
7 changes: 7 additions & 0 deletions api/product/product.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package productHandler

import "products/internal/db/product"

type ProductHandler struct {
queries product.Querier
}
Comment thread
jlpdeveloper marked this conversation as resolved.
32 changes: 32 additions & 0 deletions internal/db/platform/db.go

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

Loading
Loading