Skip to content

Commit

Permalink
[Server] / #4 / add wildcard search
Browse files Browse the repository at this point in the history
[Server] / #4 / add wildcard search
  • Loading branch information
Dev-SeungcheolLee authored Nov 16, 2020
2 parents 0c87345 + c538b18 commit e61f6a5
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 28 deletions.
41 changes: 30 additions & 11 deletions controller/hashtags/searchTag.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
const { hashtag, post_hashtag, post } = require('../../models');
const { hashtag, post } = require('../../models');
const sequelize = require('sequelize');
const Op = sequelize.Op;

module.exports = {
post: async (req, res) => {
const { userid } = req.session;
const { query } = req.body;

if (userid) {
await hashtag.findAll({
where: { tag: query },
include: {
model: post,
through: { attributes: [] }
// if (userid) {
await hashtag.findAll({
where: {
tag: {
[Op.like]: '%' + query + '%'
}
}).then(result => {
const posts = [];
},
include: {
model: post,
through: { attributes: [] }
}
})
.then(result => result.map(record => record.get({ plain: true })))
.then(result => {
const temp = [];

for (let el of result) {
posts.push(el.posts[0]);
temp.push(el.posts[0]);
}

const posts = temp.reduce((acc, cur) => {
if (!acc.some(obj => obj.id === cur.id)) {
acc.push(cur);
}
return acc;
}, []);

res.status(200).json(posts);

}).catch(err => res.status(500).send(err));
}

// }

}
};
4 changes: 2 additions & 2 deletions controller/posts/createNew.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { post } = require('../../models');

module.exports = {
post: (req, res) => {
post: async (req, res) => {
const { userid } = req.session;
const { location, mapimgpath, content } = req.body;

if (userid) {
post.create({
await post.create({
location: location,
mapimgpath: mapimgpath,
content: content,
Expand Down
4 changes: 2 additions & 2 deletions controller/posts/editPost.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { post } = require('../../models');

module.exports = {
patch: (req, res) => {
patch: async (req, res) => {
const { userid } = req.session;
const { id, location, mapimgpath, content } = req.body;

if (userid) {
post.update({
await post.update({
location: location,
mapimgpath: mapimgpath,
content: content
Expand Down
45 changes: 37 additions & 8 deletions controller/posts/removePost.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
const { post } = require('../../models');
const { hashTagsController } = require('..');
const { post, post_hashtag, hashtag } = require('../../models');

module.exports = {
post: (req, res) => {
post: async (req, res) => {
const { userid } = req.session;
const { id } = req.body;
const { postid } = req.body;

if (userid) {
post.destroy({
await post.findOne({
where: {
id: id,
userid: userid
id: postid
},
include: {
model: hashtag,
through: { attributes: [] }
}
}).then(result => res.status(200).json('post removed'))
.catch(err => res.status(500).send(err));
}).then(async result => {
const { hashtags } = result;
let tagIdArr = [];

for (let el of hashtags) {
tagIdArr.push(el.id);
}

await post.destroy({
where: {
id: postid,
userid: userid
}
}).catch(err => console.log(err));

await hashtag.destroy({
where: {
id: tagIdArr
}
}).catch(err => console.log(err));

await post_hashtag.destroy({
where: {
postid: postid
}
}).catch(err => console.log(err));
}).then(result => res.status(200).json('post removed'));
} else {
res.status(401).send('로그인 정보가 없습니다.');
}
Expand Down
10 changes: 7 additions & 3 deletions controller/posts/requestAll.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const { post } = require('../../models');
const { post, hashtag } = require('../../models');

module.exports = {
get: (req, res) => {
get: async (req, res) => {
const { userid } = req.session;

if (userid) {
post.findAll({
await post.findAll({
where: {
userid: userid
},
include: {
model: hashtag,
through: { attributes: [] }
}
}).then(result => res.status(200).json(result))
.catch(err => res.status(500).send(err));
Expand Down
7 changes: 5 additions & 2 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ module.exports = (sequelize, DataTypes) => {
email: DataTypes.STRING,
password: DataTypes.STRING,
username: DataTypes.STRING,
phonenum: DataTypes.STRING
phonenum: DataTypes.STRING,
googleId: DataTypes.STRING,
token: DataTypes.STRING
}, {
hooks: {
beforeCreate: data => {
Expand All @@ -29,7 +31,8 @@ module.exports = (sequelize, DataTypes) => {
}
},
beforeFind: data => {
if (Object.keys(data).indexOf('where') !== -1) {
// if (Object.keys(data).indexOf('where') !== -1) {
if (data.where.password) {
let shasum = crypto.createHmac('sha512', 'to-go-SecRet!$');
shasum.update(data.where.password);
data.where.password = shasum.digest('hex');
Expand Down

0 comments on commit e61f6a5

Please sign in to comment.