Skip to content

Commit c56ecac

Browse files
committed
feat(app): implement codebayu service
1 parent 145a5b5 commit c56ecac

File tree

11 files changed

+116
-27
lines changed

11 files changed

+116
-27
lines changed

.env.example

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,13 @@ GOOGLE_CLIENT_ID=
3535
GOOGLE_CLIENT_SECRET=
3636

3737
# Codewars
38-
NEXT_PUBLIC_USER_ID=
38+
NEXT_PUBLIC_USER_ID=
39+
40+
# Google Adsense
41+
GOOGLE_ADSENSE_UNIT_BLOG_CLIENT=
42+
GOOGLE_ADSENSE_UNIT_BLOG_SLOT=
43+
44+
# API codebayu
45+
CODEBAYU_SERVICE=
46+
API_KEY=
47+
API_SECRET=

app/about/page.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { Metadata } from 'next'
2+
import { revalidatePath } from 'next/cache'
23

34
import Container from '@/components/elements/Container'
45
import PageHeading from '@/components/elements/PageHeading'
6+
import axios from 'axios'
57

8+
import { CODEBAYU_SERVICE } from '@/common/constant'
69
import { METADATA } from '@/common/constant/metadata'
10+
import { getRequestHeader } from '@/common/helpers'
711
import { careerDto } from '@/common/helpers/dto'
8-
import { prisma } from '@/common/libs/prisma'
9-
import { CareerProps } from '@/common/types/careers'
12+
import { IResponseCodeBayuService } from '@/common/types'
13+
import { CareerProps, ICareerCMS } from '@/common/types/careers'
1014

1115
import About from '@/modules/about'
1216

@@ -34,6 +38,10 @@ export default async function AboutPage() {
3438
}
3539

3640
async function getCareers(): Promise<CareerProps[]> {
37-
const response = await prisma.career.findMany({ orderBy: { startDate: 'desc' } })
38-
return response.map(careerDto)
41+
revalidatePath('/')
42+
const headers = getRequestHeader()
43+
const response = await axios.get(`${CODEBAYU_SERVICE}/career`, { headers })
44+
const data = response.data as IResponseCodeBayuService<ICareerCMS[]>
45+
if (data.statusCode !== 200) return []
46+
return data.data.map(careerDto).sort((a, b) => new Date(b.start_date).getTime() - new Date(a.start_date).getTime())
3947
}

app/experience/[slug]/page.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { Metadata } from 'next'
2+
import { revalidatePath } from 'next/cache'
23

34
import BackButton from '@/components/elements/BackButton'
45
import Container from '@/components/elements/Container'
6+
import axios from 'axios'
57

8+
import { CODEBAYU_SERVICE } from '@/common/constant'
69
import { METADATA } from '@/common/constant/metadata'
10+
import { getRequestHeader } from '@/common/helpers'
711
import { careerDto } from '@/common/helpers/dto'
8-
import { prisma } from '@/common/libs/prisma'
9-
import { CareerProps } from '@/common/types/careers'
12+
import { IResponseCodeBayuService } from '@/common/types'
13+
import { CareerProps, ICareerCMS } from '@/common/types/careers'
1014

1115
import ExperienceDetail from '@/modules/experience'
1216

