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 #39 from TouristGuideApp/v4_integration
Browse files Browse the repository at this point in the history
V4 integration
  • Loading branch information
ErminaTrontzou authored Dec 11, 2022
2 parents 4fbc573 + 611aa94 commit f17f5dd
Show file tree
Hide file tree
Showing 30 changed files with 1,436 additions and 749 deletions.
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ dependencies {
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'
implementation 'org.jsoup:jsoup:1.15.3'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
testCompileOnly 'junit:junit:4.12'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.8.1'
testImplementation "org.mockito:mockito-core:3.+"
}

tasks.named('test') {

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class TourguideApplication {
public static void main(String[] args) {
SpringApplication.run(TourguideApplication.class, args);
System.out.println(" /==================================================================\\\n / ---- ______ _________ _ ___ _ __ _ _____ \\\n { | || | | _____/ \\--- ---/ / \\ | | | | | \\ | | | ___/ }\n { | ___/ | _____ | | / | \\ | | | | | \\ | | \\ \\__ }\n { | | | _____| | | / ___ \\ | | | | | | | | | \\_ \\ }\n { | | | |____ | | / / \\ \\ | |____ | | | / | | ___/ / }\n \\ \\_/ \\______| \\_/ \\/ \\/ |_____/ |_| \\__/ |_| \\___/ /\n \\=================================================================/\n \n \n &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n \n \n /\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\ ___________\\\n <` ΤΡΙΑΙΝΑ ΤΟΥ ΕΠΑΜEΙΝΩΝΤΑ`> _-\' ____----/\n `\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/` _\' . /\n ______________________________-----------/ \\_________\\\n /_________________________________________ ______----/\n \\ ` /\n \'_ . \\_ \n `_ `\'\'-------\\\n `\'-------------/");
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,137 +1,27 @@
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.TagService;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;
import gr.thegoodsideofe1.tourguide.services.FlickrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("api/v1/flickr")
@ResponseBody
public class FlickrController {
@Autowired
ImageRepository imageRepository;

@Autowired
TagService tagService;
FlickrService flickrService;

@Transactional
@GetMapping("/getByTitle/{title}")
public ResponseEntity<?> getNewImages(@PathVariable String title){
if (getFlickr(title)){
HashMap<String, String> returnMap = new HashMap<String, String>();
returnMap.put("status", "success");
returnMap.put("message", "Images Imported");
return ResponseEntity.status(201).body(returnMap);
}
HashMap<String, String> returnMap = new HashMap<String, String>();
returnMap.put("status", "error");
returnMap.put("message", "Problem on image import");
return ResponseEntity.status(200).body(returnMap);
}

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;
return flickrService.getNewImagesForLocation(title);
}

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;
@PostMapping("/imageToSave")
public ResponseEntity<?>imageToSave(@RequestBody Image photo){
return flickrService.saveImage(photo);
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
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.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
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")
@RestController
@RequestMapping("api/v1/images")
Expand All @@ -33,45 +13,23 @@ public class ImageController {
@Autowired
ImageService imageService;

@Autowired
ImageRepository imageRepository;

@Autowired
TagService tagService;


@Transactional
@GetMapping("")
public List<Image> list(){
@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseEntity<?> list() {
return imageService.listAllImages();
}

@Transactional
@GetMapping("/{id}")
public ResponseEntity<Image> get (@PathVariable Integer id){
try{
Image image = imageService.getImage(id);
return new ResponseEntity<Image>(image, HttpStatus.OK);
}catch (NoSuchElementException e){
return new ResponseEntity<Image>(HttpStatus.NOT_FOUND);
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<?> getSpecificImage(@PathVariable Long id) {
return imageService.getImage(id);
}

@Transactional
@GetMapping("/getByTitle/{title}")

@RequestMapping(value = "/getByTitle/{title}", method = RequestMethod.GET)
public ResponseEntity<?> imageByTitle(@PathVariable String title,
@RequestParam(value="page", defaultValue = "1") int page,
@RequestParam(value = "size", defaultValue = "8") int size) {
int imagesCount = imageService.getImageCount(title);
if (imagesCount != 0){
Pageable paging = PageRequest.of(page, size);
Page<Image> imagesPage = imageRepository.findAllImagesByTitle(title, paging);
return new ResponseEntity<>(imagesPage, HttpStatus.OK);
}
HashMap<String, String> returnMap = new HashMap<String, String>();
returnMap.put("status", "error");
returnMap.put("message", "No images with your search criteria");
return ResponseEntity.status(204).body(returnMap);
@RequestParam(value="page", defaultValue = "0") int page,
@RequestParam(value="size", defaultValue = "8") int size){
return imageService.getImageByTitle(title,page,size);
}
}

Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
package gr.thegoodsideofe1.tourguide.controllers;


import gr.thegoodsideofe1.tourguide.entities.ImageTags;
import gr.thegoodsideofe1.tourguide.services.ImageTagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.NoSuchElementException;

@RestController
@RequestMapping("/api/v1/image_tags")
public class ImageTagsController {
@Autowired
ImageTagsService imageTagsService;

@GetMapping("")
public List<ImageTags> list(){
@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseEntity<?> list() {
return imageTagsService.listAllImageTags();
}

@GetMapping("/{id}")
public ResponseEntity<ImageTags> get (@PathVariable Integer id){
try{
ImageTags imageTags = imageTagsService.getImageTags(id);
return new ResponseEntity<ImageTags>(imageTags, HttpStatus.OK);
}catch (NoSuchElementException e){
return new ResponseEntity<ImageTags>(HttpStatus.NOT_FOUND);
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<?> getSpecificImage(@PathVariable int id) {
return imageTagsService.getImageTags(id);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package gr.thegoodsideofe1.tourguide.controllers;

import gr.thegoodsideofe1.tourguide.entities.Tag;
import gr.thegoodsideofe1.tourguide.services.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -17,18 +15,13 @@ public class TagController {
@Autowired
TagService tagService;

@GetMapping("")
public List<Tag> list(){
@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseEntity<?> list() {
return tagService.listAllTags();
}

@GetMapping("/{id}")
public ResponseEntity<Tag> get (@PathVariable Integer id){
try{
Tag tag = tagService.getTag(id);
return new ResponseEntity<Tag>(tag, HttpStatus.OK);
}catch(NoSuchElementException e){
return new ResponseEntity<Tag>(HttpStatus.NOT_FOUND);
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<?> getSpecificImage(@PathVariable int id) {
return tagService.getTag(id);
}
}
Loading

0 comments on commit f17f5dd

Please sign in to comment.