All files app.js

100% Statements 43/43
100% Branches 6/6
100% Functions 8/8
100% Lines 43/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 912x 2x   2x   2x   2x 2x   2x           2x 2x     2x 6x   6x 6x 6x     2x 3x 3x   3x 10x     3x 1x     2x               2x   2x     2x 2x   2x 9x     2x 1x     1x   1x     2x 3x   3x 3x     3x 1x     2x   2x   2x   2x     2x  
const express = require("express");
const cors = require("cors");
 
const { uuid } = require("uuidv4");
 
const app = express();
 
app.use(express.json());
app.use(cors());
 
const repositories = [];
 
/**
 * Object id, title url techs likes
 */
 
app.get("/repositories", (request, response) => {
  return response.json(repositories);
});
 
app.post("/repositories", (request, response) => {
  const { title, url, techs } = request.body;
 
  const repository = { id: uuid(), title, url, techs, likes: 0 };
  repositories.push(repository);
  response.json(repository);
});
 
app.put("/repositories/:id", (request, response) => {
  const { id } = request.params;
  const { title, url, techs } = request.body;
 
  const repositoryIndex = repositories.findIndex(
    (repository) => repository.id === id
  );
 
  if (repositoryIndex < 0) {
    return response.status(400).json({ err: "Repository not found" });
  }
 
  const repository = {
    ...repositories[repositoryIndex],
    id,
    title,
    url,
    techs,
  };
 
  repositories[repositoryIndex] = repository;
 
  return response.json(repository);
});
 
app.delete("/repositories/:id", (request, response) => {
  const { id } = request.params;
 
  const repositoryIndex = repositories.findIndex(
    (repository) => repository.id === id
  );
 
  if (repositoryIndex < 0) {
    return response.status(400).json({ err: "Repository not found" });
  }
 
  repositories.splice(repositoryIndex, 1);
 
  return response.status(204).send();
});
 
app.post("/repositories/:id/like", (request, response) => {
  const { id } = request.params;
 
  const repositoryIndex = repositories.findIndex(
    (repository) => repository.id === id
  );
 
  if (repositoryIndex < 0) {
    return response.status(400).json({ err: "Repository not found" });
  }
 
  const oldRepository = repositories[repositoryIndex];
 
  const updateRepository = { ...oldRepository, likes: oldRepository.likes + 1 };
 
  repositories[repositoryIndex] = updateRepository;
 
  return response.json(updateRepository);
});
 
module.exports = app;