package com.example.trial.profile.resource; import org.springframework.beans.factory.annotation.Autowired; import com.example.trial.lovers.bdd.Lover; import com.example.trial.lovers.bdd.LoverRepository; import com.example.trial.profile.process.Profile; import com.example.trial.profile.process.ProfileRepository; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.ArrayList; import java.util.List; import java.util.Optional; @Path("profiles") public class ProfileResource { @Autowired public ProfileRepository profileRepository; @Autowired public LoverRepository loverRepository; @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Profile createProfile(Profile p) { return profileRepository.save(p); } @GET @Produces(MediaType.APPLICATION_JSON) public List<Profile> getAllProfile() { List<Profile> profiles = new ArrayList<>(); profileRepository.findAll().forEach(profiles::add); return profiles; } @PUT @Path("{id}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Profile updateTotalyProfile(@PathParam("id") Long id, Profile p) { p.setId(id); return profileRepository.save(p); } @DELETE @Path("{id}") @Produces(MediaType.APPLICATION_JSON) public Response deleteProfile(@PathParam("id") Long id) { if (profileRepository.findById(id).isPresent()) { profileRepository.deleteById(id); } return Response.noContent().build(); } /*//I dont think the Query I wrote in profile repository works... @GET @Path("{id}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) 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 @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("{id}/updateAge") public Response updateAge(@PathParam("id") Long id, String newAge) { String newAge2 = newAge.replace('"',' '); String newAge3 = newAge2.strip(); int newAge4 = Integer.parseInt(newAge3); Optional<Profile> optional = profileRepository.findById(id); if (optional.isPresent()) { Profile pBDD = optional.get(); pBDD.setAge(newAge4); profileRepository.save(pBDD); return Response.ok(pBDD).build(); } else { return Response.status(Response.Status.NOT_FOUND).build(); } } @PATCH @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("{id}/updateFirstname") public Response updateFirstname(@PathParam("id") Long id, String newFirstname) { String newFirstname2 = newFirstname.replace('"',' '); Optional<Profile> optional = profileRepository.findById(id); if (optional.isPresent()) { Profile pBDD = optional.get(); pBDD.setFirstname(newFirstname2.strip()); profileRepository.save(pBDD); return Response.ok(pBDD).build(); } else { return Response.status(Response.Status.NOT_FOUND).build(); } } @PATCH @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("{id}/updateGender") public Response updateGender(@PathParam("id") Long id, String newGender) { String newGender2 = newGender.replace('"',' '); Optional<Profile> optional = profileRepository.findById(id); if (optional.isPresent()) { Profile pBDD = optional.get(); pBDD.setGender(newGender2.strip()); profileRepository.save(pBDD); return Response.ok(pBDD).build(); } else { return Response.status(Response.Status.NOT_FOUND).build(); } } @PATCH @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("{id}/updatePet") public Response updatePet(@PathParam("id") Long id, String newPet) { String newPet2 = newPet.replace('"',' '); Optional<Profile> optional = profileRepository.findById(id); if (optional.isPresent()) { Profile pBDD = optional.get(); pBDD.setPet(newPet2.strip()); profileRepository.save(pBDD); return Response.ok(pBDD).build(); } else { return Response.status(Response.Status.NOT_FOUND).build(); } } @PATCH @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("{id}/updateContact") public Response updateContact(@PathParam("id") Long id, String newContact) { String newContact2 = newContact.replace('"',' '); Optional<Profile> optional = profileRepository.findById(id); if (optional.isPresent()) { Profile pBDD = optional.get(); pBDD.setContact(newContact2.strip()); profileRepository.save(pBDD); return Response.ok(pBDD).build(); } else { return Response.status(Response.Status.NOT_FOUND).build(); } } @PATCH @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("{id}/updateLovers") 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(lover); profileRepository.save(pBDD); return Response.ok(pBDD).build(); } else { return Response.status(Response.Status.NOT_FOUND).build(); } } @GET @Path("{id}/lovers") @Produces(MediaType.APPLICATION_JSON) // GET /profiles/{id}/lovers public List<Lover> listerLovers(@PathParam("id") Long id) { return profileRepository.findById(id).get().getLovers(); } @GET @Path("{idLover}/loversmatch") @Produces(MediaType.APPLICATION_JSON) // GET /profiles/{idLover}/loversmatch public List<Profile> listerLoversMatch(@PathParam("idLover") Long idLover) { List<Profile> matches = new ArrayList<>(); String profilePartnerGender = loverRepository.findById(idLover).get().getPartnerGender(); String profilePartnerPet = loverRepository.findById(idLover).get().getPartnerPet(); matches = profileRepository.findByGenderAndPet(profilePartnerGender, profilePartnerPet); return matches; } @POST @Path("{idProfile}/lovers") @Produces(MediaType.APPLICATION_JSON) public Response addLoverDejaExistant(@PathParam("idProfile") Long idProfile, LoverInput lovers) { Optional<Profile> pOpt = profileRepository.findById(idProfile); Optional<Lover> lOpt = loverRepository.findById(lovers.getIdLover()); if (!pOpt.isPresent() || !lOpt.isPresent()) { return Response.status(Response.Status.NOT_FOUND).build(); } Profile p = pOpt.get(); Lover l = lOpt.get(); p.getLovers().add(l); profileRepository.save(p); return Response.ok(p).build(); } }