Exemplo n.º 1
0
func (a *Article) Create() error {
	if a == nil {
		return apierror.NewServerError("article not instanced")
	}

	if a.Slug == "" {
		a.Slug = slug.Make(a.Title)
	}

	if bson.IsObjectIdHex(a.Slug) {
		return apierror.NewBadRequest("slug cannot be a ObjectId")
	}

	a.CreatedAt = time.Now()

	// To prevent duplicates on the slug, we'll retry the insert() up to 10 times
	originalSlug := a.Slug
	var err error
	for i := 0; i < 10; i++ {
		a.ID = bson.NewObjectId()
		err = Query().Insert(a)

		if err != nil {
			// In case of duplicate we'll add "-X" at the end of the slug, where X is
			// a number
			a.Slug = fmt.Sprintf("%s-%d", originalSlug, i)

			if mgo.IsDup(err) == false {
				return apierror.NewServerError(err.Error())
			}
		} else {
			// everything went well
			return nil
		}
	}

	// after 10 try we just return an error
	return apierror.NewConflict(err.Error())
}
Exemplo n.º 2
0
// ParseParams will parse the params from the given request, and store them
// into the endpoint
func (r *Request) ParseParams() error {
	params := reflect.ValueOf(r.Params)
	if params.Kind() == reflect.Ptr {
		params = params.Elem()
	}

	sources, err := r.ParamsBySource()
	if err != nil {
		return err
	}

	nbParams := params.NumField()
	for i := 0; i < nbParams; i++ {
		param := params.Field(i)
		paramInfo := params.Type().Field(i)
		tags := paramInfo.Tag

		if param.Kind() == reflect.Ptr {
			param = param.Elem()
		}

		// We make sure we can update the value of field
		if !param.CanSet() {
			return apierror.NewServerError("Field %s could not be set", paramInfo.Name)
		}

		// We control the type of
		paramLocation := strings.ToLower(tags.Get("from"))
		source, found := sources[paramLocation]
		if !found {
			source = sources["url"]
		}

		args := &setParamValueArgs{
			param:     &param,
			paramInfo: &paramInfo,
			tags:      &tags,
			source:    &source,
		}

		if err := r.setParamValue(args); err != nil {
			return err
		}
	}

	return nil
}
Exemplo n.º 3
0
func (req *Request) Error(e error) {
	if req == nil {
		return
	}

	err, casted := e.(*apierror.ApiError)
	if !casted {
		err = apierror.NewServerError(e.Error()).(*apierror.ApiError)
	}

	switch err.Code() {
	case http.StatusInternalServerError:
		logger.Errorf("%s - %s", err.Error(), req)
		http.Error(req.Response, `{"error":"Something went wrong"}`, http.StatusInternalServerError)
	default:
		if app.GetContext().Params.Debug {
			logger.Errorf("%s - %s", err.Error(), req)
		}
		http.Error(req.Response, fmt.Sprintf(`{"error":"%s"}`, err.Error()), err.Code())
	}
}
Exemplo n.º 4
0
// HandlerAdd represents an API handler to add a new article
func HandlerAdd(req *router.Request) {
	params, ok := req.Params.(*HandlerAddParams)
	if !ok {
		req.Error(apierror.NewServerError("Couldn't cast params"))
	}

	a := &Article{
		Title:       params.Title,
		Subtitle:    params.Subtitle,
		Content:     params.Content,
		Description: params.Description,
		IsDeleted:   false,
		IsPublished: false,
	}

	if err := a.Save(); err != nil {
		req.Error(err)
	}

	req.Created(a)
}