public async Task <UserResponse> DeleteAsync(int id) { var existingUser = await _userRepository.FindByIdAsync(id); if (existingUser == null) { return(new UserResponse("User not found")); } try { IEnumerable <UserPlan> userPlans = await _userPlanRepository.ListByUserIdAsync(id); userPlans.ToList().ForEach(userPlan => { _userPlanRepository.Remove(userPlan); }); _userRepository.Remove(existingUser); await _unitOfWork.CompleteAsync(); return(new UserResponse(existingUser)); } catch (Exception ex) { return(new UserResponse($"An error ocurred while deleting user: {ex.Message}")); } }
public async Task <UserPlanResponse> UnassignUserPlanAsync(int userId, int planId) { try { PlanUser userPlan = await _userPlanRepository.FindByUserIdAndPlanId(userId, planId); _userPlanRepository.Remove(userPlan); await _unitOfWork.CompleteAsync(); return(new UserPlanResponse(userPlan)); } catch (Exception ex) { return(new UserPlanResponse($"An error ocurred while unnasigning Plan to User {ex.Message}")); } }