정민우

[Add] container list,remove 추가

......@@ -108,18 +108,26 @@ exports.listImage = async (req, res) => {
return sendError(res, 401, 'InvalidToken')
}
try{
let dockerfiles = await Dockerfile.findAndCountAll({
let dockerfiles = await Dockerfile.findAll({
where: {
userId: id
},
attributes: ['id']
})
const dockerfileIds = dockerfiles.map(dockerfile => dockerfile.id)
let images = await Image.findAndCountAll({
where: {
dockerfileId: {
[Op.or]: dockerfileIds
}
},
order: [
['createdAt', 'desc']
]
})
const result = {
count: dockerfiles.count,
data: dockerfiles.rows
count: images.count,
data: images.rows
}
return sendResponse(res, result, 200)
} catch(error) {
......@@ -143,8 +151,8 @@ exports.removeImage = async (req, res) => {
const imageId = req.body.id
let image = await Image.findByPk(imageId)
if (!image) {
logging('image', 'error', { code: 404, message: 'NoDockerfileFound' }, req)
return sendError(res, 404, 'NoDockerfileFound')
logging('image', 'error', { code: 404, message: 'NoImageFound' }, req)
return sendError(res, 404, 'NoImageFound')
}
let dockerfile = await Dockerfile.findByPk(image.dockerfileId)
if (!user || user.id !== dockerfile.userId) {
......@@ -166,7 +174,46 @@ exports.removeImage = async (req, res) => {
}
exports.listContainer = async (req, res) => {
const id = req.decoded.id
if (!id) {
return sendError(res, 401, 'InvalidToken')
}
try{
let dockerfiles = await Dockerfile.findAll({
where: {
userId: id
},
attributes: ['id']
})
const dockerfileIds = dockerfiles.map(dockerfile => dockerfile.id)
let images = await Image.findAll({
where: {
dockerfileId: {
[Op.or]: dockerfileIds
}
},
attributes: ['id']
})
const imageIds = images.map(image => image.id)
let containers = await Container.findAndCountAll({
where: {
dockerfileId: {
[Op.or]: imageIds
}
},
order: [
['createdAt', 'desc']
]
})
const result = {
count: containers.count,
data: containers.rows
}
return sendResponse(res, result, 200)
} catch(error) {
logging('container', 'error', { code: 500, message: error.message }, req)
return sendError(res, 500, error.message)
}
}
exports.createContainer = async (req, res) => {
......@@ -178,7 +225,39 @@ exports.stopContainer = async (req, res) => {
}
exports.removeContainer = async (req, res) => {
const requiredKey = ['id']
const required = checkRequiredExist(req.body, requiredKey)
if (required) {
logging('container', 'error', { code: 400, message: 'missingKey:${required}' }, req)
return sendError(res, 400, `missingKey:${required}`)
}
try {
const user = await currentUser(req.headers.authorization)
const containerId = req.body.id
let container = await Image.findByPk(containerId)
if (!container) {
logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req)
return sendError(res, 404, 'NoContainerFound')
}
let image = await Image.findByPk(container.imageId)
let dockerfile = await Dockerfile.findByPk(image.dockerfileId)
if (!user || user.id !== dockerfile.userId) {
logging('container', 'error', { code: 403, message: 'Unauthorized' }, req)
return sendError(res, 403, 'Unauthorized')
}
await Container.destroy({
where: {
id: container.id
}
})
logging('container', 'delete', null, req)
return sendResponse(res, true, 201)
} catch (error) {
logging('container', 'error', { code: 500, message: error.message }, req)
return sendError(res, 500, error.message)
}
}
//admin
......