Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
planner-team-one
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
21
Issues
21
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Евгений Положенцев
planner-team-one
Commits
c741d378
Commit
c741d378
authored
Nov 02, 2022
by
“Yevgeniy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#8
added postgres sql connection code that wont work
parent
bdf3f7b2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
2598 additions
and
1169 deletions
+2598
-1169
package-lock.json
planner-api/package-lock.json
+2287
-1104
package.json
planner-api/package.json
+4
-1
app-data-source.ts
planner-api/src/app-data-source.ts
+13
-0
User.ts
planner-api/src/models/User.ts
+228
-36
users.ts
planner-api/src/routers/users.ts
+52
-20
server.ts
planner-api/src/server.ts
+13
-8
tsconfig.json
planner-api/tsconfig.json
+1
-0
No files found.
planner-api/package-lock.json
View file @
c741d378
This diff is collapsed.
Click to expand it.
planner-api/package.json
View file @
c741d378
...
...
@@ -29,6 +29,9 @@
"cors"
:
"^2.8.5"
,
"express"
:
"^4.18.2"
,
"mongoose"
:
"^6.7.0"
,
"nanoid"
:
"^3.3.4"
"nanoid"
:
"^3.3.4"
,
"pg"
:
"^8.8.0"
,
"reflect-metadata"
:
"^0.1.13"
,
"typeorm"
:
"^0.3.10"
}
}
planner-api/src/app-data-source.ts
0 → 100644
View file @
c741d378
import
{
DataSource
}
from
"typeorm"
export
const
myDataSource
=
new
DataSource
({
type
:
"postgres"
,
host
:
"localhost"
,
port
:
3308
,
username
:
"test"
,
password
:
"test"
,
database
:
"planner"
,
entities
:
[
"./models/*.ts"
],
logging
:
true
,
synchronize
:
true
,
})
\ No newline at end of file
planner-api/src/models/User.ts
View file @
c741d378
import
{
Schema
,
model
}
from
'mongoose'
;
import
bcrypt
from
'bcrypt'
;
import
{
Column
,
Entity
,
PrimaryGeneratedColumn
,
CreateDateColumn
,
UpdateDateColumn
,
BeforeInsert
,
BeforeUpdate
,
BaseEntity
,
JoinColumn
,
ManyToOne
}
from
'typeorm'
;
import
{
nanoid
}
from
'nanoid'
;
import
bcrypt
from
'bcrypt'
;
const
SALT_WORK_FACTOR
=
10
;
// 1. Create an interface representing a document in MongoDB.
// enum UserRole {
// DIRECTOR = "director",
// WORKER = "worker",
// }
type
UserRoleType
=
"worker"
|
"director"
;
interface
IUser
{
id
:
string
;
name
:
string
;
surname
:
string
;
email
:
string
;
displayName
:
string
;
password
:
string
;
role
:
string
;
role
:
UserRoleType
;
// createdAt: Date;
}
// 2. Create a Schema corresponding to the document interface.
const
UserSchema
=
new
Schema
<
IUser
>
({
name
:
{
type
:
String
,
required
:
true
},
surname
:
{
type
:
String
,
required
:
true
},
email
:
{
type
:
String
,
required
:
true
},
displayName
:
{
type
:
String
,
required
:
true
},
password
:
{
type
:
String
,
required
:
true
},
role
:{
type
:
String
,
default
:
'user'
,
enum
:[
'user'
,
'admin'
]}
});
// How does next's type defined?
UserSchema
.
pre
(
'save'
,
async
function
(
next
:
any
):
Promise
<
void
>
{
if
(
!
this
.
isModified
(
'password'
))
return
next
();
console
.
log
(
'next'
,
next
)
const
salt
=
await
bcrypt
.
genSalt
(
SALT_WORK_FACTOR
);
const
hash
=
await
bcrypt
.
hash
(
this
.
password
,
salt
);
this
.
password
=
hash
;
next
();
@
Entity
()
export
class
User
extends
BaseEntity
implements
IUser
{
@
PrimaryGeneratedColumn
(
'uuid'
)
id
!
:
string
@
Column
({
name
:
'first_name'
,
type
:
'varchar'
,
length
:
20
,
nullable
:
false
})
name
!
:
string
@
Column
({
name
:
'last_name'
,
type
:
'varchar'
,
length
:
30
,
nullable
:
false
})
surname
!
:
string
@
Column
({
name
:
'last_name'
,
type
:
'varchar'
,
length
:
30
,
nullable
:
false
})
displayName
!
:
string
@
Column
({
type
:
'varchar'
,
length
:
20
,
unique
:
true
,
nullable
:
false
})
email
!
:
string
@
Column
({
type
:
'varchar'
,
length
:
10
,
unique
:
true
})
phone
!
:
string
@
Column
({
type
:
'varchar'
,
nullable
:
false
})
password
!
:
string
@
CreateDateColumn
()
// @CreateDateColumn({ name: 'created_at', type: Date, default: new Date() })
// createdAt: Date
@
Column
({
type
:
"enum"
,
enum
:
[
"worker"
,
"director"
],
default
:
"ghost"
})
role
!
:
UserRoleType
UserSchema
.
set
(
'toJSON'
,{
transform
:(
doc
:
any
,
ret
:
any
,
options
:
any
)
=>
{
delete
ret
.
password
;
return
ret
;
@
BeforeInsert
()
protected
async
beforeInserthashPassword
():
Promise
<
void
>
{
const
salt
=
await
bcrypt
.
genSalt
(
SALT_WORK_FACTOR
);
this
.
password
=
await
bcrypt
.
hash
(
this
.
password
,
salt
);
}
})
UserSchema
.
methods
.
checkPassword
=
function
(
password
:
string
):
Promise
<
boolean
>
{
return
bcrypt
.
compare
(
password
,
this
.
password
);
}
@
BeforeUpdate
()
protected
async
beforeUpdateHashPassword
():
Promise
<
void
>
{
const
salt
=
await
bcrypt
.
genSalt
(
SALT_WORK_FACTOR
);
this
.
password
=
await
bcrypt
.
hash
(
this
.
password
,
salt
);
}
UserSchema
.
methods
.
generateToken
=
function
(){
this
.
token
=
nanoid
();
}
// 3. Create a Model.
const
User
=
model
<
IUser
>
(
'User'
,
UserSchema
);
export
default
User
;
// const UserEntity = new EntitySchema<IUser>({
// name: "user",
// columns: {
// id: {
// type: "uuid",
// primary: true,
// generated: "uuid",
// },
// name: {
// nullable: false,
// type: String,
// length: 20,
// },
// surname: {
// nullable: false,
// type: String,
// length: 50,
// },
// password: {
// nullable: false,
// type: String,
// },
// email: {
// nullable: false,
// type: String,
// },
// displayName: {
// nullable: false,
// type: String,
// },
// role: {
// type: String,
// default: "user",
// enum: ["director","user"]
// },
// createdAt: {
// type: Date,
// default: Date.now(),
// },
// methods?:{
// type:String
// }
// },
// relations: {
// tasks: {
// type: "many-to-many",
// target: "category", // CategoryEntity
// },
// },
// })
// UserEntity.methods.checkPassword=function(password:string):Promise<boolean> {
// return bcrypt.compare(password,this.password);
// }
// export default UserEntity
// import { Schema, model } from 'mongoose';
// import bcrypt from 'bcrypt';
// import {nanoid} from 'nanoid';
// const SALT_WORK_FACTOR= 10;
// // 1. Create an interface representing a document in MongoDB.
// interface IUser {
// name: string;
// surname: string;
// email: string;
// displayName: string;
// password:string;
// role: string;
// }
// // 2. Create a Schema corresponding to the document interface.
// const UserSchema = new Schema<IUser>({
// name: { type: String, required: true },
// surname: { type: String, required: true },
// email: { type: String, required: true },
// displayName: { type: String, required: true },
// password: { type: String, required: true },
// role:{type:String,default: 'user',enum:['user','admin']}
// });
// // How does next's type defined?
// UserSchema.pre('save', async function(next:any):Promise<void>{
// if(!this.isModified('password')) return next();
// console.log('next', next)
// const salt = await bcrypt.genSalt(SALT_WORK_FACTOR);
// const hash = await bcrypt.hash(this.password, salt);
// this.password = hash;
// next();
// })
// UserSchema.set('toJSON',{
// transform:(doc:any, ret:any, options:any)=>{
// delete ret.password;
// return ret;
// }
// })
// UserSchema.methods.checkPassword =function(password:string):Promise<boolean>{
// return bcrypt.compare(password,this.password);
// }
// UserSchema.methods.generateToken =function(){
// this.token=nanoid();
// }
// // 3. Create a Model.
// const User = model<IUser>('User', UserSchema);
// export default User;
planner-api/src/routers/users.ts
View file @
c741d378
import
express
,{
Router
,
Request
,
Response
}
from
'express'
;
import
User
from
'../models/User'
;
import
{
User
}
from
'../models/User'
;
// import {DataSource} from 'typeorm';
import
{
myDataSource
}
from
'../app-data-source'
;
const
router
:
Router
=
express
.
Router
();
const
dataSource
=
myDataSource
;
router
.
post
(
'/'
,
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
router
.
get
(
'/'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
const
users
:
any
=
await
dataSource
.
manager
.
find
(
User
)
return
res
.
send
({
users
})
})
router
.
post
(
'/'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
object
>
=>
{
const
{
name
,
surname
,
password
,
email
}
=
req
.
body
;
const
displayName
=
surname
+
' '
+
name
[
0
]
+
'.'
const
user
=
new
User
({
name
:
name
,
surname
:
surname
,
email
:
email
,
password
:
password
,
displayName
:
displayName
})
const
user
=
new
User
();
user
.
name
=
name
;
user
.
surname
=
surname
;
user
.
password
=
password
;
user
.
displayName
=
displayName
;
user
.
email
=
email
;
await
user
.
save
();
return
res
.
send
({
user
})
}
catch
(
e
){
if
(
e
instanceof
Error
){
return
res
.
status
(
404
).
send
({
'message'
:
e
.
message
});
}
return
res
.
status
(
500
).
send
({
'message'
:
'Broke server'
});
}
})
router
.
get
(
'/'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
Response
<
any
,
Record
<
string
,
any
>>>
=>
{
const
users
=
await
User
.
find
()
return
res
.
send
({
users
})
})
// router.post('/', async (req : Request, res : Response) => {
// try{
// const {name,surname,password,email} = req.body;
// const displayName = surname+' '+name[0]+'.'
// await User.insert({
// name:name,
// surname:surname,
// email:email,
// password:password,
// displayName:displayName
// })
// const user:User = new User({
// name:name,
// surname:surname,
// email:email,
// password:password,
// displayName:displayName
// })
// await user.save();
// return res.send({message:'created user'})
// }catch(e ){
// if (e instanceof Error){
// return res.status(404).send({'message':e.message});
// }
// return res.status(500).send({'message':'Broke server'});
// }
// })
// router.get('/', async (req : Request, res : Response):Promise<Response<any, Record<string, any>>> => {
// const users = await User.find()
// return res.send({users})
// })
export
default
router
;
planner-api/src/server.ts
View file @
c741d378
import
express
,
{
Express
}
from
'express'
;
import
cors
from
'cors'
;
import
mongoose
from
'mongoose'
;
import
users
from
'./routers/users'
;
import
{
myDataSource
}
from
'./app-data-source'
;
console
.
log
(
'Hello world!'
);
myDataSource
.
initialize
()
.
then
(()
=>
{
console
.
log
(
"Data Source has been initialized!"
)
})
.
catch
((
err
)
=>
{
console
.
error
(
"Error during Data Source initialization:"
,
err
)
})
const
app
:
Express
=
express
();
app
.
use
(
express
.
static
(
'public'
));
...
...
@@ -13,16 +21,13 @@ const PORT = 8000;
app
.
use
(
'/users'
,
users
)
const
run
=
async
()
=>
{
mongoose
.
connect
(
`mongodb://localhost:27017/task-planner`
);
app
.
listen
(
PORT
,
()
=>
{
console
.
log
(
`Server started at http://localhost:
${
PORT
}
/`
);
})
process
.
on
(
"exit"
,
()
=>
{
mongoose
.
disconnect
();
})
}
console
.
log
(
'Hello world!'
);
run
().
catch
(
console
.
error
);
\ No newline at end of file
planner-api/tsconfig.json
View file @
c741d378
{
"compilerOptions"
:
{
"experimentalDecorators"
:
true
,
/*
Visit
https
:
//aka.ms/tsconfig
to
read
more
about
this
file
*/
/*
Projects
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment