-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathSpecificationAttributeController.cs
More file actions
385 lines (322 loc) · 14.5 KB
/
SpecificationAttributeController.cs
File metadata and controls
385 lines (322 loc) · 14.5 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using Grand.Business.Core.Extensions;
using Grand.Business.Core.Interfaces.Catalog.Products;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Permissions;
using Grand.Domain.Seo;
using Grand.Web.AdminShared.Extensions.Mapping;
using Grand.Web.AdminShared.Models.Catalog;
using Grand.Web.Common.DataSource;
using Grand.Web.Common.Filters;
using Grand.Web.Common.Security.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Grand.Web.Admin.Controllers;
[PermissionAuthorize(PermissionSystemName.SpecificationAttributes)]
public class SpecificationAttributeController : BaseAdminController
{
#region Constructors
public SpecificationAttributeController(
ISpecificationAttributeService specificationAttributeService,
ILanguageService languageService,
ITranslationService translationService,
IProductService productService,
SeoSettings seoSettings)
{
_specificationAttributeService = specificationAttributeService;
_languageService = languageService;
_translationService = translationService;
_productService = productService;
_seoSettings = seoSettings;
}
#endregion
#region Used by products
//used by products
[PermissionAuthorizeAction(PermissionActionName.Preview)]
[HttpPost]
public async Task<IActionResult> UsedByProducts(DataSourceRequest command, string specificationAttributeId)
{
var specyfication =
await _specificationAttributeService.GetSpecificationAttributeById(specificationAttributeId);
if (specyfication == null)
throw new ArgumentException("No specification found with the specified id");
var searchStoreId = string.Empty;
var specificationProducts = new List<SpecificationAttributeModel.UsedByProductModel>();
var total = 0;
var searchspecificationOptions = specyfication.SpecificationAttributeOptions.Select(x => x.Id).ToList();
if (searchspecificationOptions.Any())
{
var products = (await _productService.SearchProducts(
storeId: searchStoreId,
specificationOptions: searchspecificationOptions,
pageIndex: command.Page - 1,
pageSize: command.PageSize,
showHidden: true
)).products;
total = products.TotalCount;
foreach (var item in products)
{
var specOption =
item.ProductSpecificationAttributes.FirstOrDefault(x =>
x.SpecificationAttributeId == specificationAttributeId);
specificationProducts.Add(new SpecificationAttributeModel.UsedByProductModel {
Id = item.Id,
ProductName = item.Name,
OptionName = specyfication.SpecificationAttributeOptions
.FirstOrDefault(x => x.Id == specOption?.SpecificationAttributeOptionId)?.Name,
Published = item.Published
});
}
}
var gridModel = new DataSourceResult {
Data = specificationProducts,
Total = total
};
return Json(gridModel);
}
#endregion
#region Fields
private readonly ISpecificationAttributeService _specificationAttributeService;
private readonly IProductService _productService;
private readonly ILanguageService _languageService;
private readonly ITranslationService _translationService;
private readonly SeoSettings _seoSettings;
#endregion Fields
#region Specification attributes
//list
public IActionResult Index()
{
return RedirectToAction("List");
}
public IActionResult List()
{
return View();
}
[HttpPost]
[PermissionAuthorizeAction(PermissionActionName.List)]
public async Task<IActionResult> List(DataSourceRequest command)
{
var specificationAttributes = await _specificationAttributeService.GetSpecificationAttributes(pageIndex: command.Page - 1, pageSize: command.PageSize);
var gridModel = new DataSourceResult {
Data = specificationAttributes.Select(x => x.ToModel()),
Total = specificationAttributes.TotalCount
};
return Json(gridModel);
}
//create
[PermissionAuthorizeAction(PermissionActionName.Create)]
public async Task<IActionResult> Create()
{
var model = new SpecificationAttributeModel();
//locales
await AddLocales(_languageService, model.Locales);
return View(model);
}
[HttpPost]
[ArgumentNameFilter(KeyName = "save-continue", Argument = "continueEditing")]
[PermissionAuthorizeAction(PermissionActionName.Create)]
public async Task<IActionResult> Create(SpecificationAttributeModel model, bool continueEditing)
{
if (ModelState.IsValid)
{
var specificationAttribute = model.ToEntity();
specificationAttribute.SeName = SeoExtensions.GetSeName(
string.IsNullOrEmpty(specificationAttribute.SeName)
? specificationAttribute.Name
: specificationAttribute.SeName, _seoSettings.ConvertNonWesternChars,
_seoSettings.AllowUnicodeCharsInUrls, _seoSettings.SeoCharConversion);
await _specificationAttributeService.InsertSpecificationAttribute(specificationAttribute);
Success(_translationService.GetResource("Admin.Catalog.Attributes.SpecificationAttributes.Added"));
return continueEditing
? RedirectToAction("Edit", new { id = specificationAttribute.Id })
: RedirectToAction("List");
}
//If we got this far, something failed, redisplay form
return View(model);
}
//edit
[PermissionAuthorizeAction(PermissionActionName.Preview)]
public async Task<IActionResult> Edit(string id)
{
var specificationAttribute = await _specificationAttributeService.GetSpecificationAttributeById(id);
if (specificationAttribute == null)
//No specification attribute found with the specified id
return RedirectToAction("List");
var model = specificationAttribute.ToModel();
//locales
await AddLocales(_languageService, model.Locales, (locale, languageId) =>
{
locale.Name = specificationAttribute.GetTranslation(x => x.Name, languageId, false);
});
return View(model);
}
[HttpPost]
[ArgumentNameFilter(KeyName = "save-continue", Argument = "continueEditing")]
[PermissionAuthorizeAction(PermissionActionName.Edit)]
public async Task<IActionResult> Edit(SpecificationAttributeModel model, bool continueEditing)
{
var specificationAttribute = await _specificationAttributeService.GetSpecificationAttributeById(model.Id);
if (specificationAttribute == null)
//No specification attribute found with the specified id
return RedirectToAction("List");
if (ModelState.IsValid)
{
specificationAttribute = model.ToEntity(specificationAttribute);
specificationAttribute.SeName = SeoExtensions.GetSeName(
string.IsNullOrEmpty(specificationAttribute.SeName)
? specificationAttribute.Name
: specificationAttribute.SeName, _seoSettings.ConvertNonWesternChars,
_seoSettings.AllowUnicodeCharsInUrls, _seoSettings.SeoCharConversion);
await _specificationAttributeService.UpdateSpecificationAttribute(specificationAttribute);
Success(_translationService.GetResource("Admin.Catalog.Attributes.SpecificationAttributes.Updated"));
if (continueEditing)
{
//selected tab
await SaveSelectedTabIndex();
return RedirectToAction("Edit", new { id = specificationAttribute.Id });
}
return RedirectToAction("List");
}
//If we got this far, something failed, redisplay form
//locales
await AddLocales(_languageService, model.Locales, (locale, languageId) =>
{
locale.Name = specificationAttribute.GetTranslation(x => x.Name, languageId, false);
});
return View(model);
}
//delete
[HttpPost]
[PermissionAuthorizeAction(PermissionActionName.Delete)]
public async Task<IActionResult> Delete(string id)
{
var specificationAttribute = await _specificationAttributeService.GetSpecificationAttributeById(id);
if (specificationAttribute == null)
//No specification attribute found with the specified id
return RedirectToAction("List");
if (ModelState.IsValid)
{
await _specificationAttributeService.DeleteSpecificationAttribute(specificationAttribute);
Success(_translationService.GetResource("Admin.Catalog.Attributes.SpecificationAttributes.Deleted"));
return RedirectToAction("List");
}
Error(ModelState);
return RedirectToAction("Edit", new { id = specificationAttribute.Id });
}
#endregion
#region Specification attribute options
//list
[HttpPost]
[PermissionAuthorizeAction(PermissionActionName.Preview)]
public async Task<IActionResult> OptionList(string specificationAttributeId, DataSourceRequest command)
{
var options =
(await _specificationAttributeService.GetSpecificationAttributeById(specificationAttributeId))
.SpecificationAttributeOptions.OrderBy(x => x.DisplayOrder);
var gridModel = new DataSourceResult {
Data = options.Select(x =>
{
var model = x.ToModel();
//in order to save performance to do not check whether a product is deleted, etc
model.NumberOfAssociatedProducts = _specificationAttributeService
.GetProductSpecificationAttributeCount("", x.Id);
return model;
}),
Total = options.Count()
};
return Json(gridModel);
}
//create
[PermissionAuthorizeAction(PermissionActionName.Edit)]
public async Task<IActionResult> OptionCreatePopup(string specificationAttributeId)
{
var model = new SpecificationAttributeOptionModel {
SpecificationAttributeId = specificationAttributeId
};
//locales
await AddLocales(_languageService, model.Locales);
return View(model);
}
[HttpPost]
[PermissionAuthorizeAction(PermissionActionName.Edit)]
public async Task<IActionResult> OptionCreatePopup(SpecificationAttributeOptionModel model)
{
var specificationAttribute =
await _specificationAttributeService.GetSpecificationAttributeById(model.SpecificationAttributeId);
if (specificationAttribute == null)
//No specification attribute found with the specified id
return RedirectToAction("List");
if (ModelState.IsValid)
{
var sao = model.ToEntity();
sao.SeName = SeoExtensions.GetSeName(string.IsNullOrEmpty(sao.SeName) ? sao.Name : sao.SeName,
_seoSettings.ConvertNonWesternChars, _seoSettings.AllowUnicodeCharsInUrls,
_seoSettings.SeoCharConversion);
//clear "Color" values if it's disabled
if (!model.EnableColorSquaresRgb)
sao.ColorSquaresRgb = null;
specificationAttribute.SpecificationAttributeOptions.Add(sao);
await _specificationAttributeService.UpdateSpecificationAttribute(specificationAttribute);
return Content("");
}
//If we got this far, something failed, redisplay form
return View(model);
}
//edit
[PermissionAuthorizeAction(PermissionActionName.Edit)]
public async Task<IActionResult> OptionEditPopup(string id)
{
var sao = (await _specificationAttributeService.GetSpecificationAttributeByOptionId(id))
.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == id);
if (sao == null)
//No specification attribute option found with the specified id
return RedirectToAction("List");
var model = sao.ToModel();
model.EnableColorSquaresRgb = !string.IsNullOrEmpty(sao.ColorSquaresRgb);
//locales
await AddLocales(_languageService, model.Locales, (locale, languageId) =>
{
locale.Name = sao.GetTranslation(x => x.Name, languageId, false);
});
return View(model);
}
[HttpPost]
[PermissionAuthorizeAction(PermissionActionName.Edit)]
public async Task<IActionResult> OptionEditPopup(SpecificationAttributeOptionModel model)
{
var specificationAttribute = await _specificationAttributeService.GetSpecificationAttributeByOptionId(model.Id);
var sao = specificationAttribute.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == model.Id);
if (sao == null)
//No specification attribute option found with the specified id
return RedirectToAction("List");
if (ModelState.IsValid)
{
sao = model.ToEntity(sao);
sao.SeName = SeoExtensions.GetSeName(string.IsNullOrEmpty(sao.SeName) ? sao.Name : sao.SeName,
_seoSettings.ConvertNonWesternChars, _seoSettings.AllowUnicodeCharsInUrls,
_seoSettings.SeoCharConversion);
//clear "Color" values if it's disabled
if (!model.EnableColorSquaresRgb)
sao.ColorSquaresRgb = null;
await _specificationAttributeService.UpdateSpecificationAttribute(specificationAttribute);
return Content("");
}
//If we got this far, something failed, redisplay form
return View(model);
}
//delete
[HttpPost]
[PermissionAuthorizeAction(PermissionActionName.Edit)]
public async Task<IActionResult> OptionDelete(string id)
{
if (ModelState.IsValid)
{
var specificationAttribute = await _specificationAttributeService.GetSpecificationAttributeByOptionId(id);
var sao = specificationAttribute.SpecificationAttributeOptions.FirstOrDefault(x => x.Id == id);
if (sao == null)
throw new ArgumentException("No specification attribute option found with the specified id");
await _specificationAttributeService.DeleteSpecificationAttributeOption(sao);
return new JsonResult("");
}
return ErrorForKendoGridJson(ModelState);
}
#endregion
}