diff --git a/src/main/java/org/ntlab/nemophila/entities/ShopJson.java b/src/main/java/org/ntlab/nemophila/entities/ShopJson.java new file mode 100644 index 0000000..0b58174 --- /dev/null +++ b/src/main/java/org/ntlab/nemophila/entities/ShopJson.java @@ -0,0 +1,70 @@ +package org.ntlab.nemophila.entities; + +import com.fasterxml.jackson.annotation.JsonProperty; +import org.ntlab.nemophila.models.shops.Shop; + +import java.util.HashSet; + +public class ShopJson { + @JsonProperty("sid") + private String id; + @JsonProperty("name") + private String name; + @JsonProperty("longitude") + private double longitude; + @JsonProperty("latitude") + private double latitude; + @JsonProperty("genreSet") + private HashSet genreSets; + @JsonProperty("userIdSet") + private HashSet userIdSets; + + //Getter + public String getId() { + return id; + } + public String getName() { + return name; + } + public double getLongitude() { + return longitude; + } + public double getLatitude() { + return latitude; + } + public HashSet getGenreSets() { + return genreSets; + } + public HashSet getUserIdSets() { + return userIdSets; + } + + //Setter + public void setId(String id) { + this.id = id; + } + public void setName(String name) { + this.name = name; + } + public void setLongitude(double longitude) { + this.longitude = longitude; + } + public void setLatitude(double latitude) { + this.latitude = latitude; + } + public void setGenreSets(HashSet genreSets) { + this.genreSets = genreSets; + } + public void setUserIdSets(HashSet userIdSets) { + this.userIdSets = userIdSets; + } + + public ShopJson(Shop shop, HashSet genreSets, HashSet userIdSets) { + this.setId(shop.getId()); + this.setName(shop.getName()); + this.setLongitude(shop.getLongitude()); + this.setLatitude(shop.getLatitude()); + this.setGenreSets(genreSets); + this.setUserIdSets(userIdSets); + } +} diff --git a/src/main/java/org/ntlab/nemophila/models/shops/ShopManager.java b/src/main/java/org/ntlab/nemophila/models/shops/ShopManager.java index 01812ab..6195a6f 100644 --- a/src/main/java/org/ntlab/nemophila/models/shops/ShopManager.java +++ b/src/main/java/org/ntlab/nemophila/models/shops/ShopManager.java @@ -1,12 +1,11 @@ package org.ntlab.nemophila.models.shops; +import org.ntlab.nemophila.entities.ShopJson; +import org.ntlab.nemophila.models.accounts.Account; import org.ntlab.nemophila.models.accounts.Post; import javax.ws.rs.FormParam; import java.net.URL; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.UUID; +import java.util.*; public class ShopManager { private static ShopManager theInstance = null; @@ -34,18 +33,18 @@ double upperRightLatitude, double lowerLeftLongitude, double lowerLeftLatitude) { - Collection response = new ArrayList<>(); + Collection shops = new ArrayList<>(); for (Shop shop: shopsMap.values()) { double longitude = shop.getLongitude(); double latitude = shop.getLatitude(); if ((lowerLeftLongitude < longitude && longitude < upperRightLongitude) && (lowerLeftLatitude < latitude && latitude < upperRightLatitude)) { - response.add(shop); + shops.add(shop); } } - return response; + return shops; } //sidからその店の投稿を取得 @@ -80,4 +79,28 @@ shopsMap.remove(id); } + public ArrayList createShopJson(Collection shops) { + ArrayList result = new ArrayList<>(); + for (Shop shop: shops) { + ShopJson sj = new ShopJson(shop, getGenre(shop), getIds(shop)); + result.add(sj); + } + return result; + } + + private HashSet getGenre(Shop shop) { + HashSet genreSets = new HashSet<>(); + for (Post post: shop.getPosts()) { + genreSets.add(post.getGenre()); + } + return genreSets; + } + + private HashSet getIds(Shop shop) { + HashSet idsSet = new HashSet<>(); + for (Post post: shop.getPosts()) { + idsSet.add(post.getOwner().getId()); + } + return idsSet; + } } \ No newline at end of file diff --git a/src/main/java/org/ntlab/nemophila/resources/shops/ShopsRest.java b/src/main/java/org/ntlab/nemophila/resources/shops/ShopsRest.java index aa74e37..ce34613 100644 --- a/src/main/java/org/ntlab/nemophila/resources/shops/ShopsRest.java +++ b/src/main/java/org/ntlab/nemophila/resources/shops/ShopsRest.java @@ -1,11 +1,14 @@ package org.ntlab.nemophila.resources.shops; +import org.ntlab.nemophila.entities.ShopJson; import org.ntlab.nemophila.models.shops.Shop; import org.ntlab.nemophila.models.shops.ShopManager; import org.springframework.stereotype.Component; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; +import java.lang.reflect.Array; +import java.util.ArrayList; import java.util.Collection; @Component @@ -13,7 +16,7 @@ public class ShopsRest { @GET @Produces(MediaType.APPLICATION_JSON) - public Collection getShops(@QueryParam("monitor_upper_right_longitude") double upperRightLongitude, + public ArrayList getShops(@QueryParam("monitor_upper_right_longitude") double upperRightLongitude, @QueryParam("monitor_upper_right_latitude") double upperRightLatitude, @QueryParam("monitor_lower_left_longitude") double lowerLeftLongitude, @QueryParam("monitor_lower_left_latitude") double lowerLeftLatitude) { @@ -22,7 +25,8 @@ // System.out.println(lowerLeftLatitude); // System.out.println(lowerLeftLongitude); ShopManager shopManager = ShopManager.getInstance(); - Collection response = shopManager.getShops(upperRightLongitude, upperRightLatitude, lowerLeftLongitude, lowerLeftLatitude); + Collection shops = shopManager.getShops(upperRightLongitude, upperRightLatitude, lowerLeftLongitude, lowerLeftLatitude); + ArrayList response = shopManager.createShopJson(shops); return response; }