UserSchema
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const UserSchema = new mongoose.Schema({
email: {
type: String,
trim: true,
unique: true,
required: true,
},
password: {
type: String,
required: true,
},
nickname: {
type: String,
min: 2,
max: 10,
},
profileImgURL: String,
gender: String,
birthYear: Number,
area: String,
likePosts: [
{
type: mongoose.Types.ObjectId,
ref: 'Post',
},
],
applyPosts: [
{
type: mongoose.Types.ObjectId,
ref: 'Post',
},
],
bio: [
{
_id: mongoose.Types.ObjectId,
text: {
type: String,
min: 2,
max: 100,
default: '',
},
},
],
joinedPosts: [
{
type: mongoose.Types.ObjectId,
ref: 'Post',
},
],
kakaoId: Number,
});
UserSchema.methods.generateToken = function () {
const token = jwt.sign(
{
_id: this.id,
},
process.env.JWT_SECRET,
{
expiresIn: process.env.EXPIRE_TIME,
}
);
return token;
};
UserSchema.methods.deleteApplyPost = async function (postId) {
this.applyPosts = this.applyPosts.filter(
(applyPostId) => applyPostId.toString() !== postId.toString()
);
this.bio = this.bio.filter(
(post) => post._id.toString() !== postId.toString()
);
await this.save();
};
module.exports = UserSchema;
PostSchema
const mongoose = require('mongoose');
const PostSchema = new mongoose.Schema({
isRecruiting: {
type: Boolean,
default: true,
},
author: {
type: mongoose.Types.ObjectId,
ref: 'User',
required: true,
},
title: {
type: String,
min: 1,
max: 100,
required: true,
},
content: {
type: String,
min: 0,
max: 500,
required: true,
},
area: {
type: String,
required: true,
},
age: {
type: Number,
required: true,
},
postImgURL: String,
members: [
{
type: mongoose.Types.ObjectId,
ref: 'User',
},
],
preMembers: [
{
type: mongoose.Types.ObjectId,
ref: 'User',
},
],
likeMembers: [
{
type: mongoose.Types.ObjectId,
ref: 'User',
},
],
category: {
type: String,
required: true,
},
chat: [
{
_id: mongoose.Types.ObjectId,
nickname: String,
time: {
type: Date,
default: () => Date.now(),
},
text: String,
profileImgURL: String,
},
],
createdAt: {
type: Date,
default: () => getCurrentDate(),
},
});
function getCurrentDate() {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth();
const today = date.getDate();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const milliseconds = date.getMilliseconds();
return new Date(
Date.UTC(year, month, today, hours, minutes, seconds, milliseconds)
);
}
module.exports = PostSchema;