-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
188 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,70 @@ | ||
import { getContext } from "../utils/context.js"; | ||
import { Request, Response, NextFunction } from "express"; | ||
import { | ||
SinglePointReader, | ||
SinglePointWriter, | ||
} from '@composexp/points'; | ||
import { getContext } from '../utils/context.js' | ||
import { Request, Response, NextFunction } from 'express' | ||
import { SinglePointReader, SinglePointWriter } from '@composexp/points' | ||
|
||
export interface CreateSinglePointRequest extends Request { | ||
body: { | ||
recipient: string | ||
} | ||
} | ||
|
||
const createSinglePoint = async ( | ||
req: Request, | ||
req: CreateSinglePointRequest, | ||
res: Response, | ||
next: NextFunction | ||
next: NextFunction, | ||
) => { | ||
try { | ||
const { ceramic } = await getContext(); | ||
const writer = new SinglePointWriter({ ceramic }); | ||
await writer.addPointTo(req.body.recipient); | ||
res.locals.ceramic = ceramic; | ||
return next(); | ||
const { ceramic } = await getContext() | ||
const writer = new SinglePointWriter({ ceramic }) | ||
await writer.addPointTo(req.body.recipient) | ||
res.locals.ceramic = ceramic | ||
return next() | ||
} catch (error) { | ||
console.error(error); | ||
return error; | ||
console.error(error) | ||
return error | ||
} | ||
}; | ||
} | ||
|
||
const removeSinglePoint = async ( | ||
req: Request, | ||
req: CreateSinglePointRequest, | ||
_res: Response, | ||
next: NextFunction | ||
next: NextFunction, | ||
) => { | ||
try { | ||
const { ceramic } = await getContext(); | ||
const { ceramic } = await getContext() | ||
const reader = new SinglePointReader({ | ||
ceramic, | ||
issuer: ceramic.did!.id, | ||
}); | ||
const documents = await reader.queryPointDocumentsFor(req.body.recipient); | ||
const id = documents.documents[documents.documents.length - 1].id; | ||
const writer = new SinglePointWriter({ ceramic }); | ||
await writer.removePoint(id.toString()); | ||
return next(); | ||
}) | ||
const documents = await reader.queryPointDocumentsFor(req.body.recipient) | ||
const id = documents.documents[documents.documents.length - 1].id | ||
const writer = new SinglePointWriter({ ceramic }) | ||
await writer.removePoint(id.toString()) | ||
return next() | ||
} catch (error) { | ||
console.error(error); | ||
return error; | ||
console.error(error) | ||
return error | ||
} | ||
}; | ||
} | ||
|
||
const getSinglePoints = async ( | ||
req: Request, | ||
req: CreateSinglePointRequest, | ||
res: Response, | ||
next: NextFunction | ||
next: NextFunction, | ||
) => { | ||
try { | ||
const ceramic = res.locals.ceramic ?? (await getContext()).ceramic; | ||
const { ceramic } = await getContext() | ||
const reader = new SinglePointReader({ | ||
ceramic, | ||
issuer: ceramic.did!.id, | ||
}); | ||
const totalPoints = await reader.countPointsFor(req.body.recipient); | ||
res.locals.totalPoints = totalPoints; | ||
return next(); | ||
}) | ||
const totalPoints = await reader.countPointsFor(req.body.recipient) | ||
res.locals.totalPoints = totalPoints | ||
return next() | ||
} catch (error) { | ||
console.error(error); | ||
return error; | ||
console.error(error) | ||
return error | ||
} | ||
}; | ||
} | ||
|
||
export const singlePointController = { createSinglePoint, getSinglePoints, removeSinglePoint }; | ||
export const singlePointController = { createSinglePoint, getSinglePoints, removeSinglePoint } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,33 @@ | ||
import express from "express"; | ||
import cors from "cors"; | ||
import singleRouter from "./routes/single.js"; | ||
import multiRouter from "./routes/multi.js"; | ||
|
||
const app = express(); | ||
const port = process.env.PORT || 8080; | ||
import express, { json, Request, Response, NextFunction } from 'express' | ||
import cors from 'cors' | ||
import singleRouter from './routes/single.js' | ||
import multiRouter from './routes/multi.js' | ||
|
||
const app = express() | ||
const port = process.env.PORT || 8080 | ||
|
||
const corsOptions = { | ||
origin: ["http://localhost:8080", "https://developers.ceramic.network"], | ||
optionsSuccessStatus: 200 // For legacy browser support | ||
} | ||
origin: ['http://localhost:8080', 'https://developers.ceramic.network'], | ||
optionsSuccessStatus: 200, // For legacy browser support | ||
} | ||
|
||
app.use(express.json()); | ||
app.use(cors(corsOptions)); | ||
app.use(json()) | ||
app.use(cors(corsOptions)) | ||
// app.use(bodyParser.json()); | ||
|
||
const allowCrossDomain = ( | ||
_req: any, | ||
res: { header: (arg0: string, arg1: string) => void }, | ||
next: () => void | ||
) => { | ||
res.header("Access-Control-Allow-Origin", "*"); | ||
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE"); | ||
res.header("Access-Control-Allow-Headers", "Content-Type"); | ||
next(); | ||
}; | ||
const allowCrossDomain = (_req: Request, res: Response, next: NextFunction) => { | ||
res.header('Access-Control-Allow-Origin', '*') | ||
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE') | ||
res.header('Access-Control-Allow-Headers', 'Content-Type') | ||
next() | ||
} | ||
|
||
app.use(allowCrossDomain); | ||
app.use(allowCrossDomain) | ||
|
||
app.use("/single", singleRouter); | ||
app.use("/multi", multiRouter); | ||
app.use('/single', singleRouter) | ||
app.use('/multi', multiRouter) | ||
|
||
app.listen(port, () => { | ||
// tslint:disable-next-line:no-console | ||
console.log(`server started at http://localhost:${port}`); | ||
}); | ||
console.log(`server started at http://localhost:${port}`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,38 @@ | ||
import express from "express"; | ||
import { singlePointController } from "../controllers/singleController.js"; | ||
import { RequestHandler, Response, Router, Request } from 'express' | ||
import { singlePointController } from '../controllers/singleController.js' | ||
|
||
const router: express.Router = express.Router(); | ||
const router: Router = Router() | ||
|
||
router.get("/", singlePointController.getSinglePoints, (_req, res) => { | ||
return res.json({ totalPoints: res.locals.totalPoints }); | ||
}); | ||
type R = Response & { | ||
locals: { | ||
totalPoints: number | ||
} | ||
} | ||
|
||
router.get( | ||
'/', | ||
singlePointController.getSinglePoints as RequestHandler, | ||
(_req: Request, res: R): Response => { | ||
return res.json({ totalPoints: res.locals.totalPoints }) | ||
}, | ||
) | ||
|
||
router.post( | ||
"/create", | ||
singlePointController.createSinglePoint, | ||
singlePointController.getSinglePoints, | ||
(_req, res) => { | ||
return res.json({ totalPoints: res.locals.totalPoints }); | ||
} | ||
); | ||
'/create', | ||
singlePointController.createSinglePoint as RequestHandler, | ||
singlePointController.getSinglePoints as RequestHandler, | ||
(_req: Request, res: R): Response => { | ||
return res.json({ totalPoints: res.locals.totalPoints }) | ||
}, | ||
) | ||
|
||
router.delete( | ||
"/remove", | ||
singlePointController.removeSinglePoint, | ||
singlePointController.getSinglePoints, | ||
(_req, res) => { | ||
return res.json({ totalPoints: res.locals.totalPoints }); | ||
} | ||
); | ||
'/remove', | ||
singlePointController.removeSinglePoint as RequestHandler, | ||
singlePointController.getSinglePoints as RequestHandler, | ||
(_req: Request, res: R): Response => { | ||
return res.json({ totalPoints: res.locals.totalPoints }) | ||
}, | ||
) | ||
|
||
export default router; | ||
export default router |
Oops, something went wrong.