Пример #1
0
// POST /jobs/:uuid
func UpdateJob(ctx *Context, form api.CreateJobOptions) {
	if ctx.HasError(form) {
		return
	}

	job := &models.Job{UUID: ctx.Params(":uuid")}
	if err := job.Retrieve(); err != nil {
		if models.IsErrJobNotExist(err) {
			ctx.Handle(404, err)
		} else {
			ctx.Handle(500, "Fail to retrieve job '%s': %v", job.UUID, err)
		}
		return
	}
	if form.Name != nil {
		job.Name = *form.Name
	}
	if form.Content != nil {
		data, err := base64.StdEncoding.DecodeString(job.Content)
		if err != nil {
			ctx.Handle(500, "Fail to decode content '%s': %v", job.UUID, err)
			return
		}
		job.Content = string(data)
	}

	if err := job.Save(); err != nil {
		ctx.Handle(500, "Fail to save job '%s': %v", job.UUID, err)
		return
	}

	ctx.AutoJSON(201, job)
}
Пример #2
0
// GET /jobs/:uuid
func GetJob(ctx *Context) {
	job := &models.Job{UUID: ctx.Params(":uuid")}
	if err := job.Retrieve(); err != nil {
		if models.IsErrJobNotExist(err) {
			ctx.Handle(404, err)
		} else {
			ctx.Handle(500, "Fail to retrieve job '%s': %v", job.UUID, err)
		}
		return
	}

	job.Content = string(base64.StdEncoding.EncodeToString([]byte(job.Content)))

	ctx.AutoJSON(200, job)
}