-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubredditRoutes.kt
199 lines (191 loc) · 7.8 KB
/
SubredditRoutes.kt
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
package com.adikmt.routes
import com.adikmt.dtos.AuthCurrentUser
import com.adikmt.dtos.SerializedException
import com.adikmt.dtos.SubredditName
import com.adikmt.dtos.SubredditRequest
import com.adikmt.dtos.UserName
import com.adikmt.usecases.AddSubredditUsecase
import com.adikmt.usecases.FollowSubredditUsecase
import com.adikmt.usecases.GetAllSubredditsFollowedUsecase
import com.adikmt.usecases.GetSubredditByNameUsecase
import com.adikmt.usecases.SearchSubredditByNameUsecase
import com.adikmt.usecases.UnFollowSubredditUsecase
import com.adikmt.utils.deconstructResult
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.call
import io.ktor.server.auth.authenticate
import io.ktor.server.auth.principal
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.Routing
import io.ktor.server.routing.delete
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import org.koin.core.qualifier.named
import org.koin.ktor.ext.inject
fun Routing.subredditRoutes() {
addSubreddit()
getSubredditByName()
searchSubredditByName()
getFollowedSubreddit()
followSubreddit()
unfollowSubreddit()
}
private fun Routing.unfollowSubreddit() {
val unFollowSubredditUsecase by inject<UnFollowSubredditUsecase>(named("UnFollowSubredditUsecase"))
authenticate {
/**
* DELETE /subreddit/unFollow/{name} Unfollows a subreddit with the given
* name.
*
* Headers: Authorization: Bearer <token>
*/
delete("/subreddit/unFollow/{name}") {
try {
val name = call.parameters["name"]
val user = call.principal<AuthCurrentUser>()?.userName
user?.let { userName ->
name?.let {
val data = unFollowSubredditUsecase.unFollow(UserName(userName), SubredditName(it))
deconstructResult(this, data, HttpStatusCode.Gone)
}
call.respond(HttpStatusCode.UnprocessableEntity)
}
call.respond(HttpStatusCode.Unauthorized)
} catch (e: Exception) {
call.respond(HttpStatusCode.InternalServerError, SerializedException(e.message))
}
}
}
}
private fun Routing.followSubreddit() {
val followSubredditUsecase by inject<FollowSubredditUsecase>(named("FollowSubredditUsecase"))
authenticate {
/**
* POST /subreddit/follow/{name} Follows a subreddit with the given name.
*
* Headers: Authorization: Bearer <token>
*/
post("/subreddit/follow/{name}") {
try {
val name = call.parameters["name"]
val user = call.principal<AuthCurrentUser>()?.userName
user?.let { userName ->
name?.let {
val data = followSubredditUsecase.follow(UserName(userName), SubredditName(name))
deconstructResult(this, data, HttpStatusCode.Created)
}
call.respond(HttpStatusCode.UnprocessableEntity)
}
call.respond(HttpStatusCode.Unauthorized)
} catch (e: Exception) {
call.respond(HttpStatusCode.InternalServerError, SerializedException(e.message))
}
}
}
}
private fun Routing.searchSubredditByName() {
val searchSubredditByNameUsecase by inject<SearchSubredditByNameUsecase>(named("SearchSubredditByNameUsecase"))
authenticate {
/**
* GET /subreddit/search/{name} Search a subreddit with the given name.
*
* Headers: Authorization: Bearer <token>
*/
get("/subreddit/search/{name}") {
try {
val name = call.parameters["name"]
val user = call.principal<AuthCurrentUser>()?.userName
val limit = call.request.queryParameters["limit"]?.toInt() ?: 20
val offset = call.request.queryParameters["offset"]?.toLong() ?: 0L
user?.let { userName ->
name?.let {
val subredditList = searchSubredditByNameUsecase.get(SubredditName(it), limit, offset)
deconstructResult(this, subredditList, HttpStatusCode.OK)
}
call.respond(HttpStatusCode.UnprocessableEntity)
}
} catch (e: Exception) {
call.respond(HttpStatusCode.InternalServerError, SerializedException(e.message))
}
}
}
}
private fun Routing.getFollowedSubreddit() {
val getAllSubredditsFollowedUsecase by inject<GetAllSubredditsFollowedUsecase>(named("GetAllSubredditsFollowedUsecase"))
authenticate {
/**
* GET /subreddit/user/{userId} Retrieves a list of subreddits followed by
* a user with the given userId.
*
* Headers: Authorization: Bearer <token>
*/
get("/subreddit/user/{userId}") {
try {
val userId = call.parameters["userId"]
val user = call.principal<AuthCurrentUser>()?.userName
val limit = call.request.queryParameters["limit"]?.toInt() ?: 20
val offset = call.request.queryParameters["offset"]?.toLong() ?: 0L
user?.let { userName ->
userId?.let {
val subredditList = getAllSubredditsFollowedUsecase.get(UserName(it), limit, offset)
deconstructResult(this, subredditList, HttpStatusCode.OK)
}
call.respond(HttpStatusCode.UnprocessableEntity)
}
call.respond(HttpStatusCode.Unauthorized)
} catch (e: Exception) {
call.respond(HttpStatusCode.InternalServerError, SerializedException(e.message))
}
}
}
}
private fun Routing.getSubredditByName() {
val getSubredditByNameUsecase by inject<GetSubredditByNameUsecase>(named("GetSubredditByNameUsecase"))
authenticate {
/**
* GET /subreddit/id/{name} Retrieves the subreddit with the given name.
*
* Headers: Authorization: Bearer <token>
*/
get("/subreddit/id/{name}") {
try {
val name = call.parameters["name"]
name?.let {
val subreddit = getSubredditByNameUsecase.get(SubredditName(it))
deconstructResult(this, subreddit, HttpStatusCode.OK)
}
call.respond(HttpStatusCode.UnprocessableEntity)
} catch (e: Exception) {
call.respond(HttpStatusCode.InternalServerError, SerializedException(e.message))
}
}
}
}
private fun Routing.addSubreddit() {
val addSubredditUsecase by inject<AddSubredditUsecase>(named("AddSubredditUsecase"))
authenticate {
/**
* POST /subreddit Creates a new subreddit.
*
* Headers: Authorization: Bearer <token>
*/
post("/subreddit") {
try {
val subreddit = call.receive<SubredditRequest>()
val user = call.principal<AuthCurrentUser>()?.userName
user?.let { userName ->
subreddit?.let {
val subredditResponse = addSubredditUsecase.add(UserName(userName), subreddit)
deconstructResult(this, subredditResponse, HttpStatusCode.Created)
} ?: run {
call.respond(HttpStatusCode.UnprocessableEntity)
}
}
call.respond(HttpStatusCode.Unauthorized)
} catch (e: Exception) {
call.respond(HttpStatusCode.InternalServerError, SerializedException(e.message))
}
}
}
}