add game support for games

This commit is contained in:
Nikola Petrov 2023-08-02 18:28:58 +02:00
parent f240d4e7c5
commit 9f21d37002
7 changed files with 196 additions and 19 deletions

6
app.js
View File

@ -51,14 +51,12 @@ app.use(function (req, res, next) {
var mainRouter = require('./routes/main'); var mainRouter = require('./routes/main');
var listRouter = require('./routes/list'); var listRouter = require('./routes/list');
var photosRouter = require('./routes/api/photoRouter'); var apiRouter = require('./routes/api/apiRouter');
var movieRouter = require('./routes/api/movieRouter');
var mailRouter = require('./routes/mail'); var mailRouter = require('./routes/mail');
app.use('/', mainRouter); app.use('/', mainRouter);
app.use('/list', listRouter); app.use('/list', listRouter);
app.use('/api/photos', photosRouter); app.use('/api', apiRouter);
app.use('/api/movies', movieRouter);
app.use('/mail', mailRouter); app.use('/mail', mailRouter);
// catch 404 and forward to error handler // catch 404 and forward to error handler

View File

@ -0,0 +1,122 @@
var UserModel = require('../models/userModel');
var GameModel = require('../models/gameModel');
module.exports = {
list: function (req, res) {
GameModel.find({}, { _id: 0, __v: 0 })
.then(games => {
return res.json(games);
})
.catch(err => {
return res.status(500).json({
message: 'Error when getting games.',
error: err
});
});
},
create: async function (req, res) {
const gameCode = req.body.code;
const passp = req.body.pass;
try {
const userFound = await UserModel.findOne({ pass: passp });
if (!userFound) {
return res.status(404).json({ message: 'Wrong password' });
}
const gameFound = await GameModel.findOne({ code: gameCode });
if (gameFound) {
return res.status(409).json({ message: 'Game already exists' });
}
const uri = "https://id.twitch.tv/oauth2/token?client_id=" +userFound.client_id+ "&client_secret=" + userFound.client_secret+ "&grant_type=client_credentials";
var response = await fetch(uri, { method: 'POST' });
var mData = await response.json();
var mheaders = {
'Accept': 'application/json',
'Client-ID': userFound.client_id,
'Authorization': 'Bearer ' + mData.access_token
}
response = await fetch(
"https://api.igdb.com/v4/games",
{
method: 'POST',
headers: mheaders,
body: `fields name; where id = ${gameCode};`
}
)
const gameData = await response.json()
if (gameData.length == 0) {
return res.status(404).json({ message: 'wrong code' });
}
response = await fetch(
"https://api.igdb.com/v4/covers",
{
method: 'POST',
headers: mheaders,
body: `fields image_id; where game = ${gameCode};`
}
)
const coverData = await response.json()
response = await fetch(
"https://api.igdb.com/v4/release_dates",
{
method: 'POST',
headers: mheaders,
body: `fields human; where game = ${gameCode};`
}
)
const releasedData = await response.json()
const game = new GameModel({
code: gameCode,
title: gameData[0].name,
released: releasedData[0].human,
webImg: `https://images.igdb.com/igdb/image/upload/t_cover_big/${coverData[0].image_id}.jpg`,
});
const savedGame = await game.save();
return res.status(201).json(game);
} catch (error) {
return res.status(500).json({ message: 'Error when creating game', error: error });
}
},
remove: async function (req, res) {
var id = req.body.code;
const passp = req.body.pass;
try {
const userFound = await UserModel.findOne({ pass: passp });
if (!userFound) {
return res.status(404).json({ message: 'Wrong password' });
}
const movie = await GameModel.findOneAndDelete({ code: id });
if (!movie) {
return res.status(404).json({ message: 'No such game' });
}
return res.status(204).json();
}
catch (err) {
return res.status(500).json({ message: 'Error when deleting the game.' });
}
}
};

11
models/gameModel.js Normal file
View File

@ -0,0 +1,11 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var gameSchema = new Schema({
'title': String,
'released': String,
'webImg': String,
'code': String
});
module.exports = mongoose.model('game', gameSchema);

View File

@ -4,6 +4,8 @@ var Schema = mongoose.Schema;
var userSchema = new Schema({ var userSchema = new Schema({
'pass': String, 'pass': String,
'key': String, 'key': String,
'client_id': String,
'client_secret': String,
}); });
module.exports = mongoose.model('user', userSchema); module.exports = mongoose.model('user', userSchema);

17
routes/api/apiRouter.js Normal file
View File

@ -0,0 +1,17 @@
var express = require('express');
var router = express.Router();
var photosRouter = require('./photoRouter');
var movieRouter = require('./movieRouter');
var gameRouter = require('./gameRouter');
router.use('/photos', photosRouter);
router.use('/movies', movieRouter);
router.use('/games', gameRouter);
module.exports = router;

11
routes/api/gameRouter.js Normal file
View File

@ -0,0 +1,11 @@
var express = require('express');
var router = express.Router();
var gameController = require('../../controllers/gameController.js');
router.get('/', gameController.list);
router.post('/', gameController.create);
router.delete('/', gameController.remove);
module.exports = router;

View File

@ -4,19 +4,35 @@
<script defer src="{{this}}"></script> <script defer src="{{this}}"></script>
{{/each}} {{/each}}
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark"> <header>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<div class="container-fluid"> <div class="container-fluid">
<form class="d-flex" id="myform" action=""> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<button class="nav-link active" id="movieButton">Movies</button>
</li>
<li class="nav-item">
<button class="nav-link" id="gameButton">Games</button>
</li>
</ul>
<form class="d-flex" action="" id="myform">
<input class="form-control me-2" type="password" name="password" id="pass" placeholder="password"> <input class="form-control me-2" type="password" name="password" id="pass" placeholder="password">
<input class="form-control me-2" type="text" name="movie_id" id="movie_id" placeholder="movie id"> <input class="form-control me-2" type="text" name="input_id" id="input_id" placeholder="id">
<input class="btn btn-outline-success" type="submit" value="Submit"> <input class="btn btn-outline-success" type="submit" value="Submit">
</form> </form>
</div> </div>
</nav> </div>
</nav>
</header>
<div class="py-5 bg-body-tertiary"> <main>
<div class="container" id="root"> <div class="py-5 bg-body-tertiary mt-3">
<div class="container" id="root">
</div>
</div> </div>
</div> </main>