@@ -33,6 +37,10 @@ export default async function ExperienceDetailPage({ params }: { params: { slug:
3337
}
3438

3539
async function getCareers(): Promise<CareerProps[]> {
36-
const response = await prisma.career.findMany({ orderBy: { startDate: 'desc' } })
37-
return response.map(careerDto)
40+
revalidatePath('/experience')
41+
const headers = getRequestHeader()
42+
const response = await axios.get(`${CODEBAYU_SERVICE}/career`, { headers })
43+
const data = response.data as IResponseCodeBayuService<ICareerCMS[]>
44+
if (data.statusCode !== 200) return []
45+
return data.data.map(careerDto).sort((a, b) => new Date(b.start_date).getTime() - new Date(a.start_date).getTime())
3846
}

app/learn/[content]/page.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { Metadata } from 'next'
2+
import { revalidatePath } from 'next/cache'
23

34
import BackButton from '@/components/elements/BackButton'
45
import Container from '@/components/elements/Container'
56
import PageHeading from '@/components/elements/PageHeading'
7+
import axios from 'axios'
68

9+
import { CODEBAYU_SERVICE } from '@/common/constant'
710
import { METADATA } from '@/common/constant/metadata'
11+
import { getRequestHeader } from '@/common/helpers'
812
import { learnDto } from '@/common/helpers/dto'
9-
import { prisma } from '@/common/libs/prisma'
10-
import { ILearn } from '@/common/types/learn'
13+
import { IResponseCodeBayuService } from '@/common/types'
14+
import { ILearn, ILearnCMS } from '@/common/types/learn'
1115

1216
import ContentLists from '@/modules/learn/components/ContentLists'
1317

@@ -58,8 +62,12 @@ export default async function LearnContentPage({ params }: LearnContentPage) {
5862
}
5963

6064
async function getLearns(): Promise<ILearn[]> {
61-
const response = await prisma.learn.findMany()
62-
return response.map(learnDto)
65+
revalidatePath('/learn')
66+
const headers = getRequestHeader()
67+
const response = await axios.get(`${CODEBAYU_SERVICE}/learn`, { headers })
68+
const data = response.data as IResponseCodeBayuService<ILearnCMS[]>
69+
if (data.statusCode !== 200) return []
70+
return data.data.map(learnDto)
6371
}
6472

6573
async function getContent(contentSlug: string) {

app/learn/page.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { Metadata } from 'next'
2+
import { revalidatePath } from 'next/cache'
23

34
import Container from '@/components/elements/Container'
45
import PageHeading from '@/components/elements/PageHeading'
6+
import axios from 'axios'
57

8+
import { CODEBAYU_SERVICE } from '@/common/constant'
69
import { METADATA } from '@/common/constant/metadata'
10+
import { getRequestHeader } from '@/common/helpers'
711
import { learnDto } from '@/common/helpers/dto'
8-
import { prisma } from '@/common/libs/prisma'
9-
import { ILearn } from '@/common/types/learn'
12+
import { IResponseCodeBayuService } from '@/common/types'
13+
import { ILearn, ILearnCMS } from '@/common/types/learn'
1014

1115
import LearnModule from '@/modules/learn'
1216

@@ -37,6 +41,10 @@ export default async function LearnPage() {
3741
}
3842

3943
async function getLearns(): Promise<ILearn[]> {
40-
const response = await prisma.learn.findMany()
41-
return response.map(learnDto)
44+
revalidatePath('/learn')
45+
const headers = getRequestHeader()
46+
const response = await axios.get(`${CODEBAYU_SERVICE}/learn`, { headers })
47+
const data = response.data as IResponseCodeBayuService<ILearnCMS[]>
48+
if (data.statusCode !== 200) return []
49+
return data.data.map(learnDto)
4250
}

app/page.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import { Metadata } from 'next'
22
import { revalidatePath } from 'next/cache'
33

44
import Container from '@/components/elements/Container'
5+
import axios from 'axios'
56

7+
import { CODEBAYU_SERVICE } from '@/common/constant'
68
import { METADATA } from '@/common/constant/metadata'
9+
import { getRequestHeader } from '@/common/helpers'
710
import { learnDto } from '@/common/helpers/dto'
8-
import { prisma } from '@/common/libs/prisma'
9-
import { ILearn } from '@/common/types/learn'
11+
import { IResponseCodeBayuService } from '@/common/types'
12+
import { ILearn, ILearnCMS } from '@/common/types/learn'
1013
import { IServices } from '@/common/types/services'
1114

1215
import Home from '@/modules/home'
@@ -32,12 +35,18 @@ export default async function HomePage() {
3235

3336
async function getLearns(): Promise<ILearn[]> {
3437
revalidatePath('/')
35-
const response = await prisma.learn.findMany()
36-
return response.map(learnDto)
38+
const headers = getRequestHeader()
39+
const response = await axios.get(`${CODEBAYU_SERVICE}/learn`, { headers })
40+
const data = response.data as IResponseCodeBayuService<ILearnCMS[]>
41+
if (data.statusCode !== 200) return []
42+
return data.data.map(learnDto)
3743
}
3844

3945
async function getServices(): Promise<IServices[]> {
4046
revalidatePath('/')
41-
const response = await prisma.service.findMany()
42-
return response
47+
const headers = getRequestHeader()
48+
const response = await axios.get(`${CODEBAYU_SERVICE}/service`, { headers })
49+
const data = response.data as IResponseCodeBayuService<IServices[]>
50+
if (data.statusCode !== 200) return []
51+
return data.data
4352
}

common/constant/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export const PAGESPEED_CATEGORIES = '&category=accessibility&category=performanc
1313

1414
export const SAWERIA_URL = 'https://saweria.co/codebayu'
1515
export const CODEWARS_URL = 'https://www.codewars.com/'
16+
export const CODEBAYU_SERVICE = process.env.CODEBAYU_SERVICE || ''

common/helpers/dto.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export function careerDto(career: ICareerCMS) {
1010
location: career.location,
1111
location_type: career.locationType,
1212
type: career.type,
13-
start_date: career.startDate,
14-
end_date: career.endDate,
13+
start_date: new Date(career.startDate),
14+
end_date: career.endDate ? new Date(career.endDate) : null,
1515
link: career.link,
1616
slug: career.slug
1717
}

common/helpers/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
1+
import crypto from 'crypto'
12
import { format, parseISO } from 'date-fns'
23
import { utcToZonedTime } from 'date-fns-tz'
34

5+
import { IRequestHeader } from '../types'
6+
47
export const formatBlogSlug = (slug: string) => slug?.slice(0, -5)
58

69
export const formatDate = (date: string, type = 'MMMM dd, yyyy') => {
710
const formattedDate = format(utcToZonedTime(parseISO(date), 'Asia/Jakarta'), type)
811
return formattedDate
912
}
13+
14+
export function getRequestHeader(): IRequestHeader {
15+
const apiKey = process.env.API_KEY || ''
16+
const apiSecret = process.env.API_SECRET || ''
17+
const unixTimestamp = Math.floor(new Date().getTime() / 1000)
18+
19+
const signature = getSignature({ apiKey, apiSecret, unixTimestamp })
20+
const header = { ['x-datetime']: unixTimestamp, ['x-signature']: signature }
21+
return header
22+
}
23+
24+
export function getSignature({
25+
apiKey,
26+
apiSecret,
27+
unixTimestamp
28+
}: {
29+
apiKey: string
30+
apiSecret: string
31+
unixTimestamp: number
32+
}) {
33+
const hmac = crypto.createHmac('sha256', apiKey + unixTimestamp)
34+
hmac.update(apiSecret)
35+
const signature = hmac.digest('hex')
36+
return signature
37+
}

common/types/careers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export type ICareerCMS = {
2020
location: string
2121
locationType: string
2222
type: string
23-
startDate: Date
24-
endDate: Date | null
23+
startDate: string
24+
endDate: string | null
2525
link: string
2626
slug: string
2727
}

0 commit comments

Comments
 (0)