public async Task UpdateCustomerProductWithId(CustomerProduct customerProduct) { if (CustomerProductExists(customerProduct.CustomerProductId)) { if (!string.IsNullOrEmpty(customerProduct.ExtraInformation)) { try { CustomerReturnDto customer = await _cusRepository.GetCustomerByCustomerId(customerProduct.CustomerId); await _email.SendEmailAsync(customer.UserEmail, "AN UPDATE TO YOUR PRODUCT FROM INFOMALL", customerProduct.ExtraInformation); } catch (Exception ex) { _logger.LogInformation($"Cannot not get customer or send email: {ex.Message}"); } } try { CustomerProduct cp = await _ctx.CustomerProducts.Where(c => c.CustomerProductId == customerProduct.CustomerProductId) .Select(c => new CustomerProduct { ActualEndDate = c.ActualEndDate, ActualStartDate = c.ActualStartDate, CustomerDecription = c.CustomerDecription, CustomerId = c.CustomerId, PaymentType = customerProduct.PaymentType, ExtraInformation = !string.IsNullOrEmpty(customerProduct.ExtraInformation) ? customerProduct.ExtraInformation : c.ExtraInformation, Price = customerProduct.Price, Stage = customerProduct.Stage, ExpectedEndDate = !c.CustomerHasPaid && customerProduct.CustomerHasPaid ? DateTime.Now.AddDays(14) : c.ExpectedEndDate, ExpectedStartDate = !c.CustomerHasPaid && customerProduct.CustomerHasPaid ? DateTime.Now : c.ExpectedStartDate, CustomerHasPaid = customerProduct.CustomerHasPaid, CustomerProductId = c.CustomerProductId }).SingleOrDefaultAsync(); _ctx.Update(cp); await _ctx.SaveChangesAsync(); } catch (Exception ex) { _logger.LogInformation($"could not update customer product: {ex.Message}"); } } }
public async Task <CustomerReturnDto> GetCustomerByCustomerId(string id, bool includeMyPromotion = false, bool includeMyProducts = false) { if (CustomerExists(id)) { if (includeMyProducts && includeMyPromotion) { Customer customer = await _ctx.Customers.Include(c => c.User) .Include(c => c.CustomerProducts) .Include(c => c.MyPromotions).SingleOrDefaultAsync(); var user = await _userManager.Users.Where(u => u.Id == customer.UserId).SingleOrDefaultAsync(); string userEmail = user.Email; CustomerReturnDto customerReturn = new CustomerReturnDto { CustomerId = customer.CustomerId, UserEmail = userEmail, UserId = customer.UserId, CustomerProducts = customer.CustomerProducts, PromotionCustomers = customer.MyPromotions }; return(customerReturn); } else if (includeMyProducts) { Customer customer = await _ctx.Customers.Include(c => c.User) .Include(c => c.CustomerProducts) .SingleOrDefaultAsync(); var user = await _userManager.Users.Where(u => u.Id == customer.UserId).SingleOrDefaultAsync(); string userEmail = user.Email; CustomerReturnDto customerReturn = new CustomerReturnDto { CustomerId = customer.CustomerId, UserEmail = userEmail, UserId = customer.UserId, CustomerProducts = customer.CustomerProducts }; return(customerReturn); } else if (includeMyPromotion) { Customer customer = await _ctx.Customers.Include(c => c.User) .Include(c => c.MyPromotions) .SingleOrDefaultAsync(); var user = await _userManager.Users.Where(u => u.Id == customer.UserId).SingleOrDefaultAsync(); string userEmail = user.Email; CustomerReturnDto customerReturn = new CustomerReturnDto { CustomerId = customer.CustomerId, UserEmail = userEmail, UserId = customer.UserId, PromotionCustomers = customer.MyPromotions }; return(customerReturn); } else { Customer customer = await _ctx.Customers.Include(c => c.User) .SingleOrDefaultAsync(); var user = await _userManager.Users.Where(u => u.Id == customer.UserId).SingleOrDefaultAsync(); string userEmail = user.Email; CustomerReturnDto customerReturn = new CustomerReturnDto { CustomerId = customer.CustomerId, UserEmail = userEmail, UserId = customer.UserId }; return(customerReturn); } } return(null); }