public IActionResult Search(CustomersSearchParametersModel parameters) { if (parameters.Limit <= Configurations.MinLimit || parameters.Limit > Configurations.MaxLimit) { return(Error(HttpStatusCode.BadRequest, "limit", "Invalid limit parameter")); } if (parameters.Page <= 0) { return(Error(HttpStatusCode.BadRequest, "page", "Invalid page parameter")); } var customersDto = _customerApiService.Search(parameters.Query, parameters.Order, parameters.Page, parameters.Limit); var customersRootObject = new CustomersRootObject() { Customers = customersDto }; var json = JsonFieldsSerializer.Serialize(customersRootObject, parameters.Fields); return(new RawJsonActionResult(json)); }
public IActionResult GetCustomers(CustomersParametersModel parameters) { if (parameters.Limit < Configurations.MinLimit || parameters.Limit > Configurations.MaxLimit) { return(Error(HttpStatusCode.BadRequest, "limit", "Invalid limit parameter")); } if (parameters.Page < Configurations.DefaultPageValue) { return(Error(HttpStatusCode.BadRequest, "page", "Invalid request parameters")); } IList <CustomerDto> allCustomers = _customerApiService.GetCustomersDtos(parameters.CreatedAtMin, parameters.CreatedAtMax, parameters.Limit, parameters.Page, parameters.SinceId); var customersRootObject = new CustomersRootObject() { Customers = allCustomers }; var json = _jsonFieldsSerializer.Serialize(customersRootObject, parameters.Fields); return(new RawJsonActionResult(json)); }
public void WhenNoParametersPassedAndNoCustomersExist_ShouldCallTheSerializer() { var expectedCustomersCollection = new List <CustomerDto>(); var expectedRootObject = new CustomersRootObject() { Customers = expectedCustomersCollection }; var defaultParameters = new CustomersSearchParametersModel(); //Arange var autoMocker = new RhinoAutoMocker <CustomersController>(); autoMocker.Get <IJsonFieldsSerializer>().Expect(x => x.Serialize(expectedRootObject, defaultParameters.Fields)); autoMocker.Get <ICustomerApiService>().Stub(x => x.Search()).Return(expectedCustomersCollection); //Act autoMocker.ClassUnderTest.Search(defaultParameters); //Assert autoMocker.Get <IJsonFieldsSerializer>().AssertWasCalled(x => x.Serialize(Arg <CustomersRootObject> .Is.TypeOf, Arg <string> .Is.Equal(defaultParameters.Fields))); }
public IActionResult UpdateCustomer([ModelBinder(typeof(JsonModelBinder <CustomerDto>))] Delta <CustomerDto> customerDelta) { // Here we display the errors if the validation has failed at some point. if (!ModelState.IsValid) { return(Error()); } // Updateting the customer var currentCustomer = _customerApiService.GetCustomerEntityById(customerDelta.Dto.Id); if (currentCustomer == null) { return(Error(HttpStatusCode.NotFound, "customer", "not found")); } customerDelta.Merge(currentCustomer); if (customerDelta.Dto.RoleIds.Count > 0) { // Remove all roles while (currentCustomer.CustomerRoles.Count > 0) { currentCustomer.CustomerRoles.Remove(currentCustomer.CustomerRoles.First()); } AddValidRoles(customerDelta, currentCustomer); } if (customerDelta.Dto.Addresses.Count > 0) { var currentCustomerAddresses = currentCustomer.Addresses.ToDictionary(address => address.Id, address => address); foreach (var passedAddress in customerDelta.Dto.Addresses) { var addressEntity = passedAddress.ToEntity(); if (currentCustomerAddresses.ContainsKey(passedAddress.Id)) { _mappingHelper.Merge(passedAddress, currentCustomerAddresses[passedAddress.Id]); } else { currentCustomer.Addresses.Add(addressEntity); } } } CustomerService.UpdateCustomer(currentCustomer); InsertFirstAndLastNameGenericAttributes(customerDelta.Dto.FirstName, customerDelta.Dto.LastName, currentCustomer); if (!string.IsNullOrEmpty(customerDelta.Dto.LanguageId) && int.TryParse(customerDelta.Dto.LanguageId, out var languageId) && _languageService.GetLanguageById(languageId) != null) { _genericAttributeService.SaveAttribute(currentCustomer, NopCustomerDefaults.LanguageIdAttribute, languageId); } //password if (!string.IsNullOrWhiteSpace(customerDelta.Dto.Password)) { AddPassword(customerDelta.Dto.Password, currentCustomer); } // TODO: Localization // Preparing the result dto of the new customer // We do not prepare the shopping cart items because we have a separate endpoint for them. var updatedCustomer = currentCustomer.ToDto(); // This is needed because the entity framework won't populate the navigation properties automatically // and the country name will be left empty because the mapping depends on the navigation property // so we do it by hand here. PopulateAddressCountryNames(updatedCustomer); // Set the fist and last name separately because they are not part of the customer entity, but are saved in the generic attributes. var firstNameGenericAttribute = _genericAttributeService.GetAttributesForEntity(currentCustomer.Id, typeof(Customer).Name) .FirstOrDefault(x => x.Key == "FirstName"); if (firstNameGenericAttribute != null) { updatedCustomer.FirstName = firstNameGenericAttribute.Value; } var lastNameGenericAttribute = _genericAttributeService.GetAttributesForEntity(currentCustomer.Id, typeof(Customer).Name) .FirstOrDefault(x => x.Key == "LastName"); if (lastNameGenericAttribute != null) { updatedCustomer.LastName = lastNameGenericAttribute.Value; } var languageIdGenericAttribute = _genericAttributeService.GetAttributesForEntity(currentCustomer.Id, typeof(Customer).Name) .FirstOrDefault(x => x.Key == "LanguageId"); if (languageIdGenericAttribute != null) { updatedCustomer.LanguageId = languageIdGenericAttribute.Value; } //activity log CustomerActivityService.InsertActivity("UpdateCustomer", LocalizationService.GetResource("ActivityLog.UpdateCustomer"), currentCustomer); var customersRootObject = new CustomersRootObject(); customersRootObject.Customers.Add(updatedCustomer); var json = JsonFieldsSerializer.Serialize(customersRootObject, string.Empty); return(new RawJsonActionResult(json)); }
public IActionResult CreateCustomer([ModelBinder(typeof(JsonModelBinder <CustomerDto>))] Delta <CustomerDto> customerDelta) { // Here we display the errors if the validation has failed at some point. if (!ModelState.IsValid) { return(Error()); } //If the validation has passed the customerDelta object won't be null for sure so we don't need to check for this. // Inserting the new customer var newCustomer = _factory.Initialize(); customerDelta.Merge(newCustomer); foreach (var address in customerDelta.Dto.Addresses) { // we need to explicitly set the date as if it is not specified // it will default to 01/01/0001 which is not supported by SQL Server and throws and exception if (address.CreatedOnUtc == null) { address.CreatedOnUtc = DateTime.UtcNow; } newCustomer.Addresses.Add(address.ToEntity()); } CustomerService.InsertCustomer(newCustomer); InsertFirstAndLastNameGenericAttributes(customerDelta.Dto.FirstName, customerDelta.Dto.LastName, newCustomer); if (!string.IsNullOrEmpty(customerDelta.Dto.LanguageId) && int.TryParse(customerDelta.Dto.LanguageId, out var languageId) && _languageService.GetLanguageById(languageId) != null) { _genericAttributeService.SaveAttribute(newCustomer, NopCustomerDefaults.LanguageIdAttribute, languageId); } //password if (!string.IsNullOrWhiteSpace(customerDelta.Dto.Password)) { AddPassword(customerDelta.Dto.Password, newCustomer); } // We need to insert the entity first so we can have its id in order to map it to anything. // TODO: Localization // TODO: move this before inserting the customer. if (customerDelta.Dto.RoleIds.Count > 0) { AddValidRoles(customerDelta, newCustomer); CustomerService.UpdateCustomer(newCustomer); } // Preparing the result dto of the new customer // We do not prepare the shopping cart items because we have a separate endpoint for them. var newCustomerDto = newCustomer.ToDto(); // This is needed because the entity framework won't populate the navigation properties automatically // and the country will be left null. So we do it by hand here. PopulateAddressCountryNames(newCustomerDto); // Set the fist and last name separately because they are not part of the customer entity, but are saved in the generic attributes. newCustomerDto.FirstName = customerDelta.Dto.FirstName; newCustomerDto.LastName = customerDelta.Dto.LastName; newCustomerDto.LanguageId = customerDelta.Dto.LanguageId; //activity log CustomerActivityService.InsertActivity("AddNewCustomer", LocalizationService.GetResource("ActivityLog.AddNewCustomer"), newCustomer); var customersRootObject = new CustomersRootObject(); customersRootObject.Customers.Add(newCustomerDto); var json = JsonFieldsSerializer.Serialize(customersRootObject, string.Empty); return(new RawJsonActionResult(json)); }
public IHttpActionResult CreateCustomer([ModelBinder(typeof(JsonModelBinder <CustomerDto>))] Delta <CustomerDto> customerDelta) { // Here we display the errors if the validation has failed at some point. if (!ModelState.IsValid) { return(Error()); } //If the validation has passed the customerDelta object won't be null for sure so we don't need to check for this. // Inserting the new customer Customer newCustomer = _factory.Initialize(); customerDelta.Merge(newCustomer); foreach (var address in customerDelta.Dto.CustomerAddresses) { newCustomer.Addresses.Add(address.ToEntity()); } _customerService.InsertCustomer(newCustomer); // Moveleiros :) var lojistaId = customerDelta.Dto.LojistaId; var newStore = _storeService.GetStoreByLojistaId(lojistaId); if (newStore != null) { _customerService.AttachStoreToCustomer(newCustomer, newStore.Id); } InsertFirstAndLastNameGenericAttributes(customerDelta.Dto.FirstName, customerDelta.Dto.LastName, newCustomer); int languageId = 0; if (!string.IsNullOrEmpty(customerDelta.Dto.LanguageId) && int.TryParse(customerDelta.Dto.LanguageId, out languageId) && _languageService.GetLanguageById(languageId) != null) { _genericAttributeService.SaveAttribute(newCustomer, SystemCustomerAttributeNames.LanguageId, languageId); } //password if (!string.IsNullOrWhiteSpace(customerDelta.Dto.Password)) { AddPassword(customerDelta.Dto.Password, newCustomer); } // We need to insert the entity first so we can have its id in order to map it to anything. // TODO: Localization // TODO: move this before inserting the customer. if (customerDelta.Dto.RoleIds.Count > 0) { AddValidRoles(customerDelta, newCustomer); _customerService.UpdateCustomer(newCustomer); } // Preparing the result dto of the new customer // We do not prepare the shopping cart items because we have a separate endpoint for them. CustomerDto newCustomerDto = newCustomer.ToDto(); // This is needed because the entity framework won't populate the navigation properties automatically // and the country will be left null. So we do it by hand here. PopulateAddressCountryNames(newCustomerDto); // Set the fist and last name separately because they are not part of the customer entity, but are saved in the generic attributes. newCustomerDto.FirstName = customerDelta.Dto.FirstName; newCustomerDto.LastName = customerDelta.Dto.LastName; newCustomerDto.LanguageId = customerDelta.Dto.LanguageId; //activity log _customerActivityService.InsertActivity("AddNewCustomer", _localizationService.GetResource("ActivityLog.AddNewCustomer"), newCustomer.Id); var customersRootObject = new CustomersRootObject(); customersRootObject.Customers.Add(newCustomerDto); var json = _jsonFieldsSerializer.Serialize(customersRootObject, string.Empty); return(new RawJsonActionResult(json)); }