Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
H
hw88AlenBolatov
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
Болатов Ален
hw88AlenBolatov
Commits
3a2941ee
Commit
3a2941ee
authored
Mar 16, 2023
by
Болатов Ален
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added delete method
parent
b7d78e7b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
88 additions
and
2 deletions
+88
-2
Generic.ts
backend/src/controllers/Generic.ts
+18
-0
Location.ts
backend/src/routes/Location.ts
+36
-1
Product.ts
backend/src/routes/Product.ts
+1
-0
category.ts
backend/src/routes/category.ts
+33
-1
No files found.
backend/src/controllers/Generic.ts
View file @
3a2941ee
...
@@ -50,8 +50,26 @@ const get =
...
@@ -50,8 +50,26 @@ const get =
});
});
};
};
const
deleteDoc
=
(
model
:
Model
<
any
>
)
=>
(
req
:
Request
,
res
:
Response
)
=>
{
const
_id
=
req
.
params
.
id
;
model
.
findOneAndDelete
<
Document
>
({
_id
})
.
then
((
result
)
=>
{
if
(
result
)
{
return
res
.
status
(
200
).
send
(
'Successfuly deleted'
);
}
else
{
return
res
.
status
(
404
).
json
({
message
:
'Not found'
});
}
})
.
catch
((
error
)
=>
{
console
.
log
(
error
);
return
res
.
status
(
500
).
json
({
error
});
});
};
export
default
{
export
default
{
create
,
create
,
getAll
,
getAll
,
get
,
get
,
deleteDoc
,
};
};
backend/src/routes/Location.ts
View file @
3a2941ee
import
express
,
{
Router
}
from
'express'
;
import
express
,
{
Router
,
Request
,
Response
}
from
'express'
;
import
{
Document
}
from
'mongoose'
;
import
controller
from
'../controllers/Generic'
;
import
controller
from
'../controllers/Generic'
;
import
model
from
'../models/Location'
;
import
model
from
'../models/Location'
;
import
productModel
from
'../models/Product'
;
const
router
:
Router
=
express
.
Router
();
const
router
:
Router
=
express
.
Router
();
...
@@ -8,4 +10,37 @@ router.get('/', controller.getAll(model));
...
@@ -8,4 +10,37 @@ router.get('/', controller.getAll(model));
router
.
post
(
'/'
,
controller
.
create
(
model
));
router
.
post
(
'/'
,
controller
.
create
(
model
));
router
.
get
(
'/:id'
,
controller
.
get
(
model
));
router
.
get
(
'/:id'
,
controller
.
get
(
model
));
router
.
delete
(
'/:id'
,
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
const
id
=
req
.
params
.
id
;
if
(
!
id
.
match
(
/^
[
0-9a-fA-F
]{24}
$/
))
{
res
.
json
({
error
:
'This is not a vaild object id'
});
return
;
}
productModel
.
find
({
location
:
id
}).
then
((
result
)
=>
{
if
(
!
result
.
length
)
{
model
.
findByIdAndDelete
(
id
)
.
then
((
result
)
=>
{
if
(
result
)
{
res
.
send
(
`Deleted
${
result
}
`
);
}
else
{
res
.
send
(
`Could not find document by this id`
);
}
})
.
catch
((
err
)
=>
{
console
.
log
(
err
);
res
.
status
(
404
).
send
(
'Not found'
);
});
}
else
{
res
.
json
({
error
:
'Cannot delete since its used in products'
});
}
});
}
catch
(
err
:
unknown
)
{
console
.
log
(
err
);
res
.
status
(
500
).
json
({
err
});
}
});
export
=
router
;
export
=
router
;
backend/src/routes/Product.ts
View file @
3a2941ee
...
@@ -10,6 +10,7 @@ const router: Router = express.Router();
...
@@ -10,6 +10,7 @@ const router: Router = express.Router();
router
.
get
(
'/'
,
controller
.
getAll
(
model
,
[
'category'
,
'location'
]));
router
.
get
(
'/'
,
controller
.
getAll
(
model
,
[
'category'
,
'location'
]));
router
.
get
(
'/:id'
,
controller
.
get
(
model
,
[
'category'
,
'location'
]));
router
.
get
(
'/:id'
,
controller
.
get
(
model
,
[
'category'
,
'location'
]));
router
.
delete
(
'/:id'
,
controller
.
deleteDoc
(
model
));
router
.
post
(
router
.
post
(
'/'
,
'/'
,
...
...
backend/src/routes/category.ts
View file @
3a2941ee
import
express
,
{
Router
}
from
'express'
;
import
express
,
{
Router
,
Request
,
Response
}
from
'express'
;
import
controller
from
'../controllers/Generic'
;
import
controller
from
'../controllers/Generic'
;
import
model
from
'../models/Category'
;
import
model
from
'../models/Category'
;
import
productModel
from
'../models/Product'
;
const
router
:
Router
=
express
.
Router
();
const
router
:
Router
=
express
.
Router
();
router
.
get
(
'/'
,
controller
.
getAll
(
model
));
router
.
get
(
'/'
,
controller
.
getAll
(
model
));
router
.
post
(
'/'
,
controller
.
create
(
model
));
router
.
post
(
'/'
,
controller
.
create
(
model
));
router
.
get
(
'/:id'
,
controller
.
get
(
model
));
router
.
get
(
'/:id'
,
controller
.
get
(
model
));
router
.
delete
(
'/:id'
,
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
const
id
=
req
.
params
.
id
;
if
(
!
id
.
match
(
/^
[
0-9a-fA-F
]{24}
$/
))
{
res
.
json
({
error
:
'This is not a vaild object id'
});
return
;
}
productModel
.
find
({
category
:
id
}).
then
((
result
)
=>
{
if
(
!
result
.
length
)
{
model
.
findByIdAndDelete
(
id
)
.
then
((
result
)
=>
{
if
(
result
)
{
res
.
send
(
`Deleted
${
result
}
`
);
}
else
{
res
.
send
(
`Could not find document by this id`
);
}
})
.
catch
((
err
)
=>
{
console
.
log
(
err
);
res
.
status
(
404
).
send
(
'Not found'
);
});
}
else
{
res
.
json
({
error
:
'Cannot delete since its used in products'
});
}
});
}
catch
(
err
:
unknown
)
{
console
.
log
(
err
);
res
.
status
(
500
).
json
({
err
});
}
});
export
=
router
;
export
=
router
;
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