/
Routes.elm
1134 lines (807 loc) · 27.4 KB
/
Routes.elm
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Routes exposing (all)
import Auth exposing (Auth)
import Auth.Password as Password
import ConcurrentTask as Task exposing (ConcurrentTask)
import Domain.Articles as Articles
import Domain.Comments as Comments
import Domain.Users as Users
import Error exposing (Error)
import Iso8601
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import Lib.Database.Postgres as Pg
import Lib.Http.Server as Server
import Lib.Http.Server.Header as Header exposing (Headers)
import Lib.Http.Server.Param as Param
import Lib.Http.Server.Query as Query
import Lib.Http.Server.Response as Response
import Time
import Utils.Decode as Decode
import Utils.Encode as Encode
import Utils.Task as Task
-- All
all : Server.Routes
all =
Server.path "/api"
[ Server.post "/users/login" login
, Server.post "/users" register
, Server.get "/user" getCurrentUser
, Server.put "/user" updateUser
, Server.get "/profiles/{username}" getProfile
, Server.post "/profiles/{username}/follow" followUser
, Server.delete "/profiles/{username}/follow" unfollowUser
, Server.get "/articles" listArticles
, Server.get "/articles/feed" feedArticles
, Server.get "/articles/{slug}" getArticle
, Server.post "/articles" createArticle
, Server.put "/articles/{slug}" updateArticle
, Server.delete "/articles/{slug}" deleteArticle
, Server.post "/articles/{slug}/comments" addComment
, Server.get "/articles/{slug}/comments" getComments
, Server.delete "/articles/{slug}/comments/{id}" deleteComment
, Server.post "/articles/{slug}/favorite" favoriteArticle
, Server.delete "/articles/{slug}/favorite" unfavoriteArticle
, Server.get "/tags" getTags
]
-- Login
type alias LoginRequest =
{ body : Login
}
type alias Login =
{ email : String
, password : String
}
type alias LoginResponse =
{ user : User
}
login : Server.Endpoint LoginRequest Error LoginResponse
login =
Server.request LoginRequest
|> Server.withJsonBody
(Decode.field "user"
(Decode.succeed Login
|> Decode.required "email" Decode.nonEmpty
|> Decode.required "password" Decode.nonEmpty
)
)
|> Server.endpoint
{ handler = loginHandler
, respond = respondWith 200 encodeUser
}
loginHandler : LoginRequest -> ConcurrentTask Error LoginResponse
loginHandler req =
Users.findByEmail req.body.email
|> Task.failOnNothing Error.login
|> Task.andThen (Password.verify req.body.password)
|> Task.andThenWith userResponse Auth.generateToken
-- Register
type alias RegisterRequest =
{ body : Registration
}
type alias Registration =
{ username : String
, email : String
, password : String
}
type alias RegisterResponse =
{ user : User
}
register : Server.Endpoint RegisterRequest Error RegisterResponse
register =
Server.request RegisterRequest
|> Server.withJsonBody
(Decode.field "user"
(Decode.map3 Registration
(Decode.field "username" Decode.nonEmpty)
(Decode.field "email" Decode.nonEmpty)
(Decode.field "password" Decode.nonEmpty)
)
)
|> Server.endpoint
{ handler = registerHandler
, respond = respondWith 201 encodeUser
}
registerHandler : RegisterRequest -> ConcurrentTask Error RegisterResponse
registerHandler request =
Password.create request.body.password
|> Task.andThen
(\hashed ->
Users.create
{ email = request.body.email
, username = request.body.username
, password = hashed
}
)
|> Task.andThenWith userResponse Auth.generateToken
-- Get Current User
type alias GetCurrentUserRequest =
{ auth : Auth
}
type alias GetUserCurrentResponse =
{ user : User
}
getCurrentUser : Server.Endpoint GetCurrentUserRequest Error GetUserCurrentResponse
getCurrentUser =
Server.request GetCurrentUserRequest
|> Server.withHeaders requiredAuth
|> Server.endpoint
{ handler = getCurrentUserHandler
, respond = respondWith 200 encodeUser
}
getCurrentUserHandler : GetCurrentUserRequest -> ConcurrentTask Error GetUserCurrentResponse
getCurrentUserHandler req =
Users.findById req.auth.userId
|> serverErrorOnNothing "User not found"
|> Task.map (userResponse req.auth.token)
-- Update User
type alias UpdateUserRequest =
{ auth : Auth
, body : UpdateUserBody
}
type alias UpdateUserBody =
{ email : Maybe String
, username : Maybe String
, password : Maybe String
, image : Maybe String
, bio : Maybe String
}
type alias UpdateUserResponse =
{ user : User
}
updateUser : Server.Endpoint UpdateUserRequest Error UpdateUserResponse
updateUser =
Server.request UpdateUserRequest
|> Server.withHeaders requiredAuth
|> Server.withJsonBody
(Decode.field "user"
(Decode.map5 UpdateUserBody
(Decode.maybe (Decode.field "email" Decode.nonEmpty))
(Decode.maybe (Decode.field "username" Decode.nonEmpty))
(Decode.maybe (Decode.field "password" Decode.nonEmpty))
(Decode.maybe (Decode.field "image" Decode.nonEmpty))
(Decode.maybe (Decode.field "bio" Decode.nonEmpty))
)
)
|> Server.endpoint
{ handler = updateUserHandler
, respond = respondWith 200 encodeUser
}
updateUserHandler : UpdateUserRequest -> ConcurrentTask Error UpdateUserResponse
updateUserHandler req =
Users.update
{ auth = req.auth
, email = req.body.email
, username = req.body.username
, password = req.body.password
, image = req.body.image
, bio = req.body.bio
}
|> Task.map (userResponse req.auth.token)
-- Get Profile
type alias GetProfileRequest =
{ auth : Maybe Auth
, username : String
}
type alias GetProfileResponse =
{ profile : Profile
}
getProfile : Server.Endpoint GetProfileRequest Error GetProfileResponse
getProfile =
Server.request GetProfileRequest
|> Server.withHeaders optionalAuth
|> Server.withParams (Param.string "username")
|> Server.endpoint
{ handler = getProfileHandler
, respond = respondWith 200 encodeProfile
}
getProfileHandler : GetProfileRequest -> ConcurrentTask Error GetProfileResponse
getProfileHandler req =
Users.getProfile { auth = req.auth, username = req.username }
|> notFoundOnNothing "profile"
|> Task.map profileResponse
-- Follow User
type alias FollowUserRequest =
{ auth : Auth
, username : String
}
type alias FollowUserResponse =
{ profile : Profile
}
followUser : Server.Endpoint FollowUserRequest Error FollowUserResponse
followUser =
Server.request FollowUserRequest
|> Server.withHeaders requiredAuth
|> Server.withParams (Param.string "username")
|> Server.endpoint
{ handler = followUserHandler
, respond = respondWith 200 encodeProfile
}
followUserHandler : FollowUserRequest -> ConcurrentTask Error FollowUserResponse
followUserHandler req =
Users.follow { auth = req.auth, username = req.username }
|> Task.andThenDo (Users.getProfile { auth = Just req.auth, username = req.username })
|> serverErrorOnNothing "Missing Profile"
|> Task.map profileResponse
-- Unfollow User
type alias UnfollowUserRequest =
{ auth : Auth
, username : String
}
type alias UnfollowUserResponse =
{ profile : Profile
}
unfollowUser : Server.Endpoint UnfollowUserRequest Error UnfollowUserResponse
unfollowUser =
Server.request UnfollowUserRequest
|> Server.withHeaders requiredAuth
|> Server.withParams (Param.string "username")
|> Server.endpoint
{ handler = unfollowUserHandler
, respond = respondWith 200 encodeProfile
}
unfollowUserHandler : UnfollowUserRequest -> ConcurrentTask Error UnfollowUserResponse
unfollowUserHandler req =
Users.unfollow { auth = req.auth, username = req.username }
|> Task.andThenDo (Users.getProfile { auth = Just req.auth, username = req.username })
|> serverErrorOnNothing "Missing Profile"
|> Task.map profileResponse
-- List Articles
type alias ListArticlesRequest =
{ auth : Maybe Auth
, query : ListArticlesQuery
}
type alias ListArticlesQuery =
{ author : Maybe String
, favorited : Maybe String
, tag : Maybe String
, limit : Maybe Int
, offset : Maybe Int
}
type alias ListArticlesResponse =
{ articles : List Article
}
listArticles : Server.Endpoint ListArticlesRequest Error ListArticlesResponse
listArticles =
Server.request ListArticlesRequest
|> Server.withHeaders optionalAuth
|> Server.withQuery
(Query.succeed ListArticlesQuery
|> Query.with (Query.string "author")
|> Query.with (Query.string "favorited")
|> Query.with (Query.string "tag")
|> Query.with (Query.int "limit")
|> Query.with (Query.int "offset")
)
|> Server.endpoint
{ handler = listArticlesHandler
, respond = respondWith 200 encodeMultipleArticles
}
listArticlesHandler : ListArticlesRequest -> ConcurrentTask Error ListArticlesResponse
listArticlesHandler req =
Articles.list
{ auth = req.auth
, author = req.query.author
, favorited = req.query.favorited
, tag = req.query.tag
, limit = req.query.limit
, offset = req.query.offset
}
|> Task.map articlesResponse
-- Feed Articles
type alias FeedArticlesRequest =
{ auth : Auth
, query : FeedArticlesQuery
}
type alias FeedArticlesQuery =
{ limit : Maybe Int
, offset : Maybe Int
}
type alias FeedArticlesResponse =
{ articles : List Article
}
feedArticles : Server.Endpoint FeedArticlesRequest Error FeedArticlesResponse
feedArticles =
Server.request FeedArticlesRequest
|> Server.withHeaders requiredAuth
|> Server.withQuery
(Query.succeed FeedArticlesQuery
|> Query.with (Query.int "limit")
|> Query.with (Query.int "offset")
)
|> Server.endpoint
{ handler = feedArticlesHandler
, respond = respondWith 200 encodeMultipleArticles
}
feedArticlesHandler : FeedArticlesRequest -> ConcurrentTask Error FeedArticlesResponse
feedArticlesHandler req =
Articles.feed
{ auth = req.auth
, limit = req.query.limit
, offset = req.query.offset
}
|> Task.map articlesResponse
-- Get Article
type alias GetArticleRequest =
{ auth : Maybe Auth
, slug : String
}
type alias GetArticleResponse =
{ article : Article
}
getArticle : Server.Endpoint GetArticleRequest Error GetArticleResponse
getArticle =
Server.request GetArticleRequest
|> Server.withHeaders optionalAuth
|> Server.withParams (Param.string "slug")
|> Server.endpoint
{ handler = getArticleHandler
, respond = respondWith 200 encodeSingleArticle
}
getArticleHandler : GetArticleRequest -> ConcurrentTask Error GetArticleResponse
getArticleHandler req =
Articles.get { auth = req.auth, slug = req.slug }
|> notFoundOnNothing "article"
|> Task.map articleResponse
-- Create Article
type alias CreateArticleRequest =
{ auth : Auth
, body : CreateArticleBody
}
type alias CreateArticleBody =
{ title : String
, description : String
, body : String
, tagList : List String
}
type alias CreateArticleResponse =
{ article : Article
}
createArticle : Server.Endpoint CreateArticleRequest Error CreateArticleResponse
createArticle =
Server.request CreateArticleRequest
|> Server.withHeaders requiredAuth
|> Server.withJsonBody
(Decode.field "article"
(Decode.succeed CreateArticleBody
|> Decode.required "title" Decode.nonEmpty
|> Decode.required "description" Decode.nonEmpty
|> Decode.required "body" Decode.nonEmpty
|> Decode.required "tagList" (Decode.list Decode.nonEmpty)
)
)
|> Server.endpoint
{ handler = createArticleHandler
, respond = respondWith 201 encodeSingleArticle
}
createArticleHandler : CreateArticleRequest -> ConcurrentTask Error CreateArticleResponse
createArticleHandler req =
Articles.create
{ auth = req.auth
, title = req.body.title
, description = req.body.description
, body = req.body.body
, tagList = req.body.tagList
}
|> Task.andThen (\created -> Articles.get { auth = Just req.auth, slug = created.slug })
|> serverErrorOnNothing "Missing article"
|> Task.map articleResponse
-- Update Article
type alias UpdateArticleRequest =
{ auth : Auth
, slug : String
, body : UpdateArticleBody
}
type alias UpdateArticleBody =
{ title : Maybe String
, description : Maybe String
, body : Maybe String
}
type alias UpdateArticleResponse =
{ article : Article
}
updateArticle : Server.Endpoint UpdateArticleRequest Error UpdateArticleResponse
updateArticle =
Server.request UpdateArticleRequest
|> Server.withHeaders requiredAuth
|> Server.withParams (Param.string "slug")
|> Server.withJsonBody
(Decode.field "article"
(Decode.map3 UpdateArticleBody
(Decode.maybe (Decode.field "title" Decode.nonEmpty))
(Decode.maybe (Decode.field "description" Decode.nonEmpty))
(Decode.maybe (Decode.field "body" Decode.nonEmpty))
)
)
|> Server.endpoint
{ handler = updateArticleHandler
, respond = respondWith 200 encodeSingleArticle
}
updateArticleHandler : UpdateArticleRequest -> ConcurrentTask Error UpdateArticleResponse
updateArticleHandler req =
Articles.update
{ auth = req.auth
, slug = req.slug
, title = req.body.title
, description = req.body.description
, body = req.body.body
}
|> Task.andThen (\updated -> Articles.get { auth = Just req.auth, slug = updated.slug })
|> serverErrorOnNothing "Missing article"
|> Task.map articleResponse
-- Delete Article
type alias DeleteArticleRequest =
{ auth : Auth
, slug : String
}
type alias DeleteArticleResponse =
()
deleteArticle : Server.Endpoint DeleteArticleRequest Error DeleteArticleResponse
deleteArticle =
Server.request DeleteArticleRequest
|> Server.withHeaders requiredAuth
|> Server.withParams (Param.string "slug")
|> Server.endpoint
{ handler = deleteArticleHandler
, respond = respondNoContent
}
deleteArticleHandler : DeleteArticleRequest -> ConcurrentTask Error DeleteArticleResponse
deleteArticleHandler req =
Articles.delete
{ auth = req.auth
, slug = req.slug
}
-- Add Comment
type alias AddCommentRequest =
{ auth : Auth
, body : AddCommentBody
, slug : String
}
type alias AddCommentBody =
{ body : String
}
type alias AddCommentResponse =
{ comment : Comment
}
addComment : Server.Endpoint AddCommentRequest Error AddCommentResponse
addComment =
Server.request AddCommentRequest
|> Server.withHeaders requiredAuth
|> Server.withJsonBody
(Decode.field "comment"
(Decode.succeed AddCommentBody
|> Decode.required "body" Decode.nonEmpty
)
)
|> Server.withParams (Param.string "slug")
|> Server.endpoint
{ handler = addCommentHandler
, respond = respondWith 201 encodeSingleComment
}
addCommentHandler : AddCommentRequest -> ConcurrentTask Error AddCommentResponse
addCommentHandler req =
Comments.add
{ auth = req.auth
, slug = req.slug
, body = req.body.body
}
|> Task.map commentResponse
-- Get Comments
type alias GetCommentsRequest =
{ auth : Maybe Auth
, slug : String
}
type alias GetCommentsResponse =
{ comments : List Comment
}
getComments : Server.Endpoint GetCommentsRequest Error GetCommentsResponse
getComments =
Server.request GetCommentsRequest
|> Server.withHeaders optionalAuth
|> Server.withParams (Param.string "slug")
|> Server.endpoint
{ handler = getCommentsHandler
, respond = respondWith 200 encodeMultipleComments
}
getCommentsHandler : GetCommentsRequest -> ConcurrentTask Error GetCommentsResponse
getCommentsHandler req =
Comments.forArticle
{ auth = req.auth
, slug = req.slug
}
|> Task.map commentsResponse
-- Delete Comment
type alias DeleteCommentRequest =
{ auth : Auth
, params : DeleteCommentParams
}
type alias DeleteCommentParams =
{ slug : String
, commentId : Int
}
type alias DeleteCommentResponse =
()
deleteComment : Server.Endpoint DeleteCommentRequest Error DeleteCommentResponse
deleteComment =
Server.request DeleteCommentRequest
|> Server.withHeaders requiredAuth
|> Server.withParams
(Param.succeed DeleteCommentParams
|> Param.with (Param.string "slug")
|> Param.with (Param.int "id")
)
|> Server.endpoint
{ handler = deleteCommentHandler
, respond = respondNoContent
}
deleteCommentHandler : DeleteCommentRequest -> ConcurrentTask Error DeleteCommentResponse
deleteCommentHandler req =
Comments.delete
{ auth = req.auth
, id = req.params.commentId
}
-- Favorite Article
type alias FavoriteArticleRequest =
{ auth : Auth
, slug : String
}
type alias FavoriteArticleResponse =
{ article : Article
}
favoriteArticle : Server.Endpoint FavoriteArticleRequest Error FavoriteArticleResponse
favoriteArticle =
Server.request FavoriteArticleRequest
|> Server.withHeaders requiredAuth
|> Server.withParams (Param.string "slug")
|> Server.endpoint
{ handler = favoriteArticleHandler
, respond = respondWith 200 encodeSingleArticle
}
favoriteArticleHandler : FavoriteArticleRequest -> ConcurrentTask Error FavoriteArticleResponse
favoriteArticleHandler req =
Articles.favorite
{ userId = req.auth.userId
, slug = req.slug
}
|> Task.andThenDo (Articles.get { auth = Just req.auth, slug = req.slug })
|> serverErrorOnNothing "Missing article"
|> Task.map articleResponse
-- Unfavorite Article
type alias UnfavoriteArticleRequest =
{ auth : Auth
, slug : String
}
type alias UnfavoriteArticleResponse =
{ article : Article
}
unfavoriteArticle : Server.Endpoint UnfavoriteArticleRequest Error UnfavoriteArticleResponse
unfavoriteArticle =
Server.request UnfavoriteArticleRequest
|> Server.withHeaders requiredAuth
|> Server.withParams (Param.string "slug")
|> Server.endpoint
{ handler = unfavoriteArticleHandler
, respond = respondWith 200 encodeSingleArticle
}
unfavoriteArticleHandler : UnfavoriteArticleRequest -> ConcurrentTask Error FavoriteArticleResponse
unfavoriteArticleHandler req =
Articles.unfavorite
{ userId = req.auth.userId
, slug = req.slug
}
|> Task.andThenDo (Articles.get { auth = Just req.auth, slug = req.slug })
|> serverErrorOnNothing "Missing article"
|> Task.map articleResponse
-- Tags
type alias GetTagsRequest =
()
type alias GetTagsResponse =
{ tags : List String
}
getTags : Server.Endpoint GetTagsRequest Error GetTagsResponse
getTags =
Server.request ()
|> Server.endpoint
{ handler = getTagsHandler
, respond = respondWith 200 encodeTags
}
getTagsHandler : GetTagsRequest -> ConcurrentTask Error GetTagsResponse
getTagsHandler _ =
Articles.allTags
|> Task.map tagsResponse
-- Auth
requiredAuth : Headers Error Auth
requiredAuth =
Header.string "Authorization"
|> Header.andThen toAuthHeader
optionalAuth : Headers Error (Maybe Auth)
optionalAuth =
Header.optional requiredAuth
toAuthHeader : String -> ConcurrentTask Error Auth
toAuthHeader header =
if String.startsWith "Token " header then
Auth.verifyToken (String.dropLeft 6 header)
else
Task.fail (Error.auth "Invalid Auth Header")
-- Helpers
serverErrorOnNothing : String -> ConcurrentTask Error (Maybe a) -> ConcurrentTask Error a
serverErrorOnNothing message =
Task.failOnNothing (Error.server message)
notFoundOnNothing : String -> ConcurrentTask Error (Maybe a) -> ConcurrentTask Error a
notFoundOnNothing value =
Task.failOnNothing (Error.notFound value)
-- User
type alias User =
{ email : String
, token : String
, username : String
, bio : Maybe String
, image : Maybe String
}
userResponse : String -> Users.User -> { user : User }
userResponse token user =
{ user =
{ email = user.email
, username = user.username
, image = user.image
, bio = user.bio
, token = token
}
}
encodeUser : { response | user : User } -> Encode.Value
encodeUser { user } =
Encode.object
[ ( "user"
, Encode.object
[ ( "email", Encode.string user.email )
, ( "token", Encode.string user.token )
, ( "username", Encode.string user.username )
, ( "bio", Encode.maybe Encode.string user.bio )
, ( "image", Encode.maybe Encode.string user.image )
]
)
]
-- Profile
type alias Profile =
{ username : String
, bio : Maybe String
, image : Maybe String
, following : Maybe Bool
}
profileResponse : Users.Profile -> { profile : Profile }
profileResponse profile =
{ profile = profile }
encodeProfile : { response | profile : Profile } -> Encode.Value
encodeProfile res =
Encode.object
[ ( "profile"
, Encode.object
[ ( "username", Encode.string res.profile.username )
, ( "bio", Encode.maybe Encode.string res.profile.bio )
, ( "image", Encode.maybe Encode.string res.profile.image )
, ( "following", Encode.maybe Encode.bool res.profile.following )
]
)
]
-- Article
type alias Article =
{ slug : String
, title : String
, description : String
, body : String
, tagList : List String
, createdAt : Time.Posix
, updatedAt : Time.Posix
, favorited : Bool
, favoritesCount : Int
, author : Author
}
type alias Author =
{ username : String
, bio : Maybe String
, image : Maybe String
, following : Maybe Bool
}
articlesResponse : List Articles.Article -> { articles : List Article }
articlesResponse articles =
{ articles = articles }
articleResponse : Articles.Article -> { article : Article }
articleResponse article =
{ article = article }
encodeMultipleArticles : { response | articles : List Article } -> Encode.Value
encodeMultipleArticles res =
Encode.object
[ ( "articles", Encode.list encodeArticle res.articles )
, ( "articlesCount", Encode.int (List.length res.articles) )
]
encodeSingleArticle : { response | article : Article } -> Encode.Value
encodeSingleArticle res =