/MavenServerProfile/src/main/java/server/CRUDManagement.java

https://github.com/christian-lang-2012/NubayAPI · Java · 174 lines · 112 code · 37 blank · 25 comment · 17 complexity · e55898d348d08d156b356630c75889da MD5 · raw file

  1. package server;
  2. import java.util.HashMap;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. import javax.ws.rs.core.Response;
  6. import org.springframework.stereotype.Service;
  7. import CRUD.ICRUDService;
  8. import ProfileObjects.profile.Profile;
  9. import customExceptions.CustomException;
  10. @Service("CRUDManagementService")
  11. public class CRUDManagement implements ICRUDService {
  12. HashMap<Integer, Profile> tempServer = new HashMap<Integer, Profile>();
  13. public static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  14. int currentID = 1;
  15. public Response createProfile(Profile profile) throws CustomException
  16. {
  17. Profile newProfile = profile;
  18. newProfile.setUserID(currentID);
  19. if(newProfile.getName() == null)
  20. {
  21. throw new CustomException("NO_NAME - User name is missing.");
  22. }
  23. if(newProfile.getName().length() > 50)
  24. {
  25. throw new CustomException("NAME_TOO_LONG - User name is over the character limit");
  26. }
  27. if(newProfile.getName().length() < 1)
  28. {
  29. throw new CustomException("NAME_TOO_SHORT - User name is under the character limit");
  30. }
  31. if(newProfile.getLocation().getCity().length() > 50)
  32. {
  33. throw new CustomException("LOCATION_TOO_LONG - Location is over character limit");
  34. }
  35. if(!validateEmailAddress(profile.getEmail()))
  36. {
  37. throw new CustomException("INVALID_EMAIL - Email is not a valid email address");
  38. }
  39. /* if(newProfile.getImage().getImageURI() == null)
  40. {
  41. throw new CustomException("INVALID_IMAGE_LINK - The image link does not lead to an image.");
  42. }
  43. if(newProfile.getImage().getSize() == null)
  44. {
  45. //if not a jpg, gif, png OR correct size....
  46. throw new CustomException("INVALID_IMAGE - The image does not meet dimension or format requirements");
  47. } */
  48. newProfile.setUserRating(0);
  49. tempServer.put(newProfile.getUserID(), newProfile);
  50. currentID++;
  51. return Response.status(201).entity(newProfile).build();
  52. }
  53. public Response updateProfile(int id,Profile profile) throws CustomException
  54. {
  55. if(tempServer.containsKey(id))
  56. {
  57. if(profile == null){
  58. throw new CustomException("NO_DATA � No data was given to update.");
  59. }
  60. System.out.println("Profile id: "+profile.getUserID());
  61. System.out.println("Input id: "+id);
  62. if(id == profile.getUserID())
  63. {
  64. if(profile.getName().length() > 50)
  65. {
  66. throw new CustomException("NAME_TOO_LONG - User name is over the character limit");
  67. }
  68. if(profile.getName().length() < 1)
  69. {
  70. throw new CustomException("NAME_TOO_SHORT - User name is under the character limit");
  71. }
  72. if(profile.getLocation().getCity().length() > 50)
  73. {
  74. throw new CustomException("LOCATION_TOO_LONG - Location is over character limit");
  75. }
  76. if(!validateEmailAddress(profile.getEmail()))
  77. {
  78. throw new CustomException("INVALID_EMAIL - Email is not a valid email address");
  79. }
  80. /* if(profile.getImage().getImageURI() == null)
  81. {
  82. throw new CustomException("INVALID_IMAGE_LINK - The image link does not lead to an image.");
  83. }
  84. if(profile.getImage().getSize() == null)
  85. {
  86. //if not a jpg, gif, png OR correct size....
  87. throw new CustomException("INVALID_IMAGE - The image does not meet dimension or format requirements");
  88. } */
  89. tempServer.put(id,profile);
  90. // Profile updateProfile = (Profile) tempServer.get(id);
  91. // updateProfile = profile;
  92. // tempServer.put(updateProfile.getUserID(), updateProfile);
  93. return Response.ok().build();
  94. }
  95. else
  96. {
  97. throw new CustomException("ID_MISMATCH - The id on the request and the profile don�t match");
  98. }
  99. }
  100. else
  101. {
  102. return Response.status(404).build(); //id doesn't exist
  103. }
  104. }
  105. public Response getProfile(int id) throws CustomException
  106. {
  107. if(tempServer.containsKey(id))
  108. {
  109. Profile profile = (Profile) tempServer.get(id);
  110. return Response.ok(profile).build();
  111. }
  112. else
  113. {
  114. return Response.status(404).build();
  115. //throw new CustomException("INVALID_ID � The given ID doesn�t exist.");
  116. }
  117. }
  118. public Response deleteProfile(int id) throws CustomException
  119. {
  120. if(tempServer.containsKey(id))
  121. {
  122. tempServer.remove(id);
  123. return Response.status(204).build();
  124. }
  125. else
  126. {
  127. return Response.status(404).build();
  128. //throw new CustomException("INVALID_ID � The given ID doesn�t exist.");
  129. }
  130. }
  131. private boolean validateEmailAddress(String s){
  132. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(s);
  133. return matcher.find();
  134. }
  135. }