public async Task <Order> GetFullAndEnsureExistsAsync(int id) { var order = await dbContext .Orders .Where(s => s.Id == id) .Include(s => s.Address) .Include(s => s.OrderDiscount) .Include(s => s.OrderShipment) .Include(x => x.OrderPayment) .Include(s => s.User) .Include(s => s.ProductOrders) .ThenInclude(s => s.ProductCategoryDiscount) .Include(s => s.ProductOrders) .ThenInclude(s => s.Product) .ThenInclude(s => s.Images) .Include(s => s.ProductOrders) .ThenInclude(s => s.ProductOrderCharms) .ThenInclude(s => s.Charm) .FirstOrDefaultAsync(); if (order == null) { throw new StreetwoodException(ErrorCode.GenericNotExist(typeof(Order), $"Order id: {id} not found")); } return(order); }
public async Task<Order> GetAndEnsureExistAsync(int id) { var order = await dbContext.Orders.FindAsync(id); if (order == null) { throw new StreetwoodException(ErrorCode.GenericNotExist(typeof(Order), "Order id: {id} not found")); } return order; }
public static T EnsureFirstExists <T>(this IEnumerable <T> collection, Func <T, bool> condition) { var record = collection.FirstOrDefault(condition); if (record == null) { throw new StreetwoodException(ErrorCode.GenericNotExist(typeof(T))); } return(record); }
public async Task <T> GetAndEnsureExistAsync(Guid id) { var result = await dbSet.FindAsync(id); if (result == null) { throw new StreetwoodException(ErrorCode.GenericNotExist(typeof(T))); } return(result); }
public async Task UpdateUserPasswordAsync(string email, string newPassword, string token) { var user = await userRepository.GetByEmailAndEnsureExistAsync(email, ErrorCode.GenericNotExist(typeof(User))); if (user.ChangePasswordToken != token) { throw new StreetwoodException(ErrorCode.InvalidChangePasswordToken); } user.SetPassword(newPassword, encrypter); user.SetChangePasswordToken(); await userRepository.SaveChangesAsync(); }
public async Task <User> CreateChangePasswordTokenAsync(string email) { var user = await userRepository.GetByEmailAndEnsureExistAsync(email, ErrorCode.GenericNotExist(typeof(User))); if (user.UserStatus == UserStatus.Deleted) { throw new StreetwoodException(ErrorCode.AccessingDeactivatedUser); } user.SetChangePasswordToken(stringGenerator); await userRepository.SaveChangesAsync(); return(user); }
public async Task <OrderDiscount> GetRawByCodeAsync(string code) { if (string.IsNullOrEmpty(code)) { return(null); } var discount = await orderDiscountRepository .GetByCodeAsync(code); if (discount == null) { throw new StreetwoodException(ErrorCode.GenericNotExist(typeof(OrderDiscount))); } return(discount); }
public async Task <T> GetAsync(Guid id) => await dbSet.FindAndEnsureExistAsync(id, ErrorCode.GenericNotExist <T>());