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
9ebab5b6
Commit
9ebab5b6
authored
Mar 15, 2023
by
Болатов Ален
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#4
defined location model
parent
cb658c25
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
72 additions
and
16 deletions
+72
-16
Generic.ts
backend/src/controllers/Generic.ts
+34
-15
index.ts
backend/src/index.ts
+2
-0
Location.ts
backend/src/models/Location.ts
+23
-0
Location.ts
backend/src/routes/Location.ts
+11
-0
category.ts
backend/src/routes/category.ts
+2
-1
No files found.
backend/src/controllers/Generic.ts
View file @
9ebab5b6
...
@@ -2,8 +2,6 @@ import {Request, Response} from 'express';
...
@@ -2,8 +2,6 @@ import {Request, Response} from 'express';
import
{
Model
}
from
'mongoose'
;
import
{
Model
}
from
'mongoose'
;
const
create
=
(
model
:
Model
<
any
>
)
=>
(
req
:
Request
,
res
:
Response
)
=>
{
const
create
=
(
model
:
Model
<
any
>
)
=>
(
req
:
Request
,
res
:
Response
)
=>
{
console
.
log
(
`Create new document for
${
model
.
modelName
}
`
);
const
doc
=
new
model
({
const
doc
=
new
model
({
...
req
.
body
,
...
req
.
body
,
});
});
...
@@ -14,22 +12,43 @@ const create = (model: Model<any>) => (req: Request, res: Response) => {
...
@@ -14,22 +12,43 @@ const create = (model: Model<any>) => (req: Request, res: Response) => {
.
catch
((
error
:
any
)
=>
res
.
status
(
500
).
json
({
error
}));
.
catch
((
error
:
any
)
=>
res
.
status
(
500
).
json
({
error
}));
};
};
const
getAll
=
(
model
:
Model
<
any
>
)
=>
(
req
:
Request
,
res
:
Response
)
=>
{
const
getAll
=
console
.
log
(
`Getting all documents for
${
model
.
modelName
}
`
);
(
model
:
Model
<
any
>
,
populate
?:
string
[])
=>
(
req
:
Request
,
res
:
Response
)
=>
{
model
.
find
<
Document
>
({},
{
description
:
0
})
.
populate
(
populate
||
[])
.
then
((
results
)
=>
{
console
.
log
(
results
);
return
res
.
status
(
200
).
send
(
results
);
})
.
catch
((
error
)
=>
{
console
.
log
(
error
);
return
res
.
status
(
500
).
json
({
error
});
});
};
model
const
get
=
.
find
<
Document
>
({},
{
description
:
0
})
(
model
:
Model
<
any
>
,
populate
?:
string
[])
=>
(
req
:
Request
,
res
:
Response
)
=>
{
.
then
((
results
)
=>
{
const
id
=
req
.
params
.
id
;
console
.
log
(
results
);
return
res
.
status
(
200
).
send
(
results
);
model
})
.
findById
<
Document
>
(
id
)
.
catch
((
error
)
=>
{
.
populate
(
populate
||
[])
console
.
log
(
error
);
.
then
((
result
)
=>
{
return
res
.
status
(
500
).
json
({
error
});
if
(
result
)
{
});
return
res
.
status
(
200
).
send
(
result
);
};
}
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
,
};
};
backend/src/index.ts
View file @
9ebab5b6
...
@@ -3,6 +3,7 @@ import 'dotenv/config';
...
@@ -3,6 +3,7 @@ import 'dotenv/config';
import
cors
from
'cors'
;
import
cors
from
'cors'
;
import
{
connect
}
from
'mongoose'
;
import
{
connect
}
from
'mongoose'
;
import
categoryRouter
from
'./routes/Category'
;
import
categoryRouter
from
'./routes/Category'
;
import
locationRouter
from
'./routes/Location'
;
const
app
:
Express
=
express
();
const
app
:
Express
=
express
();
app
.
use
(
json
());
app
.
use
(
json
());
...
@@ -19,3 +20,4 @@ app.listen(process.env.PORT, () => {
...
@@ -19,3 +20,4 @@ app.listen(process.env.PORT, () => {
});
});
app
.
use
(
'/categories'
,
categoryRouter
);
app
.
use
(
'/categories'
,
categoryRouter
);
app
.
use
(
'/locations'
,
locationRouter
);
backend/src/models/Location.ts
0 → 100644
View file @
9ebab5b6
import
mongoose
,
{
Document
,
Schema
}
from
'mongoose'
;
export
interface
ILocation
{
name
:
string
;
description
:
string
;
}
export
interface
ILocationModel
extends
ILocation
,
Document
{}
const
LocationSchema
:
Schema
=
new
Schema
(
{
name
:
{
type
:
String
,
required
:
true
,
},
description
:
{
type
:
String
,
},
},
{
versionKey
:
false
}
);
export
default
mongoose
.
model
<
ILocationModel
>
(
'Location'
,
LocationSchema
);
backend/src/routes/Location.ts
0 → 100644
View file @
9ebab5b6
import
express
,
{
Router
}
from
'express'
;
import
controller
from
'../controllers/Generic'
;
import
model
from
'../models/Location'
;
const
router
:
Router
=
express
.
Router
();
router
.
get
(
'/'
,
controller
.
getAll
(
model
));
router
.
post
(
'/'
,
controller
.
create
(
model
));
router
.
get
(
'/:id'
,
controller
.
get
(
model
));
export
=
router
;
backend/src/routes/category.ts
View file @
9ebab5b6
import
express
,
{
Router
}
from
'express'
;
import
express
,
{
Router
}
from
'express'
;
import
controller
from
'../controllers/Generic'
;
import
controller
from
'../controllers/Generic'
;
import
model
from
'../models/
c
ategory'
;
import
model
from
'../models/
C
ategory'
;
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
));
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