Skip to content

Commit

Permalink
Merge branch 'develop' into mypages
Browse files Browse the repository at this point in the history
  • Loading branch information
rlagns1234 committed Feb 17, 2024
2 parents cb8242d + 5f380ca commit 9fe1817
Show file tree
Hide file tree
Showing 20 changed files with 326 additions and 9 deletions.
3 changes: 3 additions & 0 deletions config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ module.exports = function () {
require('../src/search/search.route.js')(app);
require('../src/help/help.route.js')(app);
require('../src/mypages/mypagesRoute.js')(app);
require('../src/popular/popular.route.js')(app);
require('../src/post/post.route.js')(app);
require('../src/category/category.route')(app);

// error handling
app.use((req, res, next) => {
Expand Down
16 changes: 14 additions & 2 deletions src/auth/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports.join = async (req, res, next) => {

try {
const result = await authService.join(userData);
return res.send(response(baseResponse.SUCCESS, result));
return res.send(response(result));
} catch (error) {
console.error(error);
res.status(400).json({ success: false, message: error.message });
Expand Down Expand Up @@ -46,4 +46,16 @@ exports.login = async (req, res, next) => {
console.error(error);
return next(error);
}
};
};

//로그아웃
// exports.login = async (req, res, next) => {
// try {
// const user_id = res.locals.decoded.userId;
// const result = await authService.logout(user_id);
// return res.send(response(baseResponse.SUCCESS_LOGOUT, result));
// } catch (error) {
// console.error(error);
// return next(error);
// }
// };
2 changes: 2 additions & 0 deletions src/auth/auth.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ module.exports = function (app) {
app.get('/auth/nick_name', authController.checkNickName);
// 3.로그인
app.post('/auth/login', authController.login);
// 4.로그아웃
// app.get('/auth/logout', verifyAToken, authController.logout);
};
16 changes: 11 additions & 5 deletions src/auth/auth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,9 @@ exports.login = async (login_id, password) => {
try {
//학번 일치 확인
const user = await authProvider.checkLoginIdExist(login_id);
console.log(user);
console.log('id,', user.id);
if (!user) {
return errResponse(baseResponse.MEMBER_NOT_FOUND);
}
if (!user.password) {
return errResponse(baseResponse.MEMBER_NOT_FOUND);
}
//비밀번호 일치 확인
const comparePassword = await bcrypt.compare(password, user.password);
if (!comparePassword) {
Expand All @@ -68,6 +63,17 @@ exports.login = async (login_id, password) => {
}
};

//로그아웃
// exports.logout = async (user_id) => {
// try {
// user = checkUserIdExist(user_id);

// return user.user_id
// } catch (error) {
// console.error(error);
// }
// };

//아이디 유효성 검사
const isValidLoginId = (login_id) => {
// 아이디는 4~12글자로 이루어진 영어 대소문자와 숫자로만 구성
Expand Down
21 changes: 21 additions & 0 deletions src/category/category.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const categoryProvider = require('./category.provider');
const { errResponse } = require('../../config/response');

// 전체 카테고리 가져오기
module.exports.getAllCategories = async (req, res, next) => {
try {
// 카테고리 서비스를 호출하여 전체 카테고리를 가져옵니다.
const categories = await categoryProvider.getAllCategories();

// 성공적으로 카테고리를 가져온 경우 응답을 보냅니다.
res.status(200).json({
success: true,
message: "전체 카테고리를 성공적으로 불러왔습니다.",
data: categories
});
} catch (error) {
// 오류가 발생한 경우 오류 응답을 보냅니다.
console.error('Error fetching categories:', error);
res.status(500).json(errResponse({ code: 500, message: "카테고리를 불러오는 데 실패했습니다." }));
}
};
45 changes: 45 additions & 0 deletions src/category/category.dto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const Category = require('../../models/category');
const Post = require('../../models/post');
const { Op } = require('sequelize');




exports.getAllCategories = async () => {
const d = new Date();
const year = d.getFullYear(); // 년
const month = d.getMonth(); // 월
const day = d.getDate();
const seven = new Date(year, month, day - 7);

const categories = await Category.findAll();


const EX_categories = await Promise.all(categories.map(async (category) => {
const post = await Post.findAll({
where: {
category_id: category.category_id
}
});

const recent = await Post.count({
where: {
[Op.and]: [
{category_id: category.category_id},
{createdAt: {[Op.gte]: seven}}
]
}
});

category.dataValues.post = post;
category.dataValues.count = post.length;
category.dataValues.recent = recent;
return category;
}));

// EX_POST_LIKE를 post.like 기준으로 내림차순으로 정렬
EX_categories.sort((a, b) => b.dataValues.count - a.dataValues.count);

if (EX_categories === null) return false;
else return EX_categories;
};
13 changes: 13 additions & 0 deletions src/category/category.provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const categoryDto = require('./category.dto');

// 전체 카테고리 가져오기 비즈니스 로직
module.exports.getAllCategories = async () => {
try {
// DAO를 통해 전체 카테고리를 가져옵니다.
const categories = await categoryDto.getAllCategories();
return categories;
} catch (error) {
throw error;
}
};

10 changes: 10 additions & 0 deletions src/category/category.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const categoryController = require('./category.controller');
const asyncHandler = require('express-async-handler');


module.exports = function (app) {
app.get('/posting/category', asyncHandler(categoryController.getAllCategories));
};



2 changes: 1 addition & 1 deletion src/like/like.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exports.like = async (req, res, next) => {
exports.likeDel = async (req, res, next) => {
try {
const post_id = req.params.post_id;
const { user_id } = req.body;
const user_id = res.locals.decoded.userId;
console.log("post_id", post_id);
console.log("user_id", user_id);
if (!user_id || !post_id) {
Expand Down
2 changes: 1 addition & 1 deletion src/like/like.dto.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exports.getAllPostLikeLengthResponseDTO = async (post_id) => {
});

if (EX_LIKE === null) return 0;
else return EX_LIKE;
else return parseInt(EX_LIKE);
};

//사용자가 좋아요 누른 게시물 전부 받아오기
Expand Down
26 changes: 26 additions & 0 deletions src/popular/popular.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const popularProvider = require('./popular.provider.js');
const status = require('../../config/response.status.js');
const { response, errResponse } = require('../../config/response.js');

exports.popularWallpaper = async (req, res, next) => {
try {
const getPWallpaper = await popularProvider.getPWallpaper();

console.log("getPWallpaper", getPWallpaper);
return res.send(response(status.SUCCESS, getPWallpaper));
} catch (error) {
console.error('Error controler popularWallpaper:', error);
res.status(400).json({ success: false, message: error.message });
}
};

exports.popularCategory = async (req, res, next) => {
try {
const getPCategory = await popularProvider.getPCategory();

return res.send(response(status.SUCCESS, getPCategory));
} catch (error) {
console.error('Error controler popularCategory:', error);
res.status(400).json({ success: false, message: error.message });
}
};
48 changes: 48 additions & 0 deletions src/popular/popular.dto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const Post = require('../../models/post.js');
const Category = require('../../models/category.js');
const likePost = require('../../models/like_post.js');
const likeDto = require('../like/like.dto.js');

exports.getPopularWallpaperResponseDTO = async () => {
const EX_POST = await Post.findAll();

const EX_POST_LIKE = await Promise.all(EX_POST.map(async (post) => {
const like = await likePost.count({
where: {
post_id: post.post_id
}
});

post.dataValues.like = like;

return post;
}));

// EX_POST_LIKE를 post.like 기준으로 내림차순으로 정렬
EX_POST_LIKE.sort((a, b) => b.dataValues.like - a.dataValues.like);

if (EX_POST_LIKE === null) return false;
else return EX_POST_LIKE;
};

exports.getPopularCategoryResponseDTO = async () => {
const EX_CATEGORY = await Category.findAll();

const EX_POST_CATEGORY = await Promise.all(EX_CATEGORY.map(async (category) => {
const post = await Post.findAll({
where: {
category_id: category.category_id
}
});

category.dataValues.post = post;
category.dataValues.count = post.length;
return category;
}));

// EX_POST_LIKE를 post.like 기준으로 내림차순으로 정렬
EX_POST_CATEGORY.sort((a, b) => b.dataValues.count - a.dataValues.count);

if (EX_POST_CATEGORY === null) return false;
else return EX_POST_CATEGORY;
};
9 changes: 9 additions & 0 deletions src/popular/popular.provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const popularDto = require('./popular.dto.js');

exports.getPWallpaper = async () => {
return popularDto.getPopularWallpaperResponseDTO();
};

exports.getPCategory = async () => {
return popularDto.getPopularCategoryResponseDTO();
};
9 changes: 9 additions & 0 deletions src/popular/popular.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { verifyAToken } = require('../middleware/index.js');
const popularController = require('./popular.controller.js');

module.exports = function (app) {
// 1.인기 바탕화면 가져오기
app.get('/popular/wallpaper',popularController.popularWallpaper);
// 2.인기 카테고리 가져오기
app.get('/popular/category',popularController.popularCategory);
};
24 changes: 24 additions & 0 deletions src/post/post.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const postService = require('./post.service.js');
const postProvider = require('./post.provider.js');
const { response, errResponse } = require('../../config/response.js');
const baseResponse = require('../../config/response.status.js');

//게시물 업로드
exports.postUpload = async (req, res, next) => {
try {
const user_id = res.locals.decoded.userId;
const image = req.file;
console.log("image", image);
if (!user_id ) {
return res.send(errResponse(baseResponse.BAD_REQUEST));
}
if (!image || image.length === 0) {
return res.status(400).json({ success: false, message: "실패" });
}
const result = await postService.createPost(user_id, typeof req.body.data === 'object' ? req.body.data : JSON.parse(req.body.data), image);
return res.send(result);
} catch (error) {
console.error(error);
return next(error);
}
};
36 changes: 36 additions & 0 deletions src/post/post.dao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const BaseError = require('../../config/error.js');
const status = require('../../config/response.status.js');
const Post = require('../../models/post.js');
const Link = require('../../models/link.js');
const Tag = require('../../models/tag.js');

exports.addPost = async (body) => {
try{
const newPost = await Post.create(body);
return newPost;
}catch (err) {
console.error('Error creating post:', err);
throw new BaseError(status.CREATION_FAILED);
}
}

exports.addPostLink = async (body) => {
try{
const newPostLink = await Link.create(body);
return newPostLink;
}catch (err) {
console.error('Error creating postLink:', err);
throw new BaseError(status.CREATION_FAILED);
}
}


exports.addPostTag = async (body) => {
try{
const newPostTag = await Tag.create(body);
return newPostTag;
}catch (err) {
console.error('Error creating postTag:', err);
throw new BaseError(status.CREATION_FAILED);
}
}
2 changes: 2 additions & 0 deletions src/post/post.dto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const User = require('../../models/user');
const Post = require('../../models/post');
1 change: 1 addition & 0 deletions src/post/post.provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const postDto = require('./post.dto.js');
8 changes: 8 additions & 0 deletions src/post/post.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { verifyAToken } = require('../middleware/index.js');
const imageUploader = require('../middleware/image.uploader.js');
const postController = require('./post.controller.js');

module.exports = function (app) {
// 1.게시물 업로드
app.post('/post/upload', verifyAToken, imageUploader.imageUploader.single('image'), postController.postUpload);
};
Loading

0 comments on commit 9fe1817

Please sign in to comment.