Assignment 5
Assignment 5
Code :
public ProfileService() {
profiles.put("Debayan", new Profile(1L, "Debayan", "Debayan", "Mitra"));
profiles.put("Dhoni", new Profile(2L, "Mahendra", "Dhoni", "Dhoni"));
}
@Path(value = "/profiles")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ProfileResource {
@GET
public List<Profile> getProfiles() {
return profileservice.getAllProfiles();
}
OUTPUT :
Code :
@GET
@Path(value = "/{profileName}")
public Profile getprofiles(@PathParam ("profileName") String profileName) {
return profileservice.getProfile(profileName);
}
OUTPUT
POST METHOD
Code :
@POST
public Profile addProfile(Profile profile) {
return profileservice.addProfile(profile);
}
Code :
@PUT
@Path(value = "/{profileName}")
public Profile updateProfile(@PathParam ("profileName") String profileName, Profile profile) {
profile.setProfileName(profileName);
return profileservice.updateProfile(profile);
}
OUTPUT :
Code :
@DELETE
@Path(value = "/{profileName}")
public void deleteProfie(@PathParam ("profileName") String profileName) {
profileservice.removeProfile(profileName);
}
GET method use to retrieve Profile name -> “Admin” before deletion
Using GET to retrieve the data of the Profile Name “Admin” after deletion :
SOURCE CODE :
ProfileResource.java
package org.debayanmitra.itm566.debayan.resources;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.debayanmitra.itm566.debayan.model.Profile;
import org.debayanmitra.itm566.debayan.service.ProfileService;
@Path(value = "/profiles")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ProfileResource {
ProfileService profileservice = new ProfileService();
@GET
public List<Profile> getProfiles() {
return profileservice.getAllProfiles();
}
@POST
public Profile addProfile(Profile profile) {
return profileservice.addProfile(profile);
}
@GET
@Path(value = "/{profileName}")
public Profile getprofiles(@PathParam ("profileName") String profileName) {
return profileservice.getProfile(profileName);
}
@PUT
@Path(value = "/{profileName}")
public Profile updateProfile(@PathParam ("profileName") String profileName, Profile profile) {
profile.setProfileName(profileName);
return profileservice.updateProfile(profile);
}
@DELETE
@Path(value = "/{profileName}")
public void deleteProfie(@PathParam ("profileName") String profileName) {
profileservice.removeProfile(profileName);
}
}
Profile.java
package org.debayanmitra.itm566.debayan.model;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Profile {
private long id;
private String profileName;
private String firstName;
private String lastName;
public Profile() {
}
package org.debayanmitra.itm566.debayan.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.debayanmitra.itm566.debayan.database.DatabaseClass;
import org.debayanmitra.itm566.debayan.model.Profile;
public ProfileService() {
profiles.put("Debayan", new Profile(1L, "Debayan", "Debayan", "Mitra"));
profiles.put("Dhoni", new Profile(2L, "Mahendra", "Dhoni", "Dhoni"));
}
public List<Profile>getAllProfiles(){
return new ArrayList<Profile>(profiles.values());
}
public Profile getProfile(String profileName) {
return profiles.get(profileName);
}
public Profile addProfile(Profile profile) {
profile.setId(profiles.size() + 1);
profiles.put(profile.getProfileName(), profile);
return profile;
}
public Profile updateProfile(Profile profile) {
if(profile.getProfileName().isEmpty()) {
return null;
}
profiles.put(profile.getProfileName(), profile);
return profile;
}
public Profile removeProfile(String profileName) {
return profiles.remove(profileName);
}
}
DatabaseClass.java
package org.debayanmitra.itm566.debayan.database;
import java.util.HashMap;
import java.util.Map;
import org.debayanmitra.itm566.debayan.model.Message;
import org.debayanmitra.itm566.debayan.model.Profile;