public async Task CreateCustomerAsync_CanCreateCustomer_ShouldCreateExpectedCustomer()
        {
            //Arrange
            var preferenceId = Guid.Parse("ef7f299f-92d7-459f-896e-078ed53ef99c");
            var request      = new CreateOrEditCustomerRequest()
            {
                Email         = "*****@*****.**",
                FirstName     = "Иван",
                LastName      = "Петров",
                PreferenceIds = new List <Guid>()
                {
                    preferenceId
                }
            };

            //Act
            var result = await _customersController.CreateCustomerAsync(request);

            var actionResult = result.Result as CreatedAtActionResult;
            var id           = (Guid)actionResult.Value;

            //Assert
            var actual = await _customerRepository.GetByIdAsync(id);

            actual.Email.Should().Be(request.Email);
            actual.FirstName.Should().Be(request.FirstName);
            actual.LastName.Should().Be(request.LastName);

            // actual.Preferences.Should()
            //     .ContainSingle()
            //     .And
            //     .Contain(x => x.PreferenceId == preferenceId);
        }
        public async Task <IActionResult> EditCustomersAsync(Guid id, CreateOrEditCustomerRequest request)
        {
            if (request is null)
            {
                return(BadRequest("Request is empty"));
            }

            var exsistingCustomer = await _customerRepository.GetCustomerForUpdate(id);

            if (exsistingCustomer is null)
            {
                return(NotFound("Customer wasn't found"));
            }

            if (request.PreferenceIds.Count > 0)
            {
                foreach (var item in request.PreferenceIds)
                {
                    if (!exsistingCustomer.CustomerPreferences.Any(c => c.PreferenceId == item))
                    {
                        exsistingCustomer.CustomerPreferences.Add(new CustomerPreference
                        {
                            PreferenceId = item,
                            CustomerId   = exsistingCustomer.Id
                        });
                    }
                }
            }

            var updatedCustomer = _mapper.Map(request, exsistingCustomer);

            var k = await _customerRepository.UpdateAsync(updatedCustomer);

            return(Ok(_mapper.Map <CustomerShortResponse>(updatedCustomer)));
        }
        public override async Task <Empty> EditCustomers(CustomerModel request, ServerCallContext context)
        {
            var guids = new List <Guid>
            {
                Guid.Parse(request.PreferenceId)
            };

            var customer = await _customerRepository.GetByIdAsync(Guid.Parse(request.Id));

            var preferences = await _preferenceRepository.GetRangeByIdsAsync(guids);

            var createOrEditCustomerRequest = new CreateOrEditCustomerRequest()
            {
                FirstName     = request.FirstName,
                Email         = request.Email,
                LastName      = request.LastName,
                PreferenceIds = guids
            };

            CustomerMapper.MapFromModel(createOrEditCustomerRequest, preferences, customer);

            await _customerRepository.UpdateAsync(customer);

            return(new Empty());
        }
示例#4
0
        public static Customer MapFromModel(CreateOrEditCustomerRequest model, IEnumerable <Preference> preferences, Customer customer = null)
        {
            if (customer == null)
            {
                customer = new Customer
                {
                    Id = Guid.NewGuid()
                };
            }

            customer.FirstName = model.FirstName;
            customer.LastName  = model.LastName;
            customer.Email     = model.Email;

            if (customer.Preferences != null)
            {
                customer.Preferences = preferences.Select(x => new CustomerPreference()
                {
                    CustomerId   = customer.Id,
                    Preference   = x,
                    PreferenceId = x.Id
                }).ToList();
            }

            return(customer);
        }
        public async Task <IActionResult> CreateCustomerAsync(CreateOrEditCustomerRequest request)
        {
            if (request is null)
            {
                return(BadRequest("Request is empty"));
            }

            var customer = _mapper.Map <Customer>(request);

            customer.Id = Guid.NewGuid();

            if (request.PreferenceIds.Count > 0)
            {
                customer.CustomerPreferences = new List <CustomerPreference>();
                foreach (var item in request.PreferenceIds)
                {
                    customer.CustomerPreferences.Add(new CustomerPreference
                    {
                        PreferenceId = item,
                        CustomerId   = customer.Id
                    });
                }
            }

            var createdCustomerId = await _customerRepository.CreateAsync(customer);

            return(Ok($"Customer with {createdCustomerId} Id was successfuly created"));
        }
        public async Task <IActionResult> EditCustomersAsync(Guid id, CreateOrEditCustomerRequest request)
        {
            var customer = await _customerRepository.GetByIdAsync(id);

            if (customer == null)
            {
                return(NotFound());
            }

            var preferences = await _preferenceRepository.GetRangeByIdsAsync(request.PreferenceIds);

            customer.Email     = request.Email;
            customer.FirstName = request.FirstName;
            customer.LastName  = request.LastName;
            customer.Preferences.Clear();
            customer.Preferences = preferences.Select(x => new CustomerPreference()
            {
                Customer   = customer,
                Preference = x
            }).ToList();

            await _customerRepository.UpdateAsync(customer);

            return(NoContent());
        }
