Skip to content

Commit ef21ab4

Browse files
committed
Added profiles controller & routes
1 parent 71b6875 commit ef21ab4

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

app/controllers/Profiles/main.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { HttpContext } from '@adonisjs/core/http'
2+
import { isFollowing } from '#utils/is_following'
3+
import User from '#models/user'
4+
5+
export default class ProfilesController {
6+
async show({ request, auth }: HttpContext) {
7+
const { username } = request.qs()
8+
9+
const user = await User.query()
10+
.where({ username })
11+
.preload('avatar')
12+
.preload('posts')
13+
.preload('likes')
14+
.preload('followers')
15+
.preload('following')
16+
.withCount('posts')
17+
.withCount('likes')
18+
.withCount('followers')
19+
.withCount('following')
20+
.firstOrFail()
21+
22+
if (user.id !== auth.user!.id) {
23+
await isFollowing(user, auth)
24+
}
25+
26+
return user.serialize({
27+
fields: {
28+
omit: ['email', 'createdAt', 'updatedAt', 'rememberMeToken'],
29+
},
30+
})
31+
}
32+
}

start/routes/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import './uploads.ts'
1414
import './posts.ts'
1515
import './comments.ts'
1616
import './likes.ts'
17-
import './follows.js'
17+
import './follows.ts'
18+
import './profiles.ts'
1819

1920
router.get('/', async () => {
2021
return {

start/routes/profiles.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import router from '@adonisjs/core/services/router'
2+
import { middleware } from '#start/kernel'
3+
4+
const ProfilesController = () => import('#controllers/Profiles/main')
5+
6+
router.get('/profiles', [ProfilesController, 'show']).use(middleware.auth())

0 commit comments

Comments
 (0)