This project implements a CRUD API for managing orders with persistence in Redis. It is built using Go programming language and Gin framework.
- Application: Contains the main application logic including routing and server setup.
- Handler: Handles HTTP requests and interacts with the repository.
- Model: Defines the data structures used in the application.
- Repository: Implements the data access layer using Redis as the storage backend.
- Single Responsibility Principle (SRP): Each component in the project has a single responsibility. For example, the
Handler
package handles HTTP request handling, while theRepository
package deals with data access. - Open/Closed Principle (OCP): The application is designed to be open for extension but closed for modification. Adding new features or modifying existing ones can be done by extending the application without changing the core logic.
- Liskov Substitution Principle (LSP): Interfaces are used to define contracts between components, allowing different implementations to be used interchangeably.
- Interface Segregation Principle (ISP): Interfaces are kept small and focused on specific use cases, ensuring that clients only depend on the methods they use.
- Dependency Inversion Principle (DIP): Components depend on abstractions rather than concrete implementations, allowing for easier testing and flexibility in swapping out dependencies.
- Go installed on your system
- Redis server running locally or accessible over the network
- Clone the repository:
git clone https://github.com/Prayag2003/crud-api-golang.git cd crud-api-golang
- Install dependencies:
go mod tidy
- Run the application:
go run main.go
The App
struct serves as the heart of the application, responsible for orchestrating various components such as setting up routes and initializing the HTTP server.
- Route Setup: Configures the routing logic to handle different HTTP requests and map them to corresponding handler functions.
- HTTP Server Initialization: Initializes the HTTP server with specified configurations such as network address and handler.
Handlers play a crucial role in processing HTTP requests, handling input data, executing business logic, and generating appropriate responses. They serve as intermediaries between the incoming HTTP requests and the underlying application logic.
- Request Handling: Receives HTTP requests and delegates them to appropriate functions or methods for processing.
- Input Parsing: Extracts data from incoming requests, validates it, and transforms it into a format suitable for further processing.
- Business Logic Execution: Executes the necessary business logic to fulfill the request, which may include CRUD operations on orders.
- Response Generation: Constructs and sends HTTP responses back to the client, containing relevant data or error messages.
The Order
and Item
structs define the data model used within the application. They represent the entities involved in the order management system and encapsulate their attributes and behaviors.
- Attributes: Includes fields such as
OrderID
,CustomerID
,Items
,CreatedAt
,ShippedAt
, andCompletedAt
. - Purpose: Represents an order placed by a customer, containing details such as order items, customer ID, and timestamps for creation, shipping, and completion.
- Attributes: Consists of fields like
ItemID
,Quantity
, andPrice
. - Purpose: Represents an individual item within an order, specifying its unique identifier, quantity, and price.
The RedisRepo
struct implements the repository interface, offering a set of methods to interact with the Redis database for performing CRUD operations on orders.
- Data Access: Provides methods to insert, retrieve, update, and delete order data from the Redis database.
- Error Handling: Handles errors related to data access operations, ensuring graceful degradation and recovery from unexpected scenarios.
- Transaction Management: Manages transactions when interacting with the Redis database to maintain data integrity and consistency.
This CRUD API serves as a demonstration of building a robust web service using Go and Redis. By adhering to SOLID principles, it ensures maintainability, extensibility, and testability, making it suitable for various real-world applications and scenarios.