-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductsController.cs
More file actions
153 lines (140 loc) · 4.65 KB
/
ProductsController.cs
File metadata and controls
153 lines (140 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// File: ProductsController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Business_layer;
using Data_layer; // لأن clsproduct موجود هنا
namespace API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
/// <summary>
/// Gets all products with category name (Public - no login required)
/// </summary>
[HttpGet]
public ActionResult<List<clsproduct>> GetAllProducts()
{
var products = ProductService.GetAllProducts();
return Ok(products);
}
/// <summary>
/// Gets a specific product by ID (Public)
/// </summary>
[HttpGet("{id:int}")]
public ActionResult<clsproduct> GetProductById(int id)
{
try
{
var product = ProductService.GetProductById(id);
return Ok(product);
}
catch (KeyNotFoundException ex)
{
return NotFound(new { message = ex.Message });
}
}
/// <summary>
/// Searches products by name or description (Public)
/// </summary>
[HttpGet("search")]
public ActionResult<List<clsproduct>> SearchProducts([FromQuery] string q)
{
if (string.IsNullOrWhiteSpace(q))
return BadRequest(new { message = "Search query is required." });
var products = ProductService.SearchProducts(q);
return Ok(products);
}
/// <summary>
/// Gets products by category ID (Public)
/// </summary>
[HttpGet("category/{categoryId:int}")]
public ActionResult<List<clsproduct>> GetProductsByCategoryId(int categoryId)
{
try
{
var products = ProductService.GetProductsByCategoryId(categoryId);
return Ok(products);
}
catch (ArgumentException ex)
{
return BadRequest(new { message = ex.Message });
}
}
// ====================== Admin Only Endpoints ======================
/// <summary>
/// Adds a new product (Admin only)
/// </summary>
[HttpPost]
[Authorize(Roles = "admin")]
public ActionResult<int> AddProduct([FromBody] clsproduct product)
{
try
{
int newProductId = ProductService.AddProduct(product);
return CreatedAtAction(nameof(GetProductById), new { id = newProductId }, new { id = newProductId });
}
catch (Exception ex)
{
return BadRequest(new { message = ex.Message });
}
}
/// <summary>
/// Updates an existing product (Admin only)
/// </summary>
[HttpPut("{id:int}")]
[Authorize(Roles = "admin")]
public IActionResult UpdateProduct(int id, [FromBody] clsproduct product)
{
try
{
bool success = ProductService.UpdateProduct(id, product);
if (!success)
return NotFound(new { message = "Product not found." });
return NoContent();
}
catch (Exception ex)
{
return BadRequest(new { message = ex.Message });
}
}
/// <summary>
/// Updates product stock (Admin only)
/// </summary>
[HttpPatch("{id:int}/stock")]
[Authorize(Roles = "admin")]
public IActionResult UpdateProductStock(int id, [FromBody] int newStock)
{
try
{
bool success = ProductService.UpdateProductStock(id, newStock);
if (!success)
return NotFound(new { message = "Product not found." });
return NoContent();
}
catch (Exception ex)
{
return BadRequest(new { message = ex.Message });
}
}
/// <summary>
/// Deletes a product (Admin only)
/// </summary>
[HttpDelete("{id:int}")]
[Authorize(Roles = "admin")]
public IActionResult DeleteProduct(int id)
{
try
{
bool success = ProductService.DeleteProduct(id);
if (!success)
return NotFound(new { message = "Product not found." });
return NoContent();
}
catch (Exception ex)
{
return BadRequest(new { message = ex.Message });
}
}
}
}