[Cuis-dev] success with WebClient package
Mariano Montone
marianomontone at gmail.com
Mon Jun 17 18:10:01 PDT 2024
You also have #caseOf: method for stuff like this.
handleDog: aRequest
"handle an HTTP request based on its method"
| id parts readId |
readId := [(aRequest url prefixAndSuffix: $/) last asNumber].
aRequest method caseOf: {
['GET'] -> [ | accept |
accept := aRequest headerAt: 'Accept'.
(accept includesSubString: 'application/json') ifTrue: [
^self getDogsAsJson: aRequest].
(accept includesSubString: 'text/html') ifTrue: [
^self getDogsAsHtml: aRequest]].
['POST'] -> [self createDog: aRequest].
['DELETE'] -> [self deleteDog: aRequest id: readId value].
['PUT'] -> [self updateDog: aRequest id: readId value]
}
El 17/6/24 a las 19:21, Mariano Montone escribió:
> 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
>> ]
More information about the Cuis-dev
mailing list