Help and docs
Sign in

grindlemire/dotfiles code browser

01-07-demo_410910fe_add_user_search b5c4d87
JavaScript · 57 lines · 1.2 KB
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
const express = require('express');
const app = express();
const port = 3000;

// Fake data for tasks
const tasks = [
  {
    id: 1,
    description: 'Complete monthly financial report'
  },
  {
    id: 2,
    description: 'Plan team building activity'
  },
  {
    id: 3,
    description: 'Update project documentation'
  }
];

// Fake data for users
const users = [
  {
    id: 101,
    name: 'Alice Smith'
  },
  {
    id: 102,
    name: 'Bob Johnson'
  },
  {
    id: 103,
    name: 'Charlie Brown'
  }
];

app.get('/search', (req, res) => {
  // Retrieve the query parameter
  const query = req.query.query?.toLowerCase() || '';

  // Filter tasks based on the query
  const filteredTasks = tasks.filter(task =>
    task.description.toLowerCase().includes(query)
  ).sort((a, b) => a.description.localeCompare(b.description));

  // Filter users based on the query
  const filteredUsers = users.filter(user =>
    user.name.toLowerCase().includes(query)
  ).sort((a, b) => a.name.localeCompare(b.name));

  // Return both sets of results
  res.json({ tasks: filteredTasks, users: filteredUsers });
});

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});