Add basic message querying
This commit is contained in:
parent
a2de4f12d8
commit
99ef2cb9e7
|
@ -3,16 +3,19 @@ import express from "express";
|
|||
import dotenv from "dotenv";
|
||||
import { checkFile, emp, Logger } from "./utils";
|
||||
import { ReviewData } from "./utils/ReviewData";
|
||||
import { MessagesResponder } from "./routes/messages";
|
||||
dotenv.config();
|
||||
|
||||
const app = express();
|
||||
const logger = new Logger("Main");
|
||||
const data = new ReviewData(checkFile("data.json", "utf8"));
|
||||
|
||||
const post = new PostListener(app, data);
|
||||
const post = new PostListener(app);
|
||||
const query = new MessagesResponder(app);
|
||||
const port = +process.env.PORT || 8080;
|
||||
|
||||
post.start();
|
||||
query.start();
|
||||
|
||||
app.listen(port, () => {
|
||||
logger.log(`Server started on ${emp(`http://localhost:${port}`)}`);
|
||||
|
|
30
src/routes/messages.ts
Normal file
30
src/routes/messages.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
import { Express, Request, Response } from "express";
|
||||
import { Logger, data } from "../utils";
|
||||
import { typeJson } from "../types";
|
||||
|
||||
export class MessagesResponder {
|
||||
private server: Express;
|
||||
private readonly logger = new Logger("Query");
|
||||
|
||||
constructor(server: Express) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public async start(): Promise<void> {
|
||||
this.server.get("/messages", (req: Request, res: Response) => {
|
||||
const result = data.getReviews();
|
||||
|
||||
res.writeHead(result.toString() ? 200 : 204, typeJson);
|
||||
res.write(JSON.stringify(result));
|
||||
res.end();
|
||||
});
|
||||
|
||||
this.server.get("/messages/:id", (req: Request, res: Response) => {
|
||||
const result = data.getReviewById(req.params.id);
|
||||
|
||||
res.writeHead(result.toString() ? 200 : 204, typeJson);
|
||||
res.write(JSON.stringify(result));
|
||||
res.end();
|
||||
})
|
||||
}
|
||||
}
|
|
@ -1,16 +1,14 @@
|
|||
import { userReviewSchema, typeJson } from "../types";
|
||||
import { bold, emp, Logger, responder } from "../utils";
|
||||
import { bold, emp, Logger, responder, data } from "../utils";
|
||||
import { Express, Request, Response } from "express";
|
||||
import { ReviewData } from "../utils/ReviewData";
|
||||
|
||||
export class PostListener {
|
||||
private server: Express;
|
||||
private data: ReviewData;
|
||||
private readonly logger = new Logger("Post Listener");
|
||||
|
||||
constructor(server: Express, data: ReviewData) {
|
||||
constructor(server: Express) {
|
||||
this.server = server;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public async start(): Promise<void> {
|
||||
|
@ -41,7 +39,7 @@ export class PostListener {
|
|||
this.logger.log(`${sender} ~>`, validUserReview);
|
||||
|
||||
try {
|
||||
this.data.appendReview(validUserReview);
|
||||
data.appendReview(validUserReview);
|
||||
|
||||
res.writeHead(201, typeJson);
|
||||
res.write(responder.success("review was sent"));
|
||||
|
|
|
@ -5,7 +5,7 @@ import {
|
|||
userRating,
|
||||
UserSideReview,
|
||||
} from "../types";
|
||||
import { appendId } from "./functions";
|
||||
import { appendId, checkFile } from "./functions";
|
||||
|
||||
export class ReviewData {
|
||||
public data: ServerSideReview[];
|
||||
|
@ -31,13 +31,11 @@ export class ReviewData {
|
|||
});
|
||||
}
|
||||
|
||||
public getReviews(maxCount = Infinity): ServerSideReview[] {
|
||||
public getReviews(): ServerSideReview[] {
|
||||
let final: ServerSideReview[] = [];
|
||||
|
||||
while (final.length < maxCount) {
|
||||
for (const review of this.data) {
|
||||
final.push(review);
|
||||
}
|
||||
for (const review of this.data) {
|
||||
final.push(review);
|
||||
}
|
||||
|
||||
return final;
|
||||
|
@ -101,3 +99,5 @@ export class ReviewData {
|
|||
return final;
|
||||
}
|
||||
}
|
||||
|
||||
export const data = new ReviewData(checkFile("data.json", "utf8"));
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
export * from "./functions";
|
||||
export * from "./logger";
|
||||
export * from "./responder";
|
||||
export * from "./ReviewData";
|
||||
|
|
Loading…
Reference in a new issue