Beispiel #1
0
func (r *DeleteQuery) Handle(request Request, requestData interface{}) (shared.IResponse, error) {
	deleteRequest, ok := requestData.(*deleteRequest)
	if !ok {
		return nil, fmt.Errorf("Request Type Mismatch")
	}

	model := r.Core.GetModel()

	qc := databath.GetMinimalQueryConditions(deleteRequest.Collection, "form")

	query, err := databath.GetQuery(request.GetContext(), model, qc, true)
	if err != nil {
		return nil, err
	}

	db, err := request.DB()
	if err != nil {
		return nil, err
	}

	deleteCheckResult, err := query.CheckDelete(db, deleteRequest.Id)
	if err != nil {
		return nil, err
	}

	if deleteCheckResult.Prevents {
		return nil, fmt.Errorf("Could not delete, as owners exist: \n%s", strings.Join(deleteCheckResult.GetIssues(), "\n"))
	}

	err = deleteCheckResult.ExecuteRecursive(db)
	if err != nil {
		return nil, err
	}

	sqlString, err := query.BuildDelete(deleteRequest.Id)
	if err != nil {
		return nil, err
	}

	_, err = db.Exec(sqlString)
	if err != nil {
		return nil, err
	}

	result := createResult{
		Status:   "OK",
		Message:  "Success",
		InsertId: 0,
	}

	deleteObject := map[string]interface{}{
		"collection": deleteRequest.Collection,
		"id":         deleteRequest.Id,
	}
	go request.Broadcast("delete", deleteObject)

	return JSON(result), nil

}
Beispiel #2
0
func (h *FileHandler) writeDatabaseEntry(request shared.IRequest, dbEntry map[string]interface{}, fileCollection string) error {

	qc := databath.GetMinimalQueryConditions(fileCollection, "form")

	q, err := databath.GetQuery(request.GetContext(), h.Model, qc, true)
	if err != nil {
		return err
	}
	sqlString, parameters, err := q.BuildInsert(dbEntry)
	if err != nil {
		return err
	}

	db, err := request.DB()
	if err != nil {
		return err
	}

	fmt.Println(sqlString)

	res, err := db.Exec(sqlString, parameters...)

	if err != nil {
		return err
	}

	pk, _ := res.LastInsertId()
	/*
		actionSummary := &shared_structs.ActionSummary{
			UserId:     request.Session().User().Id,
			Action:     "create",
			Collection: fileCollection,
			Pk:         uint64(pk),
			Fields:     dbEntry,
		}
	*/
	createObject := map[string]interface{}{
		"collection": fileCollection,
		"id":         uint64(pk),
		"object":     dbEntry,
	}

	request.Broadcast("create", createObject)

	return nil
}
Beispiel #3
0
func (r *CreateQuery) Handle(request Request, requestObject interface{}) (shared.IResponse, error) {
	createRequest, ok := requestObject.(*createRequest)
	if !ok {
		return nil, fmt.Errorf("Request Type Mismatch")
	}

	session := request.Session()

	db, err := request.DB()
	if err != nil {
		return nil, err
	}

	if len(createRequest.Fieldset) < 1 {
		createRequest.Fieldset = "form"
	}

	qc := databath.GetMinimalQueryConditions(createRequest.Collection, createRequest.Fieldset)

	model := r.Core.GetModel()

	query, err := databath.GetQuery(request.GetContext(), model, qc, true)
	if err != nil {
		return nil, err
	}

	hookContext := &components.HookContext{
		DB: db,
		ActionSummary: &shared.ActionSummary{
			UserId:     *session.UserID(),
			Action:     "create",
			Collection: createRequest.Collection,
			Pk:         0,
			Fields:     createRequest.Values,
		},
		Session: request.Session(),
		Core:    r.Core,
	}

	r.Core.DoPreHooks(hookContext)

	sqlString, parameters, err := query.BuildInsert(createRequest.Values)
	if err != nil {
		return nil, err
	}

	res, err := db.Exec(sqlString, parameters...)
	if err != nil {
		log.Printf("ERROR in Exec %s: %s\n", sqlString, err.Error())
		return nil, err
	}
	id, err := res.LastInsertId()
	if err != nil {
		return nil, err
	}
	hookContext.ActionSummary.Pk = uint64(id)
	result := createResult{
		Status:   "OK",
		Message:  "Success",
		InsertId: id,
	}

	createObject := map[string]interface{}{
		"collection": createRequest.Collection,
		"id":         id,
		"object":     createRequest.Values,
	}

	go r.Core.DoPostHooks(hookContext) //db, actionSummary, request.Session())
	go request.Broadcast("create", createObject)
	return JSON(result), nil
}