1

I am having a harder time than I would like to admit understanding the JSON documentation.

I would like to convert this JSON result to Scala, but I'm pretty lost. I have tried a lot of things, but I'm still learning Scala as well, so none of it is really worth posting as I'm not even sure if it makes sense.

I am using the Anorm ORM in Scala. The id is a Pk[Long]

public static Result checkName(String clubname){
      ObjectNode jresult = Json.newObject();

     if (Club.clubExists(clubname)) {
         jresult.put("error", "Club Name Exists");
         return status(409, jresult); // 409 - Conflict
     } else {
         jresult.put("status", "OK");
         return ok(jresult);
     }
 }

clubExists in the model:

public static boolean clubExists(String name) {
    Club club = find.where().eq("club_name", name).findUnique();
    return (club != null);
}

The rest of the model is pretty basic:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "club_seq")
public Long clubId;

@Column(unique=true, length = 50)
public String clubName;

public Long creator;
public DateTime created;

public Club(String clubName, Long creator) {
    this.clubName = clubName;
    this.creator = creator;
    this.created = new DateTime();
}

public static Finder<Long, Club> find = new Finder<Long, Club>(Long.class, Club.class);

public static Club create(String name, Long creator) {
    Club club = new Club(name, creator);
    club.save();
    return club;
}

1 Answer 1

3
public static Result checkName(String clubname){
     ObjectNode jresult = Json.newObject();

     if (Club.clubExists(clubname)) {
         jresult.put("error", "Club Name Exists");
         return status(409, jresult); // 409 - Conflict
     } else {
         jresult.put("status", "OK");
         return ok(jresult);
     }
}

in Scala is (adding as JSON to change the MIME type):

def checkName(clubName:String) = Action {
  val jresult = Json.obj()
  if (Club.exists(clubName)) {
    Conflict(jresult) as JSON
  } else {
    Ok(jresult) as JSON
  } 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.