Skip to content
This repository has been archived by the owner on Mar 10, 2018. It is now read-only.

Commit

Permalink
first attempt on Tags: relation in only from Tags to Proposal, with Tags
Browse files Browse the repository at this point in the history
managing the relationship (also correct display on view)
  • Loading branch information
dialex committed Aug 17, 2013
1 parent 4c03486 commit 69a4153
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 76 deletions.
6 changes: 3 additions & 3 deletions app/models/Proposal.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static Proposal createAndSave(String title, String problem, String soluti
p.save();
return p;
}

/**
* @return Increments the number of upvotes and then returns the total number of downvotes.
*/
Expand All @@ -84,8 +84,8 @@ public int getScore() {
return upvotes - downvotes;
}


public String toString() {
return "Proposal(" + id + ") | title: " + title + " | proposer: " + proposer.email;
return "Proposal(id=" + id + ", title=" + title + ", proposer=" + proposer.email + ")";
}

}
61 changes: 61 additions & 0 deletions app/models/Tag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

package models;

import java.util.List;

import javax.persistence.*;

import play.db.ebean.*;

import com.avaje.ebean.*;

/**
* A Tag is a subject/theme that classifies a specimen (like a Proposal).
* Its main objective is to catalog/group similar specimens, easing their
* search.
*/
@Entity
public class Tag extends Model {

@Id
public String name;
public String desc;
@ManyToMany
public List<Proposal> taggedProposals;

/**
* Finder
*/
public static Finder<String, Tag> find = new Finder<String, Tag>(
String.class, Tag.class);

/**
* Constructor
* @param name
* @param description
*/
public Tag(String name, String description) {
this.name = name;
this.desc = description;
}

/**
* @return All proposals tagged with that Tag
*/
public List<Proposal> getTaggedProposals() {
return taggedProposals;
}

/**
* Tags a specific Proposal with the current Tag.
* @param prop Proposal object to tag
*/
public void tagProposal(Proposal prop) {
taggedProposals.add(prop);
}

public String toString() {
return "Tag (name=" + name +")";
}

}
20 changes: 19 additions & 1 deletion app/views/index.scala.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@(proposals: List[Proposal], users: List[User])
@(proposals: List[Proposal], users: List[User], tags: List[Tag])

@main("DCID") {

Expand All @@ -19,4 +19,22 @@ <h2>Proposals</h2>
}
</ul>

<h2>Tags</h2>
@for(tag <- tags) {
<h3>@tag.name</h3>
<p><em>@tag.desc</em></p>
<ul>
@for(prop <- tag.getTaggedProposals()) {
<li>@prop.title</li>
}
</ul>
}

<h2>Proposals + Tags</h2>
<ul>
@for(prop <- proposals) {

}
</ul>

}
95 changes: 23 additions & 72 deletions backlog.txt
Original file line number Diff line number Diff line change
@@ -1,82 +1,33 @@

package models;

import java.util.List;

import javax.persistence.*;

import play.db.ebean.*;

import com.avaje.ebean.*;

/**
* A Tag is a subject or theme that classifies a specimen (like a
* Proposal). Its main objective is to catalog/group similar specimens,
* easing their search.
*/
@Entity
public class Tag extends Model {

@Id
public String name;
public String description;
@ManyToMany
public List<Proposal> taggedProposals;

/**
* Finder
*/
public static Finder<String, Tag> find = new Finder<String, Tag>(
String.class, Tag.class);

/**
* Constructor
* @param name
* @param description
*/
public Tag(String name, String description) {
this.name = name;
this.description = description;
}

public static Tag createAndSave(String name, String description) {
Tag t = new Tag(name, description);
t.save();
return t;
}

/**
* @param tagId The name of a specific Tag
* @return All proposals tagged with that Tag
*/
public List<Proposal> getTaggedProposals(String tagId) {
return taggedProposals;
}

/**
* Tags a specific Proposal with the current Tag.
* @param proposalId Identifier of Proposal to tag
*/
public void tagProposal(Long proposalId) {
Proposal p = Proposal.find.where().eq("id", proposalId).findUnique();
taggedProposals.add(p)
// /**
// * Tags a specific Proposal with the current Tag.
// * @param proposalId Identifier of Proposal to tag
// */
// public void tagProposal(Long proposalId) {
// Proposal p = Proposal.find.where().eq("id", proposalId).findUnique();
// taggedProposals.add(p)
// }


/*
public static Tag findOrCreateByName(String name) {
Tag tag = find.where().eq("name", name).findUnique();
if(tag == null) {
tag = new Tag(name, "No description."); // TODO i18d
}
return tag;
}

public String toString() {
return "Tag(" + name +")";
}

/*
public static Tag findOrCreateByName(String name) {
Tag tag = find.where().eq("name", name).findUnique();
if(tag == null) {
tag = new Tag(name, "No description."); // TODO i18d
}
return tag;
}
*/
*/

}
// public static Tag createAndSave(String name, String description) {
// Tag t = new Tag(name, description);
// t.save();
// return t;
// }


/**
Expand Down
24 changes: 24 additions & 0 deletions conf/evolutions/default/1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,57 @@ create table proposal (
constraint pk_proposal primary key (id))
;

create table tag (
name varchar(255) not null,
desc varchar(255),
constraint pk_tag primary key (name))
;

create table user (
email varchar(255) not null,
name varchar(255),
password varchar(255),
constraint pk_user primary key (email))
;


create table tag_proposal (
tag_name varchar(255) not null,
proposal_id bigint not null,
constraint pk_tag_proposal primary key (tag_name, proposal_id))
;
create sequence proposal_seq;

create sequence tag_seq;

create sequence user_seq;

alter table proposal add constraint fk_proposal_proposer_1 foreign key (proposer_email) references user (email) on delete restrict on update restrict;
create index ix_proposal_proposer_1 on proposal (proposer_email);



alter table tag_proposal add constraint fk_tag_proposal_tag_01 foreign key (tag_name) references tag (name) on delete restrict on update restrict;

alter table tag_proposal add constraint fk_tag_proposal_proposal_02 foreign key (proposal_id) references proposal (id) on delete restrict on update restrict;

# --- !Downs

SET REFERENTIAL_INTEGRITY FALSE;

drop table if exists proposal;

drop table if exists tag;

drop table if exists tag_proposal;

drop table if exists user;

SET REFERENTIAL_INTEGRITY TRUE;

drop sequence if exists proposal_seq;

drop sequence if exists tag_seq;

drop sequence if exists user_seq;

13 changes: 13 additions & 0 deletions conf/test-data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,16 @@


# Tags

- &tag1 !!models.Tag
name: Tecnologia
desc: Propostas que incluem a utilização de algum tipo de tecnologia digital.
taggedProposals:
- *prop1

- &tag2 !!models.Tag
name: Ambiente
desc: Propostas que visam melhorar o ambiente e os espaços verdes.
taggedProposals:
- *prop2
- *prop3

0 comments on commit 69a4153

Please sign in to comment.