editVessel.ts (17.06.2024)

PHOTO EMBED

Sun Jun 16 2024 23:06:12 GMT+0000 (Coordinated Universal Time)

Saved by @rafal_rydz

@ -0,0 +1,59 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { rdsConnection } from "@/lib/aws";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse,
) {
  if (req.method === "POST") {
    const {
      imo,
      cargo_type,
      cargo_sub_type,
      mmsi,
      vessel_name,
      year_of_build,
      flag,
      grt,
      dwt,
      overall_length,
      beam,
      maximum_draft,
    } = req.body;

    const query = `
      UPDATE spotship_vessel
      SET cargo_type = ?, cargo_sub_type = ?, mmsi = ?, vessel_name = ?, year_of_build = ?, flag = ?, grt = ?, dwt = ?, overall_length = ?, beam = ?, maximum_draft = ?
      WHERE imo = ?
    `;

    rdsConnection.query(
      query,
      [
        cargo_type,
        cargo_sub_type,
        mmsi,
        vessel_name,
        year_of_build,
        flag,
        grt,
        dwt,
        overall_length,
        beam,
        maximum_draft,
        imo,
      ],
      (error, results) => {
        if (error) {
          return res.status(500).json({ success: false, error: error.message });
        }
        return res.status(200).json({ success: true, data: results });
      },
    );
  } else {
    res.setHeader("Allow", ["POST"]);
    return res
      .status(405)
      .send(`Method ${req.method ?? "Undefined"} Not Allowed`);
  }
}
content_copyCOPY