-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProductNortWindController.cs
More file actions
224 lines (205 loc) · 6.65 KB
/
ProductNortWindController.cs
File metadata and controls
224 lines (205 loc) · 6.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using DataAccess.Models;
using System.ComponentModel.DataAnnotations;
using Microsoft.Data.SqlClient;
namespace WebAPI.Controllers;
[Route("api/[controller]")]
[ApiController]
public class ProductNorthWindController : ControllerBase
{
private readonly NorthWindContext _context;
public ProductNorthWindController(NorthWindContext context)
{
_context = context;
}
// GET: api/products - Retrieve all products
[HttpGet]
public async Task<ActionResult<IEnumerable<Product>>> GetProducts()
{
try
{
var products = await _context.Products
.ToListAsync();
return products.Count > 0 ? Ok(products) : NoContent();
}
catch (Exception)
{
return StatusCode(500, new { error = "Failed to retrieve products. Please try again later." });
}
}
// GET: api/products/{id} - Retrieve a single product by ID
[HttpGet("{id}")]
public async Task<ActionResult<Product>> GetProduct(int id)
{
try
{
var product = await _context.Products
.FirstOrDefaultAsync(p => p.ProductId == id);
if (product == null)
{
return NotFound(new { error = $"Product with ID {id} not found." });
}
return Ok(product);
}
catch (Exception)
{
return StatusCode(500, new { error = "Failed to retrieve product. Please try again later." });
}
}
// GET: api/products/suppliers - Retrieve all suppliers
[HttpGet("suppliers")]
public async Task<ActionResult<IEnumerable<Supplier>>> GetSuppliers()
{
try
{
var suppliers = await _context.Suppliers
.ToListAsync();
return suppliers.Count > 0 ? Ok(suppliers) : NoContent();
}
catch (Exception)
{
return StatusCode(500, new { error = "Failed to retrieve suppliers. Please try again later." });
}
}
// GET: api/products/suppliers/{id} - Retrieve a single supplier by ID
[HttpGet("suppliers/{id}")]
public async Task<ActionResult<Supplier>> GetSupplier(int id)
{
try
{
var supplier = await _context.Suppliers
.FirstOrDefaultAsync(s => s.SupplierId == id);
if (supplier == null)
{
return NotFound(new { error = $"Supplier with ID {id} not found." });
}
return Ok(supplier);
}
catch (Exception)
{
return StatusCode(500, new { error = "Failed to retrieve supplier. Please try again later." });
}
}
// GET: api/products/categories - Retrieve all categories
[HttpGet("categories")]
public async Task<ActionResult<IEnumerable<Category>>> GetCategories()
{
try
{
var categories = await _context.Categories
.ToListAsync();
return categories.Count > 0 ? Ok(categories) : NoContent();
}
catch (Exception)
{
return StatusCode(500, new { error = "Failed to retrieve categories. Please try again later." });
}
}
// GET: api/products/categories/{id} - Retrieve a single category by ID
[HttpGet("categories/{id}")]
public async Task<ActionResult<Category>> GetCategory(int id)
{
try
{
var category = await _context.Categories
.FirstOrDefaultAsync(c => c.CategoryId == id);
if (category == null)
{
return NotFound(new { error = $"Category with ID {id} not found." });
}
return Ok(category);
}
catch (Exception)
{
return StatusCode(500, new { error = "Failed to retrieve category. Please try again later." });
}
}
// POST: api/products - Add a new product
[HttpPost]
public async Task<ActionResult<Product>> CreateProduct([FromBody] Product product)
{
if (product == null)
{
return BadRequest(new { error = "Invalid product data." });
}
var validationResults = new List<ValidationResult>();
var validationContext = new ValidationContext(product);
if (!Validator.TryValidateObject(product, validationContext, validationResults, true))
{
var errors = validationResults.Select(vr => vr.ErrorMessage);
return BadRequest(new { error = "Validation failed.", details = errors });
}
try
{
_context.Products.Add(product);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetProduct), new { id = product.ProductId }, product);
}
catch (Exception)
{
return StatusCode(500, new { error = "Failed to create product. Please try again later." });
}
}
// PUT: api/products/{id} - Update an existing product
[HttpPut("{id}")]
public async Task<IActionResult> UpdateProduct(int id, [FromBody] Product product)
{
if (product == null || product.ProductId != id)
{
return BadRequest(new { error = "Invalid product data or ID mismatch." });
}
var validationResults = new List<ValidationResult>();
var validationContext = new ValidationContext(product);
if (!Validator.TryValidateObject(product, validationContext, validationResults, true))
{
var errors = validationResults.Select(vr => vr.ErrorMessage);
return BadRequest(new { error = "Validation failed.", details = errors });
}
try
{
var existingProduct = await _context.Products.FindAsync(id);
if (existingProduct == null)
{
return NotFound(new { error = $"Product with ID {id} not found." });
}
existingProduct.ProductName = product.ProductName;
existingProduct.SupplierId = product.SupplierId;
existingProduct.CategoryId = product.CategoryId;
existingProduct.QuantityPerUnit = product.QuantityPerUnit;
existingProduct.UnitPrice = product.UnitPrice;
existingProduct.Discontinued = product.Discontinued;
await _context.SaveChangesAsync();
return NoContent();
}
catch (Exception)
{
return StatusCode(500, new { error = "Failed to update product. Please try again later." });
}
}
// DELETE: api/products/{id} - Remove a product
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteProduct(int id)
{
try
{
var product = await _context.Products.FindAsync(id);
if (product == null)
{
return NotFound(new { error = $"Product with ID {id} not found." });
}
_context.Products.Remove(product);
await _context.SaveChangesAsync();
return NoContent();
}
catch (DbUpdateException ex) when (ex.InnerException is SqlException sqlEx && sqlEx.Number == 547)
{
// Handle SQL Server error 547: foreign key constraint violation
return BadRequest(new { error = $"Cannot delete product with ID {id} because it is referenced in other tables." });
}
catch (Exception)
{
return StatusCode(500, new { error = "Failed to delete product. Please try again later." });
}
}
}