Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TDDD27_2024_Myndigheter
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Advanced_web
TDDD27_2024_Myndigheter
Commits
d1fcadac
Commit
d1fcadac
authored
1 year ago
by
Anna Granberg
Browse files
Options
Downloads
Patches
Plain Diff
funkar ej:( måste forska vidare
parent
5d9b8c81
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
my-app/app/api/checkdb/route.ts
+20
-6
20 additions, 6 deletions
my-app/app/api/checkdb/route.ts
my-app/lib/dbConnect.ts
+44
-0
44 additions, 0 deletions
my-app/lib/dbConnect.ts
my-app/lib/myndigheter.ts
+23
-0
23 additions, 0 deletions
my-app/lib/myndigheter.ts
with
87 additions
and
6 deletions
my-app/app/api/checkdb/route.ts
+
20
−
6
View file @
d1fcadac
import
{
dbConnect
}
from
'
../../../lib/db
'
;
import
{
NextResponse
}
from
'
next/server
'
;
import
{
NextRequest
}
from
"
next/server
"
;
import
{
NextApiRequest
,
NextApiResponse
}
from
"
next
"
;
import
dbConnect
from
"
../../../lib/dbConnect
"
;
import
Movie
from
"
../../../lib/myndigheter
"
;
export
async
function
GET
(){
const
con
=
await
dbConnect
();
return
new
NextResponse
(
'
connected
'
);
}
\ No newline at end of file
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
,
)
{
const
{
method
}
=
req
;
await
dbConnect
();
if
(
method
===
"
GET
"
){
try
{
const
movies
=
await
Movie
.
find
({});
res
.
status
(
200
).
json
({
success
:
true
,
data
:
movies
});
}
catch
(
error
)
{
res
.
status
(
400
).
json
({
success
:
false
});
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
my-app/lib/dbConnect.ts
0 → 100644
+
44
−
0
View file @
d1fcadac
//CONNECTING TO OUR DATABASE
import
mongoose
from
"
mongoose
"
;
declare
global
{
var
mongoose
:
any
;
}
const
MONGODB_URI
=
process
.
env
.
MONGODB_URI
!
;
if
(
!
MONGODB_URI
)
{
throw
new
Error
(
"
Please define the MONGODB_URI environment variable inside .env.local
"
,
);
}
let
cached
=
global
.
mongoose
;
if
(
!
cached
)
{
cached
=
global
.
mongoose
=
{
conn
:
null
,
promise
:
null
};
}
async
function
dbConnect
()
{
if
(
cached
.
conn
)
{
return
cached
.
conn
;
}
if
(
!
cached
.
promise
)
{
const
opts
=
{
bufferCommands
:
false
,
};
cached
.
promise
=
mongoose
.
connect
(
MONGODB_URI
,
opts
).
then
((
mongoose
)
=>
{
return
mongoose
;
});
}
try
{
cached
.
conn
=
await
cached
.
promise
;
}
catch
(
e
)
{
cached
.
promise
=
null
;
throw
e
;
}
return
cached
.
conn
;
}
export
default
dbConnect
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
my-app/lib/myndigheter.ts
0 → 100644
+
23
−
0
View file @
d1fcadac
import
mongoose
from
"
mongoose
"
;
export
interface
Movie
extends
mongoose
.
Document
{
_id
:
string
;
title
:
string
;
}
/* correspond to a collection in your MongoDB database. */
const
MovieSchema
=
new
mongoose
.
Schema
<
Movie
>
({
title
:
{
type
:
String
,
required
:
[
true
,
"
Please provide a name for this pet.
"
],
maxlength
:
[
60
,
"
Name cannot be more than 60 characters
"
],
},
});
const
MovieModel
=
mongoose
.
models
.
Movie
||
mongoose
.
model
<
Movie
>
(
"
Movie
"
,
MovieSchema
);
export
default
MovieModel
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment