示例#1
0
func GetComponent(db *sql.DB, component_id string) (*entity.Component, error) {
	defer recoverDbPanic()

	stmt := getStatement("component_item", COMPONENT_ITEM_QUERY, db)

	if stmt == nil {
		return nil, errors.New("Query could not be created.")
	}

	rows, err := stmt.Query(component_id)

	defer rows.Close()

	if err != nil || !rows.Next() {
		return nil, err
	}

	component := entity.NewComponent()

	err = rows.Scan(
		&component.Id,
		&component.Gtin,
		&component.Manufacturer,
		&component.ProductName,
		&component.Wattage,
		&component.DailyOperatingTime,
		&component.DaysUntilReplace,
		&component.Mtbf,
		&component.Uri,
		&component.EdgeLength,
		&component.Volume,
	)

	return component, err
}
示例#2
0
func UpdateComponent(ren render.Render, mediaManager *media.Manager, r *http.Request, db *sql.DB, params martini.Params, sessionMessages *template.Messages) {
	id, _ := strconv.ParseInt(params["id"], 10, 64)

	component := entity.NewComponent()

	component.Populate(getComponentMap(r, params["id"]))

	val, errors := validation.ValidateComponent(component, db, COMPONENT)

	if val {
		_, err := repository.UpdateComponent(component, db, params["id"])

		if err != nil {
			log.Println(err)
			sessionMessages.AddDanger("Failed to update component.")
		} else {
			sessionMessages.AddSuccess("Successfully updated component.")

			createMedia(r, mediaManager, db, sessionMessages, COMPONENT, id)
		}

		ren.Redirect("/component")
	} else {
		for k, msg := range errors {
			sessionMessages.AddDanger(k + ": " + msg)
		}

		ren.Redirect("/component/" + params["id"])
	}
}
示例#3
0
func CreateComponent(ren render.Render, mediaManager *media.Manager, r *http.Request, db *sql.DB, params martini.Params, sessionMessages *template.Messages) {
	component := entity.NewComponent()

	component.Populate(getComponentMap(r, ""))

	val, errors := validation.ValidateComponent(component, db, COMPONENT)

	if val {
		res, err := repository.InsertComponent(component, db)

		if err != nil {
			log.Println(err)
			sessionMessages.AddDanger("Failed to create component.")
		} else {
			sessionMessages.AddSuccess("Successfully created component.")

			id, _ := res.LastInsertId()

			createMedia(r, mediaManager, db, sessionMessages, "component", id)
		}

		ren.Redirect("/component")
	} else {
		for k, msg := range errors {
			sessionMessages.AddDanger(k + ": " + msg)
		}

		ren.Redirect("/component/new")
	}
}
示例#4
0
func GetComponents(db *sql.DB) ([]*entity.Component, error) {
	defer recoverDbPanic()

	components := []*entity.Component{}

	stmt := getStatement("component_list", COMPONENT_LIST_QUERY, db)

	if stmt == nil {
		return components, errors.New("Query could not be created.")
	}

	rows, err := stmt.Query()

	defer rows.Close()

	if err != nil {
		return components, err
	}

	for rows.Next() {
		component := entity.NewComponent()

		err := rows.Scan(
			&component.Id,
			&component.Gtin,
			&component.Manufacturer,
			&component.ProductName,
			&component.Wattage,
			&component.DailyOperatingTime,
			&component.DaysUntilReplace,
			&component.Mtbf,
			&component.Uri,
			&component.EdgeLength,
			&component.Volume,
		)

		if err != nil {
			return components, err
		}

		components = append(components, component)
	}

	return components, err
}