WIP: Quick refactor of Admin code

This commit is contained in:
Michael Rode
2019-09-03 00:55:07 -05:00
parent efdfc798a0
commit 43b06c9406
12 changed files with 64 additions and 37 deletions

View File

@@ -58,8 +58,8 @@ Create database, test database, and run the migrations
## Running the tests
`npm test`
## Client (run from WhatToWatch/client)
# install dependencies
# Client (run from WhatToWatch/client)
## install dependencies
`npm install`
## Run application

View File

@@ -11,6 +11,10 @@ class Admin extends Component {
console.log('mike--', this.props.auth)
}
loginAsUser = user => {
console.log('I was clicked and got a user', user)
}
renderUsers() {
if (this.props.auth.users) {
const usersList = this.props.auth.users.map(user => {
@@ -23,7 +27,13 @@ class Admin extends Component {
paragraph
className="z-depth-2 code"
>
{user.email}
<button
onClick={() => this.loginAsUser(user)}
className=""
>
{user.email}
</button>
</Typography>
)
})

View File

@@ -1,6 +1,6 @@
import React, {Component} from 'react';
import {connect} from 'react-redux';
import {Link, Redirect} from 'react-router-dom';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Link, Redirect } from 'react-router-dom';
import HeroSimple from '../HeroSimple';
import * as actions from '../../actions';
import Typography from '@material-ui/core/Typography';
@@ -18,7 +18,7 @@ class PlexPin extends Component {
return <Redirect to="/most-watched" />;
}
if (!this.props.auth.plexToken) {
console.log(this.props.auth);
console.log('mike--', this.props.auth);
return (
<div>
<HeroSimple />
@@ -65,9 +65,9 @@ class PlexPin extends Component {
}
}
function mapStateToProps({auth}) {
function mapStateToProps({ auth }) {
console.log('auth state to prop', auth);
return {auth};
return { auth };
}
export default connect(

View File

@@ -11,7 +11,7 @@ export default function (state = {}, action) {
console.log('action - payload', action.type);
switch (action.type) {
case types.FETCH_USER:
return { ...state, user: action.payload };
return action.payload || false;
case types.FETCH_USERS:
return { ...state, users: action.payload };
case types.FETCH_PIN:

8
package-lock.json generated
View File

@@ -9525,6 +9525,14 @@
"passport-oauth2": "1.x.x"
}
},
"passport-http": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/passport-http/-/passport-http-0.3.0.tgz",
"integrity": "sha1-juU9Q4C+nGDfIVGSUCmCb3cRVgM=",
"requires": {
"passport-strategy": "1.x.x"
}
},
"passport-local": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/passport-local/-/passport-local-1.0.0.tgz",

View File

@@ -61,6 +61,7 @@
"onchange": "^6.0.0",
"passport": "^0.4.0",
"passport-google-oauth20": "^2.0.0",
"passport-http": "^0.3.0",
"passport-local": "^1.0.0",
"pg": "^7.12.1",
"pg-hstore": "^2.3.3",

2
restore-db.sh Normal file
View File

@@ -0,0 +1,2 @@
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U postgres -d recommend_development latest.dump

View File

@@ -8,4 +8,9 @@ router.get('/users', async (req, res) => {
res.send(users);
});
router.get('/login-as-user', async (req, res) => {
res.send(users);
});
export default router;

View File

@@ -3,42 +3,42 @@ import plexApi from './plexApi';
import models from '../../db/models';
import config from '../../../config';
import MovieDb from 'moviedb-promise';
import {Op} from 'sequelize';
import { Op } from 'sequelize';
const mdb = new MovieDb(config.server.movieApiKey);
const updateOrCreate = async (model, where, newItem, beforeCreate) => {
const item = await model.findOne({where});
const item = await model.findOne({ where });
if (!item) {
const createItem = await model.create(newItem, {
returning: true,
plain: true,
raw: true,
});
return {createItem, created: true};
return { createItem, created: true };
} else {
const updatedItem = await model.update(
newItem,
{where: where},
{returning: true, plain: true, raw: true},
{ where: where },
{ returning: true, plain: true, raw: true },
);
return {item, created: false};
return { item, created: false };
}
};
const importTvPosters = async user => {
try {
const mostWatched = await models.PlexLibrary.findAll({
where: {UserId: user.id, type: 'show', views: {[Op.gt]: 0}},
where: { UserId: user.id, type: 'show', views: { [Op.gt]: 0 } },
});
const imageUrls = await mostWatched.map(async show => {
const res = await mdb.searchTv({query: show.title});
const res = await mdb.searchTv({ query: show.title });
return models.PlexLibrary.update(
{
poster_path: res.results[0].poster_path,
},
{
where: {UserId: user.id, title: show.title},
where: { UserId: user.id, title: show.title },
},
);
});
@@ -132,7 +132,7 @@ const importMostWatched = async user => {
};
const importMostWatchedData = async (sectionKey, user) => {
const mostWatchedData = await plexApi.getMostWatched({sectionKey}, user);
const mostWatchedData = await plexApi.getMostWatched({ sectionKey }, user);
const mostWatchedDbData = await updateLibrary(mostWatchedData, user);
return mostWatchedDbData;
};

View File

@@ -1,7 +1,7 @@
import config from '../../../config';
import helpers from '../helpers';
const getUsersUrlParams = function(user) {
const getUsersUrlParams = function (user) {
return {
host: user.plexUrl,
path: '/users',
@@ -11,7 +11,7 @@ const getUsersUrlParams = function(user) {
};
};
const getSectionsUrlParams = function(user) {
const getSectionsUrlParams = function (user) {
return {
host: user.plexUrl,
path: '/library/sections',
@@ -21,20 +21,20 @@ const getSectionsUrlParams = function(user) {
};
};
const mostWatchedUrlParams = function(accountId, sectionKey, limit = 10, user) {
const mostWatchedUrlParams = function (accountId, sectionKey, limit = 10, user) {
return {
host: user.plexUrl,
path: '/library/all/top',
queryParams: {
...(accountId && {accountId}),
...(sectionKey && {type: sectionKey}),
...(limit && {limit}),
...(accountId && { accountId }),
...(sectionKey && { type: sectionKey }),
...(limit && { limit }),
'X-Plex-Token': user.plexToken,
},
};
};
const getLibraryDataBySectionUrlParams = function(sectionId, user) {
const getLibraryDataBySectionUrlParams = function (sectionId, user) {
return {
host: user.plexUrl,
path: `/library/sections/${sectionId}/all`,
@@ -44,7 +44,7 @@ const getLibraryDataBySectionUrlParams = function(sectionId, user) {
};
};
const getUsers = async function(user) {
const getUsers = async function (user) {
try {
const urlParams = getUsersUrlParams(user);
const getUsersUrl = helpers.buildUrl(urlParams);
@@ -55,14 +55,15 @@ const getUsers = async function(user) {
}
};
const getMostWatched = async function(
{accountId, sectionKey, limit = 10},
const getMostWatched = async function (
{ accountId, sectionKey, limit = 10 },
user,
) {
try {
const urlParams = mostWatchedUrlParams(accountId, sectionKey, limit, user);
const mostWatchedUrl = helpers.buildUrl(urlParams);
const response = await helpers.request(mostWatchedUrl);
console.log('most-watched-raw-response---', response)
return response.MediaContainer.Metadata;
} catch (error) {
console.log(error);
@@ -74,7 +75,7 @@ const getMostWatched = async function(
}
};
const getSections = async function(user) {
const getSections = async function (user) {
try {
const urlParams = getSectionsUrlParams(user);
const getSectionsUrl = helpers.buildUrl(urlParams);
@@ -88,7 +89,7 @@ const getSections = async function(user) {
}
};
const getLibraryDataBySection = async function({sectionKey}, user) {
const getLibraryDataBySection = async function ({ sectionKey }, user) {
try {
console.log('sectionId--', sectionKey);
const urlParams = getLibraryDataBySectionUrlParams(sectionKey, user);

View File

@@ -1,11 +1,11 @@
import models from '../../db/models';
import helpers from '../helpers';
import {Op} from 'sequelize';
import { Op } from 'sequelize';
const getMostWatched = async (req, res) => {
try {
const mostWatched = await models.PlexLibrary.findAll({
where: {UserId: req.user.id, type: 'show', views: {[Op.gt]: 0}},
where: { UserId: req.user.id, type: 'show', views: { [Op.gt]: 0 } },
});
console.log('TCL: getMostWatched -> mostWatched', mostWatched);
res.json(mostWatched);

View File

@@ -4,7 +4,7 @@ import helpers from '../helpers';
const similarMedia = async (req, res) => {
try {
const {showName} = req.query;
const { showName } = req.query;
console.log('Show name', showName);
const media = 'show';
const response = await tdawApi.similarMedia(
@@ -20,7 +20,7 @@ const similarMedia = async (req, res) => {
};
const mostWatched = async (req, res) => {
console.log('was i called');
console.log('express-request-object---', req);
const response = await tdawApi.mostWatched();
console.log(response);
res.json(response);
@@ -28,7 +28,7 @@ const mostWatched = async (req, res) => {
const qlooMedia = async (req, res) => {
try {
const {mediaName, mediaType} = req.query;
const { mediaName, mediaType } = req.query;
const mediaId = await tdawApi.qlooMediaId(mediaName, mediaType);
const response = await tdawApi.qlooMedia(mediaId, mediaType);
res.json(response);