Esempio n. 1
0
// :WEBAPI:
// {
//   "url": "{schema}://{host}/api/v1/search",
//   "method": "GET",
//   "parameters": [
//     {"name": "prefix", "type": "string", "description": "filter objects by prefix", "default": "NaN"},
//     {"name": "limit",  "type": "number", "description": "shows only specified number of retults", "default": "1000"}
//   ],
//   "description": "Returns list of tasks and subtasks"
// }
func SearchHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	p, ok := ctx.Value("http.request.query.params").(*url.Values)
	if !ok {
		ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to obtain params from context")
		return
	}

	apiSearch(ctx, w, r, []Query{
		Query{
			CollName: task.CollName,
			Pattern:  db.QueryDoc{"search.key": db.QueryDoc{"$regex": "^" + p.Get("prefix")}},
			Sort:     []string{"-taskid"},
			Iterator: func(iter db.Iter) interface{} {
				t := task.New()
				if !iter.Next(t) {
					return nil
				}
				return t
			},
		},
		Query{
			CollName: subtask.CollName,
			Pattern:  db.QueryDoc{"search.key": db.QueryDoc{"$regex": "^" + p.Get("prefix")}},
			Sort:     []string{"-taskid"},
			Iterator: func(iter db.Iter) interface{} {
				t := subtask.New()
				if !iter.Next(t) {
					return nil
				}
				return t
			},
		},
	})
}
Esempio n. 2
0
// :WEBAPI:
// {
//   "url": "{schema}://{host}/api/v1/tasks/{taskid}/subtasks/{subtaskid}",
//   "method": "GET",
//   "arguments": [
//     {"name": "taskid",    "type": "integer", "description": "task number"},
//     {"name": "subtaskid", "type": "integer", "description": "subtask number"}
//   ],
//   "description": "Creates new subtask for specified task"
// }
func SubtaskGetHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	p, ok := ctx.Value("http.request.query.params").(*url.Values)
	if !ok {
		ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to obtain params from context")
		return
	}

	apiGet(ctx, w, r, Query{
		CollName: subtask.CollName,
		Pattern:  subtask.MakeID(util.ToInt64(p.Get("task")), util.ToInt64(p.Get("subtask"))),
		One: func(query db.Query) (interface{}, error) {
			t := subtask.New()
			err := query.One(t)
			return t, err
		},
	})
}
Esempio n. 3
0
// :WEBAPI:
// {
//   "url": "{schema}://{host}/api/v1/tasks/{taskid}/subtasks",
//   "method": "POST",
//   "arguments": [
//     {"name": "taskid", "type": "integer", "description": "task number"}
//   ],
//   "description": "Creates new subtask for specified task"
// }
func SubtaskCreateHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	p, ok := ctx.Value("http.request.query.params").(*url.Values)
	if !ok {
		ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to obtain params from context")
		return
	}

	msg, err := ioutil.ReadAll(r.Body)
	if err != nil {
		ahttp.HTTPResponse(w, http.StatusBadRequest, "Unable to read body: %s", err)
		return
	}
	logger.GetHTTPEntry(ctx).WithFields(nil).Debugf("SubtaskCreateHandler: Request body: %s", string(msg))

	t := subtask.New()
	if err = json.Unmarshal(msg, t); err != nil {
		ahttp.HTTPResponse(w, http.StatusBadRequest, "Invalid JSON: %s", err)
		return
	}

	if !t.Owner.IsDefined() {
		ahttp.HTTPResponse(w, http.StatusBadRequest, "owner: mandatory field is not specified")
		return
	}

	if !t.Type.IsDefined() {
		t.Type.Set("unknown")
	}

	if !t.Status.IsDefined() {
		t.Status.Set("active")
	}

	t.TaskID.Set(util.ToInt64(p.Get("task")))
	t.TimeCreate.Set(time.Now().Unix())

	logger.GetHTTPEntry(ctx).WithFields(nil).Debugf("SubtaskCreateHandler: SubTask: %+v", t)

	if !writeSubTask(ctx, w, t) {
		return
	}

	ahttp.HTTPResponse(w, http.StatusOK, "OK")
}
Esempio n. 4
0
// :WEBAPI:
// {
//   "url": "{schema}://{host}/api/v1/tasks/{taskid}/subtasks",
//   "method": "GET",
//   "arguments": [
//     {"name": "taskid", "type": "integer", "description": "task number"}
//   ],
//   "parameters": [
//     {"name": "limit", "type": "number", "description": "shows only specified number of retults", "default": "1000"}
//   ],
//   "description": "Returns information about specified subtask"
// }
func SubtaskListHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	p, ok := ctx.Value("http.request.query.params").(*url.Values)
	if !ok {
		ahttp.HTTPResponse(w, http.StatusInternalServerError, "Unable to obtain params from context")
		return
	}

	apiSearch(ctx, w, r, []Query{
		Query{
			CollName: subtask.CollName,
			Sort:     []string{"subtaskid"},
			Pattern:  db.QueryDoc{"taskid": util.ToInt64(p.Get("task"))},
			Iterator: func(iter db.Iter) interface{} {
				t := subtask.New()
				if !iter.Next(t) {
					return nil
				}
				return t
			},
		},
	})
}