|
| 1 | +@RestResource(UrlMapping='/*/customers/*') |
| 2 | +global with sharing class CustomerWebServiceDemo { |
| 3 | + @HttpGet |
| 4 | + global static void doGet() { |
| 5 | + libak_RestFramework.handleRequest(CustomerRestRouter.class); |
| 6 | + } |
| 7 | + @HttpPost |
| 8 | + global static void doPost() { |
| 9 | + libak_RestFramework.handleRequest(CustomerRestRouter.class); |
| 10 | + } |
| 11 | + @HttpPut |
| 12 | + global static void doPut() { |
| 13 | + libak_RestFramework.handleRequest(CustomerRestRouter.class); |
| 14 | + } |
| 15 | + @HttpDelete |
| 16 | + global static void doDelete() { |
| 17 | + libak_RestFramework.handleRequest(CustomerRestRouter.class); |
| 18 | + } |
| 19 | + |
| 20 | + public class CustomerRestRouter extends libak_RestRouter { |
| 21 | + override public libak_RestRouter setRoutes() { |
| 22 | + this.routeToRestProcessorType = new Map<String, Type>{ |
| 23 | + '/v1/customers' => CustomersProcessorV1.class, |
| 24 | + '/v1/customers/:customer_sf_id' => CustomerProcessorV1.class |
| 25 | + }; |
| 26 | + return this; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public class CustomersProcessorV1 extends libak_RestProcessor { |
| 31 | + protected override libak_RestFramework.IRestResponse handleGet() { |
| 32 | + List<Account> accounts = [ |
| 33 | + SELECT Id, Name, Phone, BillingStreet, BillingCity, BillingState, BillingPostalCode |
| 34 | + FROM Account |
| 35 | + LIMIT 100 |
| 36 | + ]; |
| 37 | + |
| 38 | + if (accounts.isEmpty()) { |
| 39 | + return new libak_ErrorResponse(404, 'Accounts are not found', ''); |
| 40 | + } else { |
| 41 | + return new libak_JsonResponse(accounts); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + protected override libak_RestFramework.IRestResponse handlePost() { |
| 46 | + Account newAccount = (Account)JSON.deserialize(this.request.requestBody.toString(), Account.class); |
| 47 | + insert newAccount; |
| 48 | + |
| 49 | + return new libak_JsonResponse(newAccount); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public class CustomerProcessorV1 extends libak_RestProcessor { |
| 54 | + protected override libak_RestFramework.IRestResponse handleGet() { |
| 55 | + List<Account> accounts = [ |
| 56 | + SELECT Id, Name, Phone, BillingStreet, BillingCity, BillingState, BillingPostalCode |
| 57 | + FROM Account |
| 58 | + WHERE Id = :this.getUriParam('customer_sf_id') |
| 59 | + ]; |
| 60 | + |
| 61 | + if (accounts.isEmpty()) { |
| 62 | + return new libak_ErrorResponse(404, 'Accounts are not found', ''); |
| 63 | + } else { |
| 64 | + return new libak_JsonResponse(accounts.get(0)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + protected override libak_RestFramework.IRestResponse handlePut() { |
| 69 | + String accountId = this.getUriParam('customer_sf_id'); |
| 70 | + List<Account> existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId]; |
| 71 | + |
| 72 | + if (existingAccounts.isEmpty()) { |
| 73 | + return new libak_ErrorResponse(404, 'Account are not found', ''); |
| 74 | + } |
| 75 | + |
| 76 | + Account updatedAccount = (Account)JSON.deserialize(this.request.requestBody.toString(), Account.class); |
| 77 | + updatedAccount.Id = accountId; |
| 78 | + update updatedAccount; |
| 79 | + return new libak_JsonResponse(updatedAccount); |
| 80 | + } |
| 81 | + |
| 82 | + protected override libak_RestFramework.IRestResponse handleDelete() { |
| 83 | + String accountId = this.getUriParam('customer_sf_id'); |
| 84 | + List<Account> existingAccounts = [SELECT Id FROM Account WHERE Id = :accountId]; |
| 85 | + |
| 86 | + if (existingAccounts.isEmpty()) { |
| 87 | + return new libak_ErrorResponse(404, 'Account are not found', ''); |
| 88 | + } |
| 89 | + |
| 90 | + delete existingAccounts.get(0); |
| 91 | + return new libak_SuccessResponse('Account deleted successfully'); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments