Skip to content

Commit

Permalink
Feat: 출석체크 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
[pyola4981] committed May 1, 2023
1 parent aba46a6 commit 6df5a5d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
27 changes: 26 additions & 1 deletion BackEnd/src/controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const { User } = require('../utils/connect');
const { User, Attendance } = require('../utils/connect');
const { Op } = require('sequelize');

const { accessToken, refreshToken } = require('../functions/signJWT');
const bcrypt = require('bcrypt');
const { success, fail } = require('../functions/responseStatus');

/**
* 제공된 이메일과 비밀번호로 로그인을 시도하고, 성공하면 토큰을 발급한다.
Expand Down Expand Up @@ -220,6 +221,29 @@ const profileView = (req, res) => {
res.render('user/profile');
};

const attendCheck = async (req, res) => {
let user_id = req.decoded.id;
const today = new Date().toISOString().slice(0, 10);

const attendance = await Attendance.findOne({
where: { User_id: user_id, attendanceDate: today }
});

if (attendance) {
return fail(res, 400, '오늘은 이미 출석체크를 하셨습니다.');
}

try {
await Attendance.create({ user_id: user_id, attendanceDate: today });
return success(res, 200, '출석이 완료되었습니다.');
} catch (err) {
console.error(err);
return fail(res, 500, `${err.message}`);
}
};



module.exports = {
loginPost,
registerPost,
Expand All @@ -228,4 +252,5 @@ module.exports = {
loginView,
registerView,
profileView,
attendCheck,
};
37 changes: 37 additions & 0 deletions BackEnd/src/models/attendance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const Sequelize = require('sequelize');

module.exports = class Attendance extends Sequelize.Model {
static init(sequelize) {
return super.init(
{
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
unique: true,
primaryKey: true,
},
attendanceDate: {
type: Sequelize.DATEONLY,
allowNull: false
}
},
{
sequelize,
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
modelName: 'Attendance',
tableName: 'attendance_info',
charset: 'utf8',
collate: 'utf8_general_ci',
},
);
}

static associate(db) {
db.Attendance.belongsTo(db.User, { foreignKey: 'user_id', targetKey: 'id', onDelete: 'cascade', onUpdate: 'cascade' });
}
};
1 change: 1 addition & 0 deletions BackEnd/src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const models = {
User: require('./user'),
Post: require('./post'),
Log: require('./log'),
Attendance: require('./attendance'),
};

module.exports = models;
1 change: 1 addition & 0 deletions BackEnd/src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = class User extends Sequelize.Model {

static associate(db) {
db.User.hasMany(db.Post, { foreignKey: 'user_id', sourceKey: 'id', onDelete: 'cascade', onUpdate: 'cascade' });
db.User.hasMany(db.Attendance, { foreignKey: 'user_id', sourceKey: 'id', onDelete: 'cascade', onUpdate: 'cascade' });
db.User.belongsToMany(db.Post, { through: 'user_post', foreignKey: 'user_id', otherKey: 'post_id' });
}
};
3 changes: 3 additions & 0 deletions BackEnd/src/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ router.get('/login', ctrl.loginView);
router.get('/register', ctrl.registerView);
router.get('/profile/output/', ctrl.profileView);

// attendance
router.post('/attend', auth, ctrl.attendCheck);

module.exports = router;

0 comments on commit 6df5a5d

Please sign in to comment.