Showing
1 changed file
with
86 additions
and
7 deletions
... | @@ -108,18 +108,26 @@ exports.listImage = async (req, res) => { | ... | @@ -108,18 +108,26 @@ exports.listImage = async (req, res) => { |
108 | return sendError(res, 401, 'InvalidToken') | 108 | return sendError(res, 401, 'InvalidToken') |
109 | } | 109 | } |
110 | try{ | 110 | try{ |
111 | - let dockerfiles = await Dockerfile.findAndCountAll({ | 111 | + let dockerfiles = await Dockerfile.findAll({ |
112 | where: { | 112 | where: { |
113 | userId: id | 113 | userId: id |
114 | }, | 114 | }, |
115 | + attributes: ['id'] | ||
116 | + }) | ||
117 | + const dockerfileIds = dockerfiles.map(dockerfile => dockerfile.id) | ||
118 | + let images = await Image.findAndCountAll({ | ||
119 | + where: { | ||
120 | + dockerfileId: { | ||
121 | + [Op.or]: dockerfileIds | ||
122 | + } | ||
123 | + }, | ||
115 | order: [ | 124 | order: [ |
116 | ['createdAt', 'desc'] | 125 | ['createdAt', 'desc'] |
117 | ] | 126 | ] |
118 | }) | 127 | }) |
119 | - | ||
120 | const result = { | 128 | const result = { |
121 | - count: dockerfiles.count, | 129 | + count: images.count, |
122 | - data: dockerfiles.rows | 130 | + data: images.rows |
123 | } | 131 | } |
124 | return sendResponse(res, result, 200) | 132 | return sendResponse(res, result, 200) |
125 | } catch(error) { | 133 | } catch(error) { |
... | @@ -143,8 +151,8 @@ exports.removeImage = async (req, res) => { | ... | @@ -143,8 +151,8 @@ exports.removeImage = async (req, res) => { |
143 | const imageId = req.body.id | 151 | const imageId = req.body.id |
144 | let image = await Image.findByPk(imageId) | 152 | let image = await Image.findByPk(imageId) |
145 | if (!image) { | 153 | if (!image) { |
146 | - logging('image', 'error', { code: 404, message: 'NoDockerfileFound' }, req) | 154 | + logging('image', 'error', { code: 404, message: 'NoImageFound' }, req) |
147 | - return sendError(res, 404, 'NoDockerfileFound') | 155 | + return sendError(res, 404, 'NoImageFound') |
148 | } | 156 | } |
149 | let dockerfile = await Dockerfile.findByPk(image.dockerfileId) | 157 | let dockerfile = await Dockerfile.findByPk(image.dockerfileId) |
150 | if (!user || user.id !== dockerfile.userId) { | 158 | if (!user || user.id !== dockerfile.userId) { |
... | @@ -166,7 +174,46 @@ exports.removeImage = async (req, res) => { | ... | @@ -166,7 +174,46 @@ exports.removeImage = async (req, res) => { |
166 | } | 174 | } |
167 | 175 | ||
168 | exports.listContainer = async (req, res) => { | 176 | exports.listContainer = async (req, res) => { |
169 | - | 177 | + const id = req.decoded.id |
178 | + if (!id) { | ||
179 | + return sendError(res, 401, 'InvalidToken') | ||
180 | + } | ||
181 | + try{ | ||
182 | + let dockerfiles = await Dockerfile.findAll({ | ||
183 | + where: { | ||
184 | + userId: id | ||
185 | + }, | ||
186 | + attributes: ['id'] | ||
187 | + }) | ||
188 | + const dockerfileIds = dockerfiles.map(dockerfile => dockerfile.id) | ||
189 | + let images = await Image.findAll({ | ||
190 | + where: { | ||
191 | + dockerfileId: { | ||
192 | + [Op.or]: dockerfileIds | ||
193 | + } | ||
194 | + }, | ||
195 | + attributes: ['id'] | ||
196 | + }) | ||
197 | + const imageIds = images.map(image => image.id) | ||
198 | + let containers = await Container.findAndCountAll({ | ||
199 | + where: { | ||
200 | + dockerfileId: { | ||
201 | + [Op.or]: imageIds | ||
202 | + } | ||
203 | + }, | ||
204 | + order: [ | ||
205 | + ['createdAt', 'desc'] | ||
206 | + ] | ||
207 | + }) | ||
208 | + const result = { | ||
209 | + count: containers.count, | ||
210 | + data: containers.rows | ||
211 | + } | ||
212 | + return sendResponse(res, result, 200) | ||
213 | + } catch(error) { | ||
214 | + logging('container', 'error', { code: 500, message: error.message }, req) | ||
215 | + return sendError(res, 500, error.message) | ||
216 | + } | ||
170 | } | 217 | } |
171 | exports.createContainer = async (req, res) => { | 218 | exports.createContainer = async (req, res) => { |
172 | 219 | ||
... | @@ -178,7 +225,39 @@ exports.stopContainer = async (req, res) => { | ... | @@ -178,7 +225,39 @@ exports.stopContainer = async (req, res) => { |
178 | 225 | ||
179 | } | 226 | } |
180 | exports.removeContainer = async (req, res) => { | 227 | exports.removeContainer = async (req, res) => { |
228 | + const requiredKey = ['id'] | ||
229 | + const required = checkRequiredExist(req.body, requiredKey) | ||
230 | + if (required) { | ||
231 | + logging('container', 'error', { code: 400, message: 'missingKey:${required}' }, req) | ||
232 | + return sendError(res, 400, `missingKey:${required}`) | ||
233 | + } | ||
234 | + try { | ||
235 | + const user = await currentUser(req.headers.authorization) | ||
236 | + | ||
237 | + const containerId = req.body.id | ||
238 | + let container = await Image.findByPk(containerId) | ||
239 | + if (!container) { | ||
240 | + logging('container', 'error', { code: 404, message: 'NoContainerFound' }, req) | ||
241 | + return sendError(res, 404, 'NoContainerFound') | ||
242 | + } | ||
243 | + let image = await Image.findByPk(container.imageId) | ||
244 | + let dockerfile = await Dockerfile.findByPk(image.dockerfileId) | ||
245 | + if (!user || user.id !== dockerfile.userId) { | ||
246 | + logging('container', 'error', { code: 403, message: 'Unauthorized' }, req) | ||
247 | + return sendError(res, 403, 'Unauthorized') | ||
248 | + } | ||
181 | 249 | ||
250 | + await Container.destroy({ | ||
251 | + where: { | ||
252 | + id: container.id | ||
253 | + } | ||
254 | + }) | ||
255 | + logging('container', 'delete', null, req) | ||
256 | + return sendResponse(res, true, 201) | ||
257 | + } catch (error) { | ||
258 | + logging('container', 'error', { code: 500, message: error.message }, req) | ||
259 | + return sendError(res, 500, error.message) | ||
260 | + } | ||
182 | } | 261 | } |
183 | 262 | ||
184 | //admin | 263 | //admin | ... | ... |
-
Please register or login to post a comment