Skip to content

Conversation

@Kosench
Copy link
Contributor

@Kosench Kosench commented Nov 20, 2025

Challenge 9 Solution

Submitted by: @Kosench
Challenge: Challenge 9

Description

This PR contains my solution for Challenge 9.

Changes

  • Added solution file to challenge-9/submissions/Kosench/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 20, 2025

Walkthrough

Adds a single Go file implementing an in-memory RESTful Book Management API with layered architecture: model, repository (thread-safe), service (validation/business rules), HTTP handlers, error mapping, and a main function wiring routes and starting a server.

Changes

Cohort / File(s) Summary
Book Management API Implementation
challenge-9/submissions/Kosench/solution-template.go
New file implementing Book model, BookRepository interface and InMemoryBookRepository (CRUD, search, RWMutex), BookService/DefaultBookService (validation: non-empty fields, ISBN uniqueness, year bounds, duplicate checks), BookHandler HTTP endpoints (GET all/by-id, POST, PUT, DELETE, search by author/title), JSON helpers, error-to-status mapping, and main() wiring routes on port 8080.

Sequence Diagram(s)

sequenceDiagram
    participant C as Client
    participant H as BookHandler
    participant S as BookService
    participant R as InMemoryBookRepository

    Note over C,H: HTTP request (GET/POST/PUT/DELETE/search)
    C->>H: HTTP Request
    H->>H: parse request, extract id/params, validate JSON
    H->>S: call service method
    S->>S: business validation (fields, ISBN uniqueness, year)
    S->>R: repository operation (Get/Create/Update/Delete/Search)
    R->>R: RWMutex lock/access in-memory map
    R-->>S: result / error
    S-->>H: result / error
    H->>H: map error -> status, marshal JSON
    H-->>C: HTTP JSON response
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Pay attention to: ISBN uniqueness checks across create/update, correct RWMutex usage for all read/write paths, and error-to-HTTP status mapping.

Possibly related PRs

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the main change: adding a solution file for Challenge 9 submitted by Kosench.
Description check ✅ Passed The description is directly related to the changeset, clearly stating it's a Challenge 9 solution submission with the specific file path and testing verification.

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fddb3a7 and 580e534.

📒 Files selected for processing (1)
  • challenge-9/submissions/Kosench/solution-template.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • challenge-9/submissions/Kosench/solution-template.go

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ca8ea29 and fddb3a7.

📒 Files selected for processing (1)
  • challenge-9/submissions/Kosench/solution-template.go (1 hunks)
🔇 Additional comments (19)
challenge-9/submissions/Kosench/solution-template.go (19)

1-15: LGTM!

The package structure and imports are well-organized. The use of standard library packages alongside github.com/google/uuid is appropriate for this RESTful API implementation.


17-21: LGTM!

Sentinel errors are well-defined and follow Go best practices. These are appropriately used throughout the codebase for error handling and status code mapping.


28-35: LGTM!

The Book model is clean and well-structured with appropriate JSON tags for API serialization.


42-51: LGTM!

The repository interface is well-designed with a comprehensive set of CRUD and search operations.


54-77: LGTM!

Excellent implementation with proper use of RWMutex for concurrent reads and defensive copying to prevent external mutations. The constructor is clean and initializes the map correctly.


79-89: LGTM!

Consistent defensive copying and proper error handling with the sentinel error ErrBookNotFound.


91-101: LGTM!

Proper write lock usage and duplicate checking before insertion.


103-121: LGTM!

Comprehensive validation with proper nil and empty checks, correct write lock usage, and existence verification before update.


123-137: LGTM!

Clean delete implementation with proper validation, locking, and existence checking.


193-213: LGTM!

Clean service layer design with proper interface definition and dependency injection through the constructor.


215-225: LGTM!

Proper validation in GetBookByID before delegating to the repository.


227-261: LGTM!

Comprehensive validation including required fields, year bounds, ISBN uniqueness, and proper UUID generation. The descriptive error messages are appropriate for API responses.


263-308: LGTM!

Excellent update logic that properly handles ISBN uniqueness only when the ISBN is being changed, preventing false conflicts. Comprehensive validation and proper ID assignment.


310-332: LGTM!

Clean implementations with appropriate validation of search parameters before delegation to the repository.


339-400: LGTM!

Excellent routing implementation with proper ID validation for different HTTP methods. The checks preventing ID in POST URLs and requiring ID for PUT/DELETE operations are particularly good API design.


402-508: LGTM!

All handlers follow a consistent pattern with proper error handling and appropriate HTTP status codes. The use of mapErrorToStatusCode ensures uniform error responses across the API.


510-514: LGTM!

Clean error response structure with appropriate JSON tag to omit status code from the response body.


520-569: LGTM!

Well-implemented helper functions. The error mapping logic properly uses errors.Is for sentinel errors and string matching for validation errors, ensuring appropriate HTTP status codes.


571-586: LGTM!

Clean initialization and dependency wiring. Registering both /api/books and /api/books/ routes ensures proper handling of both collection and individual resource endpoints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant