Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #21 from TouristGuideApp/v2_integration
Browse files Browse the repository at this point in the history
V2 integration
  • Loading branch information
ErminaTrontzou authored Nov 13, 2022
2 parents cbc6a88 + 8f6bde7 commit f61db45
Show file tree
Hide file tree
Showing 18 changed files with 238 additions and 109 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ out/

### VS Code ###
.vscode/
/src/main/resources/
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ dependencies {
implementation 'org.springframework.security:spring-security-crypto:5.7.3'
implementation 'org.json:json:20220924'
implementation group: 'org.apache.directory.studio', name: 'org.apache.commons.codec', version: '1.8'
implementation 'com.squareup.okhttp3:okhttp:4.5.0'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.7'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package gr.thegoodsideofe1.tourguide.controllers;

import gr.thegoodsideofe1.tourguide.entities.Image;
import gr.thegoodsideofe1.tourguide.entities.Tag;
import gr.thegoodsideofe1.tourguide.repositories.ImageRepository;
import gr.thegoodsideofe1.tourguide.services.ImageService;
import gr.thegoodsideofe1.tourguide.services.TagService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.MediaType;


import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import javax.transaction.Transactional;
import java.util.*;

//@CrossOrigin(origins = "http://localhost:3000")
Expand All @@ -21,12 +33,17 @@ public class ImageController {
@Autowired
ImageRepository imageRepository;

@Autowired
TagService tagService;


@Transactional
@GetMapping("")
public List<Image> list(){
return imageService.listAllImages();
}

@Transactional
@GetMapping("/{id}")
public ResponseEntity<Image> get (@PathVariable Integer id){
try{
Expand All @@ -37,10 +54,107 @@ public ResponseEntity<Image> get (@PathVariable Integer id){
}
}


@Transactional
@GetMapping("/getByTitle/{title}")
public List<Image> imageByTitle(@PathVariable String title){
return imageService.getImageByTitle(title);
List<Image> imageResponse = imageService.getImageByTitle(title);
if(imageResponse.isEmpty()) {
if(getFlickr(title)){
imageResponse = imageService.getImageByTitle(title);
}
}
return imageResponse;
}

private boolean getFlickr(String title) {
String flickrAPIURl = "https://www.flickr.com/services/rest/";
flickrAPIURl += "?method=flickr.photos.search";
flickrAPIURl += "&api_key=d4e4f456722f8f76048260df1e0c1880";
flickrAPIURl += "&format=json";
flickrAPIURl += "&text=" + title;
flickrAPIURl += "&has_geo=1";
flickrAPIURl += "&privacy_filter=1";
flickrAPIURl += "&accuracy=11";
flickrAPIURl += "&content_type=1";
flickrAPIURl += "&media=all";
flickrAPIURl += "&extras=geo, description, media, tags, url_o, date_upload, date_taken, views, owner_name";

MediaType JSON = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(flickrAPIURl)
.addHeader("Accept", "application/json")
.addHeader("charset", "utf-8")
.build();

try (Response response = client.newCall(request).execute()) {
JSONObject flickResponse = convertFlickrResponseToJSON(response.body().string());
serializeFlickrResponseData(flickResponse);
} catch (IOException e){
return false;
}
return true;
}

private void serializeFlickrResponseData(JSONObject allData){
JSONObject photosObj = new JSONObject(allData.get("photos").toString());
JSONArray photoArray = new JSONArray(photosObj.get("photo").toString());

for (int i = 0; i<photoArray.length(); i++){
JSONObject singlePhotoObj = new JSONObject(photoArray.get(i).toString());
JSONObject singlePhotoDescription = new JSONObject(singlePhotoObj.get("description").toString());

Image imageToSave = new Image();
imageToSave.setTitle(singlePhotoObj.getString("title"));
imageToSave.setDescription(singlePhotoDescription.getString("_content"));
imageToSave.setLatitude(singlePhotoObj.getString("latitude"));
imageToSave.setLongitude(singlePhotoObj.getString("longitude"));
imageToSave.setFileName(singlePhotoObj.getString("url_o"));
imageToSave.setDateTaken(singlePhotoObj.getString("datetaken"));
imageToSave.setViews(Integer.parseInt(singlePhotoObj.getString("views")));
imageToSave.setOwnerName(singlePhotoObj.getString("ownername"));

Set<Tag> allTagsToSave = serializeTags(singlePhotoObj.getString("tags"));
imageToSave.setTags(allTagsToSave);

System.out.println(imageToSave.getTitle());
imageRepository.save(imageToSave);
}
}

private JSONObject convertFlickrResponseToJSON(String bodyData){
//Trim Body Data String
String trimmedBodyData = bodyData.replace("jsonFlickrApi(", "");
StringBuffer bodyResponseBuffer = new StringBuffer(trimmedBodyData);
bodyResponseBuffer.deleteCharAt(bodyResponseBuffer.length() - 1);

//Convert to JSON
JSONObject jsonObj = new JSONObject(bodyResponseBuffer.toString());
return jsonObj;
}

private Set<Tag> serializeTags(String allTags){
Set<Tag> imageTagsSet = new HashSet<Tag>();
if (allTags.isEmpty()){
return imageTagsSet;
}
String[] splitTags = allTags.split(" ");
for (String tag: splitTags){
long tagCount = tagService.getByTagName(tag);
if (tagCount != 0){
//Tag Exists
Tag singleTagDB = tagService.getTagByTagName(tag);
imageTagsSet.add(singleTagDB);
continue;
}
//Create Tag
Tag tagToSave = new Tag();
tagToSave.setName(tag);
tagService.save(tagToSave);
imageTagsSet.add(tagToSave);
}
return imageTagsSet;
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package gr.thegoodsideofe1.tourguide.controllers;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import gr.thegoodsideofe1.tourguide.aes.AES_ENCRYPTION;
import gr.thegoodsideofe1.tourguide.entities.User;
import gr.thegoodsideofe1.tourguide.entities.UserCollection;
import gr.thegoodsideofe1.tourguide.entities.UserCollectionImage;
import gr.thegoodsideofe1.tourguide.services.UserCollectionImageService;
import gr.thegoodsideofe1.tourguide.services.UserCollectionService;
import gr.thegoodsideofe1.tourguide.services.UserService;
import org.json.HTTP;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -44,7 +40,7 @@ public ResponseEntity<Object> list(@RequestBody Map<String, String> requestBody)

if (loginUser != null) {
//User is Logged in
if (loginUser.getIs_admin()) {
if (loginUser.getIsAdmin()) {
//User is Admin
List<UserCollection> allUserCollection = userCollectionService.listAllCollections();
List<JSONObject> allUserCollectionEntities = new ArrayList<JSONObject>();
Expand All @@ -53,7 +49,7 @@ public ResponseEntity<Object> list(@RequestBody Map<String, String> requestBody)
collection.put("id", userCollection.getId());
collection.put("name", userCollection.getName());
collection.put("description", userCollection.getDescription());
collection.put("public", userCollection.getIs_public());
collection.put("public", userCollection.getIsPublic());
collection.put("user_id", userCollection.getUser_id());
}
return Responder.generateResponse("success", HttpStatus.OK, allUserCollection.toArray());
Expand All @@ -79,7 +75,7 @@ public ResponseEntity<Object> specific(@RequestBody Map<String, String> requestB
if (loginUser != null) {
//User is Logged in
UserCollection userCollection = userCollectionService.getCollection(id);
if (loginUser.getIs_admin() || userCollection.getUser_id().getId() == loginUser.getId()) {
if (loginUser.getIsAdmin() || userCollection.getUser_id().getId() == loginUser.getId()) {
//User is Admin or is Owner of the User Collection
return Responder.generateResponse("success", HttpStatus.OK, userCollection);
} else {
Expand All @@ -104,7 +100,7 @@ public ResponseEntity<Object> update(@RequestBody Map<String, String> requestBod
if (loginUser != null) {
//User is Logged in
UserCollection userCollection = userCollectionService.getCollection(id);
if (loginUser.getIs_admin() || userCollection.getUser_id().getId() == loginUser.getId()) {
if (loginUser.getIsAdmin() || userCollection.getUser_id().getId() == loginUser.getId()) {
//User is Admin or is Owner of the User Collection
if (!requestBody.get("name").isBlank()){
userCollection.setName(requestBody.get("name"));
Expand All @@ -113,7 +109,7 @@ public ResponseEntity<Object> update(@RequestBody Map<String, String> requestBod
userCollection.setName(requestBody.get("description"));
}
if (!requestBody.get("public").isEmpty()){
userCollection.setIs_public(Boolean.getBoolean(requestBody.get("public")));
userCollection.setIsPublic(Boolean.getBoolean(requestBody.get("public")));
}
userCollectionService.saveCollection(userCollection);
return Responder.generateResponse("success", HttpStatus.OK, userCollection);
Expand Down Expand Up @@ -142,7 +138,7 @@ public ResponseEntity<Object> add(@RequestBody Map<String, String> requestBody)
newUserCollection.setName(requestBody.get("name"));
newUserCollection.setDescription(requestBody.get("description"));
newUserCollection.setUser_id(loginUser);
newUserCollection.setIs_public(Boolean.getBoolean(requestBody.get("public")));
newUserCollection.setIsPublic(Boolean.getBoolean(requestBody.get("public")));
userCollectionService.saveCollection(newUserCollection);
return Responder.generateResponse("success", HttpStatus.OK, newUserCollection);
} else {
Expand All @@ -163,7 +159,7 @@ public ResponseEntity<Object> delete(@RequestBody Map<String, String> requestBod
if (loginUser != null) {
//User is Logged in
UserCollection userCollection = userCollectionService.getCollection(Integer.valueOf(id));
if (loginUser.getIs_admin() || userCollection.getUser_id().getId() == loginUser.getId()) {
if (loginUser.getIsAdmin() || userCollection.getUser_id().getId() == loginUser.getId()) {
//User is Admin or is Owner of the User Collection
//Delete Child
List<UserCollectionImage> allCollectionImages = userCollectionImageService.getAllCollectionImagesByCollectionID(userCollection.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import gr.thegoodsideofe1.tourguide.aes.AES_ENCRYPTION;
import gr.thegoodsideofe1.tourguide.entities.User;
import gr.thegoodsideofe1.tourguide.entities.UserCollection;
import gr.thegoodsideofe1.tourguide.entities.UserCollectionImage;
import gr.thegoodsideofe1.tourguide.services.UserCollectionImageService;
import gr.thegoodsideofe1.tourguide.services.UserCollectionService;
Expand Down Expand Up @@ -38,13 +37,13 @@ public String index(@RequestBody Map<String, String> requestBody) throws Excepti
User loginUser = userService.getUserByParams(userDetails[1], userDetails[0], userDetails[2], userDetails[3]);

if (loginUser != null) {
if (loginUser.getIs_admin()) {
if (loginUser.getIsAdmin()) {
List<UserCollectionImage> allCollectionImages = userCollectionImageService.listAllCollectionsImages();
for (UserCollectionImage collectionImage : allCollectionImages){
JSONObject singleCollection = new JSONObject();
singleCollection.put("id", collectionImage.getId());
singleCollection.put("image_id", collectionImage.getImage_id());
singleCollection.put("user_collection_id", collectionImage.getUser_collection_id());
singleCollection.put("image_id", collectionImage.getImageId());
singleCollection.put("user_collection_id", collectionImage.getUserCollectionId());
arrayToReturn.put(singleCollection);
}
} else {
Expand Down Expand Up @@ -73,11 +72,11 @@ public String specific(@RequestBody Map<String, String> requestBody, @PathVariab
UserCollectionImage userCollection = userCollectionImageService.getCollectionImage(id);

if (loginUser != null) {
if (loginUser.getIs_admin()) {
if (loginUser.getIsAdmin()) {
JSONObject singleCollection = new JSONObject();
singleCollection.put("id", userCollection.getId());
singleCollection.put("image_id", userCollection.getImage_id());
singleCollection.put("user_collection_id", userCollection.getUser_collection_id());
singleCollection.put("image_id", userCollection.getImageId());
singleCollection.put("user_collection_id", userCollection.getUserCollectionId());
arrayToReturn.put(singleCollection);
} else {
//User is NOT Admin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@
import gr.thegoodsideofe1.tourguide.entities.User;
import gr.thegoodsideofe1.tourguide.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import javax.transaction.Transactional;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

//@CrossOrigin(origins = "http://localhost:3000")
Expand All @@ -29,11 +24,13 @@ public class UserController {
@Autowired
private AES_ENCRYPTION aes_encryption;

@Transactional
@RequestMapping(value = "", method = RequestMethod.GET)
public List<User> list(){
return userService.listAllUsers();
}

@Transactional
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<User> get (@PathVariable Integer id){
try {
Expand Down Expand Up @@ -91,7 +88,7 @@ public Map<String, String> login(@RequestBody Map<String, String> userLoginDetai
//User Exists
if (passwordEncoder.matches(passwordParam, userToLogin.getPassword())){
//Password Param matches password in DB
String userDetailsJoined = userToLogin.getEmail() + "," + userToLogin.getUsername() + "," + userToLogin.getFirst_name() + "," + userToLogin.getLast_name();
String userDetailsJoined = userToLogin.getEmail() + "," + userToLogin.getUsername() + "," + userToLogin.getFirstName() + "," + userToLogin.getLastName();

returnResponse.put("status", "success");
returnResponse.put("token", aes_encryption.encrypt(userDetailsJoined));
Expand Down Expand Up @@ -119,7 +116,7 @@ public Map<String, String> loginUsername(@RequestBody Map<String, String> userLo

if (passwordEncoder.matches(passwordParam, userToLogin.getPassword())){
//Password Param matches password in DB
String userDetailsJoined = userToLogin.getEmail() + "," + userToLogin.getUsername() + "," + userToLogin.getFirst_name() + "," + userToLogin.getLast_name();
String userDetailsJoined = userToLogin.getEmail() + "," + userToLogin.getUsername() + "," + userToLogin.getFirstName() + "," + userToLogin.getLastName();

returnResponse.put("status", "success");
returnResponse.put("token", aes_encryption.encrypt(userDetailsJoined));
Expand Down
Loading

0 comments on commit f61db45

Please sign in to comment.