This project is an example implementation of a Web API using ASP.NET Core 8 with LINQ as the Object-Relational Mapper (ORM). The use case is the management of product data through the Product table.
- Product CRUD (Create, Read, Update, Delete):
- Add new products.
- View a list of all products.
- View product details by ID.
- Modify product data.
- Delete products.
- LINQ for Data Access:
- Implementation of database operations using LINQ for ease and flexibility.
- RESTful API Architecture:
- API design follows RESTful principles for ease of use and scalability.
- In-Memory or SQL Server Database:
- The project can be configured to use an in-memory database for development or SQL Server for production.
- ASP.NET Core 8: Web framework for building APIs.
- .NET 8: Development platform.
- Entity Framework Core: ORM for database access.
- LINQ (Language Integrated Query): Integrated query language for data manipulation.
- SQL Server (optional): Relational database.
- Swagger/OpenAPI: API documentation.
- Install .NET 8 SDK:
- Download and install the .NET 8 SDK from the official Microsoft website.
- Install Visual Studio or Visual Studio Code:
- Use Visual Studio or Visual Studio Code as your IDE.
- Install SQL Server (optional):
- If you want to use SQL Server, install and configure SQL Server on your computer.
- Database Configuration:
- Open the
appsettings.jsonfile. - Change the connection string according to your database configuration.
- To use an in-memory database, leave the default configuration.
- To use SQL Server, replace the connection string with a connection to your SQL Server database.
- Open the
- Database Migration:
- Open a terminal in the project directory.
- Run the following commands to create migrations and update the database:
dotnet ef migrations add InitialCreatedotnet ef database update
- Open the project in Visual Studio or Visual Studio Code.
- Run the application by pressing the "Run" button or using the
dotnet runcommand in the terminal. - Open a browser and access the API URL (e.g.,
https://localhost:5001/swagger) to view the Swagger documentation. - Use Postman or a similar tool to test the API endpoints.
POST /api/Auth/login- Login dan mendapatkan token JWTPOST /api/Auth/register- Registrasi pengguna baruGET /api/Auth/me- Mendapatkan informasi user yang sedang login
GET /api/Products- Mendapatkan daftar semua produkGET /api/Products/{id}- Mendapatkan detail produk berdasarkan IDPOST /api/Products- Menambahkan produk baru (Auth)PUT /api/Products/{id}- Mengubah data produk (Auth)DELETE /api/Products/{id}- Menghapus produk (Auth)
GET /api/Categories- Mendapatkan daftar semua kategoriGET /api/Categories/{id}- Mendapatkan detail kategori berdasarkan IDPOST /api/Categories- Menambahkan kategori baru (Admin only)PUT /api/Categories/{id}- Mengubah data kategori (Admin only)DELETE /api/Categories/{id}- Menghapus kategori (Admin only)
GET /api/Suppliers- Mendapatkan daftar semua supplierGET /api/Suppliers/{id}- Mendapatkan detail supplier berdasarkan IDPOST /api/Suppliers- Menambahkan supplier baru (Auth)PUT /api/Suppliers/{id}- Mengubah data supplier (Auth)DELETE /api/Suppliers/{id}- Menghapus supplier (Auth)
GET /api/Inventory- Mendapatkan daftar semua inventarisGET /api/Inventory/{id}- Mendapatkan detail inventaris berdasarkan IDPOST /api/Inventory- Menambahkan inventaris baru (Auth)PUT /api/Inventory/{id}- Mengubah data inventaris (Auth)DELETE /api/Inventory/{id}- Menghapus inventaris (Auth)
GET /api/Orders- Mendapatkan daftar semua pesananGET /api/Orders/{id}- Mendapatkan detail pesanan berdasarkan IDPOST /api/Orders- Menambahkan pesanan baru (Auth)PUT /api/Orders/{id}- Mengubah data pesanan (Auth)DELETE /api/Orders/{id}- Menghapus pesanan (Auth)
GET /api/OrderDetails- Mendapatkan daftar semua detail pesananGET /api/OrderDetails/{id}- Mendapatkan detail pesanan berdasarkan IDPOST /api/OrderDetails- Menambahkan detail pesanan baru (Auth)PUT /api/OrderDetails/{id}- Mengubah data detail pesanan (Auth)DELETE /api/OrderDetails/{id}- Menghapus detail pesanan (Auth)
GET /api/Customers- Mendapatkan daftar semua pelangganGET /api/Customers/{id}- Mendapatkan detail pelanggan berdasarkan IDPOST /api/Customers- Menambahkan pelanggan baru (Auth)PUT /api/Customers/{id}- Mengubah data pelanggan (Auth)DELETE /api/Customers/{id}- Menghapus pelanggan (Auth)
GET /api/Report/products-by-category- Laporan produk berdasarkan kategoriGET /api/Report/products-by-category/{categoryId}- Laporan produk untuk kategori tertentuGET /api/Report/purchase-details- Laporan detail pembelian untuk semua pesananGET /api/Report/purchase-details/{orderId}- Laporan detail pembelian untuk pesanan tertentuGET /api/Report/inventory-value- Laporan nilai inventaris (Auth)GET /api/Report/stock-history- Laporan riwayat perubahan stok (Auth)GET /api/Report/stock-history/product/{productId}- Laporan riwayat perubahan stok untuk produk tertentu (Auth)
-
Login dan Mendapatkan Token:
POST /api/Auth/login Content-Type: application/json { "username": "admin", "password": "Admin123!" }
-
Menambahkan Produk Baru:
POST /api/Products Authorization: Bearer your_token_here Content-Type: application/json { "name": "Laptop XYZ", "description": "High-specification laptop", "price": 12000000, "categoryId": "c8ad5706-e54c-4c23-b5d8-c22db2984193", "supplierId": "a67c9d1e-25a8-4f44-b73e-f536b8bce410" }
-
Mendapatkan Daftar Produk:
GET /api/products
-
Membuat Pesanan Baru:
POST /api/Orders Authorization: Bearer your_token_here Content-Type: application/json { "customerId": "f1b2c3d4-e5f6-7a8b-9c0d-1e2f3a4b5c6d", "orderDetails": [ { "productId": "a1b2c3d4-e5f6-7a8b-9c0d-1e2f3a4b5c6d", "quantity": 2, "unitPrice": 1200000 }, { "productId": "b1b2c3d4-e5f6-7a8b-9c0d-1e2f3a4b5c6d", "quantity": 1, "unitPrice": 500000 } ] }
-
Mendapatkan Laporan Nilai Inventaris:
GET /api/Report/inventory-value Authorization: Bearer your_token_here
If you would like to contribute to this project, please fork the repository and submit a pull request.
This project is licensed under the [MIT/Apache 2.0/etc.] license.
The API uses JWT (JSON Web Token) authentication. To access protected endpoints, you need to:
-
Register a new user or use the default admin account:
- Default admin credentials:
- Username: admin
- Email: admin@example.com
- Password: Admin123!
- Default admin credentials:
-
Obtain a JWT token by logging in:
POST /api/Auth/login
Content-Type: application/json
{
"username": "your_username",
"password": "your_password"
}- Use the token in subsequent requests:
- Add the Authorization header:
Bearer your_token_here - In Swagger UI:
- Click the "Authorize" button (lock icon) at the top right of the page
- In the popup dialog, enter your token in the format:
Bearer your_token_here - Click "Authorize" and close the dialog
- All subsequent API calls will include your token
- Add the Authorization header:
- Start the application and navigate to the Swagger UI (usually at
/swagger) - First, make a POST request to
/api/Auth/loginwith your credentials to get a token - Click the "Authorize" button (lock icon) at the top right of the Swagger UI
- In the popup dialog, enter your token in the format:
Bearer your_token_here- Make sure to include the word "Bearer" followed by a space before your token
- Click "Authorize" and close the dialog
- Now you can access protected endpoints through the Swagger UI
- The lock icons next to endpoints indicate whether they require authentication
-
Public endpoints (no authentication required):
GET /api/Category GET /api/Category/{id}
-
Admin-only endpoints (requires admin role):
POST /api/Category PUT /api/Category/{id} DELETE /api/Category/{id}
-
User: Basic authenticated user
- Can access protected endpoints
- Cannot modify system data
-
Admin: Administrative user
- Full access to all endpoints
- Can manage categories, products, and other system data
- Can view list of users
- Login and get token:
POST /api/Auth/login
Content-Type: application/json
{
"username": "admin",
"password": "Admin123!"
}- Create a new category (Admin only):
POST /api/Category
Authorization: Bearer your_token_here
Content-Type: application/json
{
"name": "New Category"
}- Get current user info:
GET /api/Auth/me
Authorization: Bearer your_token_here- 401 Unauthorized: Missing or invalid token
- 403 Forbidden: Valid token but insufficient permissions
- 400 Bad Request: Invalid input data
- 404 Not Found: Resource not found
- Id (Guid, Primary Key)
- Name (string)
- Description (string)
- Price (decimal)
- CategoryId (Guid, Foreign Key)
- SupplierId (Guid, Foreign Key)
- CreatedAt (DateTime)
- UpdatedAt (DateTime)
- Id (Guid, Primary Key)
- Name (string)
- CreatedAt (DateTime)
- UpdatedAt (DateTime)
- Id (Guid, Primary Key)
- ProductId (Guid, Foreign Key)
- StockQuantity (int)
- LastStockUpdate (DateTime)
- CreatedAt (DateTime)
- Id (Guid, Primary Key)
- InventoryId (Guid, Foreign Key)
- ProductId (Guid, Foreign Key)
- PreviousQuantity (int)
- NewQuantity (int)
- QuantityChange (int)
- ChangeType (string) - "Addition", "Reduction", "Adjustment"
- Notes (string)
- ChangedAt (DateTime)
- Id (Guid, Primary Key)
- Name (string)
- ContactPerson (string)
- ContactPhone (string)
- CreatedAt (DateTime)
- UpdatedAt (DateTime)
- Id (Guid, Primary Key)
- Name (string)
- Email (string)
- Phone (string)
- Address (string)
- CreatedAt (DateTime)
- UpdatedAt (DateTime)
- Id (Guid, Primary Key)
- OrderDate (DateTime)
- CustomerId (Guid, Foreign Key)
- TotalAmount (decimal)
- CreatedAt (DateTime)
- UpdatedAt (DateTime)
- Id (Guid, Primary Key)
- OrderId (Guid, Foreign Key)
- ProductId (Guid, Foreign Key)
- Quantity (int)
- UnitPrice (decimal)
- CreatedAt (DateTime)
- UpdatedAt (DateTime)
- Id (Guid, Primary Key)
- Username (string)
- Email (string)
- PasswordHash (string)
- Role (string) - "User", "Admin"
- CreatedAt (DateTime)
- UpdatedAt (DateTime)
-
Products ↔ Categories (Many-to-One)
- Setiap produk memiliki satu kategori
- Satu kategori dapat memiliki banyak produk
-
Products ↔ Suppliers (Many-to-One)
- Setiap produk memiliki satu supplier
- Satu supplier dapat memasok banyak produk
-
Products ↔ Inventory (One-to-Many)
- Setiap produk dapat memiliki beberapa catatan inventaris
- Satu catatan inventaris hanya terkait dengan satu produk
-
Products ↔ OrderDetails (One-to-Many)
- Satu produk dapat berada di banyak detail pesanan
- Satu detail pesanan hanya terkait dengan satu produk
-
Inventory ↔ InventoryHistory (One-to-Many)
- Satu catatan inventaris dapat memiliki banyak riwayat perubahan
- Satu riwayat perubahan hanya terkait dengan satu inventaris
-
Orders ↔ OrderDetails (One-to-Many)
- Satu pesanan dapat terdiri dari banyak detail pesanan
- Satu detail pesanan hanya terkait dengan satu pesanan
-
Customers ↔ Orders (One-to-Many)
- Satu pelanggan dapat membuat banyak pesanan
- Satu pesanan hanya terkait dengan satu pelanggan
erDiagram
Products {
guid Id PK
string Name
string Description
decimal Price
guid CategoryId FK
guid SupplierId FK
datetime CreatedAt
datetime UpdatedAt
}
Categories {
guid Id PK
string Name
datetime CreatedAt
datetime UpdatedAt
}
Inventory {
guid Id PK
guid ProductId FK
int StockQuantity
datetime LastStockUpdate
datetime CreatedAt
}
InventoryHistory {
guid Id PK
guid InventoryId FK
guid ProductId FK
int PreviousQuantity
int NewQuantity
int QuantityChange
string ChangeType
string Notes
datetime ChangedAt
}
Suppliers {
guid Id PK
string Name
string ContactPerson
string ContactPhone
datetime CreatedAt
datetime UpdatedAt
}
Customer {
guid Id PK
string Name
string Email
string Phone
string Address
datetime CreatedAt
datetime UpdatedAt
}
Orders {
guid Id PK
datetime OrderDate
guid CustomerId FK
decimal TotalAmount
datetime CreatedAt
datetime UpdatedAt
}
OrderDetails {
guid Id PK
guid OrderId FK
guid ProductId FK
int Quantity
decimal UnitPrice
datetime CreatedAt
datetime UpdatedAt
}
User {
guid Id PK
string Username
string Email
string PasswordHash
string Role
datetime CreatedAt
datetime UpdatedAt
}
Products }|--|| Categories : memiliki
Products }|--|| Suppliers : dari
Products ||--|{ Inventory : memiliki
Inventory ||--|{ InventoryHistory : mencatat
InventoryHistory }|--|| Products : berkaitan_dengan
Orders ||--|{ OrderDetails : terdiri_dari
OrderDetails }|--|| Orders : ada_di
OrderDetails }|--|| Products : berisi
Orders }|--|| Customer : ditempatkan_oleh
- Guid: Pengenal unik universal (direpresentasikan sebagai string di API)
- DateTime: Format ISO 8601 (misal: "2023-03-15T14:30:00Z")
- Decimal: Angka dengan titik desimal (misal: 1500000.00)
{
"message": "Data retrieved successfully",
"success": true,
"data": [
{ /* item data */ },
{ /* item data */ }
]
}{
"message": "Data retrieved successfully",
"success": true,
"data": { /* item data */ }
}{
"message": "Error message",
"success": false,
"errors": ["Error detail 1", "Error detail 2"]
}