Package controllers

Source Code of controllers.AgendaRestController

package controllers;

import models.Agenda;
import models.User;
import models.utils.TransformValidationErrors;
import play.data.Form;
import play.mvc.Result;
import securesocial.core.java.SecureSocial;

import static play.data.Form.form;
import static play.libs.Json.toJson;


@SecureSocial.SecuredAction(ajaxCall = true)
public class AgendaRestController extends BaseController {


    public static Result get() {
        if (getEvent().getAgenda() != null) {
            return ok(toJson(getEvent().getAgenda()));
        } else {
            return notFound();
        }
    }

    public static Result delete(Long id) {

        // Vérification du rôle d'admin
        User user = getLoggedUser();
        if (!user.admin && !user.hasEvent(getEvent())) {
            return forbidden();
        }

        Agenda agenda = Agenda.findById(id);
        if (agenda != null && getEvent().getAgenda() != null && getEvent().getAgenda().equals(agenda)) {
            agenda.delete();
        }
        // HTTP 204 en cas de succès (NO CONTENT)
        return noContent();
    }

    public static Result save() {

        // Vérification du rôle d'admin
        User user = getLoggedUser();
        if (!user.admin && !user.hasEvent(getEvent())) {
            return forbidden();
        }

        Form<Agenda> agendaForm = form(Agenda.class).bindFromRequest();

        if (agendaForm.hasErrors()) {
            return badRequest(toJson(TransformValidationErrors.transform(agendaForm.errors())));
        }

        Agenda formAgenda = agendaForm.get();

        if (getEvent().getAgenda() == null) {
            formAgenda.save();
            getEvent().setAgenda(formAgenda);
            getEvent().update();
        } else {
            // Mise à jour d'un agenda
            Agenda dbAgenda = Agenda.find.byId(getEvent().getAgenda().getId());
            dbAgenda.setDebutCfp(formAgenda.getDebutCfp());
            dbAgenda.setFinCfp(formAgenda.getFinCfp());
            dbAgenda.setFinVote(formAgenda.getFinVote());
            dbAgenda.update();
        }

        // HTTP 204 en cas de succès (NO CONTENT)
        return noContent();
    }
}
TOP

Related Classes of controllers.AgendaRestController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.