This repository has been archived by the owner on Oct 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds topic selections for subscribers
- Loading branch information
Richard Rodgers
committed
Apr 3, 2015
1 parent
2c89e4f
commit 834d587
Showing
13 changed files
with
534 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright (c) 2015 MIT Libraries | ||
* Licensed under: http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package models | ||
|
||
import play.api.db.DB | ||
import play.api._ | ||
import play.api.Play.current | ||
|
||
import anorm.SqlParser._ | ||
import anorm.~ | ||
import anorm.SQL | ||
|
||
/** An agent is a software service that can perform operations for | ||
* benefit of subscribers, such as suggesting topics to subscribe to, | ||
* automatically subscribing or unsubscribing, etc | ||
* | ||
* @author richardrodgers | ||
*/ | ||
|
||
case class Agent(id: Int, | ||
tag: String, | ||
label: String, | ||
description: String, | ||
code: String, | ||
params: String, | ||
icon: Option[String]) | ||
|
||
object Agent { | ||
|
||
val agent = { | ||
get[Int]("id") ~ get[String]("tag") ~ get[String]("label") ~ get[String]("description") ~ | ||
get[String]("code") ~ get[String]("params") ~ get[Option[String]]("icon") map { | ||
case id ~ tag ~ label ~ description ~ code ~ params ~ icon => Agent(id, tag, label, description, code, params, icon) | ||
} | ||
} | ||
|
||
def create(tag: String, label: String, description: String, code: String, params: String, icon: Option[String]) = { | ||
DB.withConnection { implicit c => | ||
SQL("insert into agent (tag, label, description, code, params, icon) values ({tag}, {label}, {description}, {code}, {params}, {icon})") | ||
.on('tag -> tag, 'label -> label, 'description -> description, 'code -> code, 'params -> params, 'icon -> icon).executeInsert() | ||
} | ||
} | ||
|
||
def make(tag: String, label: String, description: String, code: String, params: String, icon: Option[String]) = { | ||
findById(create(tag, label, description, code, params, icon).get.toInt).get | ||
} | ||
|
||
def all: List[Agent] = { | ||
DB.withConnection { implicit c => | ||
SQL("select * from agent").as(agent *) | ||
} | ||
} | ||
|
||
def findById(id: Int): Option[Agent] = { | ||
DB.withConnection { implicit c => | ||
SQL("select * from agent where id = {id}").on('id -> id).as(agent.singleOpt) | ||
} | ||
} | ||
|
||
def findByTag(tag: String): Option[Agent] = { | ||
DB.withConnection { implicit c => | ||
SQL("select * from agent where tag = {tag}").on('tag -> tag).as(agent.singleOpt) | ||
} | ||
} | ||
|
||
def mapView: Map[String, String] = { | ||
all map (cp => cp.id.toString -> cp.tag) toMap | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (c) 2015 MIT Libraries | ||
* Licensed under: http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package models | ||
|
||
import java.util.Date | ||
|
||
import play.api.db.DB | ||
import play.api._ | ||
import play.api.Play.current | ||
|
||
import anorm.SqlParser._ | ||
import anorm.~ | ||
import anorm.SQL | ||
import anorm.Row | ||
|
||
/** TopicPick is a topic suggestion (potential subscription) for a subscriber | ||
* Following a review, the pick is resolved to a subscription, or discarded | ||
* | ||
* @author richardrodgers | ||
*/ | ||
|
||
case class TopicPick(id: Int, // DB key | ||
subscriberId: Int, // DB key of subscriber | ||
topicId: Int, // DB key of selected topic | ||
agentId: Int, // DB key of agent that created it | ||
created: Date, // when pick created | ||
resolved: Date) { // when pick resolved | ||
|
||
def topic = { | ||
DB.withConnection { implicit c => | ||
SQL("select * from topic where id = {topic_id}").on('topic_id -> topicId).as(Topic.topic.singleOpt).get | ||
} | ||
} | ||
|
||
def resolve(accept: Boolean) = { | ||
// not currently remembering state, so just delete | ||
DB.withConnection { implicit c => | ||
SQL("delete from topic_pick where id = {id}").on('id -> id).executeUpdate() | ||
} | ||
} | ||
} | ||
|
||
object TopicPick { | ||
|
||
val pick = { | ||
get[Int]("id") ~ get[Int]("subscriber_id") ~ get[Int]("topic_id") ~ get[Int]("agent_id") ~ | ||
get[Date]("created") ~ get[Date]("resolved") map { | ||
case id ~ subscriberId ~ topicId ~ agentId ~ created ~ resolved => | ||
TopicPick(id, subscriberId, topicId, agentId, created, resolved) | ||
} | ||
} | ||
|
||
def picked(topicId: Int, subscriberId: Int) = { | ||
DB.withConnection { implicit c => | ||
SQL("select * from topic_pick where topic_id = {topic_id} and subscriber_id = {subscriber_id}") | ||
.on('topic_id -> topicId, 'subscriber_id -> subscriberId).as(pick.singleOpt).isDefined | ||
} | ||
} | ||
|
||
def findById(id: Int): Option[TopicPick] = { | ||
DB.withConnection { implicit c => | ||
SQL("select * from topic_pick where id = {id}").on('id -> id).as(pick.singleOpt) | ||
} | ||
} | ||
|
||
def create(subscriberId: Int, topicId: Int, agentId: Int) = { | ||
DB.withConnection { implicit c => | ||
SQL("insert into topic_pick (subscriber_id, topic_id, agent_id, created, resolved) values ({subscriber_id}, {topic_id}, {agent_id}, {created}, {resolved})") | ||
.on('subscriber_id -> subscriberId, 'topic_id -> topicId, 'agent_id -> agentId, 'created -> new Date, 'resolved -> new Date).executeInsert() | ||
} | ||
} | ||
|
||
def make(subscriberId: Int, topicId: Int, agentId: Int) = { | ||
findById(create(subscriberId, topicId, agentId).get.toInt).get | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@***************************************************************************** | ||
* Entry page for topic pick browsing (Review) * | ||
* Copyright (c) 2015 MIT Libraries * | ||
*****************************************************************************@ | ||
@(id: Int, picks: List[TopicPick], page: Int, total: Long)@*(implicit hubContext: HubContext)*@ | ||
@layout.main("Topic Picks Browse - TopicHub") { | ||
<div class="page-header"> | ||
<h2>Review Topic Picks</h2> | ||
@tags.summary(page, 10, total, picks.length) | ||
</div> | ||
<ul> | ||
@picks.map { pick => | ||
<li><a href="@routes.Application.topic(pick.topicId)">@pick.topic.tag</a> @pick.topic.name</li> | ||
} | ||
</ul> | ||
@tags.pagination(page, 10, total, routes.Application.pickBrowse(id).toString, picks.length) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# core model types | ||
# --- !Ups | ||
|
||
-- Fixes #93 tag field too short: varchar(255) | ||
ALTER TABLE topic ALTER tag TYPE varchar; | ||
|
||
CREATE SEQUENCE agent_id_seq; | ||
CREATE TABLE agent ( | ||
id integer NOT NULL DEFAULT nextval('agent_id_seq'), | ||
tag varchar UNIQUE, | ||
label varchar, | ||
description varchar, | ||
code varchar, | ||
params varchar, | ||
icon varchar, | ||
PRIMARY KEY(id) | ||
); | ||
|
||
CREATE SEQUENCE topic_pick_id_seq; | ||
CREATE TABLE topic_pick ( | ||
id integer NOT NULL DEFAULT nextval('topic_pick_id_seq'), | ||
subscriber_id integer, | ||
topic_id integer, | ||
agent_id integer, | ||
created timestamp, | ||
resolved timestamp, | ||
FOREIGN KEY(subscriber_id) REFERENCES subscriber(id), | ||
FOREIGN KEY(topic_id) REFERENCES topic(id), | ||
FOREIGN KEY(agent_id) REFERENCES agent(id), | ||
PRIMARY KEY(id) | ||
); | ||
|
||
# --- !Downs | ||
|
||
ALTER TABLE topic ALTER tag TYPE varchar(255); | ||
|
||
DROP TABLE topic_pick; | ||
DROP SEQUENCE topic_pick_id_seq; | ||
DROP TABLE agent; | ||
DROP SEQUENCE agent_id_seq; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.