Skip to content

Commit

Permalink
feat(core-data): add endpoint banner & activity from Core Data (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayocodingit authored Mar 10, 2023
1 parent 4e7b90a commit bb01906
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/external/coreData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios'
import winston from 'winston'
import { Config } from '../config/config.interface'
import { ConvertISOToDate } from '../helpers/date'

class CoreData {
constructor(private config: Config, private logger: winston.Logger) {}
Expand All @@ -18,14 +19,14 @@ class CoreData {
}
}

async Activity(startDate: string, endDate: string) {
async Activity(start_date: string, end_date: string) {
try {
const response = await axios.get(
this.config.coreData.url + `/kegiatan`,
{
params: {
start_date: startDate,
end_date: endDate,
start_date: ConvertISOToDate(start_date),
end_date: ConvertISOToDate(end_date),
},
}
)
Expand All @@ -42,7 +43,7 @@ class CoreData {
const response = await axios.get(
this.config.coreData.url + `/kegiatan/${id}`
)

const { data } = response
return data
} catch (error) {
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ import { DateTime } from 'luxon'
export const ConvertTimestampToISODate = (date: string) => {
return DateTime.fromSeconds(parseInt(date)).toISODate()
}

export const ConvertISOToDate = (date: string) => {
return DateTime.fromISO(date).toISODate()
}
28 changes: 28 additions & 0 deletions src/internal/core/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import winston from 'winston'
import { Config } from '../../config/config.interface'
import CoreData from '../../external/coreData'
import Http from '../../transport/http/http'
import Handler from './delivery/http/handler'
import Usecase from './usecase/usecase'

class Core {
constructor(
private http: Http,
private logger: winston.Logger,
private config: Config
) {
const coreData = new CoreData(config, logger)
const usecase = new Usecase(coreData, logger)

this.loadHttp(usecase)
}

private loadHttp(usecase: Usecase) {
const handler = new Handler(usecase, this.logger)
this.http.app.get('/v1/banners', handler.Banner())
this.http.app.get('/v1/activities', handler.Activity())
this.http.app.get('/v1/activities/:id', handler.ActivityByID())
}
}

export default Core
51 changes: 51 additions & 0 deletions src/internal/core/delivery/http/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { NextFunction, Response } from 'express'
import winston from 'winston'
import Usecase from '../../usecase/usecase'
import statusCode from '../../../../pkg/statusCode'
import {
ValidateFormRequest,
} from '../../../../helpers/validate'
import { Activity } from '../../entity/schema'

class Handler {
constructor(private usecase: Usecase, private logger: winston.Logger) {}
public Banner() {
return async (req: any, res: Response, next: NextFunction) => {
try {
const result = await this.usecase.Banner()
return res.status(statusCode.OK).json({
data: result,
})
} catch (error) {
return next(error)
}
}
}
public Activity() {
return async (req: any, res: Response, next: NextFunction) => {
try {
const value = ValidateFormRequest(Activity, req.query)
const result = await this.usecase.Activity(value)
return res.status(statusCode.OK).json({
data: result,
})
} catch (error) {
return next(error)
}
}
}
public ActivityByID() {
return async (req: any, res: Response, next: NextFunction) => {
try {
const result = await this.usecase.ActivityByID(req.params.id)
return res.status(statusCode.OK).json({
data: result,
})
} catch (error) {
return next(error)
}
}
}
}

export default Handler
4 changes: 4 additions & 0 deletions src/internal/core/entity/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Activity {
start_date: string
end_date: string
}
6 changes: 6 additions & 0 deletions src/internal/core/entity/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Joi from 'joi'

export const Activity = Joi.object({
start_date: Joi.string().isoDate().required(),
end_date: Joi.string().isoDate().required(),
})
Empty file.
27 changes: 27 additions & 0 deletions src/internal/core/usecase/usecase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import winston from 'winston'
import CoreData from '../../../external/coreData'
import { Activity } from '../entity/interface'

class Usecase {
constructor(private coreData: CoreData, private logger: winston.Logger) {}

public async Banner() {
const res = await this.coreData.Banner()

return res
}

public async Activity({ start_date, end_date }: Activity) {
const res = await this.coreData.Activity(start_date, end_date)

return res
}

public async ActivityByID(id: string) {
const res = await this.coreData.ActivityById(id)

return res
}
}

export default Usecase
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import config from './config/config'
import Mongo from './database/mongo/mongo'
import Core from './internal/core/core'
import News from './internal/news/news'
import PrayerTime from './internal/prayerTimes/prayerTimes'
import Logger from './pkg/logger'
Expand All @@ -15,6 +16,7 @@ const main = async () => {
// Load App Internal
new PrayerTime(http, logger, config)
new News(http, logger, config)
new Core(http, logger, config)

if (config.app.env !== 'test') {
http.Run(config.app.port.http)
Expand Down

0 comments on commit bb01906

Please sign in to comment.