{-# LANGUAGE OverloadedLabels #-}

module WikiMusic.SSR.Servant.GenreRoutes
  ( genresRoute,
    genreRoute,
    genreCreateRoute,
  )
where

import Data.UUID (UUID)
import Free.AlaCarte
import Optics
import Relude
import Text.Blaze.Html as Html
import WikiMusic.Model.Other
import WikiMusic.SSR.Backend.Rest ()
import WikiMusic.SSR.Free.Backend
import WikiMusic.SSR.Free.View
import WikiMusic.SSR.Model.Api
import WikiMusic.SSR.Model.Env
import WikiMusic.SSR.Servant.Utilities
import WikiMusic.SSR.View.Html ()

genresRoute :: (MonadIO m) => Env -> Maybe Text -> Maybe Text -> Maybe Int -> Maybe Int -> m Html
genresRoute :: forall (m :: * -> *).
MonadIO m =>
Env -> Maybe Text -> Maybe Text -> Maybe Int -> Maybe Int -> m Html
genresRoute Env
env Maybe Text
cookie Maybe Text
givenSortOrder Maybe Int
limit Maybe Int
offset = do
  Either Text GetGenresQueryResponse
maybeGenres <-
    IO (Either Text GetGenresQueryResponse)
-> m (Either Text GetGenresQueryResponse)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
      (IO (Either Text GetGenresQueryResponse)
 -> m (Either Text GetGenresQueryResponse))
-> IO (Either Text GetGenresQueryResponse)
-> m (Either Text GetGenresQueryResponse)
forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Exec f => Free f a -> IO a
exec @Backend
        ( Env
-> AuthToken
-> Limit
-> Offset
-> SortOrder
-> Include
-> Free Backend (Either Text GetGenresQueryResponse)
forall (f :: * -> *).
(Backend :<: f) =>
Env
-> AuthToken
-> Limit
-> Offset
-> SortOrder
-> Include
-> Free f (Either Text GetGenresQueryResponse)
getGenres
            Env
env
            (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars AuthToken -> AuthToken
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars AuthToken
#authToken)
            (Limit -> (Int -> Limit) -> Maybe Int -> Limit
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Int -> Limit
Limit Int
50) Int -> Limit
Limit Maybe Int
limit)
            (Offset -> (Int -> Offset) -> Maybe Int -> Offset
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Int -> Offset
Offset Int
0) Int -> Offset
Offset Maybe Int
offset)
            SortOrder
sortOrder
            (Include {$sel:value:Include :: Text
value = Text
"artworks,comments,opinions"})
        )
  Env
-> UiMode
-> Language
-> Palette
-> Either Text GetGenresQueryResponse
-> (GetGenresQueryResponse -> IO Html)
-> m Html
forall (m :: * -> *) t.
MonadIO m =>
Env
-> UiMode
-> Language
-> Palette
-> Either Text t
-> (t -> IO Html)
-> m Html
eitherView
    Env
