Skip to content

Commit

Permalink
Merge pull request #88 from passwd10/feature/issue-api
Browse files Browse the repository at this point in the history
[#75, #79] Issues, Milestones CRUD 구현
  • Loading branch information
junsushin-dev authored Nov 3, 2020
2 parents 4a01593 + 407bfa4 commit a5f2240
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 25 deletions.
2 changes: 1 addition & 1 deletion backend/src/db/models/issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
module.exports = (sequelize, DataTypes) => {
class Issue extends Model {
static associate(models) {
Issue.belongsTo(models.User); // author
Issue.belongsTo(models.User, { foreignKey: 'author' }); // author
Issue.belongsToMany(models.User, {
through: 'Assignees',
});
Expand Down
49 changes: 41 additions & 8 deletions backend/src/routes/issues.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
const express = require('express');
const router = express.Router();
const createError = require('http-errors');

router.get('/', (req, res) => {
res.json();
const {
getIssues,
addIssue,
updateIssues,
deleteIssues,
} = require('../services/issueService');

const SUCCESS_MESSAGE = { message: 'success' };

router.get('/', async (req, res, next) => {
try {
const issues = await getIssues();
res.status(200).json(issues);
} catch (error) {
next(createError(500));
}
});

router.post('/', (req, res) => {
res.json();
router.post('/', async (req, res, next) => {
try {
const newIssue = req.body;
await addIssue(newIssue);
res.status(200).json(SUCCESS_MESSAGE);
} catch (error) {
next(createError(400));
}
});

router.put('/', (req, res) => {
res.json();
router.put('/', async (req, res, next) => {
try {
const modifiedContents = req.body;
await updateIssues(modifiedContents);
res.status(200).json(SUCCESS_MESSAGE);
} catch (error) {
next(createError(500));
}
});

router.delete('/', (req, res) => {
res.json();
router.delete('/', async (req, res, next) => {
try {
const { id } = req.body;
await deleteIssues(id);
res.status(200).json(SUCCESS_MESSAGE);
} catch (error) {
next(createError(500));
}
});


Expand Down
41 changes: 33 additions & 8 deletions backend/src/routes/milestones.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,52 @@ const express = require('express');
const router = express.Router();
const createError = require('http-errors');

const { getMilestones } = require('../services/milestoneService');
const {
getMilestones,
addMilestone,
updateMilestone,
deleteMilestone,
} = require('../services/milestoneService');

const SUCCESS_MESSAGE = { message: 'success' };

router.get('/', async (req, res, next) => {
try {
const milestones = await getMilestones();
res.json(milestones);
res.status(200).json(milestones);
} catch (error) {
next(createError(500));
}
});

router.post('/', (req, res) => {
res.json();
router.post('/', async (req, res, next) => {
try {
const newMilestone = req.body;
await addMilestone(newMilestone);
res.status(200).json(SUCCESS_MESSAGE);
} catch (error) {
next(createError(400));
}
});

router.put('/', (req, res) => {
res.json();
router.put('/', async (req, res, next) => {
try {
const modifiedContents = req.body;
await updateMilestone(modifiedContents);
res.status(200).json(SUCCESS_MESSAGE);
} catch (error) {
next(createError(500));
}
});

router.delete('/', (req, res) => {
res.json();
router.delete('/', async (req, res, next) => {
try {
const { title } = req.body;
await deleteMilestone(title);
res.status(200).json(SUCCESS_MESSAGE);
} catch (error) {
next(createError(500));
}
});


Expand Down
35 changes: 35 additions & 0 deletions backend/src/services/issueService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { Issue } = require('../db/models');

const getIssues = async () => {
const issues = await Issue.findAll({
where: { isDeleted: false },
});
return issues;
};

const addIssue = async (newIssue) => {
await Issue.create(newIssue);
};

const updateIssues = async (modifiedContents) => {
const ids = modifiedContents.id;
delete modifiedContents.id;
return await Issue.update(
modifiedContents,
{ where: { id: ids, isDeleted: false } },
);
};

const deleteIssues = async (id) => {
return await Issue.update(
{ isDeleted: true },
{ where: { id: id } },
);
};

module.exports = {
getIssues,
addIssue,
updateIssues,
deleteIssues,
};
27 changes: 19 additions & 8 deletions backend/src/services/milestoneService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@ const { Milestone } = require('../db/models');

const getMilestones = async () => {
const milestones = await Milestone.findAll({
whiere: {
where: {
isDeleted: false,
},
});
return milestones;
};

const addMilestone = () => {

const addMilestone = async (newMilestone) => {
return await Milestone.create(newMilestone);
};

const updateMilestone = () => {

const updateMilestone = async (modifiedContents) => {
return await Milestone.update(
modifiedContents,
{ where: { title: modifiedContents.title } },
);
};

const deleteMilestone = () => {
//isDeleted true로 변경
const deleteMilestone = async (title) => {
return await Milestone.update(
{ isDeleted: true },
{ where: { title: title } },
);
};

module.exports = { getMilestones };
module.exports = {
getMilestones,
addMilestone,
updateMilestone,
deleteMilestone,
};

0 comments on commit a5f2240

Please sign in to comment.