/// <summary> /// check if customer is already exists /// if not exists ,new customer will be created based on order data /// </summary> /// <param name="cartCustomerModel"></param> /// <returns>created or the alraedy customer</returns> public static async Task <Customer> RegisterCustomerIfNotExists(CartCustomerModel cartCustomerModel, int ordersCount) { if (_context.Customers.Any(c => c.fullName == cartCustomerModel.phone))//error { var registeredCustomer = await _context.Customers.FirstOrDefaultAsync(c => c.fullName == cartCustomerModel.phone); registeredCustomer.consumedProducts += ordersCount; _context.Entry(registeredCustomer).Property(p => p.consumedProducts).IsModified = true; await _context.SaveChangesAsync(); return(registeredCustomer); } var user = await UserHelper.createCustomerUserByPhone(cartCustomerModel.phone); var customer = new Customer { id = user.Id, fullName = cartCustomerModel.fullName, consumedProducts = ordersCount, User = user }; _context.Customers.Add(customer); await _context.SaveChangesAsync(); return(customer); }
public async Task RegisterCustomerUser(ApplicationUser user, RegisterModel model) { var customer = new Customer { id = user.Id, imgSrc = $"{GlobalProperties.GetUsersImagesPath(_httpContext)}{GlobalVariables.DefaultUserImageName}", fullName = model.fullName }; _context.Customers.Add(customer); if (await _context.SaveChangesAsync() != 1) { throw new Exception("cannot add customer user"); } }