[Cuis-dev] success with WebClient package

Mariano Montone marianomontone at gmail.com
Mon Jun 17 15:21:07 PDT 2024


I would reformat it like this:

handleDog: aRequest

     "handle an HTTP request based on its method"

     | id parts |

     aRequest method = 'GET' ifTrue: [ | accept |
         accept := aRequest headerAt: 'Accept'.
         (accept includesSubString: 'application/json') ifTrue: [
             ^self getDogsAsJson: aRequest].

         (accept includesSubString: 'text/html') ifTrue: [
             ^self getDogsAsHtml: aRequest]].

     aRequest method = 'POST' ifTrue: [
         ^self createDog: aRequest].

     "Get the path parameter value."
     "TODO: Is this really the best way to do it?"
     parts := aRequest url prefixAndSuffix: $/.
     id := parts last asNumber.

     aRequest method = 'DELETE' ifTrue: [
         ^self deleteDog: aRequest id: id].

     aRequest method = 'PUT' ifTrue: [
         ^self updateDog: aRequest id: id]

In my opinion, Smalltalk is not very good for this kind of multiple 
conditions stuff. I know it can be refactored to involve and dispatch 
using several objects, but sometimes (like in this case) a multiple 
conditional solution is fine. In some parts I think my code would 
benefit from having a Lisp/Scheme COND type of language construct. 
Actually, it is easy to implement, but I don't use it because it is not 
part of Cuis Core, the multiple returns works for most cases.

      Mariano

El 17/6/24 a las 18:20, Mark Volkmann via Cuis-dev escribió:
> handleDog: aRequest
>     "handle an HTTP request based on its method"
> | id parts |
>
> aRequest method = 'GET' ifTrue: [
> | accept |
>
> accept := aRequest headerAt: 'Accept'.
> (accept includesSubString: 'application/json') ifTrue: [
> self getDogsAsJson: aRequest.
> ^nil
> ].
>
> (acceptincludesSubString: 'text/html') ifTrue: [
> self getDogsAsHtml: aRequest.
> ^nil
> ]
> ].
>
> aRequest method = 'POST' ifTrue: [
> self createDog: aRequest.
> ^nil
> ].
>
> "Get the path parameter value."
> "TODO: Is this really the best way to do it?"
> parts := aRequest url prefixAndSuffix: $/.
> id := parts last asNumber.
>
> aRequest method = 'DELETE' ifTrue: [
> self deleteDog: aRequest id: id.
> ^nil
> ].
>
> aRequest method = 'PUT' ifTrue: [
> self updateDog: aRequest id: id.
> ^nil
> ]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240617/9a82bb40/attachment.htm>


More information about the Cuis-dev mailing list