Skip to content

Commit ead71a4

Browse files
committed
Added profiles/likes & profiles/retweets subroutes
1 parent 969b43a commit ead71a4

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

app/controllers/Profiles/likes.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { HttpContext } from '@adonisjs/core/http'
2+
import User from '#models/user'
3+
4+
export default class ProfileLikesController {
5+
async show({ request, auth }: HttpContext) {
6+
const { username } = request.qs()
7+
8+
const user = await User.query()
9+
.where(username ? { username } : { username: auth.user!.username })
10+
.preload('avatar')
11+
.preload('likes', (likesQuery: any) => {
12+
likesQuery.preload('post', (postsQuery: any) => {
13+
postsQuery.preload('media')
14+
postsQuery.preload('user', (userQuery: any) => {
15+
userQuery.select(['id', 'fullName', 'username'])
16+
userQuery.preload('avatar')
17+
})
18+
})
19+
})
20+
.firstOrFail()
21+
22+
return user.serialize({
23+
fields: {
24+
omit: ['email', 'createdAt', 'updatedAt', 'rememberMeToken'],
25+
},
26+
})
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { HttpContext } from '@adonisjs/core/http'
2+
import User from '#models/user'
3+
4+
export default class ProfileRetweetsController {
5+
async show({ request, auth }: HttpContext) {
6+
const { username } = request.qs()
7+
8+
const user = await User.query()
9+
.where(username ? { username } : { username: auth.user!.username })
10+
.preload('avatar')
11+
.preload('retweets', (retweetsQuery: any) => {
12+
retweetsQuery.select(['id', 'postId'])
13+
retweetsQuery.preload('post', (postsQuery: any) => {
14+
postsQuery.preload('media')
15+
postsQuery.preload('user', (userQuery: any) => {
16+
userQuery.select(['id', 'fullName', 'username'])
17+
userQuery.preload('avatar')
18+
})
19+
})
20+
})
21+
.firstOrFail()
22+
23+
return user.serialize({
24+
fields: {
25+
omit: ['email', 'createdAt', 'updatedAt', 'rememberMeToken'],
26+
},
27+
})
28+
}
29+
}

start/routes/profiles.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ import router from '@adonisjs/core/services/router'
22
import { middleware } from '#start/kernel'
33

44
const ProfilesController = () => import('#controllers/Profiles/main')
5+
const ProfileLikesController = () => import('#controllers/Profiles/likes')
6+
const ProfileRetweetsController = () => import('#controllers/Profiles/retweets')
57

6-
router.get('/profiles', [ProfilesController, 'show']).use(middleware.auth())
8+
router.get('profiles', [ProfilesController, 'show']).use(middleware.auth())
9+
router.get('profiles/likes', [ProfileLikesController, 'show']).use(middleware.auth())
10+
router.get('profiles/retweets', [ProfileRetweetsController, 'show']).use(middleware.auth())

0 commit comments

Comments
 (0)