|
1 | 1 | using System.Linq; |
| 2 | +using System.Threading.Tasks; |
2 | 3 | using Microsoft.AspNetCore.Authorization; |
3 | 4 | using Microsoft.AspNetCore.Mvc; |
| 5 | +using Microsoft.EntityFrameworkCore; |
4 | 6 | using SimplCommerce.Infrastructure.Data; |
5 | 7 | using SimplCommerce.Infrastructure.Web.SmartTable; |
6 | 8 | using SimplCommerce.Module.Pricing.Models; |
7 | 9 | using SimplCommerce.Module.Pricing.ViewModels; |
8 | | -using Microsoft.EntityFrameworkCore; |
9 | 10 |
|
10 | 11 | namespace SimplCommerce.Module.Pricing.Controllers |
11 | 12 | { |
@@ -41,12 +42,12 @@ public IActionResult List([FromBody] SmartTableParam param) |
41 | 42 | } |
42 | 43 |
|
43 | 44 | [HttpGet("{id}")] |
44 | | - public IActionResult Get(long id) |
| 45 | + public async Task<IActionResult> Get(long id) |
45 | 46 | { |
46 | | - var catrtRule = _cartRuleRepository.Query() |
| 47 | + var catrtRule = await _cartRuleRepository.Query() |
47 | 48 | .Include(x => x.Coupons) |
48 | 49 | .Include(x => x.Products).ThenInclude(p => p.Product) |
49 | | - .FirstOrDefault(x => x.Id == id); |
| 50 | + .FirstOrDefaultAsync(x => x.Id == id); |
50 | 51 | var model = new CartRuleForm |
51 | 52 | { |
52 | 53 | Id = catrtRule.Id, |
@@ -78,7 +79,7 @@ public IActionResult Get(long id) |
78 | 79 | } |
79 | 80 |
|
80 | 81 | [HttpPost] |
81 | | - public IActionResult Post([FromBody] CartRuleForm model) |
| 82 | + public async Task<IActionResult> Post([FromBody] CartRuleForm model) |
82 | 83 | { |
83 | 84 | if (ModelState.IsValid) |
84 | 85 | { |
@@ -120,22 +121,22 @@ public IActionResult Post([FromBody] CartRuleForm model) |
120 | 121 | } |
121 | 122 |
|
122 | 123 | _cartRuleRepository.Add(cartRule); |
123 | | - _cartRuleRepository.SaveChanges(); |
| 124 | + await _cartRuleRepository.SaveChangesAsync(); |
124 | 125 |
|
125 | | - return Ok(); |
| 126 | + return CreatedAtAction(nameof(Get), new { id = cartRule.Id }, null); |
126 | 127 | } |
127 | | - return new BadRequestObjectResult(ModelState); |
| 128 | + return BadRequest(ModelState); |
128 | 129 | } |
129 | 130 |
|
130 | 131 | [HttpPut("{id}")] |
131 | | - public IActionResult Put(long id, [FromBody] CartRuleForm model) |
| 132 | + public async Task<IActionResult> Put(long id, [FromBody] CartRuleForm model) |
132 | 133 | { |
133 | 134 | if (ModelState.IsValid) |
134 | 135 | { |
135 | | - var cartRule = _cartRuleRepository.Query() |
| 136 | + var cartRule = await _cartRuleRepository.Query() |
136 | 137 | .Include(x => x.Coupons) |
137 | 138 | .Include(x => x.Products) |
138 | | - .FirstOrDefault(x => x.Id == id); |
| 139 | + .FirstOrDefaultAsync(x => x.Id == id); |
139 | 140 | if(cartRule == null) |
140 | 141 | { |
141 | 142 | return NotFound(); |
@@ -195,11 +196,32 @@ public IActionResult Put(long id, [FromBody] CartRuleForm model) |
195 | 196 | cartRule.Products.Remove(item); |
196 | 197 | } |
197 | 198 |
|
198 | | - _cartRuleRepository.SaveChanges(); |
| 199 | + await _cartRuleRepository.SaveChangesAsync(); |
| 200 | + return Accepted(); |
| 201 | + } |
| 202 | + return BadRequest(ModelState); |
| 203 | + } |
| 204 | + |
| 205 | + [HttpDelete("{id}")] |
| 206 | + public async Task<IActionResult> Delete(long id) |
| 207 | + { |
| 208 | + var cartRule = await _cartRuleRepository.Query().FirstOrDefaultAsync(x => x.Id == id); |
| 209 | + if (cartRule == null) |
| 210 | + { |
| 211 | + return NotFound(); |
| 212 | + } |
199 | 213 |
|
200 | | - return Ok(); |
| 214 | + try |
| 215 | + { |
| 216 | + _cartRuleRepository.Remove(cartRule); |
| 217 | + await _cartRuleRepository.SaveChangesAsync(); |
201 | 218 | } |
202 | | - return new BadRequestObjectResult(ModelState); |
| 219 | + catch (DbUpdateException) |
| 220 | + { |
| 221 | + return BadRequest(new { Error = $"The cart rule {cartRule.Name} can't not be deleted because it has been used" }); |
| 222 | + } |
| 223 | + |
| 224 | + return NoContent(); |
203 | 225 | } |
204 | 226 | } |
205 | 227 | } |
0 commit comments