Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
students
travel
Commits
208862f5
Commit
208862f5
authored
Jul 11, 2022
by
Lee Soobeom
Browse files
DB에 posting table 저장
parent
18cf7518
Changes
6
Show whitespace changes
Inline
Side-by-side
src/controllers/post.controller.ts
View file @
208862f5
...
...
@@ -2,9 +2,10 @@ import { NextFunction, Request, Response } from "express";
import
isLength
from
"
validator/lib/isLength
"
;
import
equals
from
"
validator/lib/equals
"
;
import
{
asyncWrap
}
from
"
../helpers
"
;
import
{
postDb
}
from
"
../db
"
;
export
const
posting
=
asyncWrap
(
async
(
req
,
res
)
=>
{
const
{
title
,
text
,
date
,
theme
,
city
}
=
req
.
body
;
const
{
title
,
text
,
theme
,
city
,
username
}
=
req
.
body
;
console
.
log
(
"
body
"
,
req
.
body
);
...
...
@@ -18,15 +19,25 @@ export const posting = asyncWrap(async (req, res) => {
return
res
.
status
(
422
).
send
(
"
제목을 한 글자 이상 입력해주세요
"
);
}
// 3) submit 이벤트 발생시 date값 입력
// 4) theme dropdown default-value "테마"일 경우 에러
// 3) theme dropdown default-value "테마"일 경우 에러
if
(
equals
(
theme
,
"
질문종류
"
))
{
return
res
.
status
(
422
).
send
(
"
테마를 입력해 주세요
"
);
}
//
5
) city dropdown default-value "도시"일 경우 에러
//
4
) city dropdown default-value "도시"일 경우 에러
if
(
equals
(
city
,
"
질문종류
"
))
{
return
res
.
status
(
422
).
send
(
"
도시를 선택해 주세요
"
);
}
// 5) username 확인 필요 없음
// 6)
const
newPosting
=
await
postDb
.
createPosting
({
title
,
text
,
theme
,
city
,
username
,
});
return
res
.
json
(
newPosting
);
});
src/db/index.ts
View file @
208862f5
export
*
as
userDb
from
"
./user.db
"
;
//
export * as postDb from "./post.db";
export
*
as
postDb
from
"
./post.db
"
;
src/db/post.db.ts
0 → 100644
View file @
208862f5
import
{
Posting
,
PostingType
}
from
"
../models
"
;
export
const
createPosting
=
async
(
posting
:
PostingType
)
=>
{
const
newPosting
=
await
Posting
.
create
({
title
:
posting
.
title
,
text
:
posting
.
text
,
theme
:
posting
.
theme
,
city
:
posting
.
city
,
username
:
posting
.
username
,
});
return
newPosting
;
};
src/models/index.ts
View file @
208862f5
export
{
default
as
User
,
IUser
}
from
"
./user.model
"
;
export
{
default
as
Posting
,
PostingType
}
from
"
./posting.model
"
;
export
{
default
as
Post
,
PostType
}
from
"
./post.model
"
;
src/models/post.model.ts
View file @
208862f5
import
{
model
,
Schema
}
from
"
mongoose
"
;
import
{
model
,
Schema
,
Types
}
from
"
mongoose
"
;
import
{
PostingType
}
from
"
./posting.model
"
;
export
interface
PostType
extends
PostingType
{
date
?:
string
;
...
...
@@ -6,23 +7,13 @@ export interface PostType extends PostingType {
id
?:
string
;
}
export
interface
PostingType
{
title
:
string
;
text
?:
string
;
theme
:
string
;
city
:
string
;
username
:
string
;
}
const
schema
=
new
Schema
<
PostType
>
({
id
:
{
type
:
String
},
const
postSchema
=
new
Schema
<
PostType
>
({
title
:
{
type
:
String
},
date
:
{
type
:
Date
},
text
:
{
type
:
String
},
counts
:
{
type
:
Number
},
theme
:
{
type
:
String
},
city
:
{
type
:
String
},
username
:
{
type
:
String
},
date
:
{
type
:
String
},
counts
:
{
type
:
Number
},
});
export
default
model
<
PostType
>
(
"
Post
"
,
s
chema
);
export
default
model
<
PostType
>
(
"
Post
"
,
postS
chema
);
src/models/posting.model.ts
0 → 100644
View file @
208862f5
import
{
model
,
Schema
}
from
"
mongoose
"
;
export
interface
PostingType
{
title
:
string
;
text
?:
string
;
theme
:
string
;
city
:
string
;
username
?:
string
;
}
const
postingSchema
=
new
Schema
<
PostingType
>
(
{
title
:
{
type
:
String
,
required
:
true
,
unique
:
true
,
},
text
:
{
type
:
String
,
required
:
true
,
unique
:
true
,
},
theme
:
{
type
:
String
,
unique
:
true
,
},
city
:
{
type
:
String
,
unique
:
true
,
},
// username: {
// type: String,
// unique: true,
// },
}
// username 때문에 duplicate key error 발생
);
export
default
model
<
PostingType
>
(
"
Posting
"
,
postingSchema
);
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