This is a simple example of a generic repository pattern implementation. This project provides an API for managing products and categories. You can create, update, and delete categories, as well as list the products belonging to each category. Similarly, you can perform various operations related to products, such as creating, updating, and deleting them, as well as sorting products based on their prices.
- Clone the repository
git clone https://github.com/BerkayMehmetSert/net.GenericRepository.git
- Install dependencies
dotnet restore
- Create database
dotnet ef database update
- Run the project
dotnet run
Get all categories
GET /api/category
Response body:
[
{
"id": "79613b77-5fcf-4063-4f2c-08db508b67d0",
"name": "Category 1",
"description": "Description 1"
},
{
"id": "6949cacd-f274-43fa-06be-08db508cb4eb",
"name": "Category 2",
"description": "Description 2"
}
]
Get category by id
GET /api/category/{id}
Response body:
{
"id": "79613b77-5fcf-4063-4f2c-08db508b67d0",
"name": "Category 1",
"description": "Description 1"
}
Get category by name
GET /api/category/name/{name}
Response body:
{
"id": "79613b77-5fcf-4063-4f2c-08db508b67d0",
"name": "Category 1",
"description": "Description 1"
}
Create category
POST /api/category
Request body:
{
"name": "Category 1",
"description": "Description 1"
}
Update category
PUT /api/category/{id}
Request body:
{
"name": "Category 1",
"description": "Description 1"
}
Delete category
DELETE /api/category/{id}
Get all products
GET /api/product
Response body:
[
{
"price": 110,
"category": {
"id": "79613b77-5fcf-4063-4f2c-08db508b67d0",
"name": "Category 1",
"description": "Description 1"
},
"id": "2d4f623a-1401-477a-f17a-08db5093ed92",
"name": "Product 1",
"description": "Description 1"
},
{
"price": 100,
"category": {
"id": "79613b77-5fcf-4063-4f2c-08db508b67d0",
"name": "Category 1",
"description": "Description 1"
},
"id": "141f2ca8-6aca-4fd4-1814-08db508cc9b2",
"name": "Product 2",
"description": "Description 2"
}
]
Get product by id
GET /api/product/{id}
Response body:
{
"price": 110,
"category": {
"id": "79613b77-5fcf-4063-4f2c-08db508b67d0",
"name": "Category 1",
"description": "Description 1"
},
"id": "2d4f623a-1401-477a-f17a-08db5093ed92",
"name": "Product 1",
"description": "Description 1"
}
Get product by name
GET /api/product/name/{name}
Response body:
{
"price": 110,
"category": {
"id": "79613b77-5fcf-4063-4f2c-08db508b67d0",
"name": "Category 1",
"description": "Description 1"
},
"id": "2d4f623a-1401-477a-f17a-08db5093ed92",
"name": "Product 1",
"description": "Description 1"
}
Get products descending by price
GET /api/product/price/descending
Response body:
[
{
"price": 110,
"category": {
"id": "79613b77-5fcf-4063-4f2c-08db508b67d0",
"name": "Category 1",
"description": "Description 1"
},
"id": "2d4f623a-1401-477a-f17a-08db5093ed92",
"name": "Product 1",
"description": "Description 1"
},
{
"price": 100,
"category": {
"id": "79613b77-5fcf-4063-4f2c-08db508b67d0",
"name": "Category 1",
"description": "Description 1"
},
"id": "141f2ca8-6aca-4fd4-1814-08db508cc9b2",
"name": "Product 2",
"description": "Description 2"
}
]
Get products ascending by price
GET /api/product/price/ascending
Response body:
[
{
"price": 100,
"category": {
"id": "79613b77-5fcf-4063-4f2c-08db508b67d0",
"name": "Category 1",
"description": "Description 1"
},
"id": "141f2ca8-6aca-4fd4-1814-08db508cc9b2",
"name": "Product 2",
"description": "Description 2"
},
{
"price": 110,
"category": {
"id": "79613b77-5fcf-4063-4f2c-08db508b67d0",
"name": "Category 1",
"description": "Description 1"
},
"id": "2d4f623a-1401-477a-f17a-08db5093ed92",
"name": "Product 1",
"description": "Description 1"
}
]
Create product
POST /api/product
Request body:
{
"name": "Product 1",
"description": "Description 1",
"categoryId": "79613b77-5fcf-4063-4f2c-08db508b67d0",
"price": 110
}
Update product
PUT /api/product/{id}
Request body:
{
"name": "Product 1",
"description": "Description 1",
"price": 110
}
Update product category
PUT /api/product/{id}/category/{categoryId}
Delete product
DELETE /api/product/{id}