Example #1
0
  public Result getAllProfits() {

    List<Profit> profits = Profit.findAllProfits();

    if (RequestUtils.acceptsJson(request())) {
      return ok(Json.toJson(profits));
    } else if (RequestUtils.acceptsXml(request())) {
      return ok(views.xml.profits.render(profits));
    }

    return badRequest("unsupported format");
  }
Example #2
0
  public Result retrieve(Integer pId) {

    Profit profit = Profit.findProfitWithId(pId);

    if (profit == null) {
      return notFound();
    }

    if (RequestUtils.acceptsJson(request())) {
      return ok(Json.toJson(profit));
    } else if (RequestUtils.acceptsXml(request())) {
      return ok(views.xml.profit.render(profit));
    }

    return badRequest("unsupported format");
  }
Example #3
0
  public Result create() {
    Form<Profit> form = Form.form(Profit.class).bindFromRequest();

    if (form.hasErrors()) {
      return badRequest(form.errorsAsJson());
    }

    Profit profit = form.get();

    if (Profit.existsProfitWithId(profit.getIdProfit())) {
      return Results.status(409, "already exists");
    }

    Integer idAdvisedUser = RequestUtils.getIntegerFromBody(request(), "idAdvisedUser");

    if (idAdvisedUser == null) {
      return badRequest("You need to add the id of the adviseduser");
    }

    AdvisedUser advisedUser = AdvisedUser.findAdvisedUserWithId(idAdvisedUser);

    if (advisedUser == null) {
      return Results.status(409, "there is no adviseduser with this id");
    }

    profit.setUser(advisedUser);

    profit.save();

    return created();
  }
Example #4
0
  public Result getAdvisedUserFromProfit(Integer pId) {
    Profit profit = Profit.findProfitWithId(pId);

    if (profit == null) {
      return notFound();
    }

    AdvisedUser advisedUser = profit.getUser();

    if (RequestUtils.acceptsJson(request())) {
      return ok(Json.toJson(advisedUser));
    } else if (RequestUtils.acceptsXml(request())) {
      return ok(views.xml.adviseduser.render(advisedUser));
    }

    return badRequest("unsupported format");
  }