简体   繁体   English

React.js和Redux-如何创建一个“流行”页面

[英]React.js and Redux - How to create a 'popular' page

I'm trying to create a page on my app that shows a list of the most popular events to go to. 我正在尝试在我的应用程序上创建一个页面,以显示最受欢迎的事件列表。 The events that have the most 'interested in' and 'going to' results. 对结果“最感兴趣”和“最感兴趣”的事件。

I'm using knex and sqlite3 for my database and currently have junction tables for users and the events they're interested in, and another for the user and events they're going to. 我将knex和sqlite3用于我的数据库,当前有用于用户及其感兴趣的事件的联结表,以及用于用户及其将要发生的事件的联结表。

How would I structure a page to return the most interacted with events? 我将如何构造一个页面以返回与事件交互最多的页面? Do I need to add some sort of count column to the events table and return events with the biggest count (having a function that each time it is selected the count goes up...)? 我是否需要在事件表中添加某种类型的计数列并返回具有最大计数的事件(具有每次选择计数都会增加的功能……)?

I'm mainly not sure about where to start with this/how to approach this. 我主要不确定从何处着手/如何解决这个问题。

Thanks! 谢谢!


This is how my data is structured so far: 到目前为止,这是我的数据的结构:

Events Table: 事件表:

exports.up = (knex, Promise) => {
  return knex.schema.hasTable('events').then(function (exists) {
    if (!exists) {
      return knex.schema.createTable('events', (table) => {
        table.increments('id').primary()
        table.string('eventName')
        table.date('date_start')
        table.string('time_start')
        table.date('date_end')
        table.string('time_end')
        table.string('image')
        table.string('description')
        table.string('access')
        table.string('tickets')
        table.string('restrictions')
        table.string('eventType')
      })
    }
  })
}

exports.down = (knex, Promise) => {
  return knex.schema.dropTable('events')
}

Seed Data: user_goingto_events 种子数据:user_goingto_events

exports.seed = function(knex, Promise) {
  // Deletes ALL existing entries
  return knex('user_goingto_events').del()
    .then(function () {
      // Inserts seed entries
      return knex('user_goingto_events').insert([
        {id: 1, user_id: 1, event_id: 1},
        {id: 2, user_id: 1, event_id: 2},
        {id: 3, user_id: 1, event_id: 3},
        {id: 4, user_id: 2, event_id: 4},
        {id: 5, user_id: 2, event_id: 5},
        {id: 6, user_id: 2, event_id: 6}
      ])
    })
}

Seed Data: user_interestedin_events 种子数据:user_interestedin_events

exports.seed = function(knex, Promise) {
  // Deletes ALL existing entries
  return knex('user_interestedin_events').del()
    .then(function () {
      // Inserts seed entries
      return knex('user_interestedin_events').insert([
        {id: 1, user_id: 1, event_id: 4},
        {id: 2, user_id: 1, event_id: 5},
        {id: 3, user_id: 1, event_id: 6},
        {id: 4, user_id: 2, event_id: 1},
        {id: 5, user_id: 2, event_id: 2},
        {id: 6, user_id: 2, event_id: 3}
      ])
    })
}

The approach I would take would be to SUM() the event counts for each of the 2 tables, and then UNION them together: 我将采用的方法是对2个表中的每个表的SUM()事件计数,然后将它们联合在一起:

In SQL that looks like: 在SQL中看起来像:

select event_id, count(*) as cnt_interest, 0 as cnt_going
   from user_interestedin_events
   group by event_id
UNION
select event_id, 0 as cnt_interest, count(*) as cnt_going
   from user_goingto_events
   group by event_id;

Then I would use that result as the input to summarize the counts producing a single row per event. 然后,我将使用该结果作为输入来总结计数,从而每个事件产生一行。 Theoretically, if you had a result cursor named 'part1data', then the SQL would look like this: 从理论上讲,如果您有一个名为“ part1data”的结果游标,则该SQL将如下所示:

select eventName, event_id, sum(cnt_interest) as sum_interest, sum(cnt_going) as sum_going
   from part1data
   left join events on events.id = event_id
   group by event_id;
   order by sum(cnt_interest)+sum(cnt_going) desc

You can see I ordered it by popularity, and also included the event name via a left join for the users convenience. 您可以看到我按受欢迎程度对其进行了排序,并且为了方便用户,还通过左连接添加了事件名称。

Now to put that in Knex. 现在将其放在Knex中。 Since this is a bit complicated, I would just embed it as a raw() SQL and run it as follows: 由于这有点复杂,所以我将其嵌入为raw()SQL并按以下方式运行:

const rawRpt = 
 `select eventName, event_id, sum(cnt_interest) as sum_interest, sum(cnt_going) as sum_going
   from 
     ( select event_id, count(*) as cnt_interest, 0 as cnt_going
          from user_interestedin_events
          group by event_id
        UNION
       select event_id, 0 as cnt_interest, count(*) as cnt_going
          from user_goingto_events
          group by event_id
     )
   left join events on events.id = event_id
   group by event_id;
   order by sum(cnt_interest)+sum(cnt_going) desc`

return Knex.raw(rawRpt);
   .on('query-error', function(ex, obj) {
        console.log("KNEX query-error ex:", ex, "obj:", obj);
   })
   .then(function(retList) {
        console.log("Returns:", retList);
   });

You can probably see that I embedded the first query inside the second. 您可能会看到我在第二个查询中嵌入了第一个查询。 I would also note, for 90% of the Knex SQLs that I do, I do NOT use raw(). 我还要指出,对于90%的Knex SQL,我不使用raw()。 But in this case, the complexity, the creation of multiple calculated fields, and having no input variables ... this is just easier. 但是在这种情况下,复杂度高,创建了多个计算字段并且没有输入变量……这更容易。

I hope this helps. 我希望这有帮助。 (Disclaimer - I didn't run the code so it's probably not perfect.) (免责声明-我没有运行代码,所以它可能并不完美。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM