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

Commit

Permalink
first attempt at editing an existing field (AJAX)
Browse files Browse the repository at this point in the history
Proposal.Problem editing (following JavaGuide5)
  • Loading branch information
dialex committed Dec 8, 2013
1 parent 766b6db commit c3809f0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 37 deletions.
60 changes: 28 additions & 32 deletions app/controllers/Proposals.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
package controllers;

import java.util.*;

import static play.data.Form.form;
import play.mvc.*;
import models.*;
import views.html.snippets.*;

@Security.Authenticated(Secured.class)
public class Proposals extends Controller {

public static Result createProposal() {
Proposal newProposal = Proposal.createAndSave("New proposal",
"", "", "",
request().username());

return ok(displayProposalDetail.render(newProposal));
}
// public static Result propose() {
// Proposal newProposal = Proposal.createAndSave(title, problem, solution, benefits,
// request().username())
//
//
// .create(
// "New project",
// form().bindFromRequest().get("group"),
// request().username()
// );
// return ok(item.render(newProject));
// }

package controllers;

import static play.data.Form.form;
import play.mvc.*;
import models.*;
import views.html.snippets.*;

@Security.Authenticated(Secured.class)
public class Proposals extends Controller {

public static Result createProposal() {
Proposal newProposal = Proposal.createAndSave("New proposal",
"", "", "",
request().username());

return ok(displayProposalDetail.render(newProposal));
}

public static Result editProblem(Long proposalId) {
if (Secured.isAllowedToEdit(proposalId)) {
return ok(
Proposal.editProblem(proposalId, form().bindFromRequest().get("username"))
);
} else {
return forbidden();
}
}

}
6 changes: 6 additions & 0 deletions app/controllers/Secured.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package controllers;

import models.Proposal;
import play.mvc.*;
import play.mvc.Http.*;

Expand All @@ -14,4 +15,9 @@ public String getUsername(Context ctx) {
public Result onUnauthorized(Context ctx) {
return redirect(routes.Application.login());
}

public static boolean isAllowedToEdit(Long proposalId) {
Proposal proposal = Proposal.find.byId(proposalId);
return proposal.isProposedBy(proposalId, Context.current().request().username());
}
}
35 changes: 30 additions & 5 deletions app/models/Proposal.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package models;

import java.util.*;

import java.util.Date;
import javax.persistence.*;

import play.db.ebean.*;
import play.db.ebean.Model;

/**
* This class represents a Proposal created by a User and voted by other Users.
Expand Down Expand Up @@ -34,8 +33,7 @@ public class Proposal extends Model {
* Finder *
* ====== */
public static Finder<Long, Proposal> find = new Finder<Long, Proposal>(
Long.class, Proposal.class);

Long.class, Proposal.class);

/* ================== *
* Constructor (init) *
Expand Down Expand Up @@ -238,4 +236,31 @@ public String toString() {
return "Proposal(id=" + getId() + ", title=" + getTitle() + ", proposer=" + getProposer().getName() + ")";
}

/* ============== *
* Static methods *
* ============== */

/**
* @param proposalId Identifier of the Proposal to consider
* @param userId The identifier of a User
* @return true if that User is the author of the considered proposal; false otherwise.
*/
public boolean isProposedBy(Long proposalId, String userId) {
String proposersName = find.byId(proposalId).getProposer().getName();
return proposersName.compareTo(userId) == 0 ? true : false;
}

/**
*
* @param proposalId Identifier of Proposal to edit
* @param newProblem New value to set
* @return The value of field <em>problem</em> after the edit
*/
public static String editProblem(Long proposalId, String newProblem) {
Proposal proposalToUpdate = find.byId(proposalId);
proposalToUpdate.setProblem(newProblem);
proposalToUpdate.update();
return proposalToUpdate.getProblem();
}

}

0 comments on commit c3809f0

Please sign in to comment.