Skip to content

Commit

Permalink
feat: implemented stream routes GET and POST
Browse files Browse the repository at this point in the history
  • Loading branch information
jrau1801 committed May 14, 2024
1 parent b9201a8 commit b91c787
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 6,163 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ web-build/
cache/
.env
.docker/
.apps/api/prisma/migration/
.apps/api/prisma/migrations/

yarn.lock
migrations
2 changes: 1 addition & 1 deletion apps/api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ CORS_ORIGIN=*

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="mysql://root:root@localhost:3306/lyve_db?schema=public"
DATABASE_URL="postgres://postgres:postgres@localhost:5432/lyve_db?schema=public"
2 changes: 1 addition & 1 deletion apps/api/.env.local
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ CORS_ORIGIN=*

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="mysql://root:root@localhost:3306/lyve_db?schema=public"
DATABASE_URL="postgres://postgres:postgres@localhost:5432/lyve_db?schema=public"
4 changes: 3 additions & 1 deletion apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { errorHandler } from "./middleware/errorHandler";
import config from "./config/config";
import { xssMiddleware } from "./middleware/xssMiddleware";
import { createServer } from "http";
import { userRouter } from "./routes";
import { streamRouter, userRouter } from "./routes";

const app: Express = express();
const server = createServer(app);
Expand Down Expand Up @@ -43,6 +43,8 @@ app.get("/", (_req, res) => {

app.use("/api/user", userRouter);

app.use("/api/stream", streamRouter);

app.all("*", (_req, res) => {
res.status(404).json({ error: "404 Not Found" });
});
Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/controller/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as userController from "./user.controller";
import * as streamController from "./stream.controller";

export { userController };
export { userController, streamController };
133 changes: 133 additions & 0 deletions apps/api/src/controller/stream.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import type { Request, Response } from "express";
import httpStatus from "http-status";
import prismaClient from "../config/prisma";
import { createStreamCredentials, TypedRequest } from "../types/types";

export const getStreamInfo = async (
req: Request<{ id: string }>,
res: Response
) => {
const stream = await prismaClient.stream.findFirst({
where: { id: req.params.id },
select: {
id: true,
serverId: true,
active: true,
streamer: true,
previewImgUrl: true,
viewerCount: true,
genre: true,
created_at: true
}
});

if (!stream) {
return res.status(httpStatus.NOT_FOUND).json({
success: false,
data: null,
error: [
{
name: "Not_found",
code: "404",
message: "stream not found"
}
]
});
}

return res.status(httpStatus.OK).json({
success: true,
data: {
stream
},
error: "[]"
});
};

export const createStream = async (
req: TypedRequest<createStreamCredentials>,
res: Response
) => {
const { streamerId, previewImgUrl, genre } = req.body;

if (!streamerId || !previewImgUrl || !genre) {
return res.status(httpStatus.BAD_REQUEST).json({
success: false,
data: null,
error: [
{
name: "Bad_Request",
code: "400",
msg: "id, image and genre must be defined"
}
]
});
}
//TODO add try-catch
await prismaClient.stream.create({
data: {
streamerId: streamerId,
previewImgUrl: previewImgUrl,
genre: genre
}
});

//TODO select created stream and return in json prob with a service
return res.status(httpStatus.CREATED).json({
success: true,
data: {
id: "",
serverId: "",
active: false,
streamer: {
id: streamerId,
username: "",
promotionPoints: "",
level: "",
avatar_url: "",
followerCount: "",
followed: "false"
},
previewImgUrl: previewImgUrl,
viewerCount: "",
genre: "",
created_at: ""
},
error: []
});
};

export const deleteStream = async (
req: Request<{ id: string }>,
res: Response
) => {
const { id } = req.params;

try {
const deletedStream = await prismaClient.stream.delete({
where: {
streamerId: id
}
});

return res.status(httpStatus.OK).json({
success: true,
data: {
deletedStream
},
error: "[]"
});
} catch {
return res.status(httpStatus.CONFLICT).json({
success: false,
data: null,
error: [
{
name: "Conflict",
code: "409",
message: ""
}
]
});
}
};
12 changes: 10 additions & 2 deletions apps/api/src/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getUserInfo = async (
data: null,
error: [
{
name: "not found",
name: "Not_found",
code: 404,
message: "user not found"
}
Expand All @@ -52,7 +52,15 @@ export const createUser = async (

if (!id || !username || !email) {
return res.status(httpStatus.BAD_REQUEST).json({
message: "id, username and email must be defined"
success: false,
data: null,
error: [
{
name: "Bad_Request",
code: "400",
message: "id, username and email must be defined"
}
]
});
}

Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import userRouter from "./user.route";
import streamRouter from "./stream.route";

export { userRouter };
export { userRouter, streamRouter };
12 changes: 12 additions & 0 deletions apps/api/src/routes/stream.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Router } from "express";
import { streamController } from "../controller";

const streamRouter = Router();

streamRouter.post("/create", streamController.createStream);

streamRouter.get("/:id", streamController.getStreamInfo);

streamRouter.delete("/:id/delete", streamController.deleteStream);

export default streamRouter;
6 changes: 6 additions & 0 deletions apps/api/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ export type CreateUserCredentials = {
username: string;
email: string;
};

export type createStreamCredentials = {
streamerId: string;
previewImgUrl: string;
genre: string;
};
20 changes: 9 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ services:
- "3000:3000"
environment:
- NODE_ENV=production
- DATABASE_URL=mysql://user:password@mysql/db_name
- DATABASE_URL=postgres://user:password@postgres/db_name
- RABBITMQ_URL=amqp://user:password@rabbitmq
- KEYCLOAK_URL=http://keycloak:8080/auth
- REDIS_HOST=redis
depends_on:
- mysql
- postgres
- rabbitmq
- keycloak
- redis
Expand All @@ -33,12 +33,10 @@ services:
postgres:
image: postgres:latest
environment:
- POSTGRES_PASSWORD=root
- POSTGRES_USER=root
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- PGUSER=postgres
- POSTGRES_DB=lyve_db
entrypoint: sh -c "
echo 'CREATE DATABASE IF NOT EXISTS lyve_db; CREATE DATABASE IF NOT EXISTS lyve_db_keycloak;' > /docker-entrypoint-initdb.d/init.sql;
/usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci"
ports:
- "5432:5432"
volumes:
Expand All @@ -53,8 +51,8 @@ services:
keycloak:
image: quay.io/keycloak/keycloak:24.0.3
environment:
- DB_VENDOR=mysql
- DB_ADDR=mysql
- DB_VENDOR=postgres
- DB_ADDR=postgres
- DB_DATABASE=lyve_db_keycloak
- DB_USER=user
- DB_PASSWORD=password
Expand All @@ -66,7 +64,7 @@ services:
volumes:
- ./.docker/keycloak_data:/opt/jboss/keycloak/standalone/data
depends_on:
- mysql
- postgres

blob-storage:
image: minio/minio
Expand All @@ -88,4 +86,4 @@ services:
volumes:
postgresql_data:
keycloak_data:
blob_storage_data:
blob_storage_data:
Loading

0 comments on commit b91c787

Please sign in to comment.