{"size": 1670, "ext": "js", "max_stars_repo_path": "api/plants/plants-model.js", "max_stars_repo_name": "ft-watermyplants-1/back-end", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "api/plants/plants-model.js", "max_issues_repo_name": "ft-watermyplants-1/back-end", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "api/plants/plants-model.js", "max_forks_repo_name": "ft-watermyplants-1/back-end", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.5675675676, "max_line_length": 68, "alphanum_fraction": 0.6047904192}
const db = require("../data/db-config");

function findAll(user_id) {
  return db("plants").where("user_id", user_id);
}

function findById(user_id, plant_id) {
  return db("plants")
    .where("user_id", user_id)
    .andWhere("plant_id", plant_id)
    .first();
}

function findBy(user_id, filter) {
  return db("plants").where("user_id", user_id).andWhere(filter);
}

async function add(user_id, plant) {
  const [newPlant] = await db("plants").insert(
    {
      user_id: user_id,
      nickname: plant.nickname,
      species: plant.species,
      days_between_watering: plant.days_between_watering,
      notes: plant.notes,
      img_url: plant.img_url,
    },
    [
      "plant_id",
      "nickname",
      "species",
      "days_between_watering",
      "notes",
      "img_url",
      "user_id",
    ]
  );
  return newPlant;
}

async function update(user_id, plant_id, plant) {
  const [updatedPlant] = await db("plants")
    .update(
      {
        user_id: user_id,
        nickname: plant.nickname,
        species: plant.species,
        days_between_watering: plant.days_between_watering,
        notes: plant.notes,
        img_url: plant.img_url,
      },
      [
        "plant_id",
        "nickname",
        "species",
        "days_between_watering",
        "notes",
        "img_url",
        "user_id",
      ]
    )
    .where("plant_id", plant_id);
  return updatedPlant;
}

async function remove(plant_id) {
  const [removedPlant] = await db("plants")
    .del(["plant_id", "nickname", "species", "user_id"])
    .where("plant_id", plant_id);
  return removedPlant;
}

module.exports = { findAll, findById, findBy, add, update, remove };
