Add part4
This commit is contained in:
23
part4/src/Data/Article/Author.elm
Normal file
23
part4/src/Data/Article/Author.elm
Normal file
@@ -0,0 +1,23 @@
|
||||
module Data.Article.Author exposing (Author, decoder)
|
||||
|
||||
import Data.User as User exposing (Username)
|
||||
import Data.UserPhoto as UserPhoto exposing (UserPhoto)
|
||||
import Json.Decode as Decode exposing (Decoder)
|
||||
import Json.Decode.Pipeline exposing (custom, decode, required)
|
||||
|
||||
|
||||
decoder : Decoder Author
|
||||
decoder =
|
||||
decode Author
|
||||
|> required "username" User.usernameDecoder
|
||||
|> required "bio" (Decode.nullable Decode.string)
|
||||
|> required "image" UserPhoto.decoder
|
||||
|> required "following" Decode.bool
|
||||
|
||||
|
||||
type alias Author =
|
||||
{ username : Username
|
||||
, bio : Maybe String
|
||||
, image : UserPhoto
|
||||
, following : Bool
|
||||
}
|
||||
48
part4/src/Data/Article/Comment.elm
Normal file
48
part4/src/Data/Article/Comment.elm
Normal file
@@ -0,0 +1,48 @@
|
||||
module Data.Article.Comment exposing (Comment, CommentId, commentIdDecoder, decoder, idToString)
|
||||
|
||||
import Data.Article.Author as Author exposing (Author)
|
||||
import Date exposing (Date)
|
||||
import Json.Decode as Decode exposing (Decoder)
|
||||
import Json.Decode.Extra
|
||||
import Json.Decode.Pipeline exposing (custom, decode, required)
|
||||
|
||||
|
||||
type alias Comment =
|
||||
{ id : CommentId
|
||||
, body : String
|
||||
, createdAt : Date
|
||||
, updatedAt : Date
|
||||
, author : Author
|
||||
}
|
||||
|
||||
|
||||
|
||||
-- SERIALIZATION --
|
||||
|
||||
|
||||
decoder : Decoder Comment
|
||||
decoder =
|
||||
decode Comment
|
||||
|> required "id" commentIdDecoder
|
||||
|> required "body" Decode.string
|
||||
|> required "createdAt" Json.Decode.Extra.date
|
||||
|> required "updatedAt" Json.Decode.Extra.date
|
||||
|> required "author" Author.decoder
|
||||
|
||||
|
||||
|
||||
-- IDENTIFIERS --
|
||||
|
||||
|
||||
type CommentId
|
||||
= CommentId Int
|
||||
|
||||
|
||||
idToString : CommentId -> String
|
||||
idToString (CommentId id) =
|
||||
toString id
|
||||
|
||||
|
||||
commentIdDecoder : Decoder CommentId
|
||||
commentIdDecoder =
|
||||
Decode.map CommentId Decode.int
|
||||
22
part4/src/Data/Article/Feed.elm
Normal file
22
part4/src/Data/Article/Feed.elm
Normal file
@@ -0,0 +1,22 @@
|
||||
module Data.Article.Feed exposing (Feed, decoder)
|
||||
|
||||
import Data.Article as Article exposing (Article)
|
||||
import Json.Decode as Decode exposing (Decoder)
|
||||
import Json.Decode.Pipeline exposing (decode, required)
|
||||
|
||||
|
||||
type alias Feed =
|
||||
{ articles : List (Article ())
|
||||
, articlesCount : Int
|
||||
}
|
||||
|
||||
|
||||
|
||||
-- SERIALIZATION --
|
||||
|
||||
|
||||
decoder : Decoder Feed
|
||||
decoder =
|
||||
decode Feed
|
||||
|> required "articles" (Decode.list Article.decoder)
|
||||
|> required "articlesCount" Decode.int
|
||||
Reference in New Issue
Block a user