Commit 07d61a62 authored by elisabeth kee's avatar elisabeth kee

Corrections modify profile

parent d1a28cb5
package com.example.trial.lovers.bdd; package com.example.trial.lovers.bdd;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
public interface LoverRepository extends CrudRepository<Lover, Long> { } public interface LoverRepository extends CrudRepository<Lover, Long> {
\ No newline at end of file Optional<Lover> findById(Long id);
@Query("SELECT l from Lover l where l.id = :id")
List<Lover> getLoverById(@Param("id") Long id);
}
\ No newline at end of file
...@@ -3,12 +3,20 @@ package com.example.trial.profile.process; ...@@ -3,12 +3,20 @@ package com.example.trial.profile.process;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
public interface ProfileRepository extends CrudRepository<Profile, Long> { public interface ProfileRepository extends CrudRepository<Profile, Long> {
Optional<Profile> findByFirstname(String firstname); Optional<Profile> findByFirstname(String firstname);
Optional<Profile> findById(Long id); Optional<Profile> findById(Long id);
@Query("SELECT p from Profile p where p.id = :id")
List<Profile> getProfileById(@Param("id") Long id);
List<Profile> findByAge(int age); List<Profile> findByAge(int age);
List<Profile> findByGenderAndPet(String gender, String pet); List<Profile> findByGenderAndPet(String gender, String pet);
} }
\ No newline at end of file
...@@ -54,17 +54,17 @@ public class ProfileResource { ...@@ -54,17 +54,17 @@ public class ProfileResource {
} }
return Response.noContent().build(); return Response.noContent().build();
} }
/*
@GET @GET
@Path("{id}") @Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response getprofileById(@PathParam("id") Long id) { public List<Profile> getProfileById(@PathParam("id") Long id) {
Optional<Profile> p = profileRepository.findById(id); List<Profile> profile = new ArrayList<>();
if (p.isPresent()) { if (profileRepository.findById(id).isPresent()) {
return Response.ok(p.get()).build(); profile = profileRepository.getProfileById(id);
} else {
return Response.status(Response.Status.NOT_FOUND).build();
} }
return profile;
} }
@PATCH @PATCH
...@@ -84,7 +84,7 @@ public class ProfileResource { ...@@ -84,7 +84,7 @@ public class ProfileResource {
} else { } else {
return Response.status(Response.Status.NOT_FOUND).build(); return Response.status(Response.Status.NOT_FOUND).build();
} }
} }*/
@PATCH @PATCH
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
...@@ -162,15 +162,17 @@ public class ProfileResource { ...@@ -162,15 +162,17 @@ public class ProfileResource {
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Path("{id}/updateLovers") @Path("{id}/updateLovers")
public Response updateLovers(@PathParam("id") Long id, List<Lover> newLovers) { public Response updateLovers(@PathParam("id") Long id, Long idLover) {
Optional<Profile> optional = profileRepository.findById(id); Optional<Profile> optional = profileRepository.findById(id);
List<Lover> lover = loverRepository.getLoverById(idLover);
if (optional.isPresent()) { if (optional.isPresent()) {
Profile pBDD = optional.get(); Profile pBDD = optional.get();
pBDD.setLovers(newLovers); pBDD.setLovers(lover);
profileRepository.save(pBDD); profileRepository.save(pBDD);
return Response.ok(pBDD).build(); return Response.ok(pBDD).build();
} else { }
else {
return Response.status(Response.Status.NOT_FOUND).build(); return Response.status(Response.Status.NOT_FOUND).build();
} }
} }
...@@ -191,7 +193,7 @@ public class ProfileResource { ...@@ -191,7 +193,7 @@ public class ProfileResource {
List<Profile> matches = new ArrayList<>(); List<Profile> matches = new ArrayList<>();
String profilePartnerGender = loverRepository.findById(idLover).get().getPartnerGender(); String profilePartnerGender = loverRepository.findById(idLover).get().getPartnerGender();
String profilePartnerPet = loverRepository.findById(idLover).get().getPartnerPet(); String profilePartnerPet = loverRepository.findById(idLover).get().getPartnerPet();
profileRepository.findByGenderAndPet(profilePartnerGender, profilePartnerPet).forEach(matches::add); matches = profileRepository.findByGenderAndPet(profilePartnerGender, profilePartnerPet);
return matches; return matches;
} }
......
...@@ -8,7 +8,7 @@ $(document).ready(function() { ...@@ -8,7 +8,7 @@ $(document).ready(function() {
let $selectLovers = $("#selectLovers"); let $selectLovers = $("#selectLovers");
let $selectLovers2 = $("#selectLovers2"); let $selectLovers2 = $("#selectLovers2");
let $modifyFirstnameText = $("#firstname-modify"); let $modifyFirstnameText = $("#firstname-modify");
let $modifyFirstnameButton = $("#modifyFirstname"); let $modifyGenderButton = $("#modifyGender");
let $listModifiedProfile = $("#showModifiedProfile"); let $listModifiedProfile = $("#showModifiedProfile");
let $buttonSeeModifications = $("#seeModifications"); let $buttonSeeModifications = $("#seeModifications");
...@@ -49,18 +49,17 @@ $(document).ready(function() { ...@@ -49,18 +49,17 @@ $(document).ready(function() {
$('#modifyLover').click(function(){ $('#modifyLover').click(function(){
let idProfile = $listeProfilesSelect2.val(); let idProfile = $listeProfilesSelect2.val();
let idLover = $('#selectLovers2').val();
$.ajax({ $.ajax({
type: "PATCH", type: "PATCH",
url: "http://localhost:8080/api/profiles/"+idProfile+"/updateLovers", url: "http://localhost:8080/api/profiles/"+idProfile+"/updateLovers",
data: JSON.stringify(idLover),
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
dataType: "json", dataType: "json"
success: function(data){
}
}); });
return false;
}); });
$listeLovers.on("click", "li button", function() { $listeLovers.on("click", "li button", function() {
let elemid = $(this).parent().attr('id'); let elemid = $(this).parent().attr('id');
let idLover = elemid.replace('lover-',''); let idLover = elemid.replace('lover-','');
...@@ -79,33 +78,102 @@ $(document).ready(function() { ...@@ -79,33 +78,102 @@ $(document).ready(function() {
} }
}); });
}); });
/*
$('#modifyFirstname').click(function(){ $('#seeModifications').click(function(){
let idProfile = $listeProfilesSelect2.val(); let idProfile = $listeProfilesSelect2.val();
let newFirstname = $('#firstname-modify').val();
$.ajax({ $.ajax({
type: "PATCH", type: "GET",
url: "http://localhost:8080/api/profiles/"+idProfile+"/updateFirstname", url: "http://localhost:8080/api/profiles/"+idProfile,
data: JSON.stringify(newFirstname), data: JSON.stringify(idProfile),
contentType: "application/json; charset=utf-8", contentType: "application/json; charset=utf-8",
dataType: "json", dataType: "json",
/*
//You blocked this code because you want to show the new profile at the very end after they
//have made all the modifications they desire by clicking on the button $('#seeModifications')
success: function(data){ success: function(data){
showModifiedProfile(data); showModifiedProfile(data);
}, },
error: function (xhr, ajaxOptions, thrownError) { error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr); console.log(xhr);
alert("Impossible à faire la MAJ du firstname"); alert("Impossible à voir la MAJ du profile");
}*/ }
});
return false;
});
*/
$('#modifyFirstname').click(function(){
let idProfile = $listeProfilesSelect2.val();
let newFirstname = $('#firstname-modify').val();
$.ajax({
type: "PATCH",
url: "http://localhost:8080/api/profiles/"+idProfile+"/updateFirstname",
data: JSON.stringify(newFirstname),
contentType: "application/json; charset=utf-8",
dataType: "json"
}); });
$('#firstname-modify').val(''); $('#firstname-modify').val('');
return false; return false;
}); });
/*
$('#modifyAge').click(function(){
let idProfile = $listeProfilesSelect2.val();
let newAge = $('#age-modify').val();
$.ajax({
type: "PATCH",
url: "http://localhost:8080/api/profiles/"+idProfile+"/updateAge",
data: JSON.stringify(newAge),
contentType: "application/json; charset=utf-8",
dataType: "json"
});
$('#age-modify').val('');
return false;
});
*/
$('#modifyGender').click(function(){
let idProfile = $listeProfilesSelect2.val();
let newGender = $('#gender-modify').val();
$.ajax({
type: "PATCH",
url: "http://localhost:8080/api/profiles/"+idProfile+"/updateGender",
data: JSON.stringify(newGender),
contentType: "application/json; charset=utf-8",
dataType: "json"
});
$('#gender-modify').val('');
return false;
});
$('#modifyPet').click(function(){
let idProfile = $listeProfilesSelect2.val();
let newPet = $('#pet-modify').val();
$.ajax({
type: "PATCH",
url: "http://localhost:8080/api/profiles/"+idProfile+"/updatePet",
data: JSON.stringify(newPet),
contentType: "application/json; charset=utf-8",
dataType: "json"
});
$('#pet-modify').val('');
return false;
});
$('#modifyContact').click(function(){
let idProfile = $listeProfilesSelect2.val();
let newContact = $('#contact-modify').val();
$.ajax({
type: "PATCH",
url: "http://localhost:8080/api/profiles/"+idProfile+"/updateContact",
data: JSON.stringify(newContact),
contentType: "application/json; charset=utf-8",
dataType: "json"
});
$('#contact-modify').val('');
return false;
});
$('#addbtnProfile').click(function(){ $('#addbtnProfile').click(function(){
let firstname = $('#firstname-input').val(); let firstname = $('#firstname-input').val();
let age = $('#age-input').val(); let age = $('#age-input').val();
...@@ -162,7 +230,7 @@ $(document).ready(function() { ...@@ -162,7 +230,7 @@ $(document).ready(function() {
/* Ajoute un élément li dans la liste de lovers*/ /* Ajoute un élément li dans la liste de lovers*/
function showModifiedProfile(profile) { function showModifiedProfile(profile) {
liToAppend4 = `<li id="${profile.id}" class="list-group-item">You are: ${profile.firstname} - age: ${profile.age}`; liToAppend4 = `<li id="${profile.id}" class="list-group-item">You are: ${profile.firstname} - age: ${profile.age} - gender: ${profile.gender} - who likes: ${profile.pet} - contact info: ${profile.contact}`;
profile.lovers.forEach( lover => liToAppend4+= ` - interested in: ${lover.partnerGender} - who likes ${lover.partnerPet}`); profile.lovers.forEach( lover => liToAppend4+= ` - interested in: ${lover.partnerGender} - who likes ${lover.partnerPet}`);
liToAppend4+= `</li>`; liToAppend4+= `</li>`;
$listModifiedProfile.append(liToAppend4); $listModifiedProfile.append(liToAppend4);
...@@ -233,19 +301,4 @@ $(document).ready(function() { ...@@ -233,19 +301,4 @@ $(document).ready(function() {
appendToListProfileDelete(data); appendToListProfileDelete(data);
} }
}); });
}; }})
\ No newline at end of file
/* Ajout un lover à une profile existante*/
function modifyLoverToProfile(idProfile) {
$.ajax({
type: "PATCH",
url: "http://localhost:8080/api/profiles/"+idProfile+"/updateLovers",
data: JSON.stringify({"idLover" : idLover}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
}
});
};
});
\ No newline at end of file
...@@ -51,21 +51,21 @@ ...@@ -51,21 +51,21 @@
<ul class= "list-group" id="listMatches"></ul> <ul class= "list-group" id="listMatches"></ul>
</p> </p>
<p> <p>
<h2 style="color:darkcyan;">Everyone changes at some point in their lives...</h2> <h2 style="color:orangered;">Everyone changes at some point in their lives...</h2>
<div class="form-group"> <div class="form-group">
<p>Select the profile you wish to modify : </p> <p>Select the profile you wish to modify : </p>
<p><select id="listProfilesSelect2"></select></p> <p><select id="listProfilesSelect2"></select></p>
<p>Modify your profile info : </p> <p>Modify your profile info : </p>
<p>Firstname : <input type="text" name="firstname" id="firstname-modify"><input type="button" id="modifyFirstname" name="addbtn" value="Modify" class="btn btn-primary btn-sm"></p> <p>Firstname : <input type="text" name="firstname" id="firstname-modify"><input type="button" id="modifyFirstname" name="addbtn" value="Modify" class="btn btn-primary btn-sm"></p>
<p>Age : <input type="number" name="age" id="age-modify"><input type="button" id="modifyAge" name="addbtn" value="Modify" class="btn btn-primary btn-sm"></p>
<p>Gender (Male/Female/Other): <input type="text" name="gender" id="gender-modify"><input type="button" id="modifyGender" name="addbtn" value="Modify" class="btn btn-primary btn-sm"></p> <p>Gender (Male/Female/Other): <input type="text" name="gender" id="gender-modify"><input type="button" id="modifyGender" name="addbtn" value="Modify" class="btn btn-primary btn-sm"></p>
<p>For a pet, what do you prefer (cats/dogs/other) ? : <input type="text" name="pet" id="pet-modify"><input type="button" id="modifyPet" name="addbtn" value="Modify" class="btn btn-primary btn-sm"></p> <p>For a pet, what do you prefer (cats/dogs/other) ? : <input type="text" name="pet" id="pet-modify"><input type="button" id="modifyPet" name="addbtn" value="Modify" class="btn btn-primary btn-sm"></p>
<p>Contact information for potential lovers : <input type="text" name="contact" id="contact-modify"><input type="button" id="modifyContact" name="addbtn" value="Modify" class="btn btn-primary btn-sm"></p> <p>Contact information for potential lovers : <input type="text" name="contact" id="contact-modify"><input type="button" id="modifyContact" name="addbtn" value="Modify" class="btn btn-primary btn-sm"></p>
<p>Age : <input type="text" name="age" id="age-modify"><input type="button" id="modifyAge" name="addbtn" value="Modify" class="btn btn-primary btn-sm"></p>
<p>Want to choose a new love interest ?: </p> <p>Want to choose a new love interest ?: </p>
<p><select id="selectLovers2"></select><input type="button" id="modifyLover" name="addbtn" value="Modify" class="btn btn-primary btn-sm"></p> <p><select id="selectLovers2"></select><input type="button" id="modifyLover" name="addbtn" value="Modify" class="btn btn-primary btn-sm"></p>
<p>See your new profile now that you have made all your modifications: </p> <p>See your new profile now that you have made all your modifications: </p>
<p><input type="button" id="seeModifications" name="addbtn" value="Modified" class="btn btn-primary btn-sm"></p> <p><input type="button" id="seeModifications" name="addbtn" value="Modified" class="btn btn-primary btn-sm"></p>
<ul class="list-group" id="showModifiedProfile"> <ul class="list-group" id="showModifiedProfile">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment