Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
I
initial_project
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
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
Нұрасыл Қайратұлы
initial_project
Commits
7a06930d
Commit
7a06930d
authored
Aug 09, 2024
by
Нұрасыл Қайратұлы
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
3144fc0a
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
541 additions
and
22 deletions
+541
-22
package-lock.json
server/package-lock.json
+362
-19
package.json
server/package.json
+4
-0
appDataSource.ts
server/src/appDataSource.ts
+2
-1
user.controller.ts
server/src/controllers/user.controller.ts
+30
-0
registration-user.dto.ts
server/src/dto/registration-user.dto.ts
+18
-0
sign-in-user.dto.ts
server/src/dto/sign-in-user.dto.ts
+14
-0
user.entity.ts
server/src/entities/user.entity.ts
+29
-0
index.ts
server/src/index.ts
+2
-1
IUser.interface.ts
server/src/interfaces/IUser.interface.ts
+6
-0
user.repository.ts
server/src/repositories/user.repository.ts
+42
-0
user.route.ts
server/src/routes/user.route.ts
+19
-0
category.service.ts
server/src/services/category.service.ts
+0
-1
user.service.ts
server/src/services/user.service.ts
+13
-0
No files found.
server/package-lock.json
View file @
7a06930d
This diff is collapsed.
Click to expand it.
server/package.json
View file @
7a06930d
...
...
@@ -13,11 +13,14 @@
"author"
:
""
,
"license"
:
"ISC"
,
"dependencies"
:
{
"@types/lodash"
:
"^4.17.7"
,
"@types/multer"
:
"^1.4.11"
,
"bcrypt"
:
"^5.1.1"
,
"class-transformer"
:
"^0.5.1"
,
"class-validator"
:
"^0.14.1"
,
"cors"
:
"^2.8.5"
,
"express"
:
"^4.18.2"
,
"lodash"
:
"^4.17.21"
,
"multer"
:
"^1.4.5-lts.1"
,
"mysql"
:
"^2.18.1"
,
"nanoid"
:
"^3.3.6"
,
...
...
@@ -31,6 +34,7 @@
"typescript"
:
"^5.1.6"
},
"devDependencies"
:
{
"@types/bcrypt"
:
"^5.0.2"
,
"@types/cors"
:
"^2.8.17"
,
"@types/express"
:
"^4.17.17"
,
"@types/node"
:
"^20.4.2"
,
...
...
server/src/appDataSource.ts
View file @
7a06930d
import
{
DataSource
}
from
"typeorm"
;
import
{
Product
}
from
"./entities/product.entity"
;
import
{
Category
}
from
"./entities/category.entity"
;
import
{
User
}
from
"./entities/user.entity"
;
export
const
AppDataSource
=
new
DataSource
({
type
:
"postgres"
,
...
...
@@ -11,5 +12,5 @@ export const AppDataSource = new DataSource({
database
:
"postgres"
,
schema
:
"public"
,
synchronize
:
true
,
entities
:
[
Product
,
Category
]
entities
:
[
Product
,
Category
,
User
]
})
\ No newline at end of file
server/src/controllers/user.controller.ts
0 → 100644
View file @
7a06930d
import
{
UserService
}
from
"@/services/user.service"
;
import
{
RequestHandler
}
from
"express"
;
import
{
plainToInstance
}
from
'class-transformer'
;
// import { SignInUserDto } from "@/dto/sign-in-user.dto";
import
{
RegistrationUserDto
}
from
"@/dto/registration-user.dto"
;
export
class
UserController
{
private
service
:
UserService
;
constructor
()
{
this
.
service
=
new
UserService
();
}
signIn
:
RequestHandler
=
async
(
req
,
res
):
Promise
<
void
>
=>
{
res
.
send
(
req
.
headers
.
authorization
)
// try {
// const signInDto = plainToInstance(SignInUserDto, req.body)
// const user = await this.service.signIn(signInDto)
// res.send(user)
// } catch (e) {
// res.status(401).send((e as Error).message)
// }
}
registration
:
RequestHandler
=
async
(
req
,
res
):
Promise
<
void
>
=>
{
const
registrationInDto
=
plainToInstance
(
RegistrationUserDto
,
req
.
body
)
const
user
=
await
this
.
service
.
registration
(
registrationInDto
)
res
.
send
(
user
)
}
}
server/src/dto/registration-user.dto.ts
0 → 100644
View file @
7a06930d
import
{
Expose
}
from
'class-transformer'
;
import
{
IsNotEmpty
,
IsString
,
IsOptional
}
from
'class-validator'
;
export
class
RegistrationUserDto
{
@
Expose
()
@
IsNotEmpty
({
message
:
'Логин не должен быть пустым'
})
@
IsString
({
message
:
'Логин должен быть строкой'
})
username
!
:
string
;
@
Expose
()
@
IsNotEmpty
({
message
:
'Пароль не должен быть пустым'
})
@
IsString
({
message
:
'Пароль должен быть строкой'
})
password
!
:
string
;
@
Expose
()
@
IsOptional
()
displayName
?:
string
}
server/src/dto/sign-in-user.dto.ts
0 → 100644
View file @
7a06930d
import
{
Expose
}
from
'class-transformer'
;
import
{
IsNotEmpty
,
IsString
}
from
'class-validator'
;
export
class
SignInUserDto
{
@
Expose
()
@
IsNotEmpty
({
message
:
'Логин не должен быть пустым'
})
@
IsString
({
message
:
'Логин должен быть строкой'
})
username
!
:
string
;
@
Expose
()
@
IsNotEmpty
({
message
:
'Пароль не должен быть пустым'
})
@
IsString
({
message
:
'Пароль должен быть строкой'
})
password
!
:
string
;
}
server/src/entities/user.entity.ts
0 → 100644
View file @
7a06930d
import
{
Entity
,
PrimaryGeneratedColumn
,
Column
,
Unique
}
from
"typeorm"
;
import
bcrypt
from
'bcrypt'
;
@
Entity
()
@
Unique
([
'username'
])
export
class
User
{
@
PrimaryGeneratedColumn
()
id
!
:
number
;
@
Column
()
username
!
:
string
@
Column
()
password
!
:
string
@
Column
({
nullable
:
true
})
token
!
:
string
@
Column
({
nullable
:
true
})
displayName
!
:
string
async
comparePassword
(
password
:
string
):
Promise
<
boolean
>
{
return
await
bcrypt
.
compare
(
password
,
this
.
password
);
}
generateToken
()
{
this
.
token
=
crypto
.
randomUUID
();
}
}
\ No newline at end of file
server/src/index.ts
View file @
7a06930d
...
...
@@ -4,11 +4,12 @@ import App from './app';
import
logger
from
'./middlewares/logger'
;
import
{
ArticleRoute
}
from
'./routes/article.route'
;
import
{
ProductRoute
}
from
'./routes/product.route'
;
import
{
UserRoute
}
from
'./routes/user.route'
;
const
app
=
new
App
({
port
:
8000
,
middlewares
:
[
logger
(),
cors
()],
controllers
:
[
new
ArticleRoute
(),
new
ProductRoute
(),
new
CategoryRoute
()],
controllers
:
[
new
ArticleRoute
(),
new
ProductRoute
(),
new
CategoryRoute
()
,
new
UserRoute
()
],
});
app
.
listen
();
server/src/interfaces/IUser.interface.ts
0 → 100644
View file @
7a06930d
export
interface
IUser
{
id
:
number
username
:
string
password
?:
string
displayName
?:
string
}
\ No newline at end of file
server/src/repositories/user.repository.ts
0 → 100644
View file @
7a06930d
import
{
AppDataSource
}
from
"@/appDataSource"
;
import
{
Repository
}
from
"typeorm"
;
import
{
User
}
from
"@/entities/user.entity"
;
import
{
IUser
}
from
"@/interfaces/IUser.interface"
;
import
{
SignInUserDto
}
from
"@/dto/sign-in-user.dto"
;
import
{
RegistrationUserDto
}
from
"@/dto/registration-user.dto"
;
import
bcrypt
from
'bcrypt'
;
import
_
from
"lodash"
;
const
SALT_WORK_FACTOR
=
10
;
export
class
UserRepo
{
private
repo
:
Repository
<
User
>
constructor
()
{
this
.
repo
=
AppDataSource
.
getRepository
(
User
)
}
async
signIn
(
signInUserDto
:
SignInUserDto
):
Promise
<
IUser
>
{
const
userData
=
await
this
.
repo
.
findOne
({
where
:
{
username
:
signInUserDto
.
username
}})
if
(
!
userData
)
throw
new
Error
(
'Invalid username or password'
)
const
isCorrect
=
await
userData
.
comparePassword
(
signInUserDto
.
password
)
if
(
!
isCorrect
)
throw
new
Error
(
'Invalid username or password'
)
userData
.
generateToken
()
const
user
=
await
this
.
repo
.
save
(
userData
)
const
userWithoutPass
=
_
.
omit
(
user
,
'password'
)
return
userWithoutPass
}
async
registration
(
registrationUserDto
:
RegistrationUserDto
):
Promise
<
IUser
>
{
const
salt
=
await
bcrypt
.
genSalt
(
SALT_WORK_FACTOR
)
registrationUserDto
.
password
=
await
bcrypt
.
hash
(
registrationUserDto
.
password
,
salt
)
const
user
=
await
this
.
repo
.
save
(
registrationUserDto
)
const
userWithoutPass
=
_
.
omit
(
user
,
'password'
)
return
userWithoutPass
}
}
export
const
userRepo
=
new
UserRepo
()
\ No newline at end of file
server/src/routes/user.route.ts
0 → 100644
View file @
7a06930d
import
{
Router
}
from
'express'
;
import
{
IRoute
}
from
'../interfaces/IRoute.interface'
;
import
{
UserController
}
from
'@/controllers/user.controller'
;
export
class
UserRoute
implements
IRoute
{
public
path
=
'/user'
;
public
router
=
Router
();
private
controller
:
UserController
;
constructor
()
{
this
.
controller
=
new
UserController
();
this
.
init
();
}
private
init
()
{
this
.
router
.
post
(
'/registration'
,
this
.
controller
.
registration
);
this
.
router
.
post
(
'/signIn'
,
this
.
controller
.
signIn
);
}
}
server/src/services/category.service.ts
View file @
7a06930d
...
...
@@ -2,7 +2,6 @@ import { CategoryDto } from "@/dto/category.dto";
import
{
Category
}
from
"@/entities/category.entity"
;
import
{
categoryRepo
}
from
"@/repositories/category.repository"
;
export
class
CategoryService
{
getAllCategories
=
async
():
Promise
<
Category
[]
>
=>
{
return
await
categoryRepo
.
getAll
()
...
...
server/src/services/user.service.ts
0 → 100644
View file @
7a06930d
import
{
SignInUserDto
}
from
"@/dto/sign-in-user.dto"
import
{
RegistrationUserDto
}
from
"@/dto/registration-user.dto"
import
{
userRepo
}
from
"@/repositories/user.repository"
export
class
UserService
{
async
signIn
(
signInUserDto
:
SignInUserDto
)
{
return
await
userRepo
.
signIn
(
signInUserDto
)
}
async
registration
(
registrationUserDto
:
RegistrationUserDto
)
{
return
await
userRepo
.
registration
(
registrationUserDto
)
}
}
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