示例#7
0
        public static Customer MapFromModel(CreateOrEditCustomerRequest request, IEnumerable <Preference> preference,
                                            Customer customer = null)
        {
            customer ??= new Customer
            {
                Preferences = new List <CustomerPreference>(),
                PromoCodes  = new List <PromoCodeCustomer>(),
            };

            customer.FirstName = request.FirstName;
            customer.LastName  = request.LastName;
            customer.Email     = request.Email;

            customer.Preferences?.Clear();
            if (preference?.Any() == false)
            {
                return(customer);
            }

            customer.Preferences = preference
                                   .Select(x => new CustomerPreference(customer, x))
                                   .ToList();

            return(customer);
        }
示例#8
0
        public async Task UpdateAsync(Guid id, CreateOrEditCustomerRequest request)
        {
            var customer = await _customerRepository.GetByIdAsync(id);

            var preferences = await _preferenceRepository.GetRangeByIdsAsync(request.PreferenceIds);

            CustomerMapper.MapFromModel(request, preferences, customer);

            await _customerRepository.UpdateAsync(customer);
        }
示例#9
0
        public async Task <ActionResult <CustomerResponse> > CreateCustomerAsync(CreateOrEditCustomerRequest request)
        {
            var preferences = await GetPreferencesAsync(request.PreferenceIds);

            var customer = CustomerMapper.MapFromModel(request, preferences);

            await _customerRepository.AddAsync(customer);

            return(CreatedAtAction(nameof(GetCustomerAsync), new { id = customer.Id }, null));
        }
示例#10
0
        public async Task AddAsync(CreateOrEditCustomerRequest request)
        {
            //Получаем предпочтения из бд и сохраняем большой объект
            var preferences = await _preferenceRepository
                              .GetRangeByIdsAsync(request.PreferenceIds);

            Customer customer = CustomerMapper.MapFromModel(request, preferences);

            await _customerRepository.AddAsync(customer);
        }
示例#11
0
        public async Task <ActionResult <CustomerResponse> > CreateCustomerAsync(CreateOrEditCustomerRequest request)
        {
            //Получаем предпочтения из бд и сохраняем большой объект
            var preferences = await _preferenceRepository
                              .GetRangeByIdsAsync(request.PreferenceIds);

            Customer customer = CustomerMapper.MapFromModel(request, preferences);

            await _customerRepository.AddAsync(customer);

            return(CreatedAtAction(nameof(GetCustomerAsync), new { id = customer.Id }, customer.Id));
        }
        public async Task <ActionResult <CustomerResponse> > CreateCustomerAsync(CreateOrEditCustomerRequest request)
        {
            if (request == null)
            {
                return(BadRequest());
            }

            var preferences = await GetPreferencesAsync(request.PreferenceIds);

            var customer = CustomerMapper.MapFromModel(request, preferences);

            await _customerRepository.CreateAsync(customer);

            var response = new CustomerResponse(customer);

            return(CreatedAtRoute(null, response));
        }
示例#13
0
        public async Task <IActionResult> EditCustomersAsync(Guid id, CreateOrEditCustomerRequest request)
        {
            var customer = await _customerRepository.GetByIdAsync(id);

            if (customer == null)
            {
                return(NotFound());
            }

            var preferences = await _preferenceRepository.GetRangeByIdsAsync(request.PreferenceIds);

            CustomerMapper.MapFromModel(request, preferences, customer);

            await _customerRepository.UpdateAsync(customer);

            return(NoContent());
        }
示例#14
0
        public async Task <IActionResult> EditCustomersAsync(Guid id, CreateOrEditCustomerRequest request)
        {
            var customer = await _customerRepository.GetByIdAsync(id);

            if (customer == null)
            {
                return(NotFound());
            }

            var preferences = (await _preferencesGateway.GetPreferencesAsync())
                              .Where(item => request.PreferenceIds.Contains(item.Id));

            CustomerMapper.MapFromModel(request, preferences, customer);

            await _customerRepository.UpdateAsync(customer);

            return(NoContent());
        }
示例#15
0
        public async Task <IActionResult> EditCustomersAsync(Guid id, CreateOrEditCustomerRequest request)
        {
            var preferences = new List <Preference>();

            foreach (var preferenceId in request.PreferenceIds)
            {
                var preference = await _preferenceRepository.GetByIdAsync(preferenceId);

                if (preference == null)
                {
                    return(BadRequest());
                }
                preferences.Add(preference);
            }

            var customer = await _customerRepository.GetByIdAsync(id);

            var customerPreference = await _customerPreference.GetWhere(x => x.CustomerId == id);

            foreach (var item in customerPreference)
            {
                await _customerPreference.DeleteAsync(item);
            }

            var customerPreferences = preferences.Select(x => new CustomerPreference()
            {
                Id           = Guid.NewGuid(),
                CustomerId   = customer.Id,
                Customer     = customer,
                PreferenceId = x.Id,
                Preference   = x
            }).ToList();

            customer.FirstName   = request.FirstName;
            customer.LastName    = request.LastName;
            customer.Email       = request.Email;
            customer.Preferences = customerPreferences;


            await _customerRepository.UpdateAsync(customer);

            return(NoContent());
        }
