Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature controller seperation #7

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 36 additions & 88 deletions force-app/main/default/classes/IdeaListViewComponentController.cls
Original file line number Diff line number Diff line change
@@ -1,55 +1,53 @@
public with sharing class IdeaListViewComponentController {

@AuraEnabled(cacheable=true)
public static List<IdeaWrapper> getIdeasWithVotes(String sourceType, String sortField, String sortOrder, String statusFilter, Id recordId) {
public static Map<String, Object> 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<Idea__c> 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<String> statusList = statusFilter.split(',');
if (!statusList.isEmpty()) {
baseQuery += ' AND Status__c IN :statusList';
}
} catch (Exception e) {
System.debug('Error processing statusFilter: ' + e.getMessage());
List<String> 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<Id> ideaIds = new Set<Id>();
for (Idea__c idea : ideas) {
ideaIds.add(idea.Id);
}

// Map to hold votes related to the ideas
Map<Id, Idea_Vote__c> ideaVotesMap = new Map<Id, Idea_Vote__c>();
if (!ideaIds.isEmpty()) {
Expand All @@ -60,85 +58,35 @@ public with sharing class IdeaListViewComponentController {
ideaVotesMap.put(vote.Idea__c, vote);
}
}

// Wrap the Ideas and associate the votes
List<IdeaWrapper> ideaList = new List<IdeaWrapper>();
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<String, Object> resultMap = new Map<String, Object>();
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
Expand Down
62 changes: 62 additions & 0 deletions force-app/main/default/classes/IdeaVoteComponentController.cls
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>61.0</apiVersion>
<status>Active</status>
</ApexClass>
64 changes: 64 additions & 0 deletions force-app/main/default/classes/IdeaVotingController.cls
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>61.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -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 */
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
<template>
<lightning-card title={title}>
<template if:true={ideas}>
<lightning-datatable
data={ideas}
columns={columns}
key-field="ideaId"
onrowaction={handleRowAction}
hide-checkbox-column="true"
resize-column-disabled
>
</lightning-datatable>
<div class="datatable-container">
<lightning-datatable
data={ideas}
columns={columns}
key-field="ideaId"
onrowaction={handleRowAction}
hide-checkbox-column="true"
resize-column-disabled
>
</lightning-datatable>
</div>

<lightning-layout horizontal-align="space">
<lightning-layout-item flexibility="auto">
<lightning-button
label="Previous"
icon-name="utility:chevronleft"
onclick={handlePrevious}
disabled={isPreviousDisabled}
>
</lightning-button>
</lightning-layout-item>
<lightning-layout-item flexibility="auto">
Page {page} of {totalPage}
</lightning-layout-item>
<lightning-layout-item flexibility="auto">
<lightning-button
label="Next"
icon-name="utility:chevronright"
icon-position="right"
onclick={handleNext}
disabled={isNextDisabled}
>
</lightning-button>
</lightning-layout-item>
</lightning-layout>
</template>
<template if:true={error}>
<div class="slds-text-color_error">
Expand Down
Loading