Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add object api #782

Draft
wants to merge 3 commits into
base: feat/add-object-bookmarks
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ tags:
description: |
Documentation de l'API pour le domaine alimentaire au format OpenAPI

**⚠️ En construction, les résultats fournis sont incomplets et probablement invalides.**
- name: Objet
description: |
Documentation de l'API pour le domaine objet au format OpenAPI

**⚠️ En construction, les résultats fournis sont incomplets et probablement invalides.**
externalDocs:
description: Γ€ propos
Expand Down Expand Up @@ -449,6 +454,27 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/InvalidParametersError"

/object/simulator:
get:
tags:
- Objet
summary: Calcul des impacts environnementaux d'un objet
parameters:
- $ref: "#/components/parameters/itemsParam"
responses:
200:
description: OpΓ©ration rΓ©ussie
content:
application/json:
schema:
$ref: "#/components/schemas/GenericImpactsResponse"
400:
description: Paramètres invalides
content:
application/json:
schema:
$ref: "#/components/schemas/InvalidParametersError"
components:
securitySchemes:
token:
Expand Down Expand Up @@ -1112,6 +1138,33 @@ components:
type: number
minimum: 0.67
maximum: 1.45

itemsParam:
name: "items[]"
in: query
description: |
Liste des procΓ©dΓ©s composant le produit.
Le format de chaque entrΓ©e est composΓ© de la quantitΓ© du procΓ©dΓ© et de son identifiant.

- `0.00088;07e9e916-e02b-45e2-a298-2b5084de6242` signifie *0.00088mΒ³ de planche de bois*;
- `1.645313;3295b2a5-328a-4c00-b046-e2ddeb0da823` signifie *1.645313kg de composant en plastique*;

> Par exemple, les paramètres à passer pour une chaise avec 0.00088m3 de planche de bois et 1.645313kg de composant plastique sont :
>
> ```js
> items[]=0.00088;07e9e916-e02b-45e2-a298-2b5084de6242&items[]=1.645313;3295b2a5-328a-4c00-b046-e2ddeb0da823
> ```
required: true
style: form
schema:
type: array
items:
type: string
examples:
table:
value: ["0.002;07e9e916-e02b-45e2-a298-2b5084de6242"]
chaise:
value: ["0.00088;07e9e916-e02b-45e2-a298-2b5084de6242","1.645313;3295b2a5-328a-4c00-b046-e2ddeb0da823"]
schemas:
Impacts:
type: object
Expand Down
30 changes: 30 additions & 0 deletions src/Data/Bookmark.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ module Data.Bookmark exposing
, decode
, encode
, findByFoodQuery
, findByObjectQuery
, findByTextileQuery
, isFood
, isObject
, isTextile
, sort
, toId
Expand All @@ -14,6 +16,7 @@ module Data.Bookmark exposing

import Data.Food.Query as FoodQuery
import Data.Food.Recipe as Recipe
import Data.Object.Query as ObjectQuery
import Data.Scope as Scope exposing (Scope)
import Data.Textile.Inputs as Inputs
import Data.Textile.Query as TextileQuery
Expand All @@ -32,6 +35,7 @@ type alias Bookmark =

type Query
= Food FoodQuery.Query
| Object ObjectQuery.Query
| Textile TextileQuery.Query


Expand All @@ -47,6 +51,7 @@ decodeQuery : Decoder Query
decodeQuery =
Decode.oneOf
[ Decode.map Food FoodQuery.decode
, Decode.map Object ObjectQuery.decode
, Decode.map Textile TextileQuery.decode
]

Expand All @@ -66,6 +71,9 @@ encodeQuery v =
Food query ->
FoodQuery.encode query

Object query ->
ObjectQuery.encode query

Textile query ->
TextileQuery.encode query

Expand All @@ -80,6 +88,16 @@ isFood { query } =
False


isObject : Bookmark -> Bool
isObject { query } =
case query of
Object _ ->
True

