-
-
Notifications
You must be signed in to change notification settings - Fork 713
Add solution for Challenge 9 by Kosench #770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughAdds 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
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Pre-merge checks❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
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. Comment |
There was a problem hiding this 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
📒 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/uuidis 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
RWMutexfor 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
GetBookByIDbefore 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
mapErrorToStatusCodeensures 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.Isfor sentinel errors and string matching for validation errors, ensuring appropriate HTTP status codes.
571-586: LGTM!Clean initialization and dependency wiring. Registering both
/api/booksand/api/books/routes ensures proper handling of both collection and individual resource endpoints.
Challenge 9 Solution
Submitted by: @Kosench
Challenge: Challenge 9
Description
This PR contains my solution for Challenge 9.
Changes
challenge-9/submissions/Kosench/solution-template.goTesting
Thank you for reviewing my submission! 🚀