Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
ajs_22_shop_api
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
Нұрасыл Қайратұлы
ajs_22_shop_api
Commits
6edad83d
Commit
6edad83d
authored
Sep 02, 2025
by
Нұрасыл Қайратұлы
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add classwork-87
parent
dd77e9f7
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
680 additions
and
37 deletions
+680
-37
.env.example
server/.env.example
+6
-0
package.json
server/package.json
+5
-1
pnpm-lock.yaml
server/pnpm-lock.yaml
+450
-0
app.module.ts
server/src/app.module.ts
+25
-1
categories.controller.ts
server/src/categories/categories.controller.ts
+45
-0
categories.module.ts
server/src/categories/categories.module.ts
+13
-0
categories.service.ts
server/src/categories/categories.service.ts
+42
-0
create-category.dto.ts
server/src/categories/dto/create-category.dto.ts
+10
-0
update-category.dto.ts
server/src/categories/dto/update-category.dto.ts
+4
-0
category.entity.ts
server/src/categories/entities/category.entity.ts
+17
-0
create-product.dto.ts
server/src/products/dto/create-product.dto.ts
+4
-0
product.ts
server/src/products/entity/product.ts
+23
-0
products.controller.ts
server/src/products/products.controller.ts
+1
-1
products.module.ts
server/src/products/products.module.ts
+6
-1
products.service.ts
server/src/products/products.service.ts
+29
-33
No files found.
server/.env.example
0 → 100644
View file @
6edad83d
DB_HOST=
DB_PORT=
DB_USER=
DB_PASS=
DB_NAME=
PORT=
server/package.json
View file @
6edad83d
...
@@ -21,15 +21,19 @@
...
@@ -21,15 +21,19 @@
},
},
"dependencies"
:
{
"dependencies"
:
{
"@nestjs/common"
:
"^11.0.1"
,
"@nestjs/common"
:
"^11.0.1"
,
"@nestjs/config"
:
"^4.0.2"
,
"@nestjs/core"
:
"^11.0.1"
,
"@nestjs/core"
:
"^11.0.1"
,
"@nestjs/mapped-types"
:
"*"
,
"@nestjs/mapped-types"
:
"*"
,
"@nestjs/platform-express"
:
"^11.0.1"
,
"@nestjs/platform-express"
:
"^11.0.1"
,
"@nestjs/serve-static"
:
"^5.0.3"
,
"@nestjs/serve-static"
:
"^5.0.3"
,
"@nestjs/typeorm"
:
"^11.0.0"
,
"class-transformer"
:
"^0.5.1"
,
"class-transformer"
:
"^0.5.1"
,
"class-validator"
:
"^0.14.2"
,
"class-validator"
:
"^0.14.2"
,
"multer"
:
"^2.0.2"
,
"multer"
:
"^2.0.2"
,
"pg"
:
"^8.16.3"
,
"reflect-metadata"
:
"^0.2.2"
,
"reflect-metadata"
:
"^0.2.2"
,
"rxjs"
:
"^7.8.1"
"rxjs"
:
"^7.8.1"
,
"typeorm"
:
"^0.3.26"
},
},
"devDependencies"
:
{
"devDependencies"
:
{
"@eslint/eslintrc"
:
"^3.2.0"
,
"@eslint/eslintrc"
:
"^3.2.0"
,
...
...
server/pnpm-lock.yaml
View file @
6edad83d
This diff is collapsed.
Click to expand it.
server/src/app.module.ts
View file @
6edad83d
import
{
Module
}
from
'@nestjs/common'
;
import
{
Module
}
from
'@nestjs/common'
;
import
{
ConfigModule
,
ConfigService
}
from
'@nestjs/config'
;
import
{
TypeOrmModule
}
from
'@nestjs/typeorm'
;
import
{
ProductsModule
}
from
'./products/products.module'
;
import
{
ProductsModule
}
from
'./products/products.module'
;
import
{
CategoriesModule
}
from
'./categories/categories.module'
;
@
Module
({
@
Module
({
imports
:
[
ProductsModule
],
imports
:
[
ConfigModule
.
forRoot
({
isGlobal
:
true
,
envFilePath
:
'.env'
,
}),
TypeOrmModule
.
forRootAsync
({
imports
:
[
ConfigModule
],
inject
:
[
ConfigService
],
useFactory
:
(
configService
:
ConfigService
)
=>
({
type
:
'postgres'
,
host
:
configService
.
get
<
string
>
(
'DB_HOST'
),
port
:
configService
.
get
<
number
>
(
'DB_PORT'
),
username
:
configService
.
get
<
string
>
(
'DB_USER'
),
password
:
configService
.
get
<
string
>
(
'DB_PASS'
),
database
:
configService
.
get
<
string
>
(
'DB_NAME'
),
autoLoadEntities
:
true
,
synchronize
:
true
,
}),
}),
ProductsModule
,
CategoriesModule
,
],
controllers
:
[],
controllers
:
[],
providers
:
[],
providers
:
[],
})
})
...
...
server/src/categories/categories.controller.ts
0 → 100644
View file @
6edad83d
import
{
Body
,
Controller
,
Delete
,
Get
,
Param
,
Post
,
Put
,
}
from
'@nestjs/common'
;
import
{
CategoriesService
}
from
'./categories.service'
;
import
{
CreateCategoryDto
}
from
'./dto/create-category.dto'
;
import
{
UpdateCategoryDto
}
from
'./dto/update-category.dto'
;
@
Controller
(
'categories'
)
export
class
CategoriesController
{
constructor
(
private
readonly
categoriesService
:
CategoriesService
)
{}
@
Post
()
create
(@
Body
()
createCategoryDto
:
CreateCategoryDto
)
{
return
this
.
categoriesService
.
create
(
createCategoryDto
);
}
@
Get
()
findAll
()
{
return
this
.
categoriesService
.
findAll
();
}
@
Get
(
':id'
)
findOne
(@
Param
(
'id'
)
id
:
string
)
{
return
this
.
categoriesService
.
findOne
(
+
id
);
}
@
Put
(
':id'
)
update
(
@
Param
(
'id'
)
id
:
string
,
@
Body
()
updateCategoryDto
:
UpdateCategoryDto
,
)
{
return
this
.
categoriesService
.
update
(
+
id
,
updateCategoryDto
);
}
@
Delete
(
':id'
)
remove
(@
Param
(
'id'
)
id
:
string
)
{
return
this
.
categoriesService
.
remove
(
+
id
);
}
}
server/src/categories/categories.module.ts
0 → 100644
View file @
6edad83d
import
{
Module
}
from
'@nestjs/common'
;
import
{
TypeOrmModule
}
from
'@nestjs/typeorm'
;
import
{
CategoriesController
}
from
'./categories.controller'
;
import
{
CategoriesService
}
from
'./categories.service'
;
import
{
Category
}
from
'./entities/category.entity'
;
@
Module
({
imports
:
[
TypeOrmModule
.
forFeature
([
Category
])],
controllers
:
[
CategoriesController
],
providers
:
[
CategoriesService
],
exports
:
[
CategoriesService
],
})
export
class
CategoriesModule
{}
server/src/categories/categories.service.ts
0 → 100644
View file @
6edad83d
import
{
Injectable
}
from
'@nestjs/common'
;
import
{
InjectRepository
}
from
'@nestjs/typeorm'
;
import
{
Repository
}
from
'typeorm'
;
import
{
CreateCategoryDto
}
from
'./dto/create-category.dto'
;
import
{
UpdateCategoryDto
}
from
'./dto/update-category.dto'
;
import
{
Category
}
from
'./entities/category.entity'
;
@
Injectable
()
export
class
CategoriesService
{
constructor
(
@
InjectRepository
(
Category
)
private
readonly
categoryRepo
:
Repository
<
Category
>
,
)
{}
async
create
(
createCategoryDto
:
CreateCategoryDto
):
Promise
<
Category
>
{
const
category
=
this
.
categoryRepo
.
create
(
createCategoryDto
);
return
await
this
.
categoryRepo
.
save
(
category
);
}
async
findAll
():
Promise
<
Category
[]
>
{
return
await
this
.
categoryRepo
.
find
();
}
async
findOne
(
id
:
number
):
Promise
<
Category
|
null
>
{
return
await
this
.
categoryRepo
.
findOneBy
({
id
});
}
async
update
(
id
:
number
,
updateCategoryDto
:
UpdateCategoryDto
,
):
Promise
<
Category
|
null
>
{
await
this
.
categoryRepo
.
update
(
id
,
updateCategoryDto
);
return
await
this
.
categoryRepo
.
findOneBy
({
id
});
}
async
remove
(
id
:
number
):
Promise
<
number
>
{
await
this
.
categoryRepo
.
delete
(
id
);
return
id
;
}
}
server/src/categories/dto/create-category.dto.ts
0 → 100644
View file @
6edad83d
import
{
IsOptional
,
IsString
}
from
'class-validator'
;
export
class
CreateCategoryDto
{
@
IsString
({
message
:
'Title must have be string'
})
title
:
string
;
@
IsOptional
()
@
IsString
({
message
:
'Description must have be string'
})
description
?:
string
;
}
server/src/categories/dto/update-category.dto.ts
0 → 100644
View file @
6edad83d
import
{
PartialType
}
from
'@nestjs/mapped-types'
;
import
{
CreateCategoryDto
}
from
'./create-category.dto'
;
export
class
UpdateCategoryDto
extends
PartialType
(
CreateCategoryDto
)
{}
server/src/categories/entities/category.entity.ts
0 → 100644
View file @
6edad83d
import
{
Product
}
from
'src/products/entity/product'
;
import
{
Column
,
Entity
,
OneToMany
,
PrimaryGeneratedColumn
}
from
'typeorm'
;
@
Entity
({
name
:
'categories'
})
export
class
Category
{
@
PrimaryGeneratedColumn
()
id
:
number
;
@
Column
({
type
:
'varchar'
,
length
:
150
})
title
:
string
;
@
Column
({
type
:
'text'
,
nullable
:
true
})
description
?:
string
;
@
OneToMany
(()
=>
Product
,
(
product
)
=>
product
.
category
)
products
:
Product
[];
}
server/src/products/dto/create-product.dto.ts
View file @
6edad83d
...
@@ -12,4 +12,8 @@ export class CreateProductDto {
...
@@ -12,4 +12,8 @@ export class CreateProductDto {
@
Type
(()
=>
Number
)
@
Type
(()
=>
Number
)
@
IsNumber
()
@
IsNumber
()
price
:
number
;
price
:
number
;
@
Type
(()
=>
Number
)
@
IsNumber
()
categoryId
:
number
;
}
}
server/src/products/entity/product.ts
0 → 100644
View file @
6edad83d
import
{
Category
}
from
'src/categories/entities/category.entity'
;
import
{
Column
,
Entity
,
ManyToOne
,
PrimaryGeneratedColumn
}
from
'typeorm'
;
@
Entity
({
name
:
'products'
})
export
class
Product
{
@
PrimaryGeneratedColumn
()
id
:
number
;
@
Column
({
type
:
'varchar'
,
length
:
150
})
title
:
string
;
@
Column
({
type
:
'text'
,
nullable
:
true
})
description
?:
string
;
@
Column
({
type
:
'numeric'
})
price
:
number
;
@
Column
({
type
:
'text'
,
nullable
:
true
})
image
?:
string
;
@
ManyToOne
(()
=>
Category
,
(
category
)
=>
category
.
products
)
category
:
Category
;
}
server/src/products/products.controller.ts
View file @
6edad83d
...
@@ -21,7 +21,7 @@ export class ProductsController {
...
@@ -21,7 +21,7 @@ export class ProductsController {
@
Body
()
createProductDto
:
CreateProductDto
,
@
Body
()
createProductDto
:
CreateProductDto
,
@
UploadedFile
()
image
:
Express
.
Multer
.
File
,
@
UploadedFile
()
image
:
Express
.
Multer
.
File
,
)
{
)
{
return
this
.
productsService
.
create
(
createProductDto
,
image
.
filename
);
return
this
.
productsService
.
create
(
createProductDto
,
image
?
.
filename
);
}
}
@
Get
()
@
Get
()
...
...
server/src/products/products.module.ts
View file @
6edad83d
import
{
Module
}
from
'@nestjs/common'
;
import
{
Module
}
from
'@nestjs/common'
;
import
{
ProductsService
}
from
'./products.service'
;
import
{
TypeOrmModule
}
from
'@nestjs/typeorm'
;
import
{
CategoriesModule
}
from
'src/categories/categories.module'
;
import
{
Category
}
from
'src/categories/entities/category.entity'
;
import
{
Product
}
from
'./entity/product'
;
import
{
ProductsController
}
from
'./products.controller'
;
import
{
ProductsController
}
from
'./products.controller'
;
import
{
ProductsService
}
from
'./products.service'
;
@
Module
({
@
Module
({
imports
:
[
TypeOrmModule
.
forFeature
([
Product
,
Category
]),
CategoriesModule
],
controllers
:
[
ProductsController
],
controllers
:
[
ProductsController
],
providers
:
[
ProductsService
],
providers
:
[
ProductsService
],
})
})
...
...
server/src/products/products.service.ts
View file @
6edad83d
import
{
Injectable
}
from
'@nestjs/common'
;
import
{
Injectable
,
NotFoundException
}
from
'@nestjs/common'
;
import
{
randomUUID
}
from
'crypto'
;
import
{
InjectRepository
}
from
'@nestjs/typeorm'
;
import
*
as
fs
from
'fs'
;
import
{
Category
}
from
'src/categories/entities/category.entity'
;
import
*
as
path
from
'path'
;
import
{
Repository
}
from
'typeorm'
;
import
{
IProduct
}
from
'src/interface/product'
;
import
{
CreateProductDto
}
from
'./dto/create-product.dto'
;
import
{
CreateProductDto
}
from
'./dto/create-product.dto'
;
import
{
Product
}
from
'./entity/product'
;
@
Injectable
()
@
Injectable
()
export
class
ProductsService
{
export
class
ProductsService
{
constructor
()
{
constructor
(
this
.
init
();
@
InjectRepository
(
Product
)
}
private
readonly
productRepo
:
Repository
<
Product
>
,
@
InjectRepository
(
Category
)
private
readonly
categoryRepo
:
Repository
<
Category
>
,
)
{}
private
products
:
IProduct
[]
=
[];
async
create
(
private
readonly
filePath
=
path
?.
resolve
(
createProductDto
:
CreateProductDto
,
process
.
cwd
(),
fileName
?:
string
,
'src/data/products.json'
,
):
Promise
<
Product
>
{
);
const
category
=
await
this
.
categoryRepo
.
findOneBy
({
id
:
createProductDto
.
categoryId
,
});
private
init
():
void
{
if
(
!
category
)
throw
new
NotFoundException
(
'Category not found'
);
try
{
const
fileContents
=
fs
.
readFileSync
(
this
.
filePath
);
this
.
products
=
JSON
.
parse
(
fileContents
.
toString
())
as
IProduct
[];
}
catch
{
this
.
products
=
[];
}
}
create
(
createProductDto
:
CreateProductDto
,
fileName
:
string
)
{
const
product
=
this
.
productRepo
.
create
({
const
product
=
{
...
createProductDto
,
...
createProductDto
,
id
:
randomUUID
(),
image
:
fileName
,
image
:
fileName
,
};
category
,
});
this
.
products
.
push
(
product
);
this
.
save
();
}
findAll
():
IProduct
[]
{
return
await
this
.
productRepo
.
save
(
product
);
return
this
.
products
;
}
}
private
save
():
void
{
async
findAll
():
Promise
<
Product
[]
>
{
fs
.
writeFileSync
(
this
.
filePath
,
JSON
.
stringify
(
this
.
products
,
null
,
2
));
return
await
this
.
productRepo
.
find
({
relations
:
{
category
:
true
,
},
});
}
}
}
}
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