Skip to content

Commit

Permalink
fix: closes #24
Browse files Browse the repository at this point in the history
  • Loading branch information
hfreire committed Jun 6, 2017
1 parent 297376a commit a5308ae
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 24 deletions.
8 changes: 5 additions & 3 deletions src/database/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ const createSchema = function () {
'id VARCHAR(36) NOT NULL, ' +
'channel VARCHAR(32) NOT NULL, ' +
'channel_id VARCHAR(64) NOT NULL, ' +
'created_date DATETIME DEFAULT CURRENT_TIMESTAMP, ' +
'updated_date DATETIME DEFAULT CURRENT_TIMESTAMP, ' +
'created_date DATETIME DEFAULT (strftime(\'%Y-%m-%dT%H:%M:%fZ\', datetime(\'now\'))), ' +
'updated_date DATETIME DEFAULT (strftime(\'%Y-%m-%dT%H:%M:%fZ\', datetime(\'now\'))), ' +
'name VARCHAR(32) DEFAULT NULL,' +
'thumbnail_url VARCHAR(512) DEFAULT NULL,' +
'like INTEGER NOT NULL DEFAULT 0,' +
'liked_date DATETIME DEFAULT NULL,' +
'match INTEGER NOT NULL DEFAULT 0,' +
Expand All @@ -57,7 +59,7 @@ const createSchema = function () {
'train INTEGER NOT NULL DEFAULT 0,' +
'trained_date DATETIME DEFAULT NULL,' +
'last_checked_out_date DATETIME DEFAULT NULL,' +
'photos_similarity_mean REAL NOT NULL,' +
'photos_similarity_mean REAL DEFAULT NULL,' +
'data TEXT NOT NULL,' +
'PRIMARY KEY (channel, channel_id)' +
')')
Expand Down
5 changes: 4 additions & 1 deletion src/dates/recommendation/recommendation.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ class Recommendation {
})
})
.then(() => {
if (!recommendation.data.photos[ 0 ].thumbnailUrl) {
if (!recommendation.thumbnail_url) {
return Taste.mentalSnapshot(recommendation.data.photos[ 0 ])
.then((url) => {
recommendation.thumbnail_url = url
})
}
})
.then(() => recommendation)
Expand Down
4 changes: 1 addition & 3 deletions src/dates/taste.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ class Taste {
const thumbnail = _.find(photo.processedFiles, { width: 84, height: 84 })

return savePhoto.bind(this)(thumbnail)
.then(() => {
photo.thumbnailUrl = thumbnail.url
})
.then(() => thumbnail.url)
}

checkPhotosOut (photos) {
Expand Down
55 changes: 55 additions & 0 deletions src/routes/get-recommendation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2017, Hugo Freire <hugo@exec.sh>.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

const { Route } = require('serverful')

const Joi = require('joi')
const Boom = require('boom')

const Logger = require('modern-logger')

const Database = require('../database')

class GetRecommendation extends Route {
constructor () {
super('GET', '/recommendations/{id}', 'Recommendations', 'Returns recommendation by id')
}

handler ({ params = {} }, reply) {
const { id } = params

return Database.Recommendations.findById(id)
.then((recommendation) => {
if (!recommendation) {
return reply(Boom.notFound())
}

reply(recommendation)
})
.catch((error) => {
Logger.error(error)

reply(Boom.badImplementation(error.message, error))
})
}

auth () {
return false
}

validate () {
return {
params: {
id: Joi.string()
.required()
.description('recommendation id')
}
}
}
}

module.exports = new GetRecommendation()
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ const Logger = require('modern-logger')

const Database = require('../database')

class Recommendations extends Route {
class GetRecommendations extends Route {
constructor () {
super('GET', '/recommendations', 'Recommendations', 'Returns all recommendations')
}

handler ({ query = {} }, reply) {
const { page = 1, limit = 25, criteria } = query
const { page = 1, limit = 25, criteria, select } = query

return Promise.try(() => {
if (criteria) {
return JSON.parse(criteria)
}
})
.then((criteria) => Database.Recommendations.findAll(page, limit, criteria))
.then((criteria) => Database.Recommendations.findAll(page, limit, criteria, select))
.then(({ results, totalCount }) => reply({ results, totalCount }))
.catch((error) => {
Logger.error(error)
Expand Down Expand Up @@ -61,10 +61,13 @@ class Recommendations extends Route {
.description('recommendations page results limit'),
criteria: Joi.string()
.optional()
.description('recommendations criteria')
.description('recommendations criteria'),
select: Joi.array().single()
.optional()
.description('recommendations select')
}
}
}
}

module.exports = new Recommendations()
module.exports = new GetRecommendations()
19 changes: 11 additions & 8 deletions src/web/app/recommendations/recommendations.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,21 @@ export class RecommendationsComponent {
this.fadeOutState = 'in'
}

openPersonDialog (recommendation: any) {
const config = new MdDialogConfig()
config.width = '450px'
config.data = { recommendation }

this.dialogRef = this.dialog.open(RecommendationDialogComponent, config)
openRecommendationDialog (recommendationId: string) {
this.recommendationService.getById(recommendationId)
.subscribe((recommendation) => {
const config = new MdDialogConfig()
config.width = '450px'
config.data = { recommendation }

this.dialogRef = this.dialog.open(RecommendationDialogComponent, config)
})
}

getPage (page: number = this.currentPage, limit: number = this.itemsPerPage, criteria: any = this.currentCriteria) {
getPage (page: number = this.currentPage, limit: number = this.itemsPerPage, criteria: any = this.currentCriteria, select: any = [ 'id', 'name', 'thumbnail_url', 'like', 'train', 'match', 'photos_similarity_mean' ]) {
this.loadedPage = false

this.recommendationService.getAll(page, limit, criteria)
this.recommendationService.getAll(page, limit, criteria, select)
.subscribe(({ results, meta }) => {
this.currentPage = page
this.recommendations = results
Expand Down
4 changes: 2 additions & 2 deletions src/web/app/recommendations/recommendations.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
*ngFor="let recommendation of recommendations | paginate: { itemsPerPage: itemsPerPage, currentPage: currentPage, totalItems: totalItems }">
<div class="tint"
[ngClass]="{pass: !recommendation.like, like: recommendation.like, train: recommendation.train}"
(click)="openPersonDialog(recommendation)"
(click)="openRecommendationDialog(recommendation.id)"
mdTooltip="Similarity: {{recommendation.photos_similarity_mean}}%"
tooltipPosition="bottom">
<img [@fadeInOut]="fadeInState" alt="placeholder"
src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMU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FpoiIiKZ8mN6fSXd1A934FSCWtJ3B4Gc+Iz9Faq1rjQsudDNSyOeyOVuy4xnBx1qMdF1n7yq8welOi6z95VeYPSnRdZ+8qvMHpTous/eVXmD0p0XWfvKrzB6U6LbP3lV5g9KdFtn7yr8xvpTots/eVfmN9KzUXJzbKCrhqYZqpssTg9p5xvEfpUqX//Z"/>
<img [@fadeInOut]="fadeOutState" class="thumbnail" alt="image"
[src]="recommendation.data.photos[0].thumbnailUrl"
[src]="recommendation.thumbnail_url"
onError="this.src = './assets/images/placeholder.jpg'" (load)="isLoaded($event)"/>
</div>
</md-grid-tile>
Expand Down
13 changes: 11 additions & 2 deletions src/web/app/recommendations/recommendations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@ import { Http } from '@angular/http'
import { Observable } from 'rxjs/Observable'
import 'rxjs/add/operator/retryWhen'
import 'rxjs/add/operator/timeout'
import _ = require('lodash')

@Injectable()
export class RecommendationsService {
constructor (private http: Http) {
}

getAll (page: number = 1, limit: number = 25, criteria?: any): Observable<any> {
getAll (page: number = 1, limit: number = 25, criteria?: any, select?: any): Observable<any> {
const _criteria = criteria ? `&criteria=${JSON.stringify(criteria)}` : ''
const _select = _.reduce(select, (a, s) => `${a}&select=${s}`, '')

return this.http.get(`/recommendations?page=${page}&limit=${limit}${_criteria}`)
return this.http.get(`/recommendations?page=${page}&limit=${limit}${_criteria}${_select}`)
.retryWhen((error: any) => error.delay(500))
.timeout(2000)
.map((response) => response.json())
}

getById (id: string) {
return this.http.get(`/recommendations/${id}`)
.retryWhen((error: any) => error.delay(500))
.timeout(2000)
.map((response) => response.json())
Expand Down

0 comments on commit a5308ae

Please sign in to comment.