diff --git a/force-app/main/default/classes/IdeaListViewComponentController.cls b/force-app/main/default/classes/IdeaListViewComponentController.cls index 0d602f2..b521983 100644 --- a/force-app/main/default/classes/IdeaListViewComponentController.cls +++ b/force-app/main/default/classes/IdeaListViewComponentController.cls @@ -1,55 +1,53 @@ public with sharing class IdeaListViewComponentController { @AuraEnabled(cacheable=true) - public static List getIdeasWithVotes(String sourceType, String sortField, String sortOrder, String statusFilter, Id recordId) { + public static Map getIdeasWithVotes(String sourceType, String sortField, String sortOrder, String statusFilter, Id recordId, Integer pageSize, Integer pageNumber) { Id currentUserId = UserInfo.getUserId(); - + Integer offset = (pageNumber - 1) * pageSize; + // Base query for ideas String baseQuery = 'SELECT Id, Name, Product_Tag__c, Product_Tag__r.Name, Status__c, Subject__c, Submitted_By__c, Submitted_By__r.Name, Up__c, Down__c FROM Idea__c'; + String countQuery = 'SELECT COUNT() FROM Idea__c'; List ideas; + Integer totalRecords; try { + String whereClause = ''; if (recordId != null) { - // If recordId is provided, fetch that specific Idea - baseQuery += ' WHERE Id = :recordId'; - ideas = [SELECT Id, Name, Product_Tag__c, Product_Tag__r.Name, Status__c, Subject__c, Submitted_By__c, Submitted_By__r.Name, Up__c, Down__c FROM Idea__c WHERE Id = :recordId]; + whereClause = ' WHERE Id = :recordId'; } else if (sourceType == 'CurrentUser') { - baseQuery += ' WHERE Submitted_By__c = :currentUserId'; - ideas = Database.query(baseQuery); + whereClause = ' WHERE Submitted_By__c = :currentUserId'; } else { - baseQuery += ' WHERE Id != NULL'; // Ensures WHERE clause is always valid - ideas = Database.query(baseQuery); + whereClause = ' WHERE Id != NULL'; // Ensures WHERE clause is always valid } // Apply status filter if provided and valid if (statusFilter != null && !String.isBlank(statusFilter)) { - try { - List statusList = statusFilter.split(','); - if (!statusList.isEmpty()) { - baseQuery += ' AND Status__c IN :statusList'; - } - } catch (Exception e) { - System.debug('Error processing statusFilter: ' + e.getMessage()); + List statusList = statusFilter.split(','); + if (!statusList.isEmpty()) { + whereClause += ' AND Status__c IN :statusList'; } } - // Apply sorting based on sortField and sortOrder - baseQuery += ' ORDER BY ' + sortField + ' ' + sortOrder + ' LIMIT 100'; - - // Debugging output to verify final query - System.debug('Final Query: ' + baseQuery); - - // Execute the query - if (recordId == null) { - ideas = Database.query(baseQuery); - } - + // Append the whereClause to both queries + baseQuery += whereClause; + countQuery += whereClause; + + // Apply sorting and pagination + baseQuery += ' ORDER BY ' + sortField + ' ' + sortOrder + ' LIMIT :pageSize OFFSET :offset'; + + // Execute the queries + ideas = Database.query(baseQuery); + + // Execute the count query to get the total number of records + totalRecords = Database.countQuery(countQuery); // Changed from SOQL to countQuery + // Collect idea IDs for further processing Set ideaIds = new Set(); for (Idea__c idea : ideas) { ideaIds.add(idea.Id); } - + // Map to hold votes related to the ideas Map ideaVotesMap = new Map(); if (!ideaIds.isEmpty()) { @@ -60,85 +58,35 @@ public with sharing class IdeaListViewComponentController { ideaVotesMap.put(vote.Idea__c, vote); } } - + // Wrap the Ideas and associate the votes List ideaList = new List(); for (Idea__c idea : ideas) { Idea_Vote__c userVote = ideaVotesMap.get(idea.Id); ideaList.add(new IdeaWrapper(idea, userVote)); } - - return ideaList; + + // Prepare the return map + Map resultMap = new Map(); + resultMap.put('ideas', ideaList); + resultMap.put('totalRecords', totalRecords); + + return resultMap; } catch (Exception e) { // Log the error and throw a handled exception to the client System.debug('Error in getIdeasWithVotes: ' + e.getMessage()); throw new AuraHandledException('Error in getIdeasWithVotes: ' + e.getMessage()); } } - - // Method to handle upvote @AuraEnabled public static void handleUpVote(Id ideaId) { - Id currentUserId = UserInfo.getUserId(); - Idea_Vote__c existingVote; - - try { - existingVote = [SELECT Id, Type__c FROM Idea_Vote__c WHERE Idea__c = :ideaId AND Submitted_By__c = :currentUserId LIMIT 1]; - } catch (QueryException e) { - existingVote = null; - } - - if (existingVote != null) { - if (existingVote.Type__c == 'Up') { - // If the existing vote is already an upvote, delete it - delete existingVote; - } else { - // If the existing vote is a downvote, update it to an upvote - existingVote.Type__c = 'Up'; - update existingVote; - } - } else { - // Create a new upvote record - Idea_Vote__c newVote = new Idea_Vote__c( - Idea__c = ideaId, - Type__c = 'Up', - Submitted_By__c = currentUserId - ); - insert newVote; - } + IdeaVotingController.handleUpVote(ideaId); } - // Method to handle downvote @AuraEnabled public static void handleDownVote(Id ideaId) { - Id currentUserId = UserInfo.getUserId(); - Idea_Vote__c existingVote; - - try { - existingVote = [SELECT Id, Type__c FROM Idea_Vote__c WHERE Idea__c = :ideaId AND Submitted_By__c = :currentUserId LIMIT 1]; - } catch (QueryException e) { - existingVote = null; - } - - if (existingVote != null) { - if (existingVote.Type__c == 'Down') { - // If the existing vote is already a downvote, delete it - delete existingVote; - } else { - // If the existing vote is an upvote, update it to a downvote - existingVote.Type__c = 'Down'; - update existingVote; - } - } else { - // Create a new downvote record - Idea_Vote__c newVote = new Idea_Vote__c( - Idea__c = ideaId, - Type__c = 'Down', - Submitted_By__c = currentUserId - ); - insert newVote; - } + IdeaVotingController.handleDownVote(ideaId); } // Wrapper class to return Idea and associated IdeaVote diff --git a/force-app/main/default/classes/IdeaVoteComponentController.cls b/force-app/main/default/classes/IdeaVoteComponentController.cls new file mode 100644 index 0000000..be5bdbb --- /dev/null +++ b/force-app/main/default/classes/IdeaVoteComponentController.cls @@ -0,0 +1,62 @@ +public with sharing class IdeaVoteComponentController { + + @AuraEnabled(cacheable=true) + public static IdeaWrapper getIdeaWithVotes(Id recordId) { + Id currentUserId = UserInfo.getUserId(); + + // Check if recordId is null + if (recordId == null) { + throw new AuraHandledException('Record ID is null. Cannot fetch Idea.'); + } + + try { + // Query to get the idea record based on recordId + Idea__c idea = [SELECT Id, Name, Product_Tag__c, Product_Tag__r.Name, Status__c, Subject__c, Submitted_By__c, Submitted_By__r.Name, Up__c, Down__c + FROM Idea__c + WHERE Id = :recordId + LIMIT 1]; + + // Query to get the vote associated with the current user for this idea + Idea_Vote__c userVote; + try { + userVote = [SELECT Id, Idea__c, Type__c FROM Idea_Vote__c + WHERE Idea__c = :idea.Id + AND Submitted_By__c = :currentUserId + LIMIT 1]; + } catch (Exception e) { + userVote = null; // If no vote found, set it to null + } + + // Wrap the Idea and associated vote in an IdeaWrapper + IdeaWrapper ideaWrapper = new IdeaWrapper(idea, userVote); + + return ideaWrapper; + + } catch (Exception e) { + // Log the error and throw a handled exception to the client + System.debug('Error in getIdeaWithVotes: ' + e.getMessage()); + throw new AuraHandledException('Error in getIdeaWithVotes: ' + e.getMessage()); + } + } + + @AuraEnabled + public static void handleUpVote(Id ideaId) { + IdeaVotingController.handleUpVote(ideaId); + } + + @AuraEnabled + public static void handleDownVote(Id ideaId) { + IdeaVotingController.handleDownVote(ideaId); + } + + // Wrapper class to return Idea and associated IdeaVote + public class IdeaWrapper { + @AuraEnabled public Idea__c idea { get; set; } + @AuraEnabled public Idea_Vote__c userVote { get; set; } + + public IdeaWrapper(Idea__c idea, Idea_Vote__c userVote) { + this.idea = idea; + this.userVote = userVote; + } + } +} diff --git a/force-app/main/default/classes/IdeaVoteComponentController.cls-meta.xml b/force-app/main/default/classes/IdeaVoteComponentController.cls-meta.xml new file mode 100644 index 0000000..7d5f9e8 --- /dev/null +++ b/force-app/main/default/classes/IdeaVoteComponentController.cls-meta.xml @@ -0,0 +1,5 @@ + + + 61.0 + Active + \ No newline at end of file diff --git a/force-app/main/default/classes/IdeaVotingController.cls b/force-app/main/default/classes/IdeaVotingController.cls new file mode 100644 index 0000000..2487f1a --- /dev/null +++ b/force-app/main/default/classes/IdeaVotingController.cls @@ -0,0 +1,64 @@ +public with sharing class IdeaVotingController { + + @AuraEnabled + public static void handleUpVote(Id ideaId) { + Id currentUserId = UserInfo.getUserId(); + Idea_Vote__c existingVote; + + try { + existingVote = [SELECT Id, Type__c FROM Idea_Vote__c WHERE Idea__c = :ideaId AND Submitted_By__c = :currentUserId LIMIT 1]; + } catch (QueryException e) { + existingVote = null; + } + + if (existingVote != null) { + if (existingVote.Type__c == 'Up') { + // If the existing vote is already an upvote, delete it + delete existingVote; + } else { + // If the existing vote is a downvote, update it to an upvote + existingVote.Type__c = 'Up'; + update existingVote; + } + } else { + // Create a new upvote record + Idea_Vote__c newVote = new Idea_Vote__c( + Idea__c = ideaId, + Type__c = 'Up', + Submitted_By__c = currentUserId + ); + insert newVote; + } + } + + @AuraEnabled + public static void handleDownVote(Id ideaId) { + Id currentUserId = UserInfo.getUserId(); + Idea_Vote__c existingVote; + + try { + existingVote = [SELECT Id, Type__c FROM Idea_Vote__c WHERE Idea__c = :ideaId AND Submitted_By__c = :currentUserId LIMIT 1]; + } catch (QueryException e) { + existingVote = null; + } + + if (existingVote != null) { + if (existingVote.Type__c == 'Down') { + // If the existing vote is already a downvote, delete it + delete existingVote; + } else { + // If the existing vote is an upvote, update it to a downvote + existingVote.Type__c = 'Down'; + update existingVote; + } + } else { + // Create a new downvote record + Idea_Vote__c newVote = new Idea_Vote__c( + Idea__c = ideaId, + Type__c = 'Down', + Submitted_By__c = currentUserId + ); + insert newVote; + } + } +} diff --git a/force-app/main/default/classes/IdeaVotingController.cls-meta.xml b/force-app/main/default/classes/IdeaVotingController.cls-meta.xml new file mode 100644 index 0000000..7d5f9e8 --- /dev/null +++ b/force-app/main/default/classes/IdeaVotingController.cls-meta.xml @@ -0,0 +1,5 @@ + + + 61.0 + Active + \ No newline at end of file diff --git a/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.css b/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.css index 58ffc0c..a5f066b 100644 --- a/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.css +++ b/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.css @@ -1,12 +1,14 @@ -/* ideaListViewComponent.css */ +/* Add padding or margin to the container around the datatable */ +.datatable-container { + margin-bottom: 20px; /* Add margin below the datatable */ +} -/* Custom styles for the lightning-card */ +/* Optional: Keep the existing styles */ .lightning-card { max-width: 100%; /* Ensure the card does not exceed its container */ overflow-x: auto; /* Allow horizontal scrolling if content overflows */ } -/* Style the title of the lightning-card */ .lightning-card__header { background-color: transparent; /* Remove background color */ border-bottom: none; /* Remove border under the title */ @@ -15,7 +17,6 @@ padding: 0; /* Adjust padding */ } -/* Optionally, you can add some margin or padding to the datatable if needed */ .lightning-datatable { - margin-top: 10px; /* Example of adding some margin above the table */ + margin-top: 10px; /* Margin above the table */ } diff --git a/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.html b/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.html index 22909b1..67ef79f 100644 --- a/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.html +++ b/force-app/main/default/lwc/ideaListViewComponent/ideaListViewComponent.html @@ -1,15 +1,42 @@