_ ->
False


isTextile : Bookmark -> Bool
isTextile { query } =
case query of
Expand All @@ -101,6 +119,11 @@ findByFoodQuery foodQuery =
findByQuery (Food foodQuery)


findByObjectQuery : ObjectQuery.Query -> List Bookmark -> Maybe Bookmark
findByObjectQuery objectQuery =
findByQuery (Object objectQuery)


findByTextileQuery : TextileQuery.Query -> List Bookmark -> Maybe Bookmark
findByTextileQuery textileQuery =
findByQuery (Textile textileQuery)
Expand All @@ -112,6 +135,9 @@ scope bookmark =
Food _ ->
Scope.Food

Object _ ->
Scope.Object

Textile _ ->
Scope.Textile

Expand All @@ -135,6 +161,10 @@ toQueryDescription db bookmark =
|> Result.map Recipe.toString
|> Result.withDefault bookmark.name

Object objectQuery ->
objectQuery
|> ObjectQuery.toString

Textile textileQuery ->
textileQuery
|> Inputs.fromQuery db
Expand Down
8 changes: 8 additions & 0 deletions src/Data/Object/Process.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Data.Object.Process exposing
, encode
, encodeId
, findById
, idFromString
, idToString
)

import Data.Impact as Impact exposing (Impacts)
Expand Down Expand Up @@ -84,3 +86,9 @@ findById processes id =
idToString : Id -> String
idToString (Id uuid) =
Uuid.toString uuid


idFromString : String -> Maybe Id
idFromString string =
Uuid.fromString string
|> Maybe.map Id
31 changes: 31 additions & 0 deletions src/Data/Object/Query.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ module Data.Object.Query exposing
, amount
, amountToFloat
, b64encode
, buildApiQuery
, decode
, default
, defaultItem
, encode
, parseBase64Query
, removeItem
, toString
, updateItem
)

Expand Down Expand Up @@ -45,6 +48,17 @@ amountToFloat (Amount float) =
float


buildApiQuery : String -> Query -> String
buildApiQuery clientUrl query =
"""curl -sS -X POST %apiUrl% \\
-H "accept: application/json" \\
-H "content-type: application/json" \\
-d '%json%'
"""
|> String.replace "%apiUrl%" (clientUrl ++ "api/object/simulator")
|> String.replace "%json%" (encode query |> Encode.encode 0)


decode : Decoder Query
decode =
Decode.map Query
Expand Down Expand Up @@ -106,6 +120,23 @@ updateItem newItem query =
}


toString : Query -> String
toString query =
query.items
|> List.map
(\i ->
(i.amount
|> amountToFloat
|> String.fromFloat
)
++ " "
++ (i.processId
|> Process.idToString
)
)
|> String.join " - "



-- Parser

Expand Down
10 changes: 5 additions & 5 deletions src/Data/Object/Simulator.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ module Data.Object.Simulator exposing
)

import Data.Impact as Impact exposing (Impacts)
import Data.Object.Db exposing (Db)
import Data.Object.Process as Process exposing (Process)
import Data.Object.Query as Query exposing (Item, Query)
import Quantity
import Result.Extra as RE
import Static.Db exposing (Db)


availableProcesses : Db -> Query -> List Process
availableProcesses db query =
availableProcesses { object } query =
let
usedIds =
List.map .processId query.items
in
db.processes
object.processes
|> List.filter (\{ id } -> not (List.member id usedIds))


Expand All @@ -31,9 +31,9 @@ compute db query =


computeItemImpacts : Db -> Item -> Result String Impacts
computeItemImpacts db { amount, processId } =
computeItemImpacts { object } { amount, processId } =
processId
|> Process.findById db.processes
|> Process.findById object.processes
|> Result.map
(.impacts
>> Impact.mapImpacts (\_ -> Quantity.multiplyBy (Query.amountToFloat amount))
Expand Down
Loading