Package controllers

Source Code of controllers.AudienceRestController

package controllers;

import models.Audience;
import models.User;
import models.utils.TransformValidationErrors;
import play.data.Form;
import play.i18n.Messages;
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 AudienceRestController extends BaseController {

    public static Result get(Long id) {
        Audience audience = Audience.find.byId(id);
        if (audience == null || !audience.getEvent().equals(getEvent())) {
            return noContent();
        }
        return ok(toJson(audience));
    }


    public static Result all() {
        return ok(toJson(Audience.findByEvent(getEvent())));
    }

    public static Result delete(Long id) {

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

        Audience audience = Audience.find.byId(id);
        if (audience != null && audience.getEvent().equals(getEvent())) {
            audience.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<Audience> audienceForm = form(Audience.class).bindFromRequest();

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

        Audience formAudience = audienceForm.get();

        if (formAudience.getId() == null) {
            // Nouveau Track
            if (Audience.findByTitleAndEvent(formAudience.getTitle(),getEvent()) != null) {
                return badRequest(toJson(TransformValidationErrors.transform(Messages.get("error.audience.already.exist"))));
            }

            formAudience.setEvent(getEvent());
            formAudience.save();
        } else {
            // Mise à jour d'un track
            Audience dbAudience = Audience.find.byId(formAudience.getId());
            if (!formAudience.getTitle().equals(dbAudience.getTitle())
                    && Audience.findByTitleAndEvent(formAudience.getTitle(),getEvent()) != null) {
                return badRequest(toJson(TransformValidationErrors.transform(Messages.get("error.audience.already.exist"))));
            }


            dbAudience.setTitle(formAudience.getTitle());
            dbAudience.setDescription(formAudience.getDescription());
            dbAudience.update();
        }

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

Related Classes of controllers.AudienceRestController

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.