Forum Discussion

irach15's avatar
irach15
Maven
7 years ago

Accepted solutions via REST API V2 sorted by acceptance time

I'm trying to get the last 6-8 accepted solutions from a specific category via REST API V2 and sort them by acceptance time. 

Custom component aka build-in 'Accepted Solutions', showing post title, board, acceptance time, etc. 

I've coded a testing piece, 1st step...

<div class="disc-grid">
<#attempt>
<#assign solutions = restadmin("2.0","/search?q=" + "SELECT solution_data, is_solution, post_time FROM messages WHERE is_solution = true AND category.id = '${catID}' ORDER BY post_time DESC LIMIT 6"?url) />
<#recover>
<p>Woops...</p>
</#attempt> 

<#if solutions.data.items ?? >
<#list solutions.data.items as solution > 
 <#assign solution_id = solution.solution_data.message_id />
 <div class="item-content">
    ${solution_id}
</div>
</#list>
</#if>
<#if solutions.data.items?size <= 0>
    <p class="errorMsg">No solutions</p>
</#if>
</div>

so far what I've got, the result is different from what I've seen in build-in component.

The ids are not the same.

Questions is how to sort by recent solution aka time acceptance?

and how to (I guess run another api call) to get each solution post topic subject, etc like in default component.

Any help is great!

  • irach15 - 

     

    You can achieve this without making any other call.  You can sort the array using freemarker sort_by object, however, the array should be in a required format. 

     

    http://freemarker.apache.org/docs/ref_builtins_sequence.html#ref_builtin_sort_by

     

     

    <#assign solutions = restadmin("2.0","/search?q=" + "SELECT solution_data, is_solution, post_time FROM messages WHERE is_solution = true AND category.id = '${catID}' ORDER BY post_time DESC LIMIT 6"?url) />

    <#assign sortedMessagesList = [] />

    <#list solutions.data.items as solution >
    <#assign sortedMessagesList = sortedMessagesList + [{'solution_time': solution.solution_data.time , 'message':solution}] />
    </#list>

    ASCENDING
    <#list sortedMessagesList?sort_by("solution_time") as solution >
    <#assign solution_id = solution.message.solution_data.message_id />
    <#assign solution_time = solution.message.solution_data.time />
    <div class="item-content">
    ${solution_id} - ${solution_time}
    </div>
    </#list>

    DESENCDING
    <#list sortedMessagesList?sort_by("solution_time")?reverse as solution >
    <#assign solution_id = solution.message.solution_data.message_id />
    <#assign solution_time = solution.message.solution_data.time />
    <div class="item-content">
    ${solution_id} - ${solution_time}
    </div>
    </#list>

     

  • irach15 - 

     

    You can achieve this without making any other call.  You can sort the array using freemarker sort_by object, however, the array should be in a required format. 

     

    http://freemarker.apache.org/docs/ref_builtins_sequence.html#ref_builtin_sort_by

     

     

    <#assign solutions = restadmin("2.0","/search?q=" + "SELECT solution_data, is_solution, post_time FROM messages WHERE is_solution = true AND category.id = '${catID}' ORDER BY post_time DESC LIMIT 6"?url) />

    <#assign sortedMessagesList = [] />

    <#list solutions.data.items as solution >
    <#assign sortedMessagesList = sortedMessagesList + [{'solution_time': solution.solution_data.time , 'message':solution}] />
    </#list>

    ASCENDING
    <#list sortedMessagesList?sort_by("solution_time") as solution >
    <#assign solution_id = solution.message.solution_data.message_id />
    <#assign solution_time = solution.message.solution_data.time />
    <div class="item-content">
    ${solution_id} - ${solution_time}
    </div>
    </#list>

    DESENCDING
    <#list sortedMessagesList?sort_by("solution_time")?reverse as solution >
    <#assign solution_id = solution.message.solution_data.message_id />
    <#assign solution_time = solution.message.solution_data.time />
    <div class="item-content">
    ${solution_id} - ${solution_time}
    </div>
    </#list>