env
    (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars UiMode -> UiMode
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars UiMode
#uiMode)
    (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Language -> Language
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Language
#locale)
    (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Palette -> Palette
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Palette
#palette)
    Either Text GetGenresQueryResponse
maybeGenres
    (forall (f :: * -> *) a. Exec f => Free f a -> IO a
exec @View (Free View Html -> IO Html)
-> (GetGenresQueryResponse -> Free View Html)
-> GetGenresQueryResponse
-> IO Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Env
-> UiMode
-> Language
-> Palette
-> SortOrder
-> GetGenresQueryResponse
-> Free View Html
forall (f :: * -> *).
(View :<: f) =>
Env
-> UiMode
-> Language
-> Palette
-> SortOrder
-> GetGenresQueryResponse
-> Free f Html
genreListPage Env
env (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars UiMode -> UiMode
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars UiMode
#uiMode) (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Language -> Language
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Language
#locale) (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Palette -> Palette
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Palette
#palette) SortOrder
sortOrder)
  where
    viewVars :: ViewVars
viewVars = Maybe Text -> ViewVars
viewVarsFromCookies Maybe Text
cookie
    sortOrder :: SortOrder
sortOrder = SortOrder -> (Text -> SortOrder) -> Maybe Text -> SortOrder
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars SortOrder -> SortOrder
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars SortOrder
#genreSorting) Text -> SortOrder
SortOrder Maybe Text
givenSortOrder

genreRoute :: (MonadIO m) => Env -> Maybe Text -> UUID -> m Html
genreRoute :: forall (m :: * -> *).
MonadIO m =>
Env -> Maybe Text -> UUID -> m Html
genreRoute Env
env Maybe Text
cookie UUID
identifier = do
  Either Text GetGenresQueryResponse
maybeGenres <-
    IO (Either Text GetGenresQueryResponse)
-> m (Either Text GetGenresQueryResponse)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
      (IO (Either Text GetGenresQueryResponse)
 -> m (Either Text GetGenresQueryResponse))
-> IO (Either Text GetGenresQueryResponse)
-> m (Either Text GetGenresQueryResponse)
forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Exec f => Free f a -> IO a
exec @Backend
        ( Env
-> AuthToken
-> UUID
-> Include
-> Free Backend (Either Text GetGenresQueryResponse)
forall (f :: * -> *).
(Backend :<: f) =>
Env
-> AuthToken
-> UUID
-> Include
-> Free f (Either Text GetGenresQueryResponse)
getGenre
            Env
env
            (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars AuthToken -> AuthToken
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars AuthToken
#authToken)
            UUID
identifier
            (Include {$sel:value:Include :: Text
value = Text
"artworks,comments,opinions"})
        )
  Env
-> UiMode
-> Language
-> Palette
-> Either Text GetGenresQueryResponse
-> (GetGenresQueryResponse -> IO Html)
-> m Html
forall (m :: * -> *) t.
MonadIO m =>
Env
-> UiMode
-> Language
-> Palette
-> Either Text t
-> (t -> IO Html)
-> m Html
eitherView
    Env
env
    (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars UiMode -> UiMode
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars UiMode
#uiMode)
    (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Language -> Language
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Language
#locale)
    (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Palette -> Palette
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Palette
#palette)
    Either Text GetGenresQueryResponse
maybeGenres
    (forall (f :: * -> *) a. Exec f => Free f a -> IO a
exec @View (Free View Html -> IO Html)
-> (GetGenresQueryResponse -> Free View Html)
-> GetGenresQueryResponse
-> IO Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Env
-> UiMode
-> Language
-> Palette
-> GetGenresQueryResponse
-> Free View Html
forall (f :: * -> *).
(View :<: f) =>
Env
-> UiMode
-> Language
-> Palette
-> GetGenresQueryResponse
-> Free f Html
genreDetailPage Env
env (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars UiMode -> UiMode
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars UiMode
#uiMode) (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Language -> Language
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Language
#locale) (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Palette -> Palette
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Palette
#palette))
  where
    viewVars :: ViewVars
viewVars = Maybe Text -> ViewVars
viewVarsFromCookies Maybe Text
cookie

genreCreateRoute :: (MonadIO m) => Env -> Maybe Text -> m Html
genreCreateRoute :: forall (m :: * -> *). MonadIO m => Env -> Maybe Text -> m Html
genreCreateRoute Env
env Maybe Text
cookie = do
  IO Html -> m Html
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Html -> m Html) -> IO Html -> m Html
forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Exec f => Free f a -> IO a
exec @View (Env -> UiMode -> Language -> Palette -> Free View Html
forall (f :: * -> *).
(View :<: f) =>
Env -> UiMode -> Language -> Palette -> Free f Html
genreCreatePage Env
env (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars UiMode -> UiMode
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars UiMode
#uiMode) (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Language -> Language
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Language
#locale) (ViewVars
viewVars ViewVars -> Optic' A_Lens NoIx ViewVars Palette -> Palette
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx ViewVars Palette
#palette))
  where
    viewVars :: ViewVars
viewVars = Maybe Text -> ViewVars
viewVarsFromCookies Maybe Text
cookie