57 lines
1.9 KiB
Java
57 lines
1.9 KiB
Java
package be.jeffcheasey88.peeratcode.routes.users;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import org.json.simple.JSONObject;
|
|
|
|
import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc;
|
|
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
|
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
|
|
import be.jeffcheasey88.peeratcode.framework.RequestType;
|
|
import be.jeffcheasey88.peeratcode.framework.Response;
|
|
import be.jeffcheasey88.peeratcode.framework.Route;
|
|
import be.jeffcheasey88.peeratcode.framework.User;
|
|
import be.jeffcheasey88.peeratcode.model.Player;
|
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
|
|
public class ProfileSettings implements Response{
|
|
|
|
private DatabaseRepository repo;
|
|
|
|
public ProfileSettings(DatabaseRepository repo){
|
|
this.repo = repo;
|
|
}
|
|
|
|
@RouteDoc(path = "/settings", responseCode = 200, responseDescription = "L'utilisateur a mis à jours sont profile")
|
|
@RouteDoc(responseCode = 400, responseDescription = "L'utilisateur a envoyer une donnée unique, déjà utilisée")
|
|
|
|
@Route(path = "^/settings$", type = RequestType.POST, needLogin = true)
|
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
|
JSONObject json = reader.readJson();
|
|
|
|
String pseudo = (String) json.get("pseudo");
|
|
String email = (String) json.get("email");
|
|
String firstname = (String) json.get("firstname");
|
|
String lastname = (String) json.get("lastname");
|
|
Player player = repo.getPlayer(user.getId());
|
|
|
|
if(!player.getPseudo().equals(pseudo)){
|
|
if(!repo.updatePseudo(user.getId(), player, pseudo)){
|
|
writer.response(400, "Access-Control-Allow-Origin: *");
|
|
return;
|
|
}
|
|
player.setPseudo(pseudo);
|
|
}
|
|
|
|
if(!player.getEmail().equals(email)){
|
|
|
|
}
|
|
|
|
if((!player.getFirstname().equals(firstname)) || (!player.getLastname().equals(lastname))){
|
|
repo.updateProfile(user.getId(), player, lastname, firstname);
|
|
}
|
|
|
|
writer.response(200, "Access-Control-Allow-Origin: *");
|
|
}
|
|
|
|
}
|