-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented stream routes GET and POST
- Loading branch information
Showing
12 changed files
with
183 additions
and
6,163 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,4 +55,7 @@ web-build/ | |
cache/ | ||
.env | ||
.docker/ | ||
.apps/api/prisma/migration/ | ||
.apps/api/prisma/migrations/ | ||
|
||
yarn.lock | ||
migrations |
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
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,3 +1,4 @@ | ||
import * as userController from "./user.controller"; | ||
import * as streamController from "./stream.controller"; | ||
|
||
export { userController }; | ||
export { userController, streamController }; |
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 |
---|---|---|
@@ -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: "" | ||
} | ||
] | ||
}); | ||
} | ||
}; |
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,3 +1,4 @@ | ||
import userRouter from "./user.route"; | ||
import streamRouter from "./stream.route"; | ||
|
||
export { userRouter }; | ||
export { userRouter, streamRouter }; |
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 |
---|---|---|
@@ -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; |
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
Oops, something went wrong.