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

Corrections modify profile

parent d1a28cb5
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.query.Param;
public interface LoverRepository extends CrudRepository<Lover, Long> { }
\ No newline at end of file
public interface LoverRepository extends CrudRepository<Lover, Long> {
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;
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.query.Param;
public interface ProfileRepository extends CrudRepository<Profile, Long> {
Optional<Profile> findByFirstname(String firstname);
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> findByGenderAndPet(String gender, String pet);
}
\ No newline at end of file
......@@ -54,17 +54,17 @@ public class ProfileResource {
}
return Response.noContent().build();
}
/*
@GET
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response getprofileById(@PathParam("id") Long id) {
Optional<Profile> p = profileRepository.findById(id);
if (p.isPresent()) {
return Response.ok(p.get()).build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
public List<Profile> getProfileById(@PathParam("id") Long id) {
List<Profile> profile = new ArrayList<>();
if (profileRepository.findById(id).isPresent()) {
profile = profileRepository.getProfileById(id);
}
return profile;
}
@PATCH
......@@ -84,7 +84,7 @@ public class ProfileResource {
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
}*/
@PATCH
@Consumes(MediaType.APPLICATION_JSON)
......@@ -162,15 +162,17 @@ public class ProfileResource {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@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);
List<Lover> lover = loverRepository.getLoverById(idLover);
if (optional.isPresent()) {
Profile pBDD = optional.get();
pBDD.setLovers(newLovers);
pBDD.setLovers(lover);
profileRepository.save(pBDD);
return Response.ok(pBDD).build();
} else {
}
else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
......@@ -191,7 +193,7 @@ public class ProfileResource {
List<Profile> matches = new ArrayList<>();
String profilePartnerGender = loverRepository.findById(idLover).get().getPartnerGender();
String profilePartnerPet = loverRepository.findById(idLover).get().getPartnerPet();
profileRepository.findByGenderAndPet(profilePartnerGender, profilePartnerPet).forEach(matches::add);
matches = profileRepository.findByGenderAndPet(profilePartnerGender, profilePartnerPet);
return matches;
}
......
......@@ -8,7 +8,7 @@ $(document).ready(function() {
let $selectLovers = $("#selectLovers");
let $selectLovers2 = $("#selectLovers2");
let $modifyFirstnameText = $("#firstname-modify");
let $modifyFirstnameButton = $("#modifyFirstname");
let $modifyGenderButton = $("#modifyGender");
let $listModifiedProfile = $("#showModifiedProfile");
let $buttonSeeModifications = $("#seeModifications");
......@@ -49,18 +49,17 @@ $(document).ready(function() {
$('#modifyLover').click(function(){
let idProfile = $listeProfilesSelect2.val();
let idLover = $('#selectLovers2').val();
$.ajax({
type: "PATCH",
url: "http://localhost:8080/api/profiles/"+idProfile+"/updateLovers",
data: JSON.stringify(idLover),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
}
dataType: "json"
});
return false;
});
$listeLovers.on("click", "li button", function() {
let elemid = $(this).parent().attr('id');
let idLover = elemid.replace('lover-','');
......@@ -79,33 +78,102 @@ $(document).ready(function() {
}
});
});
$('#modifyFirstname').click(function(){
/*
$('#seeModifications').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),
type: "GET",
url: "http://localhost:8080/api/profiles/"+idProfile,
data: JSON.stringify(idProfile),
contentType: "application/json; charset=utf-8",
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){
showModifiedProfile(data);
},
error: function (xhr, ajaxOptions, thrownError) {
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('');
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(){
let firstname = $('#firstname-input').val();
let age = $('#age-input').val();
......@@ -162,7 +230,7 @@ $(document).ready(function() {
/* Ajoute un élément li dans la liste de lovers*/
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}`);
liToAppend4+= `</li>`;
$listModifiedProfile.append(liToAppend4);
......@@ -233,19 +301,4 @@ $(document).ready(function() {
appendToListProfileDelete(data);
}
});
};
/* 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
}})
\ No newline at end of file
......@@ -51,21 +51,21 @@
<ul class= "list-group" id="listMatches"></ul>
</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">
<p>Select the profile you wish to modify : </p>
<p><select id="listProfilesSelect2"></select></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>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>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>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><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><input type="button" id="seeModifications" name="addbtn" value="Modified" class="btn btn-primary btn-sm"></p>
<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