Created
April 12, 2012 16:20
-
-
Save danielberndt/2368789 to your computer and use it in GitHub Desktop.
Crud Controller
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
package controllers; | |
import models.Model; | |
import models.User; | |
import play.api.templates.Html; | |
import play.data.Form; | |
import play.mvc.Controller; | |
import play.mvc.Result; | |
public abstract class ControllerAgent<T extends Model, F1, F2> { | |
protected Form<F1> composeForm; | |
protected Form<F2> editForm; | |
public ControllerAgent(Class<F1> clazzComposeForm, Class<F2> clazzEditForm) { | |
this.composeForm = Controller.form(clazzComposeForm); | |
this.editForm = Controller.form(clazzEditForm); | |
} | |
protected abstract Html displayCompose(Form<F1> composeForm, Long... preIds); | |
protected abstract Html displayItem(T element, Long... preIds); | |
protected abstract Html displayEditForm(Form<F2> editForm, T element, Long... preIds); | |
protected abstract T createFromForm(Form<F1> composeForm, Long... preIds); | |
protected abstract T editFromForm(T element, Form<F2> editForm); | |
protected abstract F2 translateItemToEditFormInstance(T element); | |
public Result compose(Long... preIds) { | |
return Controller.ok(displayCompose(composeForm, preIds)); | |
} | |
public Result create(Long... preIds) { | |
Form<F1> filledForm = composeForm.bindFromRequest(); | |
if (filledForm.hasErrors()) return Controller.status(409,displayCompose(filledForm, preIds)); | |
T element = createFromForm(filledForm, preIds); | |
return Controller.ok(displayItem(element, preIds)); | |
} | |
public Result edit(T element, Long... preIds) { | |
if (element==null) return Controller.notFound(); | |
return Controller.ok(displayEditForm(editForm.fill(translateItemToEditFormInstance(element)), element, preIds)); | |
} | |
public Result update(T element, Long... preIds) { | |
if (element==null) return Controller.notFound(); | |
Form<F2> filledForm = editForm.bindFromRequest(); | |
if (filledForm.hasErrors()) return Controller.status(409,displayEditForm(filledForm, element, preIds)); | |
T editItem = editFromForm(element, filledForm); | |
return Controller.ok(displayItem(editItem, preIds)); | |
} | |
public Result delete(T element, User user) { | |
if (element==null) return Controller.notFound(); | |
if (element.deleteBy(user)) return Controller.noContent(); | |
else return Controller.unauthorized(); | |
} | |
public abstract static class DefaultControllerAgent<T extends Model> extends ControllerAgent<T, T, T> { | |
public DefaultControllerAgent(Class<T> clazz) { | |
super(clazz,clazz); | |
} | |
protected abstract T createItemFromForm(T element, Long... preIds); | |
@Override | |
protected T createFromForm(Form<T> composeForm, Long... preIds) { | |
return createItemFromForm(composeForm.get(), preIds); | |
} | |
@Override | |
protected T editFromForm(T element, Form<T> editForm) { | |
T editItem = editForm.get(); | |
editItem.update(element.id); | |
return editItem; | |
} | |
@Override | |
protected T translateItemToEditFormInstance(T element) { | |
return element; | |
} | |
public Result delete(T element) { | |
return delete(element, Auth.getUser()); | |
} | |
} | |
} |
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
package controllers; | |
import models.Video; | |
import models.VideoNote; | |
import play.api.templates.Html; | |
import play.data.Form; | |
import play.mvc.Result; | |
import play.mvc.Controller; | |
import views.html.video_notes.compose; | |
import views.html.video_notes.item; | |
import controllers.ControllerAgent.DefaultControllerAgent; | |
public class VideoNotes extends Controller { | |
private static DefaultControllerAgent<VideoNote> agent = | |
new DefaultControllerAgent<VideoNote>(VideoNote.class) { | |
@Override | |
protected Html displayCompose(Form<VideoNote> composeForm, Long... preIds) { | |
return compose.render(Video.find.ref(preIds[0]),composeForm); | |
} | |
@Override | |
protected Html displayItem(VideoNote element, Long... preIds) { | |
return item.render(element); | |
} | |
@Override | |
protected VideoNote createItemFromForm(VideoNote element, Long... preIds) { | |
return VideoNote.createFromForm(element, Auth.getUser(), Video.find.ref(preIds[0])); | |
} | |
@Override | |
protected Html displayEditForm(Form<VideoNote> editForm, VideoNote element, Long... preIds) { | |
throw new NoSuchMethodError("no edit for VideoNote yet"); | |
}}; | |
public static Result compose(Long videoId) { | |
return agent.compose(videoId); | |
} | |
public static Result create(Long videoId) { | |
return agent.create(videoId); | |
} | |
public static Result delete(Long videoId, Long id) { | |
return agent.delete(VideoNote.find.byId(id)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment