Created
April 7, 2014 19:03
-
-
Save joshbirk/10031430 to your computer and use it in GitHub Desktop.
ELEVATE Extra Credit: Custom REST Responses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| In the Programmers workbook there is a tutorial on creating a basic REST endpoint using Apex. A common scenario with these | |
| endpoints is needing to enhance the message that gets sent down to client. You can do this easily within Apex by defining | |
| a custom class to use as a response, which will be converted to JSON by the platform. | |
| To complete this extra credit: | |
| 1. Create a class from the code below | |
| 2. Update the POST and DELETE endpoints to use the RESTMessage class | |
| 3. Test the new endpoint with unit test in the next class | |
| */ | |
| @RestResource(urlMapping='/WarehouseREST/*') | |
| global with sharing class WarehouseREST { | |
| global class RESTMessage { | |
| public String message {get; set;} | |
| public String status {get; set;} | |
| public String info {get; set;} | |
| public RESTMessage(String m, String s, String i) { | |
| this.message = m; | |
| this.status = s; | |
| this.info = i; | |
| } | |
| } | |
| global WarehouseREST() {} | |
| @HttpPost | |
| global static String postMerchandiseToInvoice(Id invoiceID, List<Id> merchIDs) { | |
| List<Line_Item__c> line_items = new List<Line_Item__c>(); | |
| for(Id m : merchids) { | |
| Line_Item__c li = new Line_Item__c(Invoice__c = invoiceID, Merchandise__c=m, Quantity__c=1); | |
| line_items.add(li); | |
| } | |
| try { | |
| insert line_items; | |
| return line_items.size() +' item(s) added'; | |
| } catch (DMLException e) { | |
| return e.getMessage(); | |
| } | |
| } | |
| @HttpDelete | |
| global static String deleteMerchandiseFromInvoice() { | |
| Id merchId = RestContext.request.params.get('mid'); | |
| Id invoiceId = RestContext.request.params.get('iid'); | |
| List<Line_Item__c> line_items = [SELECT ID from Line_Item__c WHERE Merchandise__c = :merchID AND Invoice__c = :invoiceId]; | |
| try { | |
| delete line_items; | |
| return 'Done'; | |
| } catch (DMLException e) { | |
| return 'Error'; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @isTest | |
| global class WarehouseRESTTest { | |
| @IsTest | |
| global static void testWarehouseREST() { | |
| Merchandise__c m = new Merchandise__c(Name='Rack Server',Price__c=1245.99,Quantity__c=500); | |
| insert m; | |
| Invoice__c i = new Invoice__c(); | |
| insert i; | |
| RestRequest req = new RestRequest(); | |
| RestResponse res = new RestResponse(); | |
| WarehouseREST.RESTMessage post_response = WarehouseREST.postMerchandiseToInvoice(i.id,new Id[]{m.id}); | |
| System.assertEquals('Completed',post_response.status); | |
| // pass the req and resp objects to the method | |
| req.requestURI = 'https://www.salesforce.com/services/apexrest/WarehouseREST/?iid='+i.Id+'&'+m.id; | |
| req.httpMethod = 'DELETE'; | |
| RestContext.request = req; | |
| RestContext.response = res; | |
| WarehouseREST.RESTMessage del_response = WarehouseREST.deleteMerchandiseFromInvoice(); | |
| System.assertEquals('Completed',del_response.status); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment