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
cb658c25
Commit
cb658c25
authored
Mar 15, 2023
by
Болатов Ален
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#3
added generic controller that takes different models
parent
35c835d2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
69 additions
and
27 deletions
+69
-27
Generic.ts
backend/src/controllers/Generic.ts
+35
-0
index.ts
backend/src/index.ts
+4
-1
Category.ts
backend/src/models/Category.ts
+23
-0
category.ts
backend/src/routes/category.ts
+7
-26
No files found.
backend/src/controllers/Generic.ts
0 → 100644
View file @
cb658c25
import
{
Request
,
Response
}
from
'express'
;
import
{
Model
}
from
'mongoose'
;
const
create
=
(
model
:
Model
<
any
>
)
=>
(
req
:
Request
,
res
:
Response
)
=>
{
console
.
log
(
`Create new document for
${
model
.
modelName
}
`
);
const
doc
=
new
model
({
...
req
.
body
,
});
return
doc
.
save
()
.
then
((
result
:
any
)
=>
res
.
status
(
201
).
send
(
result
))
.
catch
((
error
:
any
)
=>
res
.
status
(
500
).
json
({
error
}));
};
const
getAll
=
(
model
:
Model
<
any
>
)
=>
(
req
:
Request
,
res
:
Response
)
=>
{
console
.
log
(
`Getting all documents for
${
model
.
modelName
}
`
);
model
.
find
<
Document
>
({},
{
description
:
0
})
.
then
((
results
)
=>
{
console
.
log
(
results
);
return
res
.
status
(
200
).
send
(
results
);
})
.
catch
((
error
)
=>
{
console
.
log
(
error
);
return
res
.
status
(
500
).
json
({
error
});
});
};
export
default
{
create
,
getAll
,
};
backend/src/index.ts
View file @
cb658c25
...
@@ -2,13 +2,14 @@ import express, {Express, json} from 'express';
...
@@ -2,13 +2,14 @@ import express, {Express, json} from 'express';
import
'dotenv/config'
;
import
'dotenv/config'
;
import
cors
from
'cors'
;
import
cors
from
'cors'
;
import
{
connect
}
from
'mongoose'
;
import
{
connect
}
from
'mongoose'
;
import
categoryRouter
from
'./routes/Category'
;
const
app
:
Express
=
express
();
const
app
:
Express
=
express
();
app
.
use
(
json
());
app
.
use
(
json
());
app
.
use
(
cors
());
app
.
use
(
cors
());
const
run
=
async
()
=>
{
const
run
=
async
()
=>
{
await
connect
(
`
${
process
.
env
.
MONGO_URL
}
/
musicApp
`
);
await
connect
(
`
${
process
.
env
.
MONGO_URL
}
/
office
`
);
};
};
run
().
catch
((
err
)
=>
console
.
log
(
err
));
run
().
catch
((
err
)
=>
console
.
log
(
err
));
...
@@ -16,3 +17,5 @@ run().catch((err) => console.log(err));
...
@@ -16,3 +17,5 @@ run().catch((err) => console.log(err));
app
.
listen
(
process
.
env
.
PORT
,
()
=>
{
app
.
listen
(
process
.
env
.
PORT
,
()
=>
{
console
.
log
(
'Server started on port '
+
process
.
env
.
PORT
);
console
.
log
(
'Server started on port '
+
process
.
env
.
PORT
);
});
});
app
.
use
(
'/categories'
,
categoryRouter
);
backend/src/models/Category.ts
0 → 100644
View file @
cb658c25
import
mongoose
,
{
Document
,
Schema
}
from
'mongoose'
;
export
interface
ICategory
{
name
:
string
;
description
:
string
;
}
export
interface
ICategoryModel
extends
ICategory
,
Document
{}
const
CategorySchema
:
Schema
=
new
Schema
(
{
name
:
{
type
:
String
,
required
:
true
,
},
description
:
{
type
:
String
,
},
},
{
versionKey
:
false
}
);
export
default
mongoose
.
model
<
ICategoryModel
>
(
'Category'
,
CategorySchema
);
backend/src/routes/category.ts
View file @
cb658c25
import
express
,
{
Request
,
Response
}
from
'express'
;
import
express
,
{
Router
}
from
'express'
;
import
{
model
,
Schema
,
HydratedDocument
,
Types
}
from
'mongoose'
;
import
controller
from
'../controllers/Generic'
;
import
model
from
'../models/category'
;
const
router
=
express
.
Router
();
const
router
:
Router
=
express
.
Router
();
interface
ICategory
{
router
.
get
(
'/'
,
controller
.
getAll
(
model
));
name
:
string
;
router
.
post
(
'/'
,
controller
.
create
(
model
));
description
:
string
;
}
const
Category
=
model
<
ICategory
>
(
export
=
router
;
'Category'
,
new
Schema
({
name
:
{
type
:
String
,
required
:
true
,
},
description
:
{
type
:
String
,
required
:
false
,
},
})
);
router
.
get
(
'/'
,
async
(
req
:
Request
,
res
:
Response
)
=>
{});
router
.
post
(
'/'
,
async
(
req
:
Request
,
res
:
Response
)
=>
{});
export
{
router
as
categoryRouter
};
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