Skip to content

Commit

Permalink
Merge pull request #8 from Devvyky/devvyky-cache-service
Browse files Browse the repository at this point in the history
clean ups
  • Loading branch information
Devvyky authored Aug 4, 2022
2 parents ab0b365 + 49062ef commit a442474
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 63 deletions.
44 changes: 22 additions & 22 deletions src/fixtures/controllers/fixturesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import logger from '../../logger';
import catchAsync from '../../utils/catchAsync';
import { signToken } from '../../utils/signToken';
import { Fixture } from '../interfaces';
import { createFixture } from '../services/fixturesService';
import { createFixture, findOne } from '../services/fixturesService';

export const createFixtures = catchAsync(
async (req: Request, res: Response, next: NextFunction): Promise<void> => {
Expand Down Expand Up @@ -37,29 +37,29 @@ export const createFixtures = catchAsync(
}
);

// export const findTeamById = catchAsync(
// async (req: Request, res: Response, next: NextFunction): Promise<void> => {
// try {
// const { id } = req.params;
export const findFixtureById = catchAsync(
async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
const { id } = req.params;

// const data = await findOne(id);
const data = await findOne(id);

// res.status(200).json({
// message: 'Team fetched successfully',
// status: 'success',
// data,
// });
// } catch (error: any) {
// logger.error(
// `Error occurred while fetching team: ${JSON.stringify(error)}`
// );
// res.status(error.statusCode || 500).json({
// status: error.status || 'error',
// message: error.message,
// });
// }
// }
// );
res.status(200).json({
message: 'Fixture fetched successfully',
status: 'success',
data,
});
} catch (error: any) {
logger.error(
`Error occurred while fetching fixtures: ${JSON.stringify(error)}`
);
res.status(error.statusCode || 500).json({
status: error.status || 'error',
message: error.message,
});
}
}
);

// export const updateTeam = catchAsync(
// async (req: Request, res: Response, next: NextFunction): Promise<void> => {
Expand Down
10 changes: 2 additions & 8 deletions src/fixtures/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { Document } from 'mongoose';

export interface Fixture extends Document {
home: {
team: string;
score?: number;
};
away: {
team: string;
score?: number;
};
home: string;
away: number;
link?: string;
readonly createdBy: string;
readonly createdAt?: Date;
Expand Down
25 changes: 7 additions & 18 deletions src/fixtures/models/fixturesModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,15 @@ import { configuration } from '../../../config/default';
const fixtureSchema = new Schema(
{
home: {
team: {
type: Schema.Types.ObjectId,
ref: 'Team',
required: true,
},
score: {
type: Number,
default: 0,
},
type: Schema.Types.ObjectId,
ref: 'Team',
required: true,
},

away: {
team: {
type: Schema.Types.ObjectId,
ref: 'Team',
required: true,
},
score: {
type: Number,
default: 0,
},
type: Schema.Types.ObjectId,
ref: 'Team',
required: true,
},
status: {
type: String,
Expand Down
19 changes: 9 additions & 10 deletions src/routes/fixtureRouter.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import express, { Router } from 'express';
import { createFixtures } from '../fixtures/controllers/fixturesController';
import { protect, restrictTo } from '../middlewares';

import {
createTeam,
findTeamAllTeams,
findTeamById,
updateTeam,
} from '../team/controllers/teamController';
createFixtures,
findFixtureById,
} from '../fixtures/controllers/fixturesController';
import { protect, restrictTo } from '../middlewares';

const router = express.Router();

router.use(protect, restrictTo('admin'));
router.use(protect);

router.route('/').post(createFixtures);
router
.route('/')
.post(restrictTo('admin'), createFixtures)
.get(restrictTo('admin', 'user'), findFixtureById);
// .get(findTeamAllTeams);
// router.route('/:id').get(findTeamById).patch(updateTeam);

Expand Down
12 changes: 9 additions & 3 deletions src/routes/teamRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ import {

const router = express.Router();

router.use(protect, restrictTo('admin'));
router.use(protect);

router.route('/').post(cleanCache, createTeam).get(findTeamAllTeams);
router.route('/:id').get(findTeamById).patch(updateTeam);
router
.route('/')
.post(restrictTo('admin'), cleanCache, createTeam)
.get(restrictTo('admin', 'user'), findTeamAllTeams);
router
.route('/:id')
.get(restrictTo('admin', 'user'), findTeamById)
.patch(restrictTo('admin'), updateTeam);

export default router;
2 changes: 1 addition & 1 deletion src/routes/userRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ const router = express.Router();
router.post('/signup', userSignup);
router.post('/login', userLogin);

router.get('/', searchTeams)
router.get('/', searchTeams);

export default router;
10 changes: 9 additions & 1 deletion src/user/controllers/authController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { NextFunction, Request, Response } from 'express';
import * as _ from 'lodash';
import { omit } from 'lodash';
// import { omit } from 'lodash';

import logger from '../../logger';
import catchAsync from '../../utils/catchAsync';
Expand Down Expand Up @@ -46,6 +48,12 @@ export const userLogin = catchAsync(

const user = await login(payload);

const data = {
...omit(user, 'passsword'),
};

console.log(data);

// req.session.user = {
// user: user.id,
// valid: true,
Expand All @@ -58,7 +66,7 @@ export const userLogin = catchAsync(
message: 'User login successful',
status: 'success',
token,
data: user,
data,
});
} catch (error: any) {
logger.error(`Error logging: ${error}`);
Expand Down

0 comments on commit a442474

Please sign in to comment.