1+ using Microsoft . AspNetCore . Authentication . JwtBearer ;
2+ using Microsoft . AspNetCore . Mvc ;
3+ using Nop . Core . Domain . Catalog ;
4+ using Nop . Plugin . Api . Attributes ;
5+ using Nop . Plugin . Api . Constants ;
6+ using Nop . Plugin . Api . Delta ;
7+ using Nop . Plugin . Api . DTOs . Errors ;
8+ using Nop . Plugin . Api . DTOs . SpecificationAttributes ;
9+ using Nop . Plugin . Api . Helpers ;
10+ using Nop . Plugin . Api . JSON . ActionResults ;
11+ using Nop . Plugin . Api . JSON . Serializers ;
12+ using Nop . Plugin . Api . ModelBinders ;
13+ using Nop . Plugin . Api . Models . ProductSpecificationAttributes ;
14+ using Nop . Plugin . Api . Services ;
15+ using Nop . Services . Catalog ;
16+ using Nop . Services . Customers ;
17+ using Nop . Services . Discounts ;
18+ using Nop . Services . Localization ;
19+ using Nop . Services . Logging ;
20+ using Nop . Services . Media ;
21+ using Nop . Services . Security ;
22+ using Nop . Services . Stores ;
23+ using System ;
24+ using System . Linq ;
25+ using System . Net ;
26+
27+ namespace Nop . Plugin . Api . Controllers
28+ {
29+ [ ApiAuthorize ( Policy = JwtBearerDefaults . AuthenticationScheme , AuthenticationSchemes = JwtBearerDefaults . AuthenticationScheme ) ]
30+ public class ProductSpecificationAttributesController : BaseApiController
31+ {
32+ private readonly ISpecificationAttributeService _specificationAttributeService ;
33+ private readonly ISpecificationAttributeApiService _specificationAttributeApiService ;
34+ private readonly IDTOHelper _dtoHelper ;
35+
36+ public ProductSpecificationAttributesController ( IJsonFieldsSerializer jsonFieldsSerializer ,
37+ ICustomerActivityService customerActivityService ,
38+ ILocalizationService localizationService ,
39+ IAclService aclService ,
40+ IStoreMappingService storeMappingService ,
41+ IStoreService storeService ,
42+ ICustomerService customerService ,
43+ IDiscountService discountService ,
44+ IPictureService pictureService ,
45+ ISpecificationAttributeService specificationAttributeService ,
46+ ISpecificationAttributeApiService specificationAttributesApiService ,
47+ IDTOHelper dtoHelper ) : base ( jsonFieldsSerializer , aclService , customerService , storeMappingService , storeService , discountService , customerActivityService , localizationService , pictureService )
48+ {
49+ _specificationAttributeService = specificationAttributeService ;
50+ _specificationAttributeApiService = specificationAttributesApiService ;
51+ _dtoHelper = dtoHelper ;
52+ }
53+
54+ /// <summary>
55+ /// Receive a list of all product specification attributes
56+ /// </summary>
57+ /// <response code="200">OK</response>
58+ /// <response code="400">Bad Request</response>
59+ /// <response code="401">Unauthorized</response>
60+ [ HttpGet ]
61+ [ Route ( "/api/productspecificationattributes" ) ]
62+ [ ProducesResponseType ( typeof ( ProductSpecificationAttributesRootObjectDto ) , ( int ) HttpStatusCode . OK ) ]
63+ [ ProducesResponseType ( typeof ( ErrorsRootObject ) , ( int ) HttpStatusCode . BadRequest ) ]
64+ [ ProducesResponseType ( typeof ( string ) , ( int ) HttpStatusCode . Unauthorized ) ]
65+ [ GetRequestsErrorInterceptorActionFilter ]
66+ public IActionResult GetProductSpecificationAttributes ( ProductSpecifcationAttributesParametersModel parameters )
67+ {
68+ if ( parameters . Limit < Configurations . MinLimit || parameters . Limit > Configurations . MaxLimit )
69+ {
70+ return Error ( HttpStatusCode . BadRequest , "limit" , "invalid limit parameter" ) ;
71+ }
72+
73+ if ( parameters . Page < Configurations . DefaultPageValue )
74+ {
75+ return Error ( HttpStatusCode . BadRequest , "page" , "invalid page parameter" ) ;
76+ }
77+
78+ var productSpecificationAttribtues = _specificationAttributeApiService . GetProductSpecificationAttributes ( productId : parameters . ProductId , specificationAttributeOptionId : parameters . SpecificationAttributeOptionId , allowFiltering : parameters . AllowFiltering , showOnProductPage : parameters . ShowOnProductPage , limit : parameters . Limit , page : parameters . Page , sinceId : parameters . SinceId ) ;
79+
80+ var productSpecificationAttributeDtos = productSpecificationAttribtues . Select ( x => _dtoHelper . PrepareProductSpecificationAttributeDto ( x ) ) . ToList ( ) ;
81+
82+ var productSpecificationAttributesRootObject = new ProductSpecificationAttributesRootObjectDto ( )
83+ {
84+ ProductSpecificationAttributes = productSpecificationAttributeDtos
85+ } ;
86+
87+ var json = _jsonFieldsSerializer . Serialize ( productSpecificationAttributesRootObject , parameters . Fields ) ;
88+
89+ return new RawJsonActionResult ( json ) ;
90+ }
91+
92+ /// <summary>
93+ /// Receive a count of all product specification attributes
94+ /// </summary>
95+ /// <response code="200">OK</response>
96+ /// <response code="401">Unauthorized</response>
97+ [ HttpGet ]
98+ [ Route ( "/api/productspecificationattributes/count" ) ]
99+ [ ProducesResponseType ( typeof ( ProductSpecificationAttributesCountRootObject ) , ( int ) HttpStatusCode . OK ) ]
100+ [ ProducesResponseType ( typeof ( string ) , ( int ) HttpStatusCode . Unauthorized ) ]
101+ [ ProducesResponseType ( typeof ( ErrorsRootObject ) , ( int ) HttpStatusCode . BadRequest ) ]
102+ [ GetRequestsErrorInterceptorActionFilter ]
103+ public IActionResult GetProductSpecificationAttributesCount ( ProductSpecifcationAttributesCountParametersModel parameters )
104+ {
105+ var productSpecificationAttributesCount = _specificationAttributeService . GetProductSpecificationAttributeCount ( productId : parameters . ProductId , specificationAttributeOptionId : parameters . SpecificationAttributeOptionId ) ;
106+
107+ var productSpecificationAttributesCountRootObject = new ProductSpecificationAttributesCountRootObject ( )
108+ {
109+ Count = productSpecificationAttributesCount
110+ } ;
111+
112+ return Ok ( productSpecificationAttributesCountRootObject ) ;
113+ }
114+
115+ /// <summary>
116+ /// Retrieve product specification attribute by spcified id
117+ /// </summary>
118+ /// <param name="id">Id of the product specification attribute</param>
119+ /// <param name="fields">Fields from the product specification attribute you want your json to contain</param>
120+ /// <response code="200">OK</response>
121+ /// <response code="404">Not Found</response>
122+ /// <response code="401">Unauthorized</response>
123+ [ HttpGet ]
124+ [ Route ( "/api/productspecificationattributes/{id}" ) ]
125+ [ ProducesResponseType ( typeof ( ProductSpecificationAttributesRootObjectDto ) , ( int ) HttpStatusCode . OK ) ]
126+ [ ProducesResponseType ( typeof ( string ) , ( int ) HttpStatusCode . Unauthorized ) ]
127+ [ ProducesResponseType ( typeof ( ErrorsRootObject ) , ( int ) HttpStatusCode . BadRequest ) ]
128+ [ ProducesResponseType ( typeof ( string ) , ( int ) HttpStatusCode . NotFound ) ]
129+ [ GetRequestsErrorInterceptorActionFilter ]
130+ public IActionResult GetProductSpecificationAttributeById ( int id , string fields = "" )
131+ {
132+ if ( id <= 0 )
133+ {
134+ return Error ( HttpStatusCode . BadRequest , "id" , "invalid id" ) ;
135+ }
136+
137+ var productSpecificationAttribute = _specificationAttributeService . GetProductSpecificationAttributeById ( id ) ;
138+
139+ if ( productSpecificationAttribute == null )
140+ {
141+ return Error ( HttpStatusCode . NotFound , "product specification attribute" , "not found" ) ;
142+ }
143+
144+ var productSpecificationAttributeDto = _dtoHelper . PrepareProductSpecificationAttributeDto ( productSpecificationAttribute ) ;
145+
146+ var productSpecificationAttributesRootObject = new ProductSpecificationAttributesRootObjectDto ( ) ;
147+ productSpecificationAttributesRootObject . ProductSpecificationAttributes . Add ( productSpecificationAttributeDto ) ;
148+
149+ var json = _jsonFieldsSerializer . Serialize ( productSpecificationAttributesRootObject , fields ) ;
150+
151+ return new RawJsonActionResult ( json ) ;
152+ }
153+
154+ [ HttpPost ]
155+ [ Route ( "/api/productspecificationattributes" ) ]
156+ [ ProducesResponseType ( typeof ( ProductSpecificationAttributesRootObjectDto ) , ( int ) HttpStatusCode . OK ) ]
157+ [ ProducesResponseType ( typeof ( string ) , ( int ) HttpStatusCode . Unauthorized ) ]
158+ [ ProducesResponseType ( typeof ( ErrorsRootObject ) , ( int ) HttpStatusCode . BadRequest ) ]
159+ [ ProducesResponseType ( typeof ( string ) , ( int ) HttpStatusCode . NotFound ) ]
160+ [ ProducesResponseType ( typeof ( ErrorsRootObject ) , 422 ) ]
161+ public IActionResult CreateProductSpecificationAttribute ( [ ModelBinder ( typeof ( JsonModelBinder < ProductSpecificationAttributeDto > ) ) ] Delta < ProductSpecificationAttributeDto > productSpecificaitonAttributeDelta )
162+ {
163+ // Here we display the errors if the validation has failed at some point.
164+ if ( ! ModelState . IsValid )
165+ {
166+ return Error ( ) ;
167+ }
168+
169+ // Inserting the new product
170+ var productSpecificationAttribute = new ProductSpecificationAttribute ( ) ;
171+ productSpecificaitonAttributeDelta . Merge ( productSpecificationAttribute ) ;
172+
173+ _specificationAttributeService . InsertProductSpecificationAttribute ( productSpecificationAttribute ) ;
174+
175+ _customerActivityService . InsertActivity ( "AddNewProductSpecificationAttribute" , productSpecificationAttribute . Id . ToString ( ) ) ;
176+
177+ // Preparing the result dto of the new product
178+ var productSpecificationAttributeDto = _dtoHelper . PrepareProductSpecificationAttributeDto ( productSpecificationAttribute ) ;
179+
180+ var productSpecificationAttributesRootObjectDto = new ProductSpecificationAttributesRootObjectDto ( ) ;
181+ productSpecificationAttributesRootObjectDto . ProductSpecificationAttributes . Add ( productSpecificationAttributeDto ) ;
182+
183+ var json = _jsonFieldsSerializer . Serialize ( productSpecificationAttributesRootObjectDto , string . Empty ) ;
184+
185+ return new RawJsonActionResult ( json ) ;
186+ }
187+
188+ [ HttpPut ]
189+ [ Route ( "/api/productspecificationattributes/{id}" ) ]
190+ [ ProducesResponseType ( typeof ( ProductSpecificationAttributesRootObjectDto ) , ( int ) HttpStatusCode . OK ) ]
191+ [ ProducesResponseType ( typeof ( string ) , ( int ) HttpStatusCode . Unauthorized ) ]
192+ [ ProducesResponseType ( typeof ( ErrorsRootObject ) , ( int ) HttpStatusCode . BadRequest ) ]
193+ [ ProducesResponseType ( typeof ( string ) , ( int ) HttpStatusCode . NotFound ) ]
194+ [ ProducesResponseType ( typeof ( ErrorsRootObject ) , 422 ) ]
195+ public IActionResult UpdateProductSpecificationAttribute ( [ ModelBinder ( typeof ( JsonModelBinder < ProductSpecificationAttributeDto > ) ) ] Delta < ProductSpecificationAttributeDto > productSpecificationAttributeDelta )
196+ {
197+ // Here we display the errors if the validation has failed at some point.
198+ if ( ! ModelState . IsValid )
199+ {
200+ return Error ( ) ;
201+ }
202+
203+ // We do not need to validate the product attribute id, because this will happen in the model binder using the dto validator.
204+ int productSpecificationAttributeId = productSpecificationAttributeDelta . Dto . Id ;
205+
206+ var productSpecificationAttribute = _specificationAttributeService . GetProductSpecificationAttributeById ( productSpecificationAttributeId ) ;
207+ if ( productSpecificationAttribute == null )
208+ {
209+ return Error ( HttpStatusCode . NotFound , "product specification attribute" , "not found" ) ;
210+ }
211+
212+ productSpecificationAttributeDelta . Merge ( productSpecificationAttribute ) ;
213+
214+ _specificationAttributeService . UpdateProductSpecificationAttribute ( productSpecificationAttribute ) ;
215+
216+ _customerActivityService . InsertActivity ( "EditProductSpecificationAttribute" , productSpecificationAttribute . Id . ToString ( ) ) ;
217+
218+ // Preparing the result dto of the new product attribute
219+ var productSpecificationAttributeDto = _dtoHelper . PrepareProductSpecificationAttributeDto ( productSpecificationAttribute ) ;
220+
221+ var productSpecificatoinAttributesRootObjectDto = new ProductSpecificationAttributesRootObjectDto ( ) ;
222+ productSpecificatoinAttributesRootObjectDto . ProductSpecificationAttributes . Add ( productSpecificationAttributeDto ) ;
223+
224+ var json = _jsonFieldsSerializer . Serialize ( productSpecificatoinAttributesRootObjectDto , string . Empty ) ;
225+
226+ return new RawJsonActionResult ( json ) ;
227+ }
228+
229+ [ HttpDelete ]
230+ [ Route ( "/api/productspecificationattributes/{id}" ) ]
231+ [ ProducesResponseType ( typeof ( void ) , ( int ) HttpStatusCode . OK ) ]
232+ [ ProducesResponseType ( typeof ( string ) , ( int ) HttpStatusCode . Unauthorized ) ]
233+ [ ProducesResponseType ( typeof ( ErrorsRootObject ) , ( int ) HttpStatusCode . BadRequest ) ]
234+ [ ProducesResponseType ( typeof ( string ) , ( int ) HttpStatusCode . NotFound ) ]
235+ [ ProducesResponseType ( typeof ( ErrorsRootObject ) , 422 ) ]
236+ public IActionResult DeleteProductSpecificationAttribute ( int id )
237+ {
238+ if ( id <= 0 )
239+ {
240+ return Error ( HttpStatusCode . BadRequest , "id" , "invalid id" ) ;
241+ }
242+
243+ var productSpecificationAttribute = _specificationAttributeService . GetProductSpecificationAttributeById ( id ) ;
244+ if ( productSpecificationAttribute == null )
245+ {
246+ return Error ( HttpStatusCode . NotFound , "product specification attribute" , "not found" ) ;
247+ }
248+
249+ _specificationAttributeService . DeleteProductSpecificationAttribute ( productSpecificationAttribute ) ;
250+
251+ //activity log
252+ _customerActivityService . InsertActivity ( "DeleteProductSpecificationAttribute" , _localizationService . GetResource ( "ActivityLog.DeleteProductSpecificationAttribute" ) , productSpecificationAttribute . Id . ToString ( ) ) ;
253+
254+ return new RawJsonActionResult ( "{}" ) ;
255+ }
256+ }
257+ }
0 commit comments