Esempio n. 1
0
func doDelete(stmt *sqlx.NamedStmt, args interface{}) error {
	res, err := stmt.Exec(args)
	if err != nil {
		return err
	}

	cnt, err := res.RowsAffected()
	if err != nil {
		return err
	}
	if cnt == 0 {
		return errRecordNotFound
	}

	return nil
}
Esempio n. 2
0
func SaveProject(w http.ResponseWriter, r *http.Request, proute routes.Proute) {

	params := proute.Json.(*SaveProjectParams)

	var err error

	_user, _ := proute.Session.Get("user")
	user := _user.(model.User)

	params.User_id = user.Id

	if params.Start_date == 0 && params.End_date == 0 {
		params.Start_date = math.MinInt32
		params.End_date = math.MaxInt32
	}

	tx, err := db.DB.Beginx()
	if err != nil {
		http.Error(w, "Save Project: Error creating transaction saving project: "+err.Error(), http.StatusBadRequest)
		return
	}

	// Insert or update project
	var stmtProject *sqlx.NamedStmt

	if params.Id == 0 {
		stmtProject, err = tx.PrepareNamed("INSERT INTO \"project\" (\"name\", \"user_id\", \"created_at\", \"updated_at\", \"start_date\", \"end_date\", \"geom\") VALUES (:name, :user_id, now(), now(), :start_date, :end_date, ST_geomFromGeoJSON(:geom)) RETURNING id")
		if err != nil {
			log.Println("Save Project: error preparing insert of project", err)
			_ = tx.Rollback()
			userSqlError(w, err)
			return
		}
		err = stmtProject.Get(&params.Id, params)
		if err != nil {
			log.Println("Save Project: error inserting project", err)
			_ = tx.Rollback()
			userSqlError(w, err)
			return
		}
	} else {
		stmtProject, err = tx.PrepareNamed("UPDATE \"project\" SET \"name\" = :name, \"user_id\" = :user_id, \"updated_at\" = now(), \"start_date\" = :start_date, \"end_date\" = :end_date, \"geom\" = ST_geomFromGeoJSON(:geom) WHERE id = :id")
		_, err = stmtProject.Exec(params)
		if err != nil {
			log.Println("Save Project: error updateing project", err)
			_ = tx.Rollback()
			userSqlError(w, err)
			return
		}
		// Delete links
		_, err = tx.NamedExec("DELETE FROM \"project__chronology\" WHERE project_id=:id", params)
		if err != nil {
			log.Println("Save Project: Error deleting project__chronology", err)
			_ = tx.Rollback()
			userSqlError(w, err)
			return
		}
		_, err = tx.NamedExec("DELETE FROM \"project__charac\" WHERE project_id=:id", params)
		if err != nil {
			log.Println("Save Project: Error deleting project__charac", err)
			_ = tx.Rollback()
			userSqlError(w, err)
			return
		}
		_, err = tx.NamedExec("DELETE FROM \"project__database\" WHERE project_id=:id", params)
		if err != nil {
			log.Println("Save Project: Error deleting project__database", err)
			_ = tx.Rollback()
			userSqlError(w, err)
			return
		}
		_, err = tx.NamedExec("DELETE FROM \"project__map_layer\" WHERE project_id=:id", params)
		if err != nil {
			log.Println("Save Project: Error deleting project__map_layer", err)
			_ = tx.Rollback()
			userSqlError(w, err)
			return
		}
		_, err = tx.NamedExec("DELETE FROM \"project__shapefile\" WHERE project_id=:id", params)
		if err != nil {
			log.Println("Save Project: Error deleting project__shapefile", err)
			_ = tx.Rollback()
			userSqlError(w, err)
			return
		}
	}
	// Insert chronologies

	stmtChronos, err := tx.PrepareNamed("INSERT INTO \"project__chronology\" (project_id, root_chronology_id) VALUES (:project_id, :id)")
	if err != nil {
		log.Println("Save Project: Error inserting chronologies", err)
		_ = tx.Rollback()
		userSqlError(w, err)
		return
	}

	for _, chronoId := range params.Chronologies {
		_, err = stmtChronos.Exec(struct {
			Id         int
			Project_id int
		}{Id: chronoId, Project_id: params.Id})
		if err != nil {
			log.Println(err)
			_ = tx.Rollback()
			userSqlError(w, err)
			return
		}
	}

	// Insert characs

	stmtCharacs, err := tx.PrepareNamed("INSERT INTO \"project__charac\" (project_id, root_charac_id) VALUES (:project_id, :id)")
	if err != nil {
		log.Println("Save Project: Error inserting chronologies", err)
		_ = tx.Rollback()
		userSqlError(w, err)
		return
	}

	for _, characId := range params.Characs {
		_, err = stmtCharacs.Exec(struct {
			Id         int
			Project_id int
		}{Id: characId, Project_id: params.Id})
		if err != nil {
			log.Println(err)
			_ = tx.Rollback()
			userSqlError(w, err)
			return
		}
	}

	// Insert layers

	stmtLayersSHP, err := tx.PrepareNamed("INSERT INTO \"project__shapefile\" (project_id, shapefile_id) VALUES (:project_id, :id)")
	if err != nil {
		log.Println("Save Project: Error inserting shapefile", err)
		_ = tx.Rollback()
		userSqlError(w, err)
		return
	}

	stmtLayersWMS, err := tx.PrepareNamed("INSERT INTO \"project__map_layer\" (project_id, map_layer_id) VALUES (:project_id, :id)")
	if err != nil {
		log.Println("Save Project: Error inserting wms layer", err)
		_ = tx.Rollback()
		userSqlError(w, err)
		return
	}

	for _, layer := range params.Layers {
		if layer.Type == "shp" {
			_, err = stmtLayersSHP.Exec(struct {
				Id         int
				Project_id int
			}{Id: layer.Id, Project_id: params.Id})
		} else if layer.Type == "wms" || layer.Type == "wmts" {
			_, err = stmtLayersWMS.Exec(struct {
				Id         int
				Project_id int
			}{Id: layer.Id, Project_id: params.Id})
		}
		if err != nil {
			log.Println(err)
			_ = tx.Rollback()
			userSqlError(w, err)
			return
		}
	}

	// Insert databases

	stmtDatabases, err := tx.PrepareNamed("INSERT INTO \"project__database\" (project_id, database_id) VALUES (:project_id, :id)")
	if err != nil {
		log.Println("Save Project: Error inserting databases", err)
		_ = tx.Rollback()
		userSqlError(w, err)
		return
	}

	for _, databaseId := range params.Databases {
		_, err = stmtDatabases.Exec(struct {
			Id         int
			Project_id int
		}{Id: databaseId, Project_id: params.Id})
		if err != nil {
			log.Println(err)
			_ = tx.Rollback()
			userSqlError(w, err)
			return
		}

	}
	// Commit
	err = tx.Commit()
	if err != nil {
		log.Println("commit failed")
		userSqlError(w, err)
		return
	}

	j, err := json.Marshal(struct {
		Project_id int `json:"project_id"`
	}{params.Id})
	w.Write(j)

}