示例#16
0
        public async Task <IActionResult> CreateCustomerAsync(CreateOrEditCustomerRequest request)
        {
            var preferences = new List <Preference>();

            foreach (var preferenceId in request.PreferenceIds)
            {
                var preference = await _preferenceRepository.GetByIdAsync(preferenceId);

                if (preference == null)
                {
                    return(BadRequest());
                }
                preferences.Add(preference);
            }

            var customer = new Customer()
            {
                Id        = Guid.NewGuid(),
                Email     = request.Email,
                FirstName = request.FirstName,
                LastName  = request.LastName,
            };

            var customerPreferences = preferences.Select(x => new CustomerPreference()
            {
                Id           = Guid.NewGuid(),
                CustomerId   = customer.Id,
                Customer     = customer,
                PreferenceId = x.Id,
                Preference   = x
            }).ToList();

            customer.Preferences = customerPreferences;

            await _customerRepository.AddAsync(customer);

            return(Ok());
        }
        public async Task CreateCustomerAsync_CanCreateCustomer_ShouldCreateExpectedCustomer()
        {
            //Arrange
            var client = _factory.CreateClient();

            var preferenceId = Guid.Parse("ef7f299f-92d7-459f-896e-078ed53ef99c");
            var request      = new CreateOrEditCustomerRequest()
            {
                Email         = "*****@*****.**",
                FirstName     = "Иван",
                LastName      = "Петров",
                PreferenceIds = new List <Guid>()
                {
                    preferenceId
                }
            };

            //Act
            var response = await client.PostAsJsonAsync("/api/v1/customers", request);

            //Assert
            response.IsSuccessStatusCode.Should().BeTrue();
            response.StatusCode.Should().Be(HttpStatusCode.Created);

            //Теперь получаем объект, который должен было создан, если REST правильно написан, то в Location будет
            //готовый URL для получения нового объекта
            var actualContent = await client.GetStringAsync(response.Headers.Location);

            var actual = JsonConvert.DeserializeObject <CustomerResponse>(actualContent);

            actual.Email.Should().Be(request.Email);
            actual.FirstName.Should().Be(request.FirstName);
            actual.LastName.Should().Be(request.LastName);
            actual.Preferences.Should()
            .ContainSingle()
            .And
            .Contain(x => x.Id == preferenceId);
        }
        public static Customer MapFromModel(CreateOrEditCustomerRequest model, IEnumerable <PreferenceDto> preferences, Customer customer = null)
        {
            if (customer == null)
            {
                customer    = new Customer();
                customer.Id = Guid.NewGuid();
            }

            customer.FirstName = model.FirstName;
            customer.LastName  = model.LastName;
            customer.Email     = model.Email;

            customer.PreferencesId = preferences.Select(p => p.Id).ToList();

            // customer.Preferences = preferences.Select(x => new CustomerPreference()
            // {
            //     CustomerId = customer.Id,
            //     Preference = x,
            //     PreferenceId = x.Id
            // }).ToList();

            return(customer);
        }
        public async Task <ActionResult <CustomerResponse> > CreateCustomerAsync(CreateOrEditCustomerRequest request)
        {
            //Получаем предпочтения из бд и сохраняем большой объект
            var preferences = await _preferenceRepository
                              .GetRangeByIdsAsync(request.PreferenceIds);

            var customer = new Customer()
            {
                Email     = request.Email,
                FirstName = request.FirstName,
                LastName  = request.LastName,
            };

            customer.Preferences = preferences.Select(x => new CustomerPreference()
            {
                Customer   = customer,
                Preference = x
            }).ToList();

            await _customerRepository.AddAsync(customer);

            return(CreatedAtAction("CreateCustomer", new { id = customer.Id }, null));
        }
示例#20
0
        public static Customer MapFromModel(CreateOrEditCustomerRequest request,
                                            IEnumerable <Preference> preferences, Customer customer = null)
        {
            if (customer == null)
            {
                customer = new Customer();
            }

            customer.Email     = request.Email;
            customer.FirstName = request.FirstName;
            customer.LastName  = request.LastName;

            if (preferences != null && preferences.Any())
            {
                customer.Preferences?.Clear();
                customer.Preferences = preferences.Select(x => new CustomerPreference()
                {
                    Customer   = customer,
                    Preference = x
                }).ToList();
            }
            return(customer);
        }
 public int EditCustomer([Service] Domain.ICustomers customers, Guid id, CreateOrEditCustomerRequest request)
 {
     customers.UpdateAsync(id, request);
     return(200);
 }
 public int AddCustomer([Service] Domain.ICustomers customers, CreateOrEditCustomerRequest request)
 {
     customers.AddAsync(request);
     return(200);
 }