Skip to content
Open
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
1 change: 1 addition & 0 deletions document-versioning/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin/
7 changes: 3 additions & 4 deletions document-versioning/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v5 v5.8.0
github.com/oapi-codegen/runtime v1.1.2
github.com/stretchr/testify v1.11.1
github.com/wI2L/jsondiff v0.7.0
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -18,7 +19,7 @@ require (
github.com/bytedance/sonic v1.15.0 // indirect
github.com/bytedance/sonic/loader v0.5.0 // indirect
github.com/cloudwego/base64x v0.1.6 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.13 // indirect
github.com/gin-contrib/sse v1.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand All @@ -37,7 +38,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/quic-go/qpack v0.6.0 // indirect
github.com/quic-go/quic-go v0.59.0 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
Expand All @@ -50,11 +51,9 @@ require (
go.uber.org/mock v0.6.0 // indirect
golang.org/x/arch v0.24.0 // indirect
golang.org/x/crypto v0.48.0 // indirect
golang.org/x/mod v0.33.0 // indirect
golang.org/x/net v0.50.0 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/sys v0.41.0 // indirect
golang.org/x/text v0.34.0 // indirect
golang.org/x/tools v0.42.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect
)
79 changes: 5 additions & 74 deletions document-versioning/go.sum

Large diffs are not rendered by default.

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

124 changes: 92 additions & 32 deletions document-versioning/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func (h *Handler) HealthCheck(c *gin.Context) {
}

// CreateDocument creates a new document
// TODO: Implement this handler
// 1. Parse the request body into api.CreateDocumentRequest
// 2. Call h.store.Create() with the name and content
// 3. Return the created document as api.DocumentResponse with status 201
Expand All @@ -47,38 +46,71 @@ func (h *Handler) CreateDocument(c *gin.Context) {
return
}

// TODO: Call the store to create the document
// doc, err := h.store.Create(c.Request.Context(), req.Name, req.Content)
if req.Name == "" {
c.JSON(http.StatusBadRequest, api.ErrorResponse{
Error: "name is required",
})
return
}
if req.Content == nil {
c.JSON(http.StatusBadRequest, api.ErrorResponse{
Error: "content is required",
})
return
}

c.JSON(http.StatusNotImplemented, api.ErrorResponse{
Error: "not implemented - complete this handler",
doc, err := h.store.Create(c.Request.Context(), req.Name, req.Content)
if err != nil {
c.JSON(http.StatusInternalServerError, api.ErrorResponse{
Error: "failed to create document: " + err.Error(),
})
return
}

c.JSON(http.StatusCreated, api.DocumentResponse{
Id: doc.ID,
Name: doc.Name,
Content: doc.Content,
CreatedAt: doc.CreatedAt,
})
}

// GetDocument retrieves a document, optionally at a specific version
// TODO: Implement this handler
// 1. The document ID is already parsed by the generated code
// 2. Check if a version query parameter was provided (params.Version)
// 3. If version provided, call h.store.GetAtVersion()
// 4. If no version, call h.store.GetCurrent()
// 5. Return the document as api.DocumentResponse
// 6. Handle errors (404 for not found)
func (h *Handler) GetDocument(c *gin.Context, id openapi_types.UUID, params api.GetDocumentParams) {
// The ID is already parsed by the generated wrapper code
// Use id directly (it's a uuid.UUID under the hood)

_ = id // Use this parsed ID

// TODO: Implement document retrieval
// Check params.Version to see if a specific version was requested
var doc *store.Document
var err error
var version int

if params.Version != nil {
doc, err = h.store.GetAtVersion(c.Request.Context(), id, *params.Version)
version = *params.Version
} else {
doc, err = h.store.GetCurrent(c.Request.Context(), id)
version = doc.CurrentVersion
}
if err != nil {
c.JSON(http.StatusNotFound, api.ErrorResponse{
Error: "document not found: " + err.Error(),
})
return
}

c.JSON(http.StatusNotImplemented, api.ErrorResponse{
Error: "not implemented - complete this handler",
c.JSON(http.StatusOK, api.DocumentResponse{
Id: doc.ID,
Name: doc.Name,
Content: doc.Content,
CreatedAt: doc.CreatedAt,
Version: version,
})
}

// UpdateDocument updates a document, creating a new version
// TODO: Implement this handler
// 1. The document ID is already parsed by the generated code
// 2. Parse the request body into api.UpdateDocumentRequest
// 3. Call h.store.Update() with the ID and new content
Expand All @@ -93,33 +125,53 @@ func (h *Handler) UpdateDocument(c *gin.Context, id openapi_types.UUID) {
return
}

_ = id // Use this parsed ID

// TODO: Call the store to update the document
doc, err := h.store.Update(c.Request.Context(), id, req.Content)
if err != nil {
c.JSON(http.StatusNotFound, api.ErrorResponse{
Error: "document not found: " + err.Error(),
})
return
}

c.JSON(http.StatusNotImplemented, api.ErrorResponse{
Error: "not implemented - complete this handler",
c.JSON(http.StatusOK, api.DocumentResponse{
Id: doc.ID,
Name: doc.Name,
Content: doc.Content,
CreatedAt: doc.CreatedAt,
Version: doc.CurrentVersion,
})
}

// ListVersions returns the version history for a document
// TODO: Implement this handler
// 1. The document ID is already parsed by the generated code
// 2. Call h.store.ListVersions()
// 3. Return the version list as api.VersionListResponse
// 4. Handle errors (404 for not found)
func (h *Handler) ListVersions(c *gin.Context, id openapi_types.UUID) {
_ = id // Use this parsed ID
count, storedVersions, err := h.store.ListVersions(c.Request.Context(), id)
if err != nil {
c.JSON(http.StatusNotFound, api.ErrorResponse{
Error: "document not found: " + err.Error(),
})
return
}

// TODO: Call the store to list versions
var versions []api.VersionInfo
for _, v := range storedVersions {
versions = append(versions, api.VersionInfo{
Version: v.Version,
CreatedAt: v.CreatedAt,
})
}

c.JSON(http.StatusNotImplemented, api.ErrorResponse{
Error: "not implemented - complete this handler",
c.JSON(http.StatusOK, api.VersionListResponse{
CurrentVersion: count,
DocumentId: id,
Versions: versions,
})
}

// RevertDocument reverts a document to a specific version
// TODO: Implement this handler
// 1. The document ID is already parsed by the generated code
// 2. Parse the request body into api.RevertRequest
// 3. Call h.store.Revert() with the ID and target version
Expand All @@ -134,11 +186,19 @@ func (h *Handler) RevertDocument(c *gin.Context, id openapi_types.UUID) {
return
}

_ = id // Use this parsed ID

// TODO: Call the store to revert the document
doc, err := h.store.Revert(c.Request.Context(), id, req.Version)
if err != nil {
c.JSON(http.StatusNotFound, api.ErrorResponse{
Error: "document not found or version invalid: " + err.Error(),
})
return
}

c.JSON(http.StatusNotImplemented, api.ErrorResponse{
Error: "not implemented - complete this handler",
c.JSON(http.StatusOK, api.DocumentResponse{
Id: doc.ID,
Name: doc.Name,
Content: doc.Content,
CreatedAt: doc.CreatedAt,
Version: doc.CurrentVersion,
})
}
62 changes: 62 additions & 0 deletions document-versioning/internal/store/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package store

import (
"context"
"errors"

"document-versioning/internal/database"

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

// MockDatabaseQueries is a mock implementation of database.Queries for testing
type MockDatabaseQueries struct {
CreateDocumentFunc func(ctx context.Context, name string) (database.Document, error)
CreateDocumentVersionFunc func(ctx context.Context, arg database.CreateDocumentVersionParams) (database.DocumentVersion, error)
GetDocumentFunc func(ctx context.Context, id pgtype.UUID) (database.Document, error)
GetDocumentVersionsFunc func(ctx context.Context, documentID pgtype.UUID) ([]database.DocumentVersion, error)
UpdateDocumentVersionFunc func(ctx context.Context, arg database.UpdateDocumentVersionParams) error
GetVersionHistoryFunc func(ctx context.Context, documentID pgtype.UUID) ([]database.GetVersionHistoryRow, error)
}

func (m *MockDatabaseQueries) CreateDocument(ctx context.Context, name string) (database.Document, error) {
if m.CreateDocumentFunc != nil {
return m.CreateDocumentFunc(ctx, name)
}
return database.Document{}, errors.New("not implemented")
}

func (m *MockDatabaseQueries) CreateDocumentVersion(ctx context.Context, arg database.CreateDocumentVersionParams) (database.DocumentVersion, error) {
if m.CreateDocumentVersionFunc != nil {
return m.CreateDocumentVersionFunc(ctx, arg)
}
return database.DocumentVersion{}, errors.New("not implemented")
}

func (m *MockDatabaseQueries) GetDocument(ctx context.Context, id pgtype.UUID) (database.Document, error) {
if m.GetDocumentFunc != nil {
return m.GetDocumentFunc(ctx, id)
}
return database.Document{}, errors.New("not implemented")
}

func (m *MockDatabaseQueries) GetDocumentVersions(ctx context.Context, documentID pgtype.UUID) ([]database.DocumentVersion, error) {
if m.GetDocumentVersionsFunc != nil {
return m.GetDocumentVersionsFunc(ctx, documentID)
}
return nil, errors.New("not implemented")
}

func (m *MockDatabaseQueries) UpdateDocumentVersion(ctx context.Context, arg database.UpdateDocumentVersionParams) error {
if m.UpdateDocumentVersionFunc != nil {
return m.UpdateDocumentVersionFunc(ctx, arg)
}
return errors.New("not implemented")
}

func (m *MockDatabaseQueries) GetVersionHistory(ctx context.Context, documentID pgtype.UUID) ([]database.GetVersionHistoryRow, error) {
if m.GetVersionHistoryFunc != nil {
return m.GetVersionHistoryFunc(ctx, documentID)
}
return nil, errors.New("not implemented")
}
Loading