Skip to content

Commit d1963aa

Browse files
committed
Continued TS conversion of backend
1 parent 56c6986 commit d1963aa

File tree

7 files changed

+68
-52
lines changed

7 files changed

+68
-52
lines changed

server/controllers/projectController.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Projects } from '../models/reactypeModels';
22
import { Request, Response, NextFunction } from 'express';
33

4+
// array of objects, objects inside
5+
type Projects = {project: {}}[]
6+
47
interface ProjectController {
58
saveProject: (req: Request, res: Response, next: NextFunction) => void;
69
getProjects: (req: Request, res: Response, next: NextFunction) => void;
@@ -43,7 +46,7 @@ saveProject: (req, res, next) => {
4346
// gets all of current user's projects
4447
getProjects: (req, res, next) => {
4548
const { userId } = req.body;
46-
Projects.find({ userId }, (err, projects) => {
49+
Projects.find({ userId }, (err, projects: Projects) => {
4750
if (err) {
4851
return next({
4952
log: `Error in projectController.getProjects: ${err}`,
@@ -62,7 +65,7 @@ getProjects: (req, res, next) => {
6265
deleteProject: (req, res, next) => {
6366
// pull project name and userId from req.body
6467
const { name, userId } = req.body;
65-
Projects.findOneAndDelete({ name, userId }, (err, deleted) => {
68+
Projects.findOneAndDelete({ name, userId }, null, (err, deleted) => {
6669
if (err) {
6770
return next({
6871
log: `Error in projectController.deleteProject: ${err}`,

server/controllers/userController.ts

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
import { Users } from '../models/reactypeModels';
33
import bcrypt from 'bcryptjs';
44
import { Request, Response, NextFunction } from 'express';
5+
import { NativeError } from 'mongoose';
6+
7+
interface newUserError extends NativeError {
8+
keyValue: {
9+
email: string;
10+
username: string;
11+
};
12+
}
513

614
type UserController = {
715
createUser: (req: Request, res: Response, next: NextFunction) => void;
@@ -53,33 +61,36 @@ const userController: UserController = {
5361
}
5462

5563
// create user using username and password
56-
Users.create({ username, password, email }, (err:any, newUser) => {
57-
// handle error of creating a new user
58-
if (err) {
59-
if (res.locals.signUpType === 'oauth') {
60-
return next();
61-
}
62-
if (err.keyValue.email) {
63-
return res.status(400).json('Email Taken');
64-
}
65-
if (err.keyValue.username && res.locals.signUpType === 'oauth') {
66-
res.locals.githubPassword = password;
67-
return next();
68-
}
69-
if (err.keyValue.username) {
70-
return res.status(400).json('Username Taken');
71-
}
72-
return next({
73-
log: `Error in userController.createUser: ${err}`,
74-
message: {
75-
err: 'Error in userController.createUser. Check server logs for details'
64+
Users.create(
65+
{ username, password, email },
66+
(err: newUserError, newUser) => {
67+
// handle error of creating a new user
68+
if (err) {
69+
if (res.locals.signUpType === 'oauth') {
70+
return next();
7671
}
77-
});
72+
if (err.keyValue.email) {
73+
return res.status(400).json('Email Taken');
74+
}
75+
if (err.keyValue.username && res.locals.signUpType === 'oauth') {
76+
res.locals.githubPassword = password;
77+
return next();
78+
}
79+
if (err.keyValue.username) {
80+
return res.status(400).json('Username Taken');
81+
}
82+
return next({
83+
log: `Error in userController.createUser: ${err}`,
84+
message: {
85+
err: 'Error in userController.createUser. Check server logs for details'
86+
}
87+
});
88+
}
89+
// if no error found when creating a new user, send back user ID in res.locals
90+
res.locals.id = newUser.id;
91+
return next();
7892
}
79-
// if no error found when creating a new user, send back user ID in res.locals
80-
res.locals.id = newUser.id;
81-
return next();
82-
});
93+
);
8394
},
8495

8596
// verifyUser - Obtain username and password from the request body, locate
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import fs from 'fs';
22
import path from 'path';
3+
import { Request, Response, NextFunction } from 'express';
34

4-
const userStylesController = {};
5-
// Rewrite file
6-
userStylesController.saveCssFile = (req, res, next) => {
7-
const newText = req.body.data;
8-
fs.writeFile(
9-
path.join(__dirname, '../assets/renderDemo.css'),
10-
newText,
11-
'utf-8',
12-
(err, data) => {
13-
if (err) throw err;
14-
next();
15-
}
16-
);
5+
const userStylesController = {
6+
// Rewrite file
7+
saveCssFile: (req: Request, res: Response, next: NextFunction) => {
8+
const newText = req.body.data;
9+
fs.writeFile(
10+
path.join(__dirname, '../assets/renderDemo.css'),
11+
newText,
12+
'utf-8',
13+
(err) => {
14+
if (err) throw err;
15+
next();
16+
}
17+
);
18+
}
1719
};
1820

1921
export default userStylesController;

server/graphQL/resolvers/mutation.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
// const { ApolloServerErrorCode.BAD_USER_INPUT } = require('apollo-server-express');
2-
3-
import { ApolloServerErrorCode } from '@apollo/server/errors'; // v4 syntax
4-
5-
//now using ApolloServerErrorCode.BAD_USER_INPUT in place of ApolloServerErrorCode.BAD_USER_INPUT
1+
const { ApolloServerErrorCode } = require('@apollo/server/errors');
62

73
import { Projects, Users, Comments } from '../../models/reactypeModels';
84

5+
import { Document } from 'mongoose';
6+
97
/*
108
* resolvers are functions that handles graphQL requests. This file defines the logic for graphQL mutation requests
119
* Link to Apollo Mutations:

server/graphQL/resolvers/query.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// const { UserInputError } = require('apollo-server-express');//v3 syntax
2-
3-
import { ApolloServerErrorCode } from '@apollo/server/errors'; // v4 syntax
2+
const { ApolloServerErrorCode } = require('@apollo/server/errors');
3+
// import { ApolloServerErrorCode } from '@apollo/server/errors'; // v4 syntax
44
// const ApolloServerErrorCode = require('@apollo/server/errors')
55
//now using ApolloServerErrorCode.BAD_USER_INPUT in place of UserInputError
66

server/models/reactypeModels.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import dotenv from 'dotenv';
1313
import bcrypt from 'bcryptjs';
1414
dotenv.config();
1515
const Schema = mongoose.Schema;
16+
import { Document } from 'mongoose';
17+
18+
interface UserDocument extends Document {
19+
password: string;
20+
}
1621

1722
const mongoURI = process.env.MONGO_DB;
1823
const URI =
@@ -44,16 +49,14 @@ const userSchema = new Schema({
4449
// collection happens (user gets put into database)
4550

4651
// cannot use arrow function here as context of 'this' is important
47-
userSchema.pre('save', function cb(next) {
52+
userSchema.pre<UserDocument>('save', function cb(next) {
4853
// within this context, 'this' refers to the document (new user) about to be saved,
4954
// in our case, it should have properties username, password, and projects array
5055
bcrypt.hash(this.password, SALT_WORK_FACTOR, (err, hash) => {
5156
if (err) {
5257
return next({
5358
log: `bcrypt password hashing error: ${err}`,
54-
message: {
55-
err: 'bcrypt hash error: check server logs for details.'
56-
}
59+
message: { err: 'bcrypt hash error: check server logs for details.' }
5760
});
5861
}
5962
this.password = hash;

server/server.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ const schema = makeExecutableSchema({ typeDefs, resolvers });
146146

147147
// //v4 syntax
148148

149-
150149
// await server.start();
151150
// app.use(
152151
// '/graphql',

0 commit comments

Comments
 (0)