summaryrefslogtreecommitdiff
path: root/lambda-calcul/haskell/src/Minilang/API.hs
blob: 0ae078469cb26697a1ad0dad3a75a9d07fff71c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}

{-# HLINT ignore "Use newtype instead of data" #-}

module Minilang.API where

import Data.Aeson (FromJSON, ToJSON)
import Data.Bifunctor (first)
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as LBS
import Data.Text (Text)
import Data.Time (UTCTime)
import GHC.Generics (Generic)
import Servant (Get, JSON, Raw, ReqBody, StdMethod (POST), Verb, (:<|>), (:>))
import System.Random (StdGen, uniformList)

data Score = Score
  { name :: String,
    grade :: Int,
    lastTest :: UTCTime
  }
  deriving (Eq, Show, Generic, ToJSON, FromJSON)

newtype User = User {username :: Text}
  deriving newtype (Eq, Show, ToJSON, FromJSON)

data Token = Token {token :: Text}
  deriving (Eq, Show, Generic, ToJSON, FromJSON)

newToken :: StdGen -> (ByteString, StdGen)
newToken = first LBS.pack . uniformList 16

type LambdaAPI =
  "api" :> "register" :> ReqBody '[JSON] User :> Verb 'POST 201 '[JSON] Token
    :<|> "api" :> "leaderboard" :> Get '[JSON] [Score]
    :<|> Raw