text: const express = require("express");
  const cors = require("cors");
  const { v4, validate } = require('uuid');
  
  const app = express();
  
  app.use(express.json());
  app.use(cors());
  
  const repositories = [];
  
  function validateRepositoryId (request, response, next) {
    const { id } = request.params;
  
    if (!validate(id)) {
      return response.status(400).json({ error: 'Invalid repository ID.' });
    }
  
    const findRepositoryIndex = repositories.findIndex(repository => repository.id === id);
  
    if (findRepositoryIndex < 0) {
      return response.status(400).json({ error: 'Repository not found.' });
    }
  
    request.findRepositoryIndex = findRepositoryIndex;
  
    return next();
  }
  
  app.use('/repositories/:id', validateRepositoryId);
  
  app.get("/repositories", (request, response) => {
    return response.json(repositories);
  });
  
  app.post("/repositories", (request, response) => {
    const { title, url, techs } = request.body;
  
    const repository = {
      id: v4(),
      title,
      url,
      techs,
      likes: 0,
    }
  
    repositories.push(repository);
  
    return response.json(repository);
  });
  
  app.put("/repositories/:id", (request, response) => {
    const { title, url, techs } = request.body;
  
    const repository = {
      id: v4(),
      title,
      url,
      techs,
      likes: 0,
    }
  
    repositories[request.repositoryIndex] = repository;
  
    return response.status(200).json(repository);
  });
  
  app.delete("/repositories/:id", (request, response) => {
    repositories.splice(request.findRepositoryIndex, 1);
  
    return response.status(204).send();
  });
  
  app.post("/repositories/:id/like", (request, response) => {
    const { id } = request.params;
  
    repositoryIndex = repositories.findIndex(repository => repository.id === id)
  
    if (repositoryIndex < 0) {
      return response.status(400).json({ message: "Repository not found. "});
    }
  
    repositories[repositoryIndex].likes++;
  
    return response.json(repositories[repositoryIndex]);
  });
  
  module.